-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetect_test.go
More file actions
345 lines (317 loc) · 8.73 KB
/
Copy pathdetect_test.go
File metadata and controls
345 lines (317 loc) · 8.73 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package slay
import (
"os"
"path/filepath"
"slices"
"testing"
)
// withTempDir creates a temp directory, chdirs into it, and restores after the test.
func withTempDir(t *testing.T) string {
t.Helper()
orig, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
dir := t.TempDir()
if err := os.Chdir(dir); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.Chdir(orig) })
return dir
}
func writeFile(t *testing.T, path, content string) {
t.Helper()
dir := filepath.Dir(path)
if dir != "." {
os.MkdirAll(dir, 0o755)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
func TestGetMainSourceFile_MainCpp(t *testing.T) {
withTempDir(t)
writeFile(t, "main.cpp", `int main() { return 0; }`)
got := GetMainSourceFile(nil)
if got != "main.cpp" {
t.Errorf("expected main.cpp, got %q", got)
}
}
func TestGetMainSourceFile_MainC(t *testing.T) {
withTempDir(t)
writeFile(t, "main.c", `int main() { return 0; }`)
got := GetMainSourceFile(nil)
if got != "main.c" {
t.Errorf("expected main.c, got %q", got)
}
}
func TestGetMainSourceFile_NamedFile(t *testing.T) {
withTempDir(t)
writeFile(t, "app.cpp", `#include <iostream>
int main() { return 0; }`)
got := GetMainSourceFile(nil)
if got != "app.cpp" {
t.Errorf("expected app.cpp, got %q", got)
}
}
func TestGetMainSourceFile_NoMain(t *testing.T) {
withTempDir(t)
writeFile(t, "lib.cpp", `void foo() {}`)
got := GetMainSourceFile(nil)
if got != "" {
t.Errorf("expected empty, got %q", got)
}
}
func TestGetMainSourceFile_ExcludesTests(t *testing.T) {
withTempDir(t)
writeFile(t, "main.cpp", `int main() { return 0; }`)
writeFile(t, "foo_test.cpp", `int main() { return 0; }`)
got := GetMainSourceFile([]string{"foo_test.cpp"})
if got != "main.cpp" {
t.Errorf("expected main.cpp, got %q", got)
}
}
func TestGetTestSources(t *testing.T) {
withTempDir(t)
writeFile(t, "main.cpp", `int main() { return 0; }`)
writeFile(t, "foo_test.cpp", `int main() { return 0; }`)
writeFile(t, "bar_test.cc", `int main() { return 0; }`)
tests := getTestSources()
if len(tests) != 2 {
t.Errorf("expected 2 test files, got %d: %v", len(tests), tests)
}
}
func TestGetTestSources_TestDotCpp(t *testing.T) {
withTempDir(t)
writeFile(t, "test.cpp", `int main() { return 0; }`)
tests := getTestSources()
if len(tests) != 1 {
t.Fatalf("expected 1 test file, got %d: %v", len(tests), tests)
}
if tests[0] != "test.cpp" {
t.Errorf("expected test.cpp, got %q", tests[0])
}
}
func TestIsTestFile(t *testing.T) {
cases := []struct {
path string
want bool
}{
{"foo_test.cpp", true},
{"bar_test.cc", true},
{"test.cpp", true},
{"test.c", true},
{"main.cpp", false},
{"helper.cpp", false},
{"testing.cpp", false},
}
for _, tc := range cases {
got := isTestFile(tc.path)
if got != tc.want {
t.Errorf("isTestFile(%q) = %v, want %v", tc.path, got, tc.want)
}
}
}
func TestGetDepSources(t *testing.T) {
withTempDir(t)
writeFile(t, "main.cpp", `int main() { return 0; }`)
writeFile(t, "util.cpp", `void util() {}`)
writeFile(t, "foo_test.cpp", `int main() { return 0; }`)
deps := getDepSources("main.cpp", []string{"foo_test.cpp"})
if len(deps) != 1 {
t.Fatalf("expected 1 dep, got %d: %v", len(deps), deps)
}
if deps[0] != "util.cpp" {
t.Errorf("expected util.cpp, got %q", deps[0])
}
}
func TestGetDepSources_WithCommon(t *testing.T) {
withTempDir(t)
writeFile(t, "main.cpp", `int main() { return 0; }`)
writeFile(t, "common/helper.cpp", `void helper() {}`)
deps := getDepSources("main.cpp", nil)
found := slices.Contains(deps, filepath.Join("common", "helper.cpp"))
if !found {
t.Errorf("expected common/helper.cpp in deps, got %v", deps)
}
}
func TestContainsMain(t *testing.T) {
dir := t.TempDir()
cases := []struct {
content string
want bool
}{
{"int main() { return 0; }", true},
{"int main(int argc, char** argv) {}", true},
{"void foo() {}", false},
{"// main(", false},
{"int\nmain() { return 0; }", true},
}
for i, tc := range cases {
path := filepath.Join(dir, "test"+string(rune('0'+i))+".cpp")
os.WriteFile(path, []byte(tc.content), 0o644)
got := containsMain(path)
if got != tc.want {
t.Errorf("containsMain(%q) = %v, want %v", tc.content, got, tc.want)
}
}
}
func TestScanSourceForFlags(t *testing.T) {
dir := t.TempDir()
tests := []struct {
name string
content string
check func(t *testing.T, p Project)
}{
{
name: "OpenMP",
content: "#pragma omp parallel\nint main() {}",
check: func(t *testing.T, p Project) { assertTrue(t, p.HasOpenMP, "HasOpenMP") },
},
{
name: "Boost",
content: "#include <boost/filesystem.hpp>\nint main() {}",
check: func(t *testing.T, p Project) {
assertTrue(t, p.HasBoost, "HasBoost")
assertContains(t, p.BoostLibs, "boost_filesystem")
},
},
{
name: "Qt6",
content: "#include <QApplication>\nint main() {}",
check: func(t *testing.T, p Project) { assertTrue(t, p.HasQt6, "HasQt6") },
},
{
name: "Filesystem",
content: "#include <filesystem>\nint main() {}",
check: func(t *testing.T, p Project) { assertTrue(t, p.HasFS, "HasFS") },
},
{
name: "Math",
content: "#include <cmath>\nint main() {}",
check: func(t *testing.T, p Project) { assertTrue(t, p.HasMathLib, "HasMathLib") },
},
{
name: "Threads",
content: "#include <thread>\nint main() {}",
check: func(t *testing.T, p Project) { assertTrue(t, p.HasThreads, "HasThreads") },
},
{
name: "Dlopen",
content: "#include <dlfcn.h>\nint main() {}",
check: func(t *testing.T, p Project) { assertTrue(t, p.HasDlopen, "HasDlopen") },
},
{
name: "Win64",
content: "#include <windows.h>\nint main() {}",
check: func(t *testing.T, p Project) { assertTrue(t, p.HasWin64, "HasWin64") },
},
{
name: "GLFW_Vulkan",
content: "#define GLFW_INCLUDE_VULKAN\n#include <GLFW/glfw3.h>\nint main() {}",
check: func(t *testing.T, p Project) { assertTrue(t, p.HasGLFWVulkan, "HasGLFWVulkan") },
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
path := filepath.Join(dir, tc.name+".cpp")
os.WriteFile(path, []byte(tc.content), 0o644)
var p Project
scanSourceForFlags(path, &p)
tc.check(t, p)
})
}
}
func TestDetectProject_HelloExample(t *testing.T) {
orig, _ := os.Getwd()
defer os.Chdir(orig)
helloDir := filepath.Join(orig, "examples", "hello")
if _, err := os.Stat(helloDir); os.IsNotExist(err) {
t.Skip("examples/hello not found")
}
os.Chdir(helloDir)
p := detectProject()
if p.MainSource != "main.cpp" {
t.Errorf("expected main.cpp, got %q", p.MainSource)
}
if p.IsC {
t.Error("expected IsC=false for C++ project")
}
if len(p.TestSources) == 0 {
t.Error("expected test sources in hello example")
}
if len(p.DepSources) == 0 {
t.Error("expected dep sources from common/ in hello example")
}
}
func TestExecutableName(t *testing.T) {
dir := t.TempDir()
orig, _ := os.Getwd()
defer os.Chdir(orig)
// Name from directory
subDir := filepath.Join(dir, "myproject")
os.MkdirAll(subDir, 0o755)
os.Chdir(subDir)
if got := executableName(); got != "myproject" {
t.Errorf("expected myproject, got %q", got)
}
// "src" directory should return "main"
srcDir := filepath.Join(dir, "src")
os.MkdirAll(srcDir, 0o755)
os.Chdir(srcDir)
if got := executableName(); got != "main" {
t.Errorf("expected main for src/ dir, got %q", got)
}
}
func TestDirectScanIncludes(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "test.cpp")
content := `#include <iostream>
#include <SDL2/SDL.h>
#include "myheader.h"
#include <boost/filesystem.hpp>
`
os.WriteFile(path, []byte(content), 0o644)
includes := directScanIncludes(path)
expected := []string{"iostream", "SDL2/SDL.h", "boost/filesystem.hpp"}
if len(includes) != len(expected) {
t.Fatalf("expected %d includes, got %d: %v", len(expected), len(includes), includes)
}
for i, want := range expected {
if includes[i] != want {
t.Errorf("include[%d] = %q, want %q", i, includes[i], want)
}
}
}
func TestUniqueStrings(t *testing.T) {
input := []string{"a", "b", "a", "c", "b"}
got := uniqueStrings(input)
if len(got) != 3 || got[0] != "a" || got[1] != "b" || got[2] != "c" {
t.Errorf("uniqueStrings(%v) = %v", input, got)
}
}
func TestAppendUnique(t *testing.T) {
s := []string{"a", "b"}
s = appendUnique(s, "b")
if len(s) != 2 {
t.Errorf("appendUnique should not duplicate, got %v", s)
}
s = appendUnique(s, "c")
if len(s) != 3 {
t.Errorf("appendUnique should append new, got %v", s)
}
}
// helpers
func assertTrue(t *testing.T, val bool, name string) {
t.Helper()
if !val {
t.Errorf("expected %s to be true", name)
}
}
func assertContains(t *testing.T, slice []string, val string) {
t.Helper()
if slices.Contains(slice, val) {
return
}
t.Errorf("expected %v to contain %q", slice, val)
}