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)
| 199 | // It then returns a function that will read in the config files into io.Writers. |
| 200 | // It automatically cleans up environment variables and written files. |
| 201 | func StubWriteConfig(t *testing.T) func(io.Writer, io.Writer) { |
| 202 | t.Helper() |
| 203 | tempDir := t.TempDir() |
| 204 | t.Setenv("GH_CONFIG_DIR", tempDir) |
| 205 | return func(wc io.Writer, wh io.Writer) { |
| 206 | config, err := os.Open(filepath.Join(tempDir, "config.yml")) |
| 207 | if err != nil { |
| 208 | return |
| 209 | } |
| 210 | defer config.Close() |
| 211 | configData, err := io.ReadAll(config) |
| 212 | if err != nil { |
| 213 | return |
| 214 | } |
| 215 | _, err = wc.Write(configData) |
| 216 | if err != nil { |
| 217 | return |
| 218 | } |
| 219 | |
| 220 | hosts, err := os.Open(filepath.Join(tempDir, "hosts.yml")) |
| 221 | if err != nil { |
| 222 | return |
| 223 | } |
| 224 | defer hosts.Close() |
| 225 | hostsData, err := io.ReadAll(hosts) |
| 226 | if err != nil { |
| 227 | return |
| 228 | } |
| 229 | _, err = wh.Write(hostsData) |
| 230 | if err != nil { |
| 231 | return |
| 232 | } |
| 233 | } |
| 234 | } |