Repository navigation
Expand file tree
/
Copy pathlicense_test.go
More file actions
142 lines (121 loc) · 3.36 KB
/
Copy pathlicense_test.go
File metadata and controls
142 lines (121 loc) · 3.36 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package tailscaleroot
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/sagernet/tailscale/util/set"
)
// gitTrackedFiles returns the set of files known to git in the current
// directory tree, or nil if the tree is not a git checkout or git is
// unavailable.
func gitTrackedFiles(t *testing.T) set.Set[string] {
out, err := exec.Command("git", "ls-files", "-z").Output()
if err != nil {
t.Logf("git ls-files failed (%v); checking all files", err)
return nil
}
tracked := set.Set[string]{}
for f := range strings.SplitSeq(string(out), "\x00") {
if f != "" {
tracked.Add(f)
}
}
return tracked
}
func normalizeLineEndings(b []byte) []byte {
return bytes.ReplaceAll(b, []byte("\r\n"), []byte("\n"))
}
// TestLicenseHeaders checks that all Go files in the tree
// directory tree have a correct-looking Tailscale license header.
func TestLicenseHeaders(t *testing.T) {
want := normalizeLineEndings([]byte(strings.TrimLeft(`
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
`, "\n")))
exceptions := set.Of(
// Subprocess test harness code
"util/winutil/testdata/testrestartableprocesses/main.go",
"util/winutil/subprocess_windows_test.go",
// WireGuard copyright
"cmd/tailscale/cli/authenticode_windows.go",
"wgengine/router/osrouter/ifconfig_windows.go",
// noiseexplorer.com copyright
"control/controlbase/noiseexplorer_test.go",
// Generated eBPF management code
"derp/xdp/bpf_bpfeb.go",
"derp/xdp/bpf_bpfel.go",
// Generated kube deepcopy funcs file starts with a Go build tag + an empty line
"k8s-operator/apis/v1alpha1/zz_generated.deepcopy.go",
)
tracked := gitTrackedFiles(t)
err := filepath.Walk(".", func(path string, fi os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("path %s: %v", path, err)
}
if tracked != nil && !fi.IsDir() && !tracked.Contains(filepath.ToSlash(path)) {
// Ignore files not known to git (scratch files, etc).
return nil
}
if exceptions.Contains(filepath.ToSlash(path)) {
return nil
}
base := filepath.Base(path)
switch base {
case ".git", "node_modules", "tempfork":
return filepath.SkipDir
}
switch base {
case "zsyscall_windows.go":
// Generated code.
return nil
}
if strings.HasSuffix(base, ".config.ts") {
return nil
}
if strings.HasSuffix(base, "_string.go") {
// Generated file from go:generate stringer
return nil
}
ext := filepath.Ext(base)
switch ext {
default:
return nil
case ".go", ".ts", ".tsx":
}
buf := make([]byte, 512)
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if n, err := io.ReadAtLeast(f, buf, 512); err != nil && err != io.ErrUnexpectedEOF {
return err
} else {
buf = buf[:n]
}
buf = normalizeLineEndings(buf)
bufNoTrunc := buf
if i := bytes.Index(buf, []byte("\npackage ")); i != -1 {
buf = buf[:i]
}
if bytes.Contains(buf, want) {
return nil
}
if bytes.Contains(bufNoTrunc, []byte("BSD-3-Clause\npackage ")) {
t.Errorf("file %s has license header as a package doc; add a blank line before the package line", path)
return nil
}
t.Errorf("file %s is missing Tailscale copyright header:\n\n%s", path, want)
return nil
})
if err != nil {
t.Fatalf("Walk: %v", err)
}
}