]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
main: check that code is formatted in unit test
authorJason A. Donenfeld <Jason@zx2c4.com>
Fri, 7 May 2021 10:56:10 +0000 (12:56 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Mon, 10 May 2021 15:48:26 +0000 (17:48 +0200)
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Makefile
format_test.go [new file with mode: 0644]

index 223766ba929f17482557abc714ea4943f61dab8f..c86ffa32b186229ef67265764bf751aa5d80c1d4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -10,7 +10,7 @@ MAKEFLAGS += --no-print-directory
 generate-version-and-build:
        @export GIT_CEILING_DIRECTORIES="$(realpath $(CURDIR)/..)" && \
        tag="$$(git describe --dirty 2>/dev/null)" && \
-       ver="$$(printf 'package main\nconst Version = "%s"\n' "$$tag")" && \
+       ver="$$(printf 'package main\n\nconst Version = "%s"\n' "$$tag")" && \
        [ "$$(cat version.go 2>/dev/null)" != "$$ver" ] && \
        echo "$$ver" > version.go && \
        git update-index --assume-unchanged version.go || true
diff --git a/format_test.go b/format_test.go
new file mode 100644 (file)
index 0000000..60e212d
--- /dev/null
@@ -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()
+}