1
16
17 package wspace
18
19 import (
20 "io/ioutil"
21 "os"
22 "path/filepath"
23 "reflect"
24 "testing"
25 )
26
27 type testCase struct {
28 input string
29 expectedRoot, expectedRest string
30 }
31
32 func runBasicTestWithRepoRootFile(t *testing.T, repoRootFile string) {
33 tmp, err := ioutil.TempDir("", "")
34 if err != nil {
35 t.Fatal(err)
36 }
37 defer os.RemoveAll(tmp)
38 if err := os.MkdirAll(filepath.Join(tmp, "a", "b", "c"), 0755); err != nil {
39 t.Fatal(err)
40 }
41 if err := ioutil.WriteFile(filepath.Join(tmp, repoRootFile), nil, 0755); err != nil {
42 t.Fatal(err)
43 }
44 if err := ioutil.WriteFile(filepath.Join(tmp, "a", "b", repoRootFile), nil, 0755); err != nil {
45 t.Fatal(err)
46 }
47
48 for _, tc := range []testCase{
49 {tmp, tmp, ""},
50 {filepath.Join(tmp, "a"), tmp, "a"},
51 {filepath.Join(tmp, "a", "b"), filepath.Join(tmp, "a", "b"), ""},
52 {filepath.Join(tmp, "a", "b", "c"), filepath.Join(tmp, "a", "b"), "c"},
53 {"a", "", ""},
54 } {
55 root, rest := FindWorkspaceRoot(tc.input)
56 if root != tc.expectedRoot || rest != tc.expectedRest {
57 t.Errorf("FindWorkspaceRoot(%q) = %q, %q; want %q, %q", tc.input, root, rest, tc.expectedRoot, tc.expectedRest)
58 }
59 }
60 }
61
62 func TestBasic(t *testing.T) {
63 runBasicTestWithRepoRootFile(t, ".buckconfig")
64 runBasicTestWithRepoRootFile(t, workspaceFile)
65 }
66
67 func TestFindRepoBuildfiles(t *testing.T) {
68 tmp, err := ioutil.TempDir("", "")
69 if err != nil {
70 t.Fatal(err)
71 }
72 defer os.RemoveAll(tmp)
73 workspace := []byte(`
74 git_repository(
75 name = "a",
76 build_file = "a.BUILD",
77 )
78 new_http_archive(
79 name = "b",
80 build_file = "b.BUILD",
81 )
82 new_local_repository(
83 name = "c",
84 build_file = "c.BUILD",
85 )
86 new_git_repository(
87 name = "d",
88 build_file = "d.BUILD",
89 )
90 git_repository(
91 name = "e",
92 build_file_content = "n/a",
93 )
94 new_http_archive(
95 name = "f",
96 build_file = "//third_party:f.BUILD",
97 )
98 `)
99 if err := ioutil.WriteFile(filepath.Join(tmp, workspaceFile), workspace, 0755); err != nil {
100 t.Fatal(err)
101 }
102 files, err := FindRepoBuildFiles(tmp)
103 if err != nil {
104 t.Fatal(err)
105 }
106 expected := map[string]string{
107 "a": filepath.Join(tmp, "a.BUILD"),
108 "b": filepath.Join(tmp, "b.BUILD"),
109 "c": filepath.Join(tmp, "c.BUILD"),
110 "d": filepath.Join(tmp, "d.BUILD"),
111 "f": filepath.Join(tmp, "third_party/f.BUILD"),
112 }
113 if !reflect.DeepEqual(files, expected) {
114 t.Errorf("FileRepoBuildFiles(`%s`) = %q; want %q", workspace, files, expected)
115 }
116 }
117
118 func checkSplitFilePathOutput(t *testing.T, name, filename, expectedWorkspaceRoot, expectedPkg, expectedLabel string) {
119 workspaceRoot, pkg, label := SplitFilePath(filename)
120 if workspaceRoot != expectedWorkspaceRoot {
121 t.Errorf("%s: expected the workspace root to be %q, was %q instead", name, expectedWorkspaceRoot, workspaceRoot)
122 }
123 if pkg != expectedPkg {
124 t.Errorf("%s: expected the package name to be %q, was %q instead", name, expectedPkg, pkg)
125 }
126 if label != expectedLabel {
127 t.Errorf("%s: expected the label to be %q, was %q instead", name, expectedLabel, label)
128 }
129 }
130
131 func TestSplitFilePath(t *testing.T) {
132 dir, err := ioutil.TempDir("", "")
133 if err != nil {
134 t.Error(err)
135 }
136 defer os.RemoveAll(dir)
137
138 if err := os.MkdirAll(filepath.Join(dir, "path", "to", "package"), os.ModePerm); err != nil {
139 t.Error(err)
140 }
141
142 filename := filepath.Join(dir, "path", "to", "package", "file.bzl")
143 checkSplitFilePathOutput(t, "No WORKSPACE file", filename, "", "", "")
144
145
146 if err := ioutil.WriteFile(filepath.Join(dir, "WORKSPACE"), []byte{}, os.ModePerm); err != nil {
147 t.Error(err)
148 }
149 checkSplitFilePathOutput(t, "WORKSPACE file exists", filename, dir, "", "path/to/package/file.bzl")
150 checkSplitFilePathOutput(t, "WORKSPACE file exists, empty package", filepath.Join(dir, "file.bzl"), dir, "", "file.bzl")
151
152
153 buildPath := filepath.Join(dir, "path", "to", "BUILD")
154 if err := ioutil.WriteFile(buildPath, []byte{}, os.ModePerm); err != nil {
155 t.Error(err)
156 }
157 checkSplitFilePathOutput(t, "WORKSPACE and BUILD files exists 1", buildPath, dir, "path/to", "BUILD")
158 checkSplitFilePathOutput(t, "WORKSPACE and BUILD files exists 2", filename, dir, "path/to", "package/file.bzl")
159
160
161 subBuildPath := filepath.Join(dir, "path", "to", "package", "BUILD.bazel")
162 if err := ioutil.WriteFile(subBuildPath, []byte{}, os.ModePerm); err != nil {
163 t.Error(err)
164 }
165 checkSplitFilePathOutput(t, "WORKSPACE and two BUILD files exists 1", subBuildPath, dir, "path/to/package", "BUILD.bazel")
166 checkSplitFilePathOutput(t, "WORKSPACE and two BUILD files exists 2", filename, dir, "path/to/package", "file.bzl")
167
168
169 if err := os.Rename(filepath.Join(dir, "WORKSPACE"), filepath.Join(dir, "WORKSPACE.bazel")); err != nil {
170 t.Error(err)
171 }
172 checkSplitFilePathOutput(t, "WORKSPACE file exists", filename, dir, "path/to/package", "file.bzl")
173 checkSplitFilePathOutput(t, "WORKSPACE file exists, empty package", filepath.Join(dir, "file.bzl"), dir, "", "file.bzl")
174
175
176 newRoot := filepath.Join(dir, "path")
177 if err := os.MkdirAll(newRoot, os.ModePerm); err != nil {
178 t.Error(err)
179 }
180 if err := ioutil.WriteFile(filepath.Join(newRoot, "WORKSPACE"), []byte{}, os.ModePerm); err != nil {
181 t.Error(err)
182 }
183 checkSplitFilePathOutput(t, "Two WORKSPACE files exist", filename, newRoot, "to/package", "file.bzl")
184 }
185
View as plain text