Skip to content

Commit 6abbf95

Browse files
authored
Merge pull request #1 from cybozu/fix_jsonnetfmt_output
Fix output of jsonnet-fmt hook with --test option
2 parents 67bf6a8 + 9f73458 commit 6abbf95

4 files changed

Lines changed: 15 additions & 7 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fmt:
1212

1313
.PHONY: lint
1414
lint:
15-
@golangci-lint run ./...
15+
@golangci-lint run -v ./...
1616

1717
.PHONY: test
1818
test:

hooks/jsonnet-fmt-hook/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log"
77
"os"
8+
"regexp"
89
"strings"
910

1011
"io/ioutil"
@@ -76,12 +77,13 @@ func summarizeDiff(diffs []diffmatchpatch.Diff) *FileDiff {
7677
}
7778
}
7879
splitLines := func(text string) []string {
79-
return strings.Split(strings.TrimSpace(strings.ReplaceAll(text, "\r\n", "\n")), "\n")
80+
return strings.Split(strings.ReplaceAll(text, "\r\n", "\n"), "\n")
8081
}
8182

8283
const maxShowLine = 3
8384
const omissionText = "...\n"
8485
lastDiffIdx := len(diffs) - 1
86+
spaceRegexp := regexp.MustCompile(`\s`)
8587

8688
for i, diff := range diffs {
8789
text := diff.Text
@@ -92,7 +94,7 @@ func summarizeDiff(diffs []diffmatchpatch.Diff) *FileDiff {
9294
writeString(&builder, ansiGreen+text+ansiReset)
9395
fileDiff.numInsert += len(text)
9496
case diffmatchpatch.DiffDelete:
95-
writeString(&builder, ansiRed+text+ansiReset)
97+
writeString(&builder, ansiRed+regexp.QuoteMeta(spaceRegexp.ReplaceAllString(text, "␣"))+ansiReset)
9698
fileDiff.numDelete += len(text)
9799
case diffmatchpatch.DiffEqual:
98100
var showHead, showTail bool

hooks/jsonnet-fmt-hook/main_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ func normalizeDiff(diff string) string {
1616
reSpaces := regexp.MustCompile(`\s+`)
1717

1818
normDiff := strings.TrimSpace(diff)
19-
normDiff = reSpaces.ReplaceAllString(normDiff, " ")
19+
normDiff = strings.ReplaceAll(normDiff, "\n", " ")
2020
normDiff = reAnsiEscape.ReplaceAllString(normDiff, "")
21+
normDiff = reSpaces.ReplaceAllString(normDiff, " ")
2122
return strings.ToLower(normDiff)
2223
}
2324

@@ -38,6 +39,10 @@ func TestHasTestOpt(t *testing.T) {
3839
opts: []string{"test", "hoge"},
3940
want: false,
4041
},
42+
{
43+
opts: []string{"-t"},
44+
want: false,
45+
},
4146
}
4247

4348
for _, param := range params {
@@ -116,9 +121,10 @@ func TestExecJsonnetFmt(t *testing.T) {
116121
text: `...
117122
metadata: {
118123
name: name,
119-
namespace: namespace, // missing the trailing comma
124+
namespace: namespace,
120125
},
121-
}`,
126+
}
127+
... `,
122128
numInsert: 1,
123129
numDelete: 0,
124130
},

testutil/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var (
1515
kind: 'SubNamespace',
1616
metadata: {
1717
name: name,
18-
namespace: namespace // missing the trailing comma
18+
namespace: namespace
1919
},
2020
}
2121
`)

0 commit comments

Comments
 (0)