-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
main: check that code is formatted in unit test
Signed-off-by: Jason A. Donenfeld <[email protected]>
- Loading branch information
Showing
2 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* SPDX-License-Identifier: MIT | ||
* | ||
* Copyright (C) 2021 WireGuard LLC. All Rights Reserved. | ||
*/ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"go/format" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestFormatting(t *testing.T) { | ||
_, caller, _, ok := runtime.Caller(0) | ||
if !ok { | ||
t.Fatalf("unable to determine source location") | ||
} | ||
dir := filepath.Dir(caller) | ||
if _, err := os.Stat(caller); err != nil { | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
t.Fatalf("unable to get working directory: %v", wd) | ||
} | ||
caller = filepath.Join(wd, filepath.Base(caller)) | ||
if _, err := os.Stat(caller); err != nil { | ||
t.Fatalf("unable to determine source location") | ||
} | ||
dir = filepath.Dir(caller) | ||
} | ||
var wg sync.WaitGroup | ||
filepath.WalkDir(dir, 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() | ||
} |