|
4 | 4 | "regexp" |
5 | 5 | "strings" |
6 | 6 | "testing" |
| 7 | + |
| 8 | + "github.com/spf13/viper" |
7 | 9 | ) |
8 | 10 |
|
9 | 11 | func TestCronitorIgnoreComment(t *testing.T) { |
@@ -118,3 +120,150 @@ func TestCronitorIgnoreComment(t *testing.T) { |
118 | 120 | t.Errorf("Expected Write() output to NOT contain '# cronitor: ignore' for second line, got: %s", secondLineOutput) |
119 | 121 | } |
120 | 122 | } |
| 123 | + |
| 124 | +func TestLineWriteWithEnvFlag(t *testing.T) { |
| 125 | + // Save original viper value |
| 126 | + originalEnv := viper.GetString("CRONITOR_ENV") |
| 127 | + |
| 128 | + // Test cases |
| 129 | + testCases := []struct { |
| 130 | + name string |
| 131 | + envValue string |
| 132 | + line Line |
| 133 | + shouldHaveEnv bool |
| 134 | + }{ |
| 135 | + { |
| 136 | + name: "With environment set", |
| 137 | + envValue: "production", |
| 138 | + line: Line{ |
| 139 | + IsJob: true, |
| 140 | + CronExpression: "0 * * * *", |
| 141 | + CommandToRun: "/path/to/script.sh", |
| 142 | + Code: "abc123", |
| 143 | + Mon: Monitor{Code: "abc123"}, |
| 144 | + }, |
| 145 | + shouldHaveEnv: true, |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "Without environment set", |
| 149 | + envValue: "", |
| 150 | + line: Line{ |
| 151 | + IsJob: true, |
| 152 | + CronExpression: "0 * * * *", |
| 153 | + CommandToRun: "/path/to/script.sh", |
| 154 | + Code: "def456", |
| 155 | + Mon: Monitor{Code: "def456"}, |
| 156 | + }, |
| 157 | + shouldHaveEnv: false, |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "With staging environment", |
| 161 | + envValue: "staging", |
| 162 | + line: Line{ |
| 163 | + IsJob: true, |
| 164 | + CronExpression: "*/5 * * * *", |
| 165 | + CommandToRun: "/usr/bin/backup.sh", |
| 166 | + Code: "xyz789", |
| 167 | + Mon: Monitor{Code: "xyz789"}, |
| 168 | + }, |
| 169 | + shouldHaveEnv: true, |
| 170 | + }, |
| 171 | + } |
| 172 | + |
| 173 | + for _, tc := range testCases { |
| 174 | + t.Run(tc.name, func(t *testing.T) { |
| 175 | + // Set the environment value |
| 176 | + viper.Set("CRONITOR_ENV", tc.envValue) |
| 177 | + |
| 178 | + // Write the line |
| 179 | + output := tc.line.Write() |
| 180 | + |
| 181 | + // Check if --env flag is present |
| 182 | + hasEnvFlag := strings.Contains(output, "--env") |
| 183 | + if tc.shouldHaveEnv && !hasEnvFlag { |
| 184 | + t.Errorf("Expected --env flag in output but not found. Output: %s", output) |
| 185 | + } |
| 186 | + if !tc.shouldHaveEnv && hasEnvFlag { |
| 187 | + t.Errorf("Did not expect --env flag in output but found it. Output: %s", output) |
| 188 | + } |
| 189 | + |
| 190 | + // If env should be present, verify the value is included |
| 191 | + if tc.shouldHaveEnv { |
| 192 | + expectedEnvString := "--env " + tc.envValue |
| 193 | + if !strings.Contains(output, expectedEnvString) { |
| 194 | + t.Errorf("Expected '%s' in output but not found. Output: %s", expectedEnvString, output) |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + // Verify the cronitor exec structure is correct |
| 199 | + if strings.Contains(output, "cronitor") { |
| 200 | + // Check the order: cronitor [--env <value>] [--no-stdout] exec <code> <command> |
| 201 | + parts := strings.Fields(output) |
| 202 | + cronitorIndex := -1 |
| 203 | + for i, part := range parts { |
| 204 | + if part == "cronitor" { |
| 205 | + cronitorIndex = i |
| 206 | + break |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + if cronitorIndex >= 0 { |
| 211 | + execIndex := -1 |
| 212 | + for i := cronitorIndex + 1; i < len(parts); i++ { |
| 213 | + if parts[i] == "exec" { |
| 214 | + execIndex = i |
| 215 | + break |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + if execIndex < 0 { |
| 220 | + t.Errorf("'exec' not found after 'cronitor' in output: %s", output) |
| 221 | + } |
| 222 | + |
| 223 | + // If env flag is present, it should come before 'exec' |
| 224 | + if tc.shouldHaveEnv { |
| 225 | + envFlagIndex := -1 |
| 226 | + for i := cronitorIndex + 1; i < execIndex; i++ { |
| 227 | + if parts[i] == "--env" { |
| 228 | + envFlagIndex = i |
| 229 | + break |
| 230 | + } |
| 231 | + } |
| 232 | + if envFlagIndex < 0 { |
| 233 | + t.Errorf("--env flag should appear between 'cronitor' and 'exec'. Output: %s", output) |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + }) |
| 239 | + } |
| 240 | + |
| 241 | + // Restore original viper value |
| 242 | + viper.Set("CRONITOR_ENV", originalEnv) |
| 243 | +} |
| 244 | + |
| 245 | +func TestLineWriteWithNoStdoutAndEnv(t *testing.T) { |
| 246 | + // Save original viper value |
| 247 | + originalEnv := viper.GetString("CRONITOR_ENV") |
| 248 | + |
| 249 | + // Set environment |
| 250 | + viper.Set("CRONITOR_ENV", "testing") |
| 251 | + |
| 252 | + line := Line{ |
| 253 | + IsJob: true, |
| 254 | + CronExpression: "0 0 * * *", |
| 255 | + CommandToRun: "/bin/daily-task", |
| 256 | + Code: "test123", |
| 257 | + Mon: Monitor{Code: "test123", NoStdoutPassthru: true}, |
| 258 | + } |
| 259 | + |
| 260 | + output := line.Write() |
| 261 | + |
| 262 | + // Check that both flags are present and in correct order |
| 263 | + if !strings.Contains(output, "cronitor --env testing --no-stdout exec test123") { |
| 264 | + t.Errorf("Expected 'cronitor --env testing --no-stdout exec test123' in output but got: %s", output) |
| 265 | + } |
| 266 | + |
| 267 | + // Restore original viper value |
| 268 | + viper.Set("CRONITOR_ENV", originalEnv) |
| 269 | +} |
0 commit comments