diff --git a/fish_test.go b/fish_test.go index f74b55aceb..ca76f746da 100644 --- a/fish_test.go +++ b/fish_test.go @@ -1,9 +1,13 @@ package cli import ( - "bytes" + "context" "os" + "os/exec" + "path/filepath" + "strings" "testing" + "time" ) func TestFishCompletion(t *testing.T) { @@ -135,11 +139,54 @@ Should be a part of the same code block return app } -func expectFileContent(t *testing.T, file, got string) { +func expectFileContent(t *testing.T, file, expected string) { data, err := os.ReadFile(file) - // Ignore windows line endings - // TODO: Replace with bytes.ReplaceAll when support for Go 1.11 is dropped - data = bytes.Replace(data, []byte("\r\n"), []byte("\n"), -1) - expect(t, err, nil) - expect(t, got, string(data)) + if err != nil { + t.FailNow() + } + + expected = strings.TrimSpace(expected) + actual := strings.TrimSpace(strings.ReplaceAll(string(data), "\r\n", "\n")) + + if expected != actual { + t.Logf("file %q content does not match expected", file) + + tryDiff(t, expected, actual) + + t.FailNow() + } +} + +func tryDiff(t *testing.T, a, b string) { + diff, err := exec.LookPath("diff") + if err != nil { + t.Logf("no diff tool available") + + return + } + + td := t.TempDir() + aPath := filepath.Join(td, "a") + bPath := filepath.Join(td, "b") + + if err := os.WriteFile(aPath, []byte(a), 0o0644); err != nil { + t.Logf("failed to write: %v", err) + t.FailNow() + + return + } + + if err := os.WriteFile(bPath, []byte(b), 0o0644); err != nil { + t.Logf("failed to write: %v", err) + t.FailNow() + } + + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + t.Cleanup(cancel) + + cmd := exec.CommandContext(ctx, diff, "-u", aPath, bPath) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + _ = cmd.Run() } diff --git a/testdata/expected-doc-full.man b/testdata/expected-doc-full.man index 86de04e47b..f5fa6cacf8 100644 --- a/testdata/expected-doc-full.man +++ b/testdata/expected-doc-full.man @@ -2,12 +2,10 @@ .TH greet 8 .SH NAME -.PP -greet - Some app +greet \- Some app .SH SYNOPSIS -.PP greet .EX @@ -18,7 +16,6 @@ greet .SH DESCRIPTION -.PP Description of the application. .PP @@ -30,7 +27,6 @@ app [first_arg] [second_arg] .SH GLOBAL OPTIONS -.PP \fB--another-flag, -b\fP: another usage text .PP @@ -42,7 +38,6 @@ app [first_arg] [second_arg] .SH COMMANDS .SH config, c -.PP another usage test .PP @@ -52,7 +47,6 @@ another usage test \fB--flag, --fl, -f\fP="": .SS sub-config, s, ss -.PP another usage test .PP @@ -62,12 +56,10 @@ another usage test \fB--sub-flag, --sub-fl, -s\fP="": .SH info, i, in -.PP retrieve generic information .SH some-command .SH usage, u -.PP standard usage text .EX @@ -90,7 +82,6 @@ Should be a part of the same code block \fB--flag, --fl, -f\fP="": .SS sub-usage, su -.PP standard usage text .PP