| 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 |