MCPcopy
hub / github.com/cli/cli / NewIsolatedTestConfig

Function NewIsolatedTestConfig

internal/config/stub.go:105–129  ·  view source on GitHub ↗

NewIsolatedTestConfig sets up a Mock keyring, creates a blank config overwrites the ghConfig.Read function that returns a singleton config in the real implementation, sets the GH_CONFIG_DIR env var so that any call to Write goes to a different location on disk, and then returns the blank config and

(t *testing.T)

Source from the content-addressed store, hash-verified

103// any call to Write goes to a different location on disk, and then returns
104// the blank config and a function that reads any data written to disk.
105func NewIsolatedTestConfig(t *testing.T) (*cfg, func(io.Writer, io.Writer)) {
106 keyring.MockInit()
107
108 c := ghConfig.ReadFromString("")
109 cfg := cfg{c}
110
111 // The real implementation of config.Read uses a sync.Once
112 // to read config files and initialise package level variables
113 // that are used from then on.
114 //
115 // This means that tests can't be isolated from each other, so
116 // we swap out the function here to return a new config each time.
117 ghConfig.Read = func(_ *ghConfig.Config) (*ghConfig.Config, error) {
118 return c, nil
119 }
120
121 // The config.Write method isn't defined in the same way as Read to allow
122 // the function to be swapped out and it does try to write to disk.
123 //
124 // We should consider whether it makes sense to change that but in the meantime
125 // we can use GH_CONFIG_DIR env var to ensure the tests remain isolated.
126 readConfigs := StubWriteConfig(t)
127
128 return &cfg, readConfigs
129}
130
131// StubWriteConfig stubs out the filesystem where config file are written.
132// It then returns a function that will read in the config files into io.Writers.

Callers 13

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 13

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