Skip to content

Commit 6b0d484

Browse files
committed
Adjust test data doc to use current md2man format
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
1 parent 6227bb0 commit 6b0d484

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

fish_test.go

+54-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package cli
22

33
import (
4-
"bytes"
4+
"context"
55
"os"
6+
"os/exec"
7+
"path/filepath"
8+
"strings"
69
"testing"
10+
"time"
711
)
812

913
func TestFishCompletion(t *testing.T) {
@@ -135,11 +139,54 @@ Should be a part of the same code block
135139
return app
136140
}
137141

138-
func expectFileContent(t *testing.T, file, got string) {
142+
func expectFileContent(t *testing.T, file, expected string) {
139143
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()
145192
}

testdata/expected-doc-full.man

+1-10
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
.TH greet 8
33

44
.SH NAME
5-
.PP
6-
greet - Some app
5+
greet \- Some app
76

87

98
.SH SYNOPSIS
10-
.PP
119
greet
1210

1311
.EX
@@ -18,7 +16,6 @@ greet
1816

1917

2018
.SH DESCRIPTION
21-
.PP
2219
Description of the application.
2320

2421
.PP
@@ -30,7 +27,6 @@ app [first_arg] [second_arg]
3027

3128

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

3632
.PP
@@ -42,7 +38,6 @@ app [first_arg] [second_arg]
4238

4339
.SH COMMANDS
4440
.SH config, c
45-
.PP
4641
another usage test
4742

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

5449
.SS sub-config, s, ss
55-
.PP
5650
another usage test
5751

5852
.PP
@@ -62,12 +56,10 @@ another usage test
6256
\fB--sub-flag, --sub-fl, -s\fP="":
6357

6458
.SH info, i, in
65-
.PP
6659
retrieve generic information
6760

6861
.SH some-command
6962
.SH usage, u
70-
.PP
7163
standard usage text
7264

7365
.EX
@@ -90,7 +82,6 @@ Should be a part of the same code block
9082
\fB--flag, --fl, -f\fP="":
9183

9284
.SS sub-usage, su
93-
.PP
9485
standard usage text
9586

9687
.PP

0 commit comments

Comments
 (0)