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)
| 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. |
| 105 | func 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. |