summaryrefslogtreecommitdiffhomepage
path: root/format_test.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2021-05-07 12:56:10 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2021-05-10 17:48:26 +0200
commit4e9e5dad09bedc372b2cf389cc791d029420a1cf (patch)
tree660c52d23b2e1c69204181a108561708dd4ee7c0 /format_test.go
parent39e0b6dade0c64673330a26d4eb86d624337daf0 (diff)
main: check that code is formatted in unit test
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'format_test.go')
-rw-r--r--format_test.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/format_test.go b/format_test.go
new file mode 100644
index 0000000..60e212d
--- /dev/null
+++ b/format_test.go
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2021 WireGuard LLC. All Rights Reserved.
+ */
+package main
+
+import (
+ "bytes"
+ "go/format"
+ "io/fs"
+ "os"
+ "path/filepath"
+ "sync"
+ "testing"
+)
+
+func TestFormatting(t *testing.T) {
+ var wg sync.WaitGroup
+ filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error {
+ if err != nil {
+ t.Errorf("unable to walk %s: %v", path, err)
+ return nil
+ }
+ if d.IsDir() || filepath.Ext(path) != ".go" {
+ return nil
+ }
+ wg.Add(1)
+ go func(path string) {
+ defer wg.Done()
+ src, err := os.ReadFile(path)
+ if err != nil {
+ t.Errorf("unable to read %s: %v", path, err)
+ return
+ }
+ formatted, err := format.Source(src)
+ if err != nil {
+ t.Errorf("unable to format %s: %v", path, err)
+ return
+ }
+ if !bytes.Equal(src, formatted) {
+ t.Errorf("unformatted code: %s", path)
+ }
+ }(path)
+ return nil
+ })
+ wg.Wait()
+}