gofmt_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package meta
  7. import (
  8. "fmt"
  9. "os"
  10. "os/exec"
  11. "path/filepath"
  12. "strings"
  13. "testing"
  14. )
  15. var gofmtCheckDirs = []string{".", "../cmd", "../lib", "../test", "../script"}
  16. // Checks that files are properly gofmt:ed.
  17. func TestCheckGoFmt(t *testing.T) {
  18. for _, dir := range gofmtCheckDirs {
  19. err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  20. if err != nil {
  21. return err
  22. }
  23. if path == ".git" {
  24. return filepath.SkipDir
  25. }
  26. if filepath.Ext(path) != ".go" || strings.HasSuffix(path, ".pb.go") {
  27. return nil
  28. }
  29. cmd := exec.Command("gofmt", "-s", "-d", path)
  30. bs, err := cmd.CombinedOutput()
  31. if err != nil {
  32. return fmt.Errorf("%w: %s", err, string(bs))
  33. }
  34. if len(bs) != 0 {
  35. t.Errorf("File %s is not formatted correctly:\n\n%s", path, string(bs))
  36. }
  37. return nil
  38. })
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. }
  43. }