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)
| 132 | // It then returns a function that will read in the config files into io.Writers. |
| 133 | // It automatically cleans up environment variables and written files. |
| 134 | func StubWriteConfig(t *testing.T) func(io.Writer, io.Writer) { |
| 135 | t.Helper() |
| 136 | tempDir := t.TempDir() |
| 137 | t.Setenv("GH_CONFIG_DIR", tempDir) |
| 138 | return func(wc io.Writer, wh io.Writer) { |
| 139 | config, err := os.Open(filepath.Join(tempDir, "config.yml")) |
| 140 | if err != nil { |
| 141 | return |
| 142 | } |
| 143 | defer config.Close() |
| 144 | configData, err := io.ReadAll(config) |
| 145 | if err != nil { |
| 146 | return |
| 147 | } |
| 148 | _, err = wc.Write(configData) |
| 149 | if err != nil { |
| 150 | return |
| 151 | } |
| 152 | |
| 153 | hosts, err := os.Open(filepath.Join(tempDir, "hosts.yml")) |
| 154 | if err != nil { |
| 155 | return |
| 156 | } |
| 157 | defer hosts.Close() |
| 158 | hostsData, err := io.ReadAll(hosts) |
| 159 | if err != nil { |
| 160 | return |
| 161 | } |
| 162 | _, err = wh.Write(hostsData) |
| 163 | if err != nil { |
| 164 | return |
| 165 | } |
| 166 | } |
| 167 | } |