2021-05-07 12:56:10 +02:00
|
|
|
/* SPDX-License-Identifier: MIT
|
|
|
|
*
|
2022-09-20 17:21:32 +02:00
|
|
|
* Copyright (C) 2017-2022 WireGuard LLC. All Rights Reserved.
|
2021-05-07 12:56:10 +02:00
|
|
|
*/
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"go/format"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-05-10 22:23:32 +02:00
|
|
|
"runtime"
|
2021-05-07 12:56:10 +02:00
|
|
|
"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
|
|
|
|
}
|
2021-05-10 22:23:32 +02:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
src = bytes.ReplaceAll(src, []byte{'\r', '\n'}, []byte{'\n'})
|
|
|
|
}
|
2021-05-07 12:56:10 +02:00
|
|
|
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()
|
|
|
|
}
|