(t *testing.T)
| 174 | } |
| 175 | |
| 176 | func TestMigrationWriteErrors(t *testing.T) { |
| 177 | tests := []struct { |
| 178 | name string |
| 179 | unwriteableFile string |
| 180 | wantErrContains string |
| 181 | }{ |
| 182 | { |
| 183 | name: "failure to write hosts", |
| 184 | unwriteableFile: "hosts.yml", |
| 185 | wantErrContains: "failed to write config after migration", |
| 186 | }, |
| 187 | { |
| 188 | name: "failure to write config", |
| 189 | unwriteableFile: "config.yml", |
| 190 | wantErrContains: "failed to write config after migration", |
| 191 | }, |
| 192 | } |
| 193 | |
| 194 | for _, tt := range tests { |
| 195 | t.Run(tt.name, func(t *testing.T) { |
| 196 | tempDir := t.TempDir() |
| 197 | t.Setenv("GH_CONFIG_DIR", tempDir) |
| 198 | |
| 199 | // Given we error when writing the files (because we chmod the files as trickery) |
| 200 | makeFileUnwriteable(t, filepath.Join(tempDir, tt.unwriteableFile)) |
| 201 | |
| 202 | c := ghConfig.ReadFromString(testFullConfig()) |
| 203 | topLevelKey := []string{"toplevelkey"} |
| 204 | hostsKey := []string{hostsKey, "newhost"} |
| 205 | |
| 206 | migration := mockMigration(func(someCfg *ghConfig.Config) error { |
| 207 | someCfg.Set(topLevelKey, "toplevelvalue") |
| 208 | someCfg.Set(hostsKey, "newhostvalue") |
| 209 | return nil |
| 210 | }) |
| 211 | |
| 212 | // When we run the migration |
| 213 | conf := cfg{c} |
| 214 | err := conf.Migrate(migration) |
| 215 | |
| 216 | // Then the error is wrapped and bubbled |
| 217 | require.ErrorContains(t, err, tt.wantErrContains) |
| 218 | }) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | func makeFileUnwriteable(t *testing.T, file string) { |
| 223 | t.Helper() |
nothing calls this directly
no test coverage detected