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

Function NewIsolatedTestConfig

internal/config/test.go:129–167  ·  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

127// Callers that want one of the auth env vars set should set it after calling this,
128// otherwise the value is cleared along with the ambient environment.
129func NewIsolatedTestConfig(t *testing.T, cfgString string) (*cfg, func(io.Writer, io.Writer)) {
130 keyring.MockInit()
131
132 // go-gh reads these ahead of any stored config, so isolating the config file is
133 // not enough on its own. A developer with GH_TOKEN exported, or any CI image that
134 // provides one, would otherwise see an authenticated config here and fail tests
135 // that assert on the logged out state.
136 for _, key := range []string{
137 "GH_TOKEN",
138 "GITHUB_TOKEN",
139 "GH_ENTERPRISE_TOKEN",
140 "GITHUB_ENTERPRISE_TOKEN",
141 "GH_HOST",
142 } {
143 t.Setenv(key, "")
144 }
145
146 c := ghConfig.ReadFromString(cfgString)
147 cfg := cfg{c}
148
149 // The real implementation of config.Read uses a sync.Once
150 // to read config files and initialise package level variables
151 // that are used from then on.
152 //
153 // This means that tests can't be isolated from each other, so
154 // we swap out the function here to return a new config each time.
155 ghConfig.Read = func(_ *ghConfig.Config) (*ghConfig.Config, error) {
156 return c, nil
157 }
158
159 // The config.Write method isn't defined in the same way as Read to allow
160 // the function to be swapped out and it does try to write to disk.
161 //
162 // We should consider whether it makes sense to change that but in the meantime
163 // we can use GH_CONFIG_DIR env var to ensure the tests remain isolated.
164 readConfigs := StubWriteConfig(t)
165
166 return &cfg, readConfigs
167}
168
169// StubWriteConfig stubs out the filesystem where config file are written.
170// It then returns a function that will read in the config files into io.Writers.

Callers 14

Test_CheckAuthFunction · 0.92
Test_setupGitRunFunction · 0.92
TestSwitchRunFunction · 0.92
Test_refreshRunFunction · 0.92
TestTokenRunFunction · 0.92
Test_loginRun_nonttyFunction · 0.92
Test_loginRun_SurveyFunction · 0.92
Test_statusRunFunction · 0.92
Test_logoutRun_ttyFunction · 0.92
Test_logoutRun_nonttyFunction · 0.92
TestRepoForkFunction · 0.92

Calls 2

MockInitFunction · 0.92
StubWriteConfigFunction · 0.85

Tested by 14

Test_CheckAuthFunction · 0.74
Test_setupGitRunFunction · 0.74
TestSwitchRunFunction · 0.74
Test_refreshRunFunction · 0.74
TestTokenRunFunction · 0.74
Test_loginRun_nonttyFunction · 0.74
Test_loginRun_SurveyFunction · 0.74
Test_statusRunFunction · 0.74
Test_logoutRun_ttyFunction · 0.74
Test_logoutRun_nonttyFunction · 0.74
TestRepoForkFunction · 0.74