Skip to content

Commit 590936f

Browse files
committed
test(diff): adapted tests following the migration of the diff package from go-swagger
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 3b04dd8 commit 590936f

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

diff/spec_analyser_test.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ import (
1212
"strings"
1313
"testing"
1414

15-
"github.com/go-openapi/testify/v2/require"
16-
17-
"github.com/go-openapi/loads"
15+
"github.com/go-openapi/analysis/internal/antest"
1816

19-
"github.com/go-swagger/go-swagger/cmd/swagger/commands/internal/cmdtest"
17+
"github.com/go-openapi/testify/v2/assert"
18+
"github.com/go-openapi/testify/v2/require"
2019
)
2120

2221
// TestDiffForVariousCombinations - computes the diffs for a number
@@ -48,7 +47,7 @@ func TestDiffForVariousCombinations(t *testing.T) {
4847
out, err, warn := diffs.ReportAllDiffs(false)
4948
require.NoError(t, err)
5049

51-
if !cmdtest.AssertReadersContent(t, true, tc.expectedLines, out) {
50+
if !assertReadersContent(t, true, tc.expectedLines, out) {
5251
t.Logf("unexpected content for fixture %q[%d] (file: %s)", tc.name, i, tc.expectedFile)
5352
}
5453

@@ -66,19 +65,17 @@ func TestDiffForVariousCombinations(t *testing.T) {
6665
}
6766

6867
func getDiffs(oldSpecPath, newSpecPath string) (SpecDifferences, error) {
69-
swaggerDoc1 := oldSpecPath
70-
specDoc1, err := loads.Spec(swaggerDoc1)
68+
spec1, err := antest.LoadSpec(oldSpecPath)
7169
if err != nil {
7270
return nil, err
7371
}
7472

75-
swaggerDoc2 := newSpecPath
76-
specDoc2, err := loads.Spec(swaggerDoc2)
73+
spec2, err := antest.LoadSpec(newSpecPath)
7774
if err != nil {
7875
return nil, err
7976
}
8077

81-
return Compare(specDoc1.Spec(), specDoc2.Spec())
78+
return Compare(spec1, spec2)
8279
}
8380

8481
func makeTestCases(t testing.TB, matches []string) []testCaseData {
@@ -113,8 +110,8 @@ func makeTestCases(t testing.TB, matches []string) []testCaseData {
113110
}
114111

115112
func TestIssue2962(t *testing.T) {
116-
oldSpec := filepath.Join("..", "..", "..", "..", "fixtures", "bugs", "2962", "old.json")
117-
newSpec := filepath.Join("..", "..", "..", "..", "fixtures", "bugs", "2962", "new.json")
113+
oldSpec := filepath.Join("fixtures", "bugs", "2962", "old.json")
114+
newSpec := filepath.Join("fixtures", "bugs", "2962", "new.json")
118115

119116
t.Run("should diff", func(t *testing.T) {
120117
diffs, err := getDiffs(oldSpec, newSpec)
@@ -133,7 +130,7 @@ func TestIssue2962(t *testing.T) {
133130
}
134131

135132
func fixturePath(file string, parts ...string) string {
136-
return filepath.Join("..", "..", "..", "..", "fixtures", "diff", strings.Join(append([]string{file}, parts...), ""))
133+
return filepath.Join("fixtures", strings.Join(append([]string{file}, parts...), ""))
137134
}
138135

139136
type testCaseData struct {
@@ -159,3 +156,24 @@ func linesInFile(t testing.TB, fileName string) io.Reader {
159156
// consumes a bit of extra memory, but no longer leaks open files
160157
return bytes.NewBuffer(file)
161158
}
159+
160+
func assertReadersContent(t testing.TB, noBlanks bool, expected, actual io.Reader) bool {
161+
t.Helper()
162+
163+
e, err := io.ReadAll(expected)
164+
require.NoError(t, err)
165+
166+
a, err := io.ReadAll(actual)
167+
require.NoError(t, err)
168+
169+
var wants, got strings.Builder
170+
_, _ = wants.Write(e)
171+
_, _ = got.Write(a)
172+
173+
if noBlanks {
174+
r := strings.NewReplacer(" ", "", "\t", "", "\n", "", "\r", "")
175+
return assert.EqualTf(t, r.Replace(wants.String()), r.Replace(got.String()), "expected:\n%s\ngot:\n%s", wants.String(), got.String())
176+
}
177+
178+
return assert.EqualT(t, wants.String(), got.String())
179+
}

diff/spec_difference_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/go-openapi/testify/v2/require"
1010

11-
"github.com/go-swagger/go-swagger/cmd/swagger/commands/diff"
11+
"github.com/go-openapi/analysis/diff"
1212
)
1313

1414
func TestMatches(t *testing.T) {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/go-openapi/swag/loading v0.25.5
99
github.com/go-openapi/swag/mangling v0.25.5
1010
github.com/go-openapi/testify/v2 v2.4.1
11+
golang.org/x/text v0.34.0
1112
)
1213

1314
require (
@@ -23,7 +24,6 @@ require (
2324
github.com/oklog/ulid/v2 v2.1.1 // indirect
2425
go.yaml.in/yaml/v3 v3.0.4 // indirect
2526
golang.org/x/net v0.50.0 // indirect
26-
golang.org/x/text v0.34.0 // indirect
2727
)
2828

2929
go 1.25.0

0 commit comments

Comments
 (0)