MCPcopy Index your code
hub / github.com/cli/cli / TestMigrationWriteErrors

Function TestMigrationWriteErrors

internal/config/migrate_test.go:176–220  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

174}
175
176func 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
222func makeFileUnwriteable(t *testing.T, file string) {
223 t.Helper()

Callers

nothing calls this directly

Calls 7

MigrateMethod · 0.95
makeFileUnwriteableFunction · 0.85
testFullConfigFunction · 0.85
mockMigrationFunction · 0.85
JoinMethod · 0.80
RunMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected