Skip to content

Commit 4e9e5da

Browse files
committed
main: check that code is formatted in unit test
Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 39e0b6d commit 4e9e5da

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ MAKEFLAGS += --no-print-directory
1010
generate-version-and-build:
1111
@export GIT_CEILING_DIRECTORIES="$(realpath $(CURDIR)/..)" && \
1212
tag="$$(git describe --dirty 2>/dev/null)" && \
13-
ver="$$(printf 'package main\nconst Version = "%s"\n' "$$tag")" && \
13+
ver="$$(printf 'package main\n\nconst Version = "%s"\n' "$$tag")" && \
1414
[ "$$(cat version.go 2>/dev/null)" != "$$ver" ] && \
1515
echo "$$ver" > version.go && \
1616
git update-index --assume-unchanged version.go || true

Diff for: format_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* SPDX-License-Identifier: MIT
2+
*
3+
* Copyright (C) 2021 WireGuard LLC. All Rights Reserved.
4+
*/
5+
package main
6+
7+
import (
8+
"bytes"
9+
"go/format"
10+
"io/fs"
11+
"os"
12+
"path/filepath"
13+
"sync"
14+
"testing"
15+
)
16+
17+
func TestFormatting(t *testing.T) {
18+
var wg sync.WaitGroup
19+
filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error {
20+
if err != nil {
21+
t.Errorf("unable to walk %s: %v", path, err)
22+
return nil
23+
}
24+
if d.IsDir() || filepath.Ext(path) != ".go" {
25+
return nil
26+
}
27+
wg.Add(1)
28+
go func(path string) {
29+
defer wg.Done()
30+
src, err := os.ReadFile(path)
31+
if err != nil {
32+
t.Errorf("unable to read %s: %v", path, err)
33+
return
34+
}
35+
formatted, err := format.Source(src)
36+
if err != nil {
37+
t.Errorf("unable to format %s: %v", path, err)
38+
return
39+
}
40+
if !bytes.Equal(src, formatted) {
41+
t.Errorf("unformatted code: %s", path)
42+
}
43+
}(path)
44+
return nil
45+
})
46+
wg.Wait()
47+
}

0 commit comments

Comments
 (0)