gofmt_test.go 1.1 KB

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