Skip to content

Commit 279acf1

Browse files
committed
Fix test cases
1 parent 974066f commit 279acf1

1 file changed

Lines changed: 55 additions & 8 deletions

File tree

hooks/jsonnet-fmt-hook/main_test.go

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,25 @@ package main
22

33
import (
44
"errors"
5+
"fmt"
6+
"regexp"
7+
"strings"
58
"testing"
69

710
"github.com/cybozu-private/pre-commit-jsonnet/testutil"
811
)
912

13+
var reAnsiEscape = regexp.MustCompile(`(?:\x1B[@-Z\\-_]|[\x80-\x9A\x9C-\x9F]|(?:\x1B\[|\x9B)[0-?]*[ -/]*[@-~])`)
14+
15+
func normalizeDiff(diff string) string {
16+
reSpaces := regexp.MustCompile(`\s+`)
17+
18+
normDiff := strings.TrimSpace(diff)
19+
normDiff = reSpaces.ReplaceAllString(normDiff, " ")
20+
normDiff = reAnsiEscape.ReplaceAllString(normDiff, "")
21+
return strings.ToLower(normDiff)
22+
}
23+
1024
func TestHasTestOpt(t *testing.T) {
1125
params := []struct {
1226
opts []string
@@ -59,7 +73,7 @@ func TestDiffJsonnetFmt(t *testing.T) {
5973
diff, err := diffJsonnetFmt(param.f)
6074
if err != nil {
6175
t.Errorf("param=%v, err=%s\n", param, err)
62-
//t.Errorf("param=%q, wantDiff=%v, %s", paam, err)
76+
continue
6377
}
6478

6579
if param.wantDiff && diff != nil && len(diff.text) > 0 {
@@ -77,6 +91,7 @@ func TestExecJsonnetFmt(t *testing.T) {
7791
defer teardown()
7892

7993
jsonnetFile := testutil.CreateJsonnet(t, tempDir, "valid.jsonnet", testutil.ValidJsonnetBody)
94+
malformedjsonnetFile := testutil.CreateJsonnet(t, tempDir, "malformed.jsonnet", testutil.MalformedJsonnetBody)
8095
invalidJsonnetFile := testutil.CreateJsonnet(t, tempDir, "invalid.jsonnet", testutil.InvalidJsonnetBody)
8196

8297
params := []struct {
@@ -90,9 +105,21 @@ func TestExecJsonnetFmt(t *testing.T) {
90105
wantErr: nil,
91106
},
92107
{
93-
f: testutil.CreateJsonnet(t, tempDir, "malformed.jsonnet", testutil.MalformedJsonnetBody),
94-
opts: []string{"--test"},
95-
wantErr: &FmtError{},
108+
f: malformedjsonnetFile,
109+
opts: []string{"--test"},
110+
wantErr: &FmtError{
111+
exitCode: 2,
112+
diff: &FileDiff{
113+
text: `...
114+
metadata: {
115+
name: name,
116+
namespace: namespace, // missing the trailing comma
117+
},
118+
}`,
119+
numInsert: 1,
120+
numDelete: 0,
121+
},
122+
},
96123
},
97124
{
98125
f: testutil.CreateJsonnet(t, tempDir, "in-place.jsonnet", testutil.MalformedJsonnetBody),
@@ -102,23 +129,43 @@ func TestExecJsonnetFmt(t *testing.T) {
102129
{
103130
f: invalidJsonnetFile,
104131
opts: []string{"--test"},
105-
wantErr: &FmtError{},
132+
wantErr: &FmtError{exitCode: 1, diff: nil},
106133
},
107134
}
108135

109136
for _, param := range params {
110137
err := execJsonnetFmt(param.f, param.opts)
138+
baseInfo := fmt.Sprintf("params=(%q, %q)", param.f, param.opts)
111139

112140
if param.wantErr == nil {
113141
if err == nil {
114142
continue
115143
}
116144

117-
t.Errorf("args='%q %q', want=%v, got=%v", param.f, param.opts, param.wantErr, err)
145+
t.Errorf("%s, want=%v, got=%v", baseInfo, param.wantErr, err)
146+
continue
147+
}
148+
149+
var want, got *FmtError
150+
151+
if !errors.As(param.wantErr, &want) {
152+
t.Errorf("unexpected error: %s, want=%v", baseInfo, param.wantErr)
153+
continue
154+
}
155+
if !errors.As(err, &got) {
156+
t.Errorf("unexpected error: %s, got=%v", baseInfo, err)
157+
}
158+
if got.exitCode != want.exitCode {
159+
t.Errorf("%s, want=%v, got=%v", baseInfo, want.exitCode, got.exitCode)
160+
}
161+
if got.diff == nil && want.diff == nil {
162+
continue
118163
}
119164

120-
if !errors.As(err, &param.wantErr) {
121-
t.Errorf("args='%q %q', want=%v, got=%v", param.f, param.opts, param.wantErr, err)
165+
gotDiff := normalizeDiff(got.diff.text)
166+
wantDiff := normalizeDiff(want.diff.text)
167+
if normalizeDiff(got.diff.text) != normalizeDiff(want.diff.text) {
168+
t.Errorf("%s, want=%v, got=%v", baseInfo, wantDiff, gotDiff)
122169
}
123170
}
124171
}

0 commit comments

Comments
 (0)