Skip to content

Commit

Permalink
Adjust test data doc to use current md2man format
Browse files Browse the repository at this point in the history
and update the file comparison helper to diff when available
rather than introducing another dependency like this is done in the
`v1-maint` branch via github.com/stretchr/testify/require
  • Loading branch information
meatballhat committed Oct 13, 2024
1 parent 6227bb0 commit 6b0d484
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
61 changes: 54 additions & 7 deletions fish_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package cli

import (
"bytes"
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)

func TestFishCompletion(t *testing.T) {
Expand Down Expand Up @@ -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()
}
11 changes: 1 addition & 10 deletions testdata/expected-doc-full.man
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
.TH greet 8

.SH NAME
.PP
greet - Some app
greet \- Some app


.SH SYNOPSIS
.PP
greet

.EX
Expand All @@ -18,7 +16,6 @@ greet


.SH DESCRIPTION
.PP
Description of the application.

.PP
Expand All @@ -30,7 +27,6 @@ app [first_arg] [second_arg]


.SH GLOBAL OPTIONS
.PP
\fB--another-flag, -b\fP: another usage text

.PP
Expand All @@ -42,7 +38,6 @@ app [first_arg] [second_arg]

.SH COMMANDS
.SH config, c
.PP
another usage test

.PP
Expand All @@ -52,7 +47,6 @@ another usage test
\fB--flag, --fl, -f\fP="":

.SS sub-config, s, ss
.PP
another usage test

.PP
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6b0d484

Please sign in to comment.