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/go.mod b/go.mod index 0543455d11..df50a8cd9f 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/urfave/cli/v2 go 1.18 require ( - github.com/BurntSushi/toml v1.3.2 - github.com/cpuguy83/go-md2man/v2 v2.0.4 + github.com/BurntSushi/toml v1.4.0 + github.com/cpuguy83/go-md2man/v2 v2.0.5 github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index c7f6ef0ab4..49387cb1c3 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,11 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= +github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc= +github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4= 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