aboutsummaryrefslogtreecommitdiff
path: root/meta/gofmt_test.go
blob: 7dc6050351c3995581b0520f3f9d4894283c1344 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (C) 2015 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

package meta

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
)

var gofmtCheckDirs = []string{".", "../cmd", "../lib", "../test", "../script"}

// Checks that files are properly gofmt:ed.
func TestCheckGoFmt(t *testing.T) {
	for _, dir := range gofmtCheckDirs {
		err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
			if err != nil {
				return err
			}
			if path == ".git" {
				return filepath.SkipDir
			}
			if filepath.Ext(path) != ".go" || strings.HasSuffix(path, ".pb.go") {
				return nil
			}
			cmd := exec.Command("gofmt", "-s", "-d", path)
			bs, err := cmd.CombinedOutput()
			if err != nil {
				return fmt.Errorf("%w: %s", err, string(bs))
			}
			if len(bs) != 0 {
				t.Errorf("File %s is not formatted correctly:\n\n%s", path, string(bs))
			}
			return nil
		})
		if err != nil {
			t.Fatal(err)
		}
	}
}