github.com/liloew/wireguard-go@v0.0.0-20220224014633-9cd745e6f114/format_test.go (about)

     1  /* SPDX-License-Identifier: MIT
     2   *
     3   * Copyright (C) 2021 WireGuard LLC. All Rights Reserved.
     4   */
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"go/format"
    10  	"io/fs"
    11  	"os"
    12  	"path/filepath"
    13  	"runtime"
    14  	"sync"
    15  	"testing"
    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  }