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