| 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 "io/ioutil" | 8 "io/ioutil" |
| 9 "os" | 9 "os" |
| 10 "path/filepath" | 10 "path/filepath" |
| 11 "runtime" |
| 11 "testing" | 12 "testing" |
| 12 | 13 |
| 13 . "github.com/smartystreets/goconvey/convey" | 14 . "github.com/smartystreets/goconvey/convey" |
| 14 ) | 15 ) |
| 15 | 16 |
| 16 func TestCwdRelToAbs(t *testing.T) { | 17 func TestCwdRelToAbs(t *testing.T) { |
| 17 Convey("CwdRelToAbs works", t, func() { | 18 Convey("CwdRelToAbs works", t, func() { |
| 18 fs := tempFileSystem() | 19 fs := tempFileSystem() |
| 19 p, err := fs.CwdRelToAbs(fs.Root()) | 20 p, err := fs.CwdRelToAbs(fs.Root()) |
| 20 So(err, ShouldBeNil) | 21 So(err, ShouldBeNil) |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 So(err, ShouldBeNil) | 66 So(err, ShouldBeNil) |
| 66 So(p, ShouldEqual, fs.join("y/z")) | 67 So(p, ShouldEqual, fs.join("y/z")) |
| 67 So(fs.isDir("y/z"), ShouldBeTrue) | 68 So(fs.isDir("y/z"), ShouldBeTrue) |
| 68 // Same one. | 69 // Same one. |
| 69 _, err = fs.EnsureDirectory(fs.join("x/../y/z")) | 70 _, err = fs.EnsureDirectory(fs.join("x/../y/z")) |
| 70 So(err, ShouldBeNil) | 71 So(err, ShouldBeNil) |
| 71 }) | 72 }) |
| 72 } | 73 } |
| 73 | 74 |
| 74 func TestEnsureSymlink(t *testing.T) { | 75 func TestEnsureSymlink(t *testing.T) { |
| 76 if runtime.GOOS == "windows" { |
| 77 t.Skip("Skipping on Windows: no symlinks") |
| 78 } |
| 79 |
| 75 Convey("EnsureSymlink checks root", t, func() { | 80 Convey("EnsureSymlink checks root", t, func() { |
| 76 fs := tempFileSystem() | 81 fs := tempFileSystem() |
| 77 So(fs.EnsureSymlink(fs.join(".."), fs.Root()), ShouldNotBeNil) | 82 So(fs.EnsureSymlink(fs.join(".."), fs.Root()), ShouldNotBeNil) |
| 78 }) | 83 }) |
| 79 | 84 |
| 80 » Convey("ensureSymlink creates new symlink", t, func() { | 85 » Convey("EnsureSymlink creates new symlink", t, func() { |
| 81 fs := tempFileSystem() | 86 fs := tempFileSystem() |
| 82 So(fs.EnsureSymlink(fs.join("symlink"), "target"), ShouldBeNil) | 87 So(fs.EnsureSymlink(fs.join("symlink"), "target"), ShouldBeNil) |
| 83 So(fs.readLink("symlink"), ShouldEqual, "target") | 88 So(fs.readLink("symlink"), ShouldEqual, "target") |
| 84 }) | 89 }) |
| 85 | 90 |
| 86 » Convey("ensureSymlink builds full path", t, func() { | 91 » Convey("EnsureSymlink builds full path", t, func() { |
| 87 fs := tempFileSystem() | 92 fs := tempFileSystem() |
| 88 So(fs.EnsureSymlink(fs.join("a/b/c"), "target"), ShouldBeNil) | 93 So(fs.EnsureSymlink(fs.join("a/b/c"), "target"), ShouldBeNil) |
| 89 So(fs.readLink("a/b/c"), ShouldEqual, "target") | 94 So(fs.readLink("a/b/c"), ShouldEqual, "target") |
| 90 }) | 95 }) |
| 91 | 96 |
| 92 » Convey("ensureSymlink replaces existing one", t, func() { | 97 » Convey("EnsureSymlink replaces existing symlink", t, func() { |
| 93 fs := tempFileSystem() | 98 fs := tempFileSystem() |
| 94 // Replace with same one, then with another one. | 99 // Replace with same one, then with another one. |
| 95 So(fs.EnsureSymlink(fs.join("symlink"), "target"), ShouldBeNil) | 100 So(fs.EnsureSymlink(fs.join("symlink"), "target"), ShouldBeNil) |
| 96 So(fs.EnsureSymlink(fs.join("symlink"), "target"), ShouldBeNil) | 101 So(fs.EnsureSymlink(fs.join("symlink"), "target"), ShouldBeNil) |
| 97 So(fs.EnsureSymlink(fs.join("symlink"), "another"), ShouldBeNil) | 102 So(fs.EnsureSymlink(fs.join("symlink"), "another"), ShouldBeNil) |
| 98 So(fs.readLink("symlink"), ShouldEqual, "another") | 103 So(fs.readLink("symlink"), ShouldEqual, "another") |
| 99 }) | 104 }) |
| 105 |
| 106 Convey("EnsureSymlink replaces existing file", t, func() { |
| 107 fs := tempFileSystem() |
| 108 fs.write("path", "blah") |
| 109 So(fs.EnsureSymlink(fs.join("path"), "target"), ShouldBeNil) |
| 110 So(fs.readLink("path"), ShouldEqual, "target") |
| 111 }) |
| 112 |
| 113 Convey("EnsureSymlink replaces existing directory", t, func() { |
| 114 fs := tempFileSystem() |
| 115 fs.write("a/b/c", "something") |
| 116 So(fs.EnsureSymlink(fs.join("a"), "target"), ShouldBeNil) |
| 117 So(fs.readLink("a"), ShouldEqual, "target") |
| 118 }) |
| 119 } |
| 120 |
| 121 func TestEnsureFile(t *testing.T) { |
| 122 Convey("EnsureFile checks root", t, func() { |
| 123 fs := tempFileSystem() |
| 124 So(fs.EnsureFile(fs.join(".."), []byte("blah"), 0666), ShouldNot
BeNil) |
| 125 }) |
| 126 |
| 127 Convey("EnsureFile creates new file", t, func() { |
| 128 fs := tempFileSystem() |
| 129 So(fs.EnsureFile(fs.join("name"), []byte("blah"), 0666), ShouldB
eNil) |
| 130 So(fs.read("name"), ShouldEqual, "blah") |
| 131 }) |
| 132 |
| 133 Convey("EnsureFile builds full path", t, func() { |
| 134 fs := tempFileSystem() |
| 135 So(fs.EnsureFile(fs.join("a/b/c"), []byte("blah"), 0666), Should
BeNil) |
| 136 So(fs.read("a/b/c"), ShouldEqual, "blah") |
| 137 }) |
| 138 |
| 139 if runtime.GOOS != "windows" { |
| 140 Convey("EnsureFile replaces existing symlink", t, func() { |
| 141 fs := tempFileSystem() |
| 142 So(fs.EnsureSymlink(fs.join("path"), "target"), ShouldBe
Nil) |
| 143 So(fs.EnsureFile(fs.join("path"), []byte("blah"), 0666),
ShouldBeNil) |
| 144 So(fs.read("path"), ShouldEqual, "blah") |
| 145 }) |
| 146 } |
| 147 |
| 148 Convey("EnsureFile replaces existing file", t, func() { |
| 149 fs := tempFileSystem() |
| 150 So(fs.EnsureFile(fs.join("path"), []byte("huh"), 0666), ShouldBe
Nil) |
| 151 So(fs.EnsureFile(fs.join("path"), []byte("blah"), 0666), ShouldB
eNil) |
| 152 So(fs.read("path"), ShouldEqual, "blah") |
| 153 }) |
| 154 |
| 155 Convey("EnsureFile replaces existing directory", t, func() { |
| 156 fs := tempFileSystem() |
| 157 fs.write("a/b/c", "something") |
| 158 So(fs.EnsureFile(fs.join("a"), []byte("blah"), 0666), ShouldBeNi
l) |
| 159 So(fs.read("a"), ShouldEqual, "blah") |
| 160 }) |
| 100 } | 161 } |
| 101 | 162 |
| 102 func TestEnsureFileGone(t *testing.T) { | 163 func TestEnsureFileGone(t *testing.T) { |
| 103 Convey("EnsureFileGone checks root", t, func() { | 164 Convey("EnsureFileGone checks root", t, func() { |
| 104 fs := tempFileSystem() | 165 fs := tempFileSystem() |
| 105 So(fs.EnsureFileGone(fs.join("../abc")), ShouldNotBeNil) | 166 So(fs.EnsureFileGone(fs.join("../abc")), ShouldNotBeNil) |
| 106 }) | 167 }) |
| 107 | 168 |
| 108 Convey("EnsureFileGone works", t, func() { | 169 Convey("EnsureFileGone works", t, func() { |
| 109 fs := tempFileSystem() | 170 fs := tempFileSystem() |
| 110 fs.write("abc", "") | 171 fs.write("abc", "") |
| 111 So(fs.EnsureFileGone(fs.join("abc")), ShouldBeNil) | 172 So(fs.EnsureFileGone(fs.join("abc")), ShouldBeNil) |
| 112 So(fs.isMissing("abc"), ShouldBeTrue) | 173 So(fs.isMissing("abc"), ShouldBeTrue) |
| 113 }) | 174 }) |
| 114 | 175 |
| 115 Convey("EnsureFileGone works with missing file", t, func() { | 176 Convey("EnsureFileGone works with missing file", t, func() { |
| 116 fs := tempFileSystem() | 177 fs := tempFileSystem() |
| 117 So(fs.EnsureFileGone(fs.join("abc")), ShouldBeNil) | 178 So(fs.EnsureFileGone(fs.join("abc")), ShouldBeNil) |
| 118 }) | 179 }) |
| 119 | 180 |
| 120 » Convey("EnsureFileGone works with symlink", t, func() { | 181 » if runtime.GOOS != "windows" { |
| 121 » » fs := tempFileSystem() | 182 » » Convey("EnsureFileGone works with symlink", t, func() { |
| 122 » » So(fs.EnsureSymlink(fs.join("abc"), "target"), ShouldBeNil) | 183 » » » fs := tempFileSystem() |
| 123 » » So(fs.EnsureFileGone(fs.join("abc")), ShouldBeNil) | 184 » » » So(fs.EnsureSymlink(fs.join("abc"), "target"), ShouldBeN
il) |
| 124 » » So(fs.isMissing("abc"), ShouldBeTrue) | 185 » » » So(fs.EnsureFileGone(fs.join("abc")), ShouldBeNil) |
| 125 » }) | 186 » » » So(fs.isMissing("abc"), ShouldBeTrue) |
| 187 » » }) |
| 188 » } |
| 126 } | 189 } |
| 127 | 190 |
| 128 func TestEnsureDirectoryGone(t *testing.T) { | 191 func TestEnsureDirectoryGone(t *testing.T) { |
| 129 Convey("EnsureDirectoryGone checks root", t, func() { | 192 Convey("EnsureDirectoryGone checks root", t, func() { |
| 130 fs := tempFileSystem() | 193 fs := tempFileSystem() |
| 131 So(fs.EnsureDirectoryGone(fs.join("../abc")), ShouldNotBeNil) | 194 So(fs.EnsureDirectoryGone(fs.join("../abc")), ShouldNotBeNil) |
| 132 }) | 195 }) |
| 133 | 196 |
| 134 Convey("EnsureDirectoryGone works", t, func() { | 197 Convey("EnsureDirectoryGone works", t, func() { |
| 135 fs := tempFileSystem() | 198 fs := tempFileSystem() |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 | 356 |
| 294 // isFile returns true if a file at a given slash separated path relative to | 357 // isFile returns true if a file at a given slash separated path relative to |
| 295 // Root() is a file (not a directory). | 358 // Root() is a file (not a directory). |
| 296 func (f *tempFileSystemImpl) isFile(rel string) bool { | 359 func (f *tempFileSystemImpl) isFile(rel string) bool { |
| 297 stat, err := os.Stat(f.join(rel)) | 360 stat, err := os.Stat(f.join(rel)) |
| 298 if err != nil { | 361 if err != nil { |
| 299 return false | 362 return false |
| 300 } | 363 } |
| 301 return !stat.IsDir() | 364 return !stat.IsDir() |
| 302 } | 365 } |
| OLD | NEW |