Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(525)

Side by Side Diff: go/src/infra/tools/cipd/pkgdef_test.go

Issue 1129043003: cipd: Refactor client to make it more readable. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « go/src/infra/tools/cipd/pkgdef.go ('k') | go/src/infra/tools/cipd/reader.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package cipd
6
7 import (
8 "io/ioutil"
9 "os"
10 "path/filepath"
11 "strings"
12 "testing"
13
14 . "github.com/smartystreets/goconvey/convey"
15 )
16
17 func TestLoadPackageDef(t *testing.T) {
18 Convey("LoadPackageDef empty works", t, func() {
19 body := strings.NewReader(`{"package": "package/name"}`)
20 def, err := LoadPackageDef(body, nil)
21 So(err, ShouldBeNil)
22 So(def, ShouldResemble, PackageDef{
23 Package: "package/name",
24 Root: ".",
25 })
26 })
27
28 Convey("LoadPackageDef works", t, func() {
29 body := strings.NewReader(`{
30 "package": "package/${var1}",
31 "root": "../..",
32 "data": [
33 {
34 "file": "some_file_${var1}"
35 },
36 {
37 "file": "another_file_${var2}"
38 },
39 {
40 "dir": "some/directory"
41 },
42 {
43 "dir": "another/${var2}",
44 "exclude": [
45 ".*\\.pyc",
46 "abc_${var2}_def"
47 ]
48 }
49 ]
50 }`)
51 def, err := LoadPackageDef(body, map[string]string{
52 "var1": "value1",
53 "var2": "value2",
54 })
55 So(err, ShouldBeNil)
56 So(def, ShouldResemble, PackageDef{
57 Package: "package/value1",
58 Root: "../..",
59 Data: []PackageChunkDef{
60 PackageChunkDef{
61 File: "some_file_value1",
62 },
63 PackageChunkDef{
64 File: "another_file_value2",
65 },
66 PackageChunkDef{
67 Dir: "some/directory",
68 },
69 PackageChunkDef{
70 Dir: "another/value2",
71 Exclude: []string{
72 ".*\\.pyc",
73 "abc_value2_def",
74 },
75 },
76 },
77 })
78 })
79
80 Convey("LoadPackageDef not yaml", t, func() {
81 body := strings.NewReader(`{ not yaml)`)
82 _, err := LoadPackageDef(body, nil)
83 So(err, ShouldNotBeNil)
84 })
85
86 Convey("LoadPackageDef bad type", t, func() {
87 body := strings.NewReader(`{"package": []}`)
88 _, err := LoadPackageDef(body, nil)
89 So(err, ShouldNotBeNil)
90 })
91
92 Convey("LoadPackageDef missing variable", t, func() {
93 body := strings.NewReader(`{
94 "package": "abd",
95 "data": [{"file": "${missing_var}"}]
96 }`)
97 _, err := LoadPackageDef(body, nil)
98 So(err, ShouldNotBeNil)
99 })
100
101 Convey("LoadPackageDef space in missing variable", t, func() {
102 body := strings.NewReader(`{
103 "package": "abd",
104 "data": [{"file": "${missing var}"}]
105 }`)
106 _, err := LoadPackageDef(body, nil)
107 So(err, ShouldNotBeNil)
108 })
109
110 Convey("LoadPackageDef bad package name", t, func() {
111 body := strings.NewReader(`{"package": "not a valid name"}`)
112 _, err := LoadPackageDef(body, nil)
113 So(err, ShouldNotBeNil)
114 })
115
116 Convey("LoadPackageDef bad file section (no dir or file)", t, func() {
117 body := strings.NewReader(`{
118 "package": "package/name",
119 "data": [
120 {"exclude": []}
121 ]
122 }`)
123 _, err := LoadPackageDef(body, nil)
124 So(err, ShouldNotBeNil)
125 })
126
127 Convey("LoadPackageDef bad file section (both dir and file)", t, func() {
128 body := strings.NewReader(`{
129 "package": "package/name",
130 "data": [
131 {"file": "abc", "dir": "def"}
132 ]
133 }`)
134 _, err := LoadPackageDef(body, nil)
135 So(err, ShouldNotBeNil)
136 })
137 }
138
139 func TestExclusion(t *testing.T) {
140 Convey("makeExclusionFilter works", t, func() {
141 filter, err := makeExclusionFilter("a/b/c", []string{
142 ".*\\.pyc",
143 ".*/pip-.*-build/.*",
144 "bin/activate",
145 "lib/.*/site-packages/.*\\.dist-info/RECORD",
146 })
147 So(err, ShouldBeNil)
148 So(filter, ShouldNotBeNil)
149
150 // Not inside "a/b/c".
151 So(filter(filepath.FromSlash("a/b/test.pyc")), ShouldBeFalse)
152
153 // *.pyc filtering.
154 So(filter(filepath.FromSlash("a/b/c/test.pyc")), ShouldBeTrue)
155 So(filter(filepath.FromSlash("a/b/c/test.py")), ShouldBeFalse)
156 So(filter(filepath.FromSlash("a/b/c/d/e/f/test.pyc")), ShouldBeT rue)
157 So(filter(filepath.FromSlash("a/b/c/d/e/f/test.py")), ShouldBeFa lse)
158
159 // Subdir filtering.
160 So(filter(filepath.FromSlash("a/b/c/x/pip-blah-build/d/e/f")), S houldBeTrue)
161
162 // Single file exclusion.
163 So(filter(filepath.FromSlash("a/b/c/bin/activate")), ShouldBeTru e)
164 So(filter(filepath.FromSlash("a/b/c/bin/activate2")), ShouldBeFa lse)
165 So(filter(filepath.FromSlash("a/b/c/d/bin/activate")), ShouldBeF alse)
166
167 // More complicated regexp.
168 p := "a/b/c/lib/python2.7/site-packages/coverage-3.7.1.dist-info /RECORD"
169 So(filter(filepath.FromSlash(p)), ShouldBeTrue)
170 })
171
172 Convey("makeExclusionFilter bad regexp", t, func() {
173 _, err := makeExclusionFilter("a/b/c", []string{"****"})
174 So(err, ShouldNotBeNil)
175 })
176 }
177
178 func TestFindFiles(t *testing.T) {
179 Convey("Given a temp directory", t, func() {
180 tempDir, err := ioutil.TempDir("", "cipd_test")
181 So(err, ShouldBeNil)
182 Reset(func() { os.RemoveAll(tempDir) })
183
184 mkF := func(path string) { writeFile(tempDir, path, "", 0666) }
185 mkD := func(path string) { mkDir(tempDir, path) }
186 mkL := func(path, target string) { writeSymlink(tempDir, path, t arget) }
187
188 Convey("FindFiles works", func() {
189 mkF("ENV/abc.py")
190 mkF("ENV/abc.pyc") // excluded via "exclude: '.*\.pyc'"
191 mkF("ENV/abc.pyo")
192 mkF("ENV/dir/def.py")
193 mkD("ENV/empty") // will be skipped
194 mkF("ENV/exclude_me") // excluded via "exclude: 'exclude _me'"
195 mkL("ENV/abs_link", filepath.Dir(tempDir))
196 mkL("ENV/rel_link", "abc.py")
197 mkL("ENV/abs_in_root", filepath.Join(tempDir, "ENV", "di r", "def.py"))
198
199 mkF("infra/xyz.py")
200 mkF("infra/zzz.pyo")
201 mkF("infra/excluded.py")
202 mkF("infra/excluded_dir/a")
203 mkF("infra/excluded_dir/b")
204
205 mkF("file1.py")
206 mkF("dir/file2.py")
207
208 mkF("garbage/a")
209 mkF("garbage/b")
210
211 pkgDef := PackageDef{
212 Package: "test",
213 Root: "../../",
214 Data: []PackageChunkDef{
215 PackageChunkDef{
216 Dir: "ENV",
217 Exclude: []string{".*\\.pyc", "e xclude_me"},
218 },
219 PackageChunkDef{
220 Dir: "infra",
221 Exclude: []string{
222 ".*\\.pyo",
223 "excluded.py",
224 "excluded_dir",
225 },
226 },
227 PackageChunkDef{File: "file1.py"},
228 PackageChunkDef{File: "dir/file2.py"},
229 // Will be "deduplicated", because alrea dy matched by first entry.
230 PackageChunkDef{File: "ENV/abc.py"},
231 },
232 }
233
234 files, err := pkgDef.FindFiles(filepath.Join(tempDir, "a ", "b"))
235 So(err, ShouldBeNil)
236 names := []string{}
237 byName := make(map[string]File, len(files))
238 for _, f := range files {
239 names = append(names, f.Name())
240 byName[f.Name()] = f
241 }
242 So(names, ShouldResemble, []string{
243 "ENV/abc.py",
244 "ENV/abc.pyo",
245 "ENV/abs_in_root",
246 "ENV/abs_link",
247 "ENV/dir/def.py",
248 "ENV/rel_link",
249 "dir/file2.py",
250 "file1.py",
251 "infra/xyz.py",
252 })
253
254 // Separately check symlinks.
255 ensureSymlinkTarget(byName["ENV/abs_in_root"], "dir/def. py")
256 ensureSymlinkTarget(byName["ENV/abs_link"], filepath.ToS lash(filepath.Dir(tempDir)))
257 ensureSymlinkTarget(byName["ENV/rel_link"], "abc.py")
258 })
259 })
260 }
OLDNEW
« no previous file with comments | « go/src/infra/tools/cipd/pkgdef.go ('k') | go/src/infra/tools/cipd/reader.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698