OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package local | 5 package local |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "fmt" | 9 "fmt" |
10 "io" | 10 "io" |
(...skipping 16 matching lines...) Expand all Loading... |
27 // Wrappers that accept paths relative to tempDir. | 27 // Wrappers that accept paths relative to tempDir. |
28 touch := func(rel string) { | 28 touch := func(rel string) { |
29 abs := filepath.Join(tempDir, filepath.FromSlash(rel)) | 29 abs := filepath.Join(tempDir, filepath.FromSlash(rel)) |
30 err := os.MkdirAll(filepath.Dir(abs), 0777) | 30 err := os.MkdirAll(filepath.Dir(abs), 0777) |
31 So(err, ShouldBeNil) | 31 So(err, ShouldBeNil) |
32 f, err := os.Create(abs) | 32 f, err := os.Create(abs) |
33 So(err, ShouldBeNil) | 33 So(err, ShouldBeNil) |
34 f.Close() | 34 f.Close() |
35 } | 35 } |
36 ensureLink := func(symlinkRel string, target string) error { | 36 ensureLink := func(symlinkRel string, target string) error { |
37 » » » return ensureSymlink(filepath.Join(tempDir, symlinkRel),
target) | 37 » » » return os.Symlink(target, filepath.Join(tempDir, symlink
Rel)) |
38 } | 38 } |
39 readLink := func(symlinkRel string) string { | |
40 val, err := os.Readlink(filepath.Join(tempDir, symlinkRe
l)) | |
41 So(err, ShouldBeNil) | |
42 return val | |
43 } | |
44 | |
45 Convey("ensureSymlink creates new symlink", func() { | |
46 So(ensureLink("symlink", "target"), ShouldBeNil) | |
47 So(readLink("symlink"), ShouldEqual, "target") | |
48 }) | |
49 | |
50 Convey("ensureSymlink builds full path", func() { | |
51 So(ensureLink(filepath.Join("a", "b", "c"), "target"), S
houldBeNil) | |
52 So(readLink(filepath.Join("a", "b", "c")), ShouldEqual,
"target") | |
53 }) | |
54 | |
55 Convey("ensureSymlink replaces existing one", func() { | |
56 So(ensureLink("symlink", "target"), ShouldBeNil) | |
57 So(ensureLink("symlink", "another"), ShouldBeNil) | |
58 So(readLink("symlink"), ShouldEqual, "another") | |
59 }) | |
60 | 39 |
61 Convey("scanPackageDir works with empty dir", func() { | 40 Convey("scanPackageDir works with empty dir", func() { |
62 err := os.Mkdir(filepath.Join(tempDir, "dir"), 0777) | 41 err := os.Mkdir(filepath.Join(tempDir, "dir"), 0777) |
63 So(err, ShouldBeNil) | 42 So(err, ShouldBeNil) |
64 files := makeStringSet() | 43 files := makeStringSet() |
65 err = scanPackageDir(filepath.Join(tempDir, "dir"), file
s) | 44 err = scanPackageDir(filepath.Join(tempDir, "dir"), file
s) |
66 So(err, ShouldBeNil) | 45 So(err, ShouldBeNil) |
67 So(len(files), ShouldEqual, 0) | 46 So(len(files), ShouldEqual, 0) |
68 }) | 47 }) |
69 | 48 |
(...skipping 13 matching lines...) Expand all Loading... |
83 names = append(names, filepath.ToSlash(n)) | 62 names = append(names, filepath.ToSlash(n)) |
84 } | 63 } |
85 names.Sort() | 64 names.Sort() |
86 So(names, ShouldResemble, sort.StringSlice{ | 65 So(names, ShouldResemble, sort.StringSlice{ |
87 "a/1", | 66 "a/1", |
88 "a/2", | 67 "a/2", |
89 "a/sym_link", | 68 "a/sym_link", |
90 "b/1", | 69 "b/1", |
91 }) | 70 }) |
92 }) | 71 }) |
93 | |
94 Convey("ensureDirectoryGone works with missing dir", func() { | |
95 So(ensureDirectoryGone(filepath.Join(tempDir, "missing")
, nil), ShouldBeNil) | |
96 }) | |
97 | |
98 Convey("ensureDirectoryGone works", func() { | |
99 touch("dir/a/1") | |
100 touch("dir/a/2") | |
101 touch("dir/b/1") | |
102 So(ensureDirectoryGone(filepath.Join(tempDir, "dir"), ni
l), ShouldBeNil) | |
103 _, err := os.Stat(filepath.Join(tempDir, "dir")) | |
104 So(os.IsNotExist(err), ShouldBeTrue) | |
105 }) | |
106 | |
107 Convey("ensureFileGone works", func() { | |
108 touch("abc") | |
109 So(ensureFileGone(filepath.Join(tempDir, "abc"), nil), S
houldBeNil) | |
110 _, err := os.Stat(filepath.Join(tempDir, "abc")) | |
111 So(os.IsNotExist(err), ShouldBeTrue) | |
112 }) | |
113 | |
114 Convey("ensureFileGone works with missing file", func() { | |
115 So(ensureFileGone(filepath.Join(tempDir, "abc"), nil), S
houldBeNil) | |
116 }) | |
117 | |
118 Convey("ensureFileGone works with symlink", func() { | |
119 ensureLink("abc", "target") | |
120 So(ensureFileGone(filepath.Join(tempDir, "abc"), nil), S
houldBeNil) | |
121 _, err := os.Stat(filepath.Join(tempDir, "abc")) | |
122 So(os.IsNotExist(err), ShouldBeTrue) | |
123 }) | |
124 }) | 72 }) |
125 } | 73 } |
126 | 74 |
127 func TestDeployInstance(t *testing.T) { | 75 func TestDeployInstance(t *testing.T) { |
128 Convey("Given a temp directory", t, func() { | 76 Convey("Given a temp directory", t, func() { |
129 tempDir, err := ioutil.TempDir("", "cipd_test") | 77 tempDir, err := ioutil.TempDir("", "cipd_test") |
130 So(err, ShouldBeNil) | 78 So(err, ShouldBeNil) |
131 Reset(func() { os.RemoveAll(tempDir) }) | 79 Reset(func() { os.RemoveAll(tempDir) }) |
132 | 80 |
133 Convey("DeployInstance new empty package instance", func() { | 81 Convey("DeployInstance new empty package instance", func() { |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 } | 397 } |
450 | 398 |
451 out = append(out, item+suffix) | 399 out = append(out, item+suffix) |
452 return nil | 400 return nil |
453 }) | 401 }) |
454 if err != nil { | 402 if err != nil { |
455 panic("Failed to walk a directory") | 403 panic("Failed to walk a directory") |
456 } | 404 } |
457 return | 405 return |
458 } | 406 } |
OLD | NEW |