File: template_test.go

package info (click to toggle)
golang-github-zmap-zlint 3.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,008 kB
  • sloc: sh: 162; makefile: 38
file content (88 lines) | stat: -rw-r--r-- 2,277 bytes parent folder | download
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package lints

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

var (
	// filesChecked is a global counter of the number of files tested by
	// checkForLeftovers.
	filesChecked int
)

// checkForLeftovers checks the given filename (assumed to be a .go src file)
// contains none of the template leftovers. An error is returned if there is
// a problem opening or reading the file, or if any template leftovers are
// found.
func checkForLeftovers(filename string) error {
	// See the `template` file in the root directory of ZLint.
	// None of these strings should appear outside of the template. They indicate
	// the programmer forgot to replace template text.
	leftovers := []string{
		`"Fill this in..."`,
		`"Change this..."`,
		"// Add conditions for application here",
		"// Add actual lint here",
		"Change this to match source TEXT",
	}

	src, err := os.ReadFile(filename)
	if err != nil {
		return err
	}

	filesChecked++
	for _, leftover := range leftovers {
		if bytes.Contains(src, []byte(leftover)) {
			return fmt.Errorf(
				"file %q contains template leftover %q",
				filename, leftover)
		}
	}

	return nil
}

// checkFile is a filepath.WalkFunc handler that checks .go files for leftovers.
func checkFile(path string, info os.FileInfo, err error) error {
	// Abort on any incoming errs from filepath.Walk
	if err != nil {
		return err
	}
	// Don't check directories
	if info.IsDir() {
		return nil
	}
	// Only check .go files
	if !strings.HasSuffix(path, ".go") {
		return nil
	}
	// Don't check the template test file, it has the strings we're checking for
	// by design!
	if strings.HasSuffix(path, "template_test.go") {
		return nil
	}

	// Check the path for leftovers
	return checkForLeftovers(path)
}

// TestLeftoverTemplates tests that no .go files under the current directory
// contain leftovers from the new lint template that are intended to be replaced
// by the programmer.
func TestLeftoverTemplates(t *testing.T) {
	if err := filepath.Walk("./", checkFile); err != nil {
		t.Errorf("%v", err)
	}

	// If no files were checked that means something fishy happened. Perhaps the
	// test was moved to a different directory?
	if filesChecked == 0 {
		t.Fatalf("failed to find any files to check while traversing ./")
	}
}