MCPcopy Create free account
hub / github.com/cli/cli / NewIsolatedTestConfig

Function NewIsolatedTestConfig

internal/config/test.go:141–196  ·  view source on GitHub ↗

NewIsolatedTestConfig returns the real config implementation, built from cfgString and isolated from the machine running the tests. Pass "" for a config with no content. It also returns a function that reads back anything written to disk. Use it when the code under test exercises real config behavi

(t *testing.T, cfgString string)

Source from the content-addressed store, hash-verified

139// Callers that want one of the auth env vars set should set it after calling this,
140// otherwise the value is cleared along with the ambient environment.
141func NewIsolatedTestConfig(t *testing.T, cfgString string) (*cfg, func(io.Writer, io.Writer)) {
142 keyring.MockInit()
143
144 // go-gh reads these ahead of any stored config, so isolating the config file is
145 // not enough on its own. A developer with GH_TOKEN exported, or any CI image that
146 // provides one, would otherwise see an authenticated config here and fail tests
147 // that assert on the logged out state.
148 for _, key := range []string{
149 "GH_TOKEN",
150 "GITHUB_TOKEN",
151 "GH_ENTERPRISE_TOKEN",
152 "GITHUB_ENTERPRISE_TOKEN",
153 "GH_HOST",
154 } {
155 t.Setenv(key, "")
156 }
157
158 c := ghConfig.ReadFromString(cfgString)
159 cfg := cfg{cfg: c}
160 cfg.auth = &AuthConfig{
161 cfg: c,
162 // Neutralize the cross-process lock so tests never take a real filesystem lock.
163 acquireRefreshLock: func(ctx context.Context, path string) (func(), error) {
164 return func() {}, nil
165 },
166 // Neutralize the disk reload. This fixture stubs ghConfig.Read to return the string-parsed config c, but the
167 // real ghConfig.Reload bypasses that stub and reads the config files from the empty temp dir, which would
168 // replace c's contents and discard the config the test set up. A no-op keeps the in-memory config authoritative.
169 reloadConfig: func() error {
170 return nil
171 },
172 }
173
174 // The real implementation of config.Read uses a sync.Once
175 // to read config files and initialise package level variables
176 // that are used from then on.
177 //
178 // This means that tests can't be isolated from each other, so
179 // we swap out the function here to return a new config each time.
180 prev := ghConfig.Read
181 ghConfig.Read = func(_ *ghConfig.Config) (*ghConfig.Config, error) {
182 return c, nil
183 }
184 t.Cleanup(func() {
185 ghConfig.Read = prev
186 })
187
188 // The config.Write method isn't defined in the same way as Read to allow
189 // the function to be swapped out and it does try to write to disk.
190 //
191 // We should consider whether it makes sense to change that but in the meantime
192 // we can use GH_CONFIG_DIR env var to ensure the tests remain isolated.
193 readConfigs := StubWriteConfig(t)
194
195 return &cfg, readConfigs
196}
197
198// StubWriteConfig stubs out the filesystem where config file are written.

Callers 15

Test_CheckAuthFunction · 0.92
Test_setupGitRunFunction · 0.92
TestSwitchRunFunction · 0.92
Test_refreshRunFunction · 0.92
TestTokenRunFunction · 0.92
TestTokenRunRefreshFunction · 0.92
Test_loginRun_nonttyFunction · 0.92
Test_loginRun_SurveyFunction · 0.92

Calls 2

MockInitFunction · 0.92
StubWriteConfigFunction · 0.85

Tested by 15

Test_CheckAuthFunction · 0.74
Test_setupGitRunFunction · 0.74
TestSwitchRunFunction · 0.74
Test_refreshRunFunction · 0.74
TestTokenRunFunction · 0.74
TestTokenRunRefreshFunction · 0.74
Test_loginRun_nonttyFunction · 0.74
Test_loginRun_SurveyFunction · 0.74