|
| 1 | +package parser |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestQuoteCronExpressions(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + input string |
| 11 | + expected string |
| 12 | + }{ |
| 13 | + { |
| 14 | + name: "Simple unquoted cron expression", |
| 15 | + input: `on: |
| 16 | + schedule: |
| 17 | + - cron: 0 14 * * 1-5`, |
| 18 | + expected: `on: |
| 19 | + schedule: |
| 20 | + - cron: "0 14 * * 1-5"`, |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "Already quoted cron expression", |
| 24 | + input: `on: |
| 25 | + schedule: |
| 26 | + - cron: "0 14 * * 1-5"`, |
| 27 | + expected: `on: |
| 28 | + schedule: |
| 29 | + - cron: "0 14 * * 1-5"`, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Single quoted cron expression", |
| 33 | + input: `on: |
| 34 | + schedule: |
| 35 | + - cron: '0 14 * * 1-5'`, |
| 36 | + expected: `on: |
| 37 | + schedule: |
| 38 | + - cron: '0 14 * * 1-5'`, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "Multiple unquoted cron expressions", |
| 42 | + input: `on: |
| 43 | + schedule: |
| 44 | + - cron: 0 14 * * 1-5 |
| 45 | + - cron: 0 9 * * *`, |
| 46 | + expected: `on: |
| 47 | + schedule: |
| 48 | + - cron: "0 14 * * 1-5" |
| 49 | + - cron: "0 9 * * *"`, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "Cron with trailing comment", |
| 53 | + input: `on: |
| 54 | + schedule: |
| 55 | + - cron: 0 14 * * 1-5 # Weekdays at 2pm`, |
| 56 | + expected: `on: |
| 57 | + schedule: |
| 58 | + - cron: "0 14 * * 1-5" # Weekdays at 2pm`, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "Cron without dash prefix", |
| 62 | + input: `on: |
| 63 | + schedule: |
| 64 | + cron: 0 14 * * 1-5`, |
| 65 | + expected: `on: |
| 66 | + schedule: |
| 67 | + cron: "0 14 * * 1-5"`, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "Every minute cron (starts with asterisk - not matched)", |
| 71 | + input: `on: |
| 72 | + schedule: |
| 73 | + - cron: * * * * *`, |
| 74 | + expected: `on: |
| 75 | + schedule: |
| 76 | + - cron: * * * * *`, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "Every 5 minutes cron (starts with asterisk - not matched)", |
| 80 | + input: `on: |
| 81 | + schedule: |
| 82 | + - cron: */5 * * * *`, |
| 83 | + expected: `on: |
| 84 | + schedule: |
| 85 | + - cron: */5 * * * *`, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "Midnight daily cron", |
| 89 | + input: `on: |
| 90 | + schedule: |
| 91 | + - cron: 0 0 * * *`, |
| 92 | + expected: `on: |
| 93 | + schedule: |
| 94 | + - cron: "0 0 * * *"`, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "Mixed quoted and unquoted", |
| 98 | + input: `on: |
| 99 | + schedule: |
| 100 | + - cron: "0 9 * * *" |
| 101 | + - cron: 0 14 * * 1-5 |
| 102 | + - cron: '0 18 * * *'`, |
| 103 | + expected: `on: |
| 104 | + schedule: |
| 105 | + - cron: "0 9 * * *" |
| 106 | + - cron: "0 14 * * 1-5" |
| 107 | + - cron: '0 18 * * *'`, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "Empty input", |
| 111 | + input: "", |
| 112 | + expected: "", |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "No cron expressions", |
| 116 | + input: `on: |
| 117 | + push: |
| 118 | + branches: [main]`, |
| 119 | + expected: `on: |
| 120 | + push: |
| 121 | + branches: [main]`, |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "Cron with extra whitespace", |
| 125 | + input: `on: |
| 126 | + schedule: |
| 127 | + - cron: 0 14 * * 1-5 `, |
| 128 | + expected: `on: |
| 129 | + schedule: |
| 130 | + - cron: "0 14 * * 1-5"`, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "Multiple schedules in one workflow", |
| 134 | + input: `on: |
| 135 | + schedule: |
| 136 | + - cron: 0 9 * * 1 |
| 137 | + - cron: 0 14 * * 1-5 |
| 138 | + - cron: 0 18 * * 5`, |
| 139 | + expected: `on: |
| 140 | + schedule: |
| 141 | + - cron: "0 9 * * 1" |
| 142 | + - cron: "0 14 * * 1-5" |
| 143 | + - cron: "0 18 * * 5"`, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "Cron expression with slashes and commas", |
| 147 | + input: `on: |
| 148 | + schedule: |
| 149 | + - cron: 0,30 */2 1,15 * *`, |
| 150 | + expected: `on: |
| 151 | + schedule: |
| 152 | + - cron: "0,30 */2 1,15 * *"`, |
| 153 | + }, |
| 154 | + { |
| 155 | + name: "Cron with comment and extra spaces", |
| 156 | + input: `on: |
| 157 | + schedule: |
| 158 | + - cron: 0 9 * * * # Morning run`, |
| 159 | + expected: `on: |
| 160 | + schedule: |
| 161 | + - cron: "0 9 * * *" # Morning run`, |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "Only cron line in input", |
| 165 | + input: ` - cron: 0 14 * * 1-5`, |
| 166 | + expected: ` - cron: "0 14 * * 1-5"`, |
| 167 | + }, |
| 168 | + { |
| 169 | + name: "Cron without list dash and with indent", |
| 170 | + input: ` cron: 0 14 * * 1-5`, |
| 171 | + expected: ` cron: "0 14 * * 1-5"`, |
| 172 | + }, |
| 173 | + } |
| 174 | + |
| 175 | + for _, tt := range tests { |
| 176 | + t.Run(tt.name, func(t *testing.T) { |
| 177 | + result := QuoteCronExpressions(tt.input) |
| 178 | + if result != tt.expected { |
| 179 | + t.Errorf("\nInput:\n%s\n\nExpected:\n%s\n\nGot:\n%s", tt.input, tt.expected, result) |
| 180 | + } |
| 181 | + }) |
| 182 | + } |
| 183 | +} |
0 commit comments