-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcovered_test.go
More file actions
159 lines (140 loc) · 3.6 KB
/
covered_test.go
File metadata and controls
159 lines (140 loc) · 3.6 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package reuse
import (
"os"
"path/filepath"
"sort"
"testing"
)
func TestIsIgnoredDir(t *testing.T) {
ignored := []string{".git", ".hg", ".sl", "LICENSES", ".reuse"}
for _, name := range ignored {
if !IsIgnoredDir(name) {
t.Errorf("expected %q to be ignored", name)
}
}
notIgnored := []string{"src", "docs", "licenses", "git", "reuse"}
for _, name := range notIgnored {
if IsIgnoredDir(name) {
t.Errorf("expected %q to not be ignored", name)
}
}
}
func TestIsIgnoredFile(t *testing.T) {
ignored := []string{
"LICENSE", "LICENSE.txt", "LICENSE-MIT", "LICENSE.md",
"LICENCE", "LICENCE.txt",
"COPYING", "COPYING.txt", "COPYING-GPL",
"foo.license", "image.png.license",
"REUSE.toml",
"data.spdx", "data.spdx.json", "data.spdx.rdf",
"data.spdx.xml", "data.spdx.yml", "data.spdx.yaml",
".git",
".hgtags",
}
for _, name := range ignored {
if !IsIgnoredFile(name) {
t.Errorf("expected %q to be ignored", name)
}
}
notIgnored := []string{
"main.go", "README.md", "Makefile",
"license_check.go", // contains "license" but not the LICENSE pattern
"COPYING_test.go", // doesn't match COPYING[-.]
}
for _, name := range notIgnored {
if IsIgnoredFile(name) {
t.Errorf("expected %q to not be ignored", name)
}
}
}
func TestIsCoveredFile(t *testing.T) {
covered := []string{
"main.go",
"src/util.go",
"docs/guide.md",
"README.md",
}
for _, path := range covered {
if !IsCoveredFile(path) {
t.Errorf("expected %q to be covered", path)
}
}
notCovered := []string{
"LICENSE",
"LICENSE.txt",
"COPYING",
".git/config",
"LICENSES/MIT.txt",
".reuse/dep5",
"REUSE.toml",
"foo.license",
"data.spdx.json",
}
for _, path := range notCovered {
if IsCoveredFile(path) {
t.Errorf("expected %q to not be covered", path)
}
}
}
func TestCoveredFiles(t *testing.T) {
root := t.TempDir()
// Create covered files.
mkfile(t, root, "main.go", "package main")
mkfile(t, root, "src/util.go", "package src")
mkfile(t, root, "README.md", "# Hello")
// Create excluded files.
mkfile(t, root, "LICENSE", "MIT License")
mkfile(t, root, "COPYING", "GPL")
mkfile(t, root, "REUSE.toml", "version = 1")
mkfile(t, root, "image.png.license", "SPDX...")
mkfile(t, root, "LICENSES/MIT.txt", "MIT text")
mkfile(t, root, ".reuse/dep5", "Format: ...")
mkfile(t, root, ".git/config", "[core]")
mkfile(t, root, "data.spdx.json", "{}")
// Create zero-byte file.
mkfile(t, root, "empty.txt", "")
files, err := CoveredFiles(root)
if err != nil {
t.Fatal(err)
}
sort.Strings(files)
expected := []string{"README.md", "main.go", "src/util.go"}
assertSlice(t, files, expected)
}
func TestCoveredFiles_NoSymlinks(t *testing.T) {
root := t.TempDir()
mkfile(t, root, "real.go", "package main")
target := filepath.Join(root, "real.go")
link := filepath.Join(root, "link.go")
if err := os.Symlink(target, link); err != nil {
t.Skip("symlinks not supported")
}
files, err := CoveredFiles(root)
if err != nil {
t.Fatal(err)
}
// Only real.go should appear, not link.go.
assertSlice(t, files, []string{"real.go"})
}
func assertSlice(t *testing.T, got, want []string) {
t.Helper()
if len(got) != len(want) {
t.Errorf("got %v (len %d), want %v (len %d)", got, len(got), want, len(want))
return
}
for i := range got {
if got[i] != want[i] {
t.Errorf("index %d: got %q, want %q", i, got[i], want[i])
}
}
}
func mkfile(t *testing.T, root, rel, content string) {
t.Helper()
path := filepath.Join(root, rel)
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatal(err)
}
}