| 16 | ) |
| 17 | |
| 18 | func TestFormatting(t *testing.T) { |
| 19 | var wg sync.WaitGroup |
| 20 | filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error { |
| 21 | if err != nil { |
| 22 | t.Errorf("unable to walk %s: %v", path, err) |
| 23 | return nil |
| 24 | } |
| 25 | if d.IsDir() || filepath.Ext(path) != ".go" { |
| 26 | return nil |
| 27 | } |
| 28 | wg.Add(1) |
| 29 | go func(path string) { |
| 30 | defer wg.Done() |
| 31 | src, err := os.ReadFile(path) |
| 32 | if err != nil { |
| 33 | t.Errorf("unable to read %s: %v", path, err) |
| 34 | return |
| 35 | } |
| 36 | if runtime.GOOS == "windows" { |
| 37 | src = bytes.ReplaceAll(src, []byte{'\r', '\n'}, []byte{'\n'}) |
| 38 | } |
| 39 | formatted, err := format.Source(src) |
| 40 | if err != nil { |
| 41 | t.Errorf("unable to format %s: %v", path, err) |
| 42 | return |
| 43 | } |
| 44 | if !bytes.Equal(src, formatted) { |
| 45 | t.Errorf("unformatted code: %s", path) |
| 46 | } |
| 47 | }(path) |
| 48 | return nil |
| 49 | }) |
| 50 | wg.Wait() |
| 51 | } |