StubWriteConfig stubs out the filesystem where config file are written. It then returns a function that will read in the config files into io.Writers. It automatically cleans up environment variables and written files.
(t *testing.T)
| 167 | // It then returns a function that will read in the config files into io.Writers. |
| 168 | // It automatically cleans up environment variables and written files. |
| 169 | func StubWriteConfig(t *testing.T) func(io.Writer, io.Writer) { |
| 170 | t.Helper() |
| 171 | tempDir := t.TempDir() |
| 172 | t.Setenv("GH_CONFIG_DIR", tempDir) |
| 173 | return func(wc io.Writer, wh io.Writer) { |
| 174 | config, err := os.Open(filepath.Join(tempDir, "config.yml")) |
| 175 | if err != nil { |
| 176 | return |
| 177 | } |
| 178 | defer config.Close() |
| 179 | configData, err := io.ReadAll(config) |
| 180 | if err != nil { |
| 181 | return |
| 182 | } |
| 183 | _, err = wc.Write(configData) |
| 184 | if err != nil { |
| 185 | return |
| 186 | } |
| 187 | |
| 188 | hosts, err := os.Open(filepath.Join(tempDir, "hosts.yml")) |
| 189 | if err != nil { |
| 190 | return |
| 191 | } |
| 192 | defer hosts.Close() |
| 193 | hostsData, err := io.ReadAll(hosts) |
| 194 | if err != nil { |
| 195 | return |
| 196 | } |
| 197 | _, err = wh.Write(hostsData) |
| 198 | if err != nil { |
| 199 | return |
| 200 | } |
| 201 | } |
| 202 | } |