|
1 | 1 | package cli
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bytes" |
| 4 | + "context" |
5 | 5 | "os"
|
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
6 | 9 | "testing"
|
| 10 | + "time" |
7 | 11 | )
|
8 | 12 |
|
9 | 13 | func TestFishCompletion(t *testing.T) {
|
@@ -135,11 +139,54 @@ Should be a part of the same code block
|
135 | 139 | return app
|
136 | 140 | }
|
137 | 141 |
|
138 |
| -func expectFileContent(t *testing.T, file, got string) { |
| 142 | +func expectFileContent(t *testing.T, file, expected string) { |
139 | 143 | data, err := os.ReadFile(file)
|
140 |
| - // Ignore windows line endings |
141 |
| - // TODO: Replace with bytes.ReplaceAll when support for Go 1.11 is dropped |
142 |
| - data = bytes.Replace(data, []byte("\r\n"), []byte("\n"), -1) |
143 |
| - expect(t, err, nil) |
144 |
| - expect(t, got, string(data)) |
| 144 | + if err != nil { |
| 145 | + t.FailNow() |
| 146 | + } |
| 147 | + |
| 148 | + expected = strings.TrimSpace(expected) |
| 149 | + actual := strings.TrimSpace(strings.ReplaceAll(string(data), "\r\n", "\n")) |
| 150 | + |
| 151 | + if expected != actual { |
| 152 | + t.Logf("file %q content does not match expected", file) |
| 153 | + |
| 154 | + tryDiff(t, expected, actual) |
| 155 | + |
| 156 | + t.FailNow() |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func tryDiff(t *testing.T, a, b string) { |
| 161 | + diff, err := exec.LookPath("diff") |
| 162 | + if err != nil { |
| 163 | + t.Logf("no diff tool available") |
| 164 | + |
| 165 | + return |
| 166 | + } |
| 167 | + |
| 168 | + td := t.TempDir() |
| 169 | + aPath := filepath.Join(td, "a") |
| 170 | + bPath := filepath.Join(td, "b") |
| 171 | + |
| 172 | + if err := os.WriteFile(aPath, []byte(a), 0o0644); err != nil { |
| 173 | + t.Logf("failed to write: %v", err) |
| 174 | + t.FailNow() |
| 175 | + |
| 176 | + return |
| 177 | + } |
| 178 | + |
| 179 | + if err := os.WriteFile(bPath, []byte(b), 0o0644); err != nil { |
| 180 | + t.Logf("failed to write: %v", err) |
| 181 | + t.FailNow() |
| 182 | + } |
| 183 | + |
| 184 | + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) |
| 185 | + t.Cleanup(cancel) |
| 186 | + |
| 187 | + cmd := exec.CommandContext(ctx, diff, "-u", aPath, bPath) |
| 188 | + cmd.Stdout = os.Stdout |
| 189 | + cmd.Stderr = os.Stderr |
| 190 | + |
| 191 | + _ = cmd.Run() |
145 | 192 | }
|
0 commit comments