gofmt_test.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. // Checks for authors that are not mentioned in AUTHORS
  7. package meta
  8. import (
  9. "os"
  10. "os/exec"
  11. "path/filepath"
  12. "testing"
  13. )
  14. var gofmtCheckDirs = []string{".", "../cmd", "../lib", "../test", "../script"}
  15. func TestCheckGoFmt(t *testing.T) {
  16. for _, dir := range gofmtCheckDirs {
  17. err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  18. if err != nil {
  19. return err
  20. }
  21. if path == ".git" {
  22. return filepath.SkipDir
  23. }
  24. if filepath.Ext(path) != ".go" {
  25. return nil
  26. }
  27. cmd := exec.Command("gofmt", "-s", "-d", path)
  28. bs, err := cmd.CombinedOutput()
  29. if err != nil {
  30. return err
  31. }
  32. if len(bs) != 0 {
  33. t.Errorf("File %s is not formatted correctly:\n\n%s", path, string(bs))
  34. }
  35. return nil
  36. })
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. }
  41. }