1
16 package wspace
17
18 import (
19 "os"
20 "path/filepath"
21 "testing"
22 )
23
24 func TestFind(t *testing.T) {
25 tmp, err := os.MkdirTemp(os.Getenv("TEST_TEMPDIR"), "")
26 if err != nil {
27 t.Fatal(err)
28 }
29 defer os.RemoveAll(tmp)
30 tmp, err = filepath.EvalSymlinks(tmp)
31 if err != nil {
32 t.Fatal(err)
33 }
34 if parent, err := FindRepoRoot(tmp); err == nil {
35 t.Skipf("WORKSPACE visible in parent %q of tmp %q", parent, tmp)
36 }
37
38 for _, tc := range []struct {
39 file, testdir string
40 shouldSucceed bool
41 }{
42 {"", tmp, false},
43 {filepath.Join(tmp, "WORKSPACE"), tmp, true},
44 {filepath.Join(tmp, "WORKSPACE.bazel"), tmp, true},
45 {filepath.Join(tmp, "WORKSPACE.bazel"), filepath.Join(tmp, "dir1"), true},
46
47 {filepath.Join(tmp, "WORKSPACE"), filepath.Join(tmp, "dir1", "WORKSPACE", "dir2"), true},
48 {filepath.Join(tmp, "WORKSPACE.bazel"), filepath.Join(tmp, "dir1", "WORKSPACE", "dir2"), true},
49
50 {filepath.Join(tmp, "WORKSPACE", "file.txt"), filepath.Join(tmp, "dir1"), false},
51 {filepath.Join(tmp, "WORKSPACE", "file.txt"), filepath.Join(tmp, "WORKSPACE", "dir1"), false},
52 } {
53 t.Run(tc.file, func(t *testing.T) {
54 if err := os.RemoveAll(tmp); err != nil {
55 t.Fatal(err)
56 }
57
58 if tc.file != "" {
59
60 if err := os.MkdirAll(filepath.Dir(tc.file), 0o755); err != nil {
61 t.Fatal(err)
62 }
63
64 if err := os.WriteFile(tc.file, nil, 0o755); err != nil {
65 t.Fatal(err)
66 }
67 }
68
69
70 if err := os.MkdirAll(tc.testdir, 0o755); err != nil {
71 t.Fatal(err)
72 }
73
74
75 dir, err := FindRepoRoot(tc.testdir)
76
77 if !tc.shouldSucceed {
78 if err == nil {
79 t.Errorf("FindRoot(%q): got %v, wanted failure", tc.testdir, dir)
80 }
81 return
82 }
83
84 if err != nil {
85 t.Errorf("FindRoot(%q): got error %v, wanted %v", tc.testdir, err, tc.file)
86 }
87
88 file := FindWORKSPACEFile(dir)
89 if file != tc.file {
90 t.Errorf("FindWorkspaceFile(FindRoot(%q)): got %v, wanted %v", tc.testdir, file, tc.file)
91 }
92 })
93 }
94 }
95
View as plain text