| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package filesystem |
| 6 |
| 7 import ( |
| 8 "io/ioutil" |
| 9 "os" |
| 10 "path/filepath" |
| 11 "testing" |
| 12 "time" |
| 13 |
| 14 "github.com/luci/luci-go/common/errors" |
| 15 |
| 16 . "github.com/smartystreets/goconvey/convey" |
| 17 ) |
| 18 |
| 19 func withTempDir(t *testing.T, fn func(string)) { |
| 20 tdir, err := ioutil.TempDir("", "vpython-filesystem") |
| 21 if err != nil { |
| 22 t.Fatalf("failed to create temporary directory: %s", err) |
| 23 } |
| 24 defer func() { |
| 25 // Mark all files writable. |
| 26 rmErr := recursiveChmod(tdir, nil, func(mode os.FileMode) os.Fil
eMode { |
| 27 return mode | 0222 |
| 28 }) |
| 29 if rmErr != nil { |
| 30 t.Errorf("failed to mark temporary directory as writable
[%s]: %s", tdir, rmErr) |
| 31 } |
| 32 if rmErr := os.RemoveAll(tdir); rmErr != nil { |
| 33 t.Errorf("failed to remove temporary directory [%s]: %s"
, tdir, rmErr) |
| 34 } |
| 35 }() |
| 36 fn(tdir) |
| 37 } |
| 38 |
| 39 func TestIsNotExist(t *testing.T) { |
| 40 t.Parallel() |
| 41 |
| 42 // Create a temporary directory so we control its contents. This will le
t us |
| 43 // safely create an IsNotExist error. |
| 44 withTempDir(t, func(tdir string) { |
| 45 dnePath := filepath.Join(tdir, "anything") |
| 46 _, err := os.Open(dnePath) |
| 47 if !os.IsNotExist(err) { |
| 48 t.Fatalf("failed to get IsNotExist error: %s", err) |
| 49 } |
| 50 |
| 51 Convey(`IsNotExist works`, t, func() { |
| 52 So(IsNotExist(nil), ShouldBeFalse) |
| 53 So(IsNotExist(errors.New("something")), ShouldBeFalse) |
| 54 |
| 55 So(IsNotExist(err), ShouldBeTrue) |
| 56 So(IsNotExist(errors.Annotate(err).Reason("annotated").E
rr()), ShouldBeTrue) |
| 57 }) |
| 58 }) |
| 59 } |
| 60 |
| 61 func TestAbsPath(t *testing.T) { |
| 62 t.Parallel() |
| 63 |
| 64 // Create a temporary directory so we control its contents. This will le
t us |
| 65 // safely create an IsNotExist error. |
| 66 withTempDir(t, func(tdir string) { |
| 67 tdirName := filepath.Base(tdir) |
| 68 |
| 69 Convey(`AbsPath works`, t, func() { |
| 70 base := filepath.Join(tdir, "..", tdirName, "file.txt") |
| 71 So(AbsPath(&base), ShouldBeNil) |
| 72 So(base, ShouldEqual, filepath.Join(tdir, "file.txt")) |
| 73 }) |
| 74 }) |
| 75 } |
| 76 |
| 77 func TestTouch(t *testing.T) { |
| 78 t.Parallel() |
| 79 |
| 80 Convey(`Can Touch a new file`, t, func() { |
| 81 withTempDir(t, func(tdir string) { |
| 82 thePast := time.Now().Add(-10 * time.Second) |
| 83 |
| 84 Convey(`Can update a directory timestamp`, func() { |
| 85 path := filepath.Join(tdir, "subdir") |
| 86 |
| 87 So(os.Mkdir(path, 0755), ShouldBeNil) |
| 88 st, err := os.Lstat(path) |
| 89 So(err, ShouldBeNil) |
| 90 initialModTime := st.ModTime() |
| 91 |
| 92 So(Touch(path, thePast, 0), ShouldBeNil) |
| 93 st, err = os.Lstat(path) |
| 94 So(err, ShouldBeNil) |
| 95 |
| 96 So(st.ModTime(), ShouldHappenBefore, initialModT
ime) |
| 97 }) |
| 98 |
| 99 Convey(`Can update an empty file timestamp`, func() { |
| 100 path := filepath.Join(tdir, "touch") |
| 101 |
| 102 So(Touch(path, time.Time{}, 0644), ShouldBeNil) |
| 103 st, err := os.Lstat(path) |
| 104 So(err, ShouldBeNil) |
| 105 initialModTime := st.ModTime() |
| 106 |
| 107 So(Touch(path, thePast, 0), ShouldBeNil) |
| 108 st, err = os.Lstat(path) |
| 109 So(err, ShouldBeNil) |
| 110 pastModTime := st.ModTime() |
| 111 So(pastModTime, ShouldHappenBefore, initialModTi
me) |
| 112 |
| 113 // Touch back to "now". |
| 114 So(Touch(path, time.Time{}, 0644), ShouldBeNil) |
| 115 st, err = os.Lstat(path) |
| 116 So(err, ShouldBeNil) |
| 117 So(st.ModTime(), ShouldHappenOnOrAfter, pastModT
ime) |
| 118 }) |
| 119 |
| 120 Convey(`Can update a populated file timestamp`, func() { |
| 121 path := filepath.Join(tdir, "touch") |
| 122 |
| 123 So(ioutil.WriteFile(path, []byte("sup"), 0644),
ShouldBeNil) |
| 124 st, err := os.Lstat(path) |
| 125 So(err, ShouldBeNil) |
| 126 initialModTime := st.ModTime() |
| 127 |
| 128 So(Touch(path, thePast, 0), ShouldBeNil) |
| 129 st, err = os.Lstat(path) |
| 130 So(err, ShouldBeNil) |
| 131 |
| 132 So(st.ModTime(), ShouldHappenBefore, initialModT
ime) |
| 133 }) |
| 134 }) |
| 135 }) |
| 136 } |
| 137 |
| 138 func TestMakeReadOnly(t *testing.T) { |
| 139 t.Parallel() |
| 140 |
| 141 Convey(`Can RemoveAll`, t, func() { |
| 142 withTempDir(t, func(tdir string) { |
| 143 |
| 144 for _, path := range []string{ |
| 145 filepath.Join(tdir, "foo", "bar"), |
| 146 filepath.Join(tdir, "foo", "baz"), |
| 147 filepath.Join(tdir, "qux"), |
| 148 } { |
| 149 base := filepath.Dir(path) |
| 150 if err := os.MkdirAll(base, 0755); err != nil { |
| 151 t.Fatalf("failed to populate directory [
%s]: %s", base, err) |
| 152 } |
| 153 if err := ioutil.WriteFile(path, []byte("junk"),
0644); err != nil { |
| 154 t.Fatalf("failed to create file [%s]: %s
", path, err) |
| 155 } |
| 156 } |
| 157 |
| 158 Convey(`Can mark the entire subdirectory read-only`, fun
c() { |
| 159 So(MakeReadOnly(tdir, nil), ShouldBeNil) |
| 160 filepath.Walk(tdir, func(path string, info os.Fi
leInfo, err error) error { |
| 161 So(err, ShouldBeNil) |
| 162 So(info.Mode()&0222, ShouldEqual, 0) |
| 163 return nil |
| 164 }) |
| 165 }) |
| 166 |
| 167 Convey(`Can selectively mark files read-only`, func() { |
| 168 // Don't mark <TMP>/foo read-only. |
| 169 fooPath := filepath.Join(tdir, "foo") |
| 170 |
| 171 So(MakeReadOnly(tdir, func(path string) bool { |
| 172 return path != fooPath |
| 173 }), ShouldBeNil) |
| 174 |
| 175 filepath.Walk(tdir, func(path string, info os.Fi
leInfo, err error) error { |
| 176 So(err, ShouldBeNil) |
| 177 if path == fooPath { |
| 178 So(info.Mode()&0222, ShouldNotEq
ual, 0) |
| 179 } else { |
| 180 So(info.Mode()&0222, ShouldEqual
, 0) |
| 181 } |
| 182 return nil |
| 183 }) |
| 184 }) |
| 185 }) |
| 186 }) |
| 187 } |
| 188 |
| 189 func TestRemoveAll(t *testing.T) { |
| 190 t.Parallel() |
| 191 |
| 192 isGone := func(path string) bool { |
| 193 if _, err := os.Lstat(path); err != nil { |
| 194 return os.IsNotExist(err) |
| 195 } |
| 196 return false |
| 197 } |
| 198 |
| 199 Convey(`Can RemoveAll`, t, func() { |
| 200 withTempDir(t, func(tdir string) { |
| 201 |
| 202 subDir := filepath.Join(tdir, "sub") |
| 203 |
| 204 Convey(`With directory contents...`, func() { |
| 205 for _, path := range []string{ |
| 206 filepath.Join(subDir, "foo", "bar"), |
| 207 filepath.Join(subDir, "foo", "baz"), |
| 208 filepath.Join(subDir, "qux"), |
| 209 } { |
| 210 base := filepath.Dir(path) |
| 211 if err := os.MkdirAll(base, 0755); err !
= nil { |
| 212 t.Fatalf("failed to populate dir
ectory [%s]: %s", base, err) |
| 213 } |
| 214 if err := ioutil.WriteFile(path, []byte(
"junk"), 0644); err != nil { |
| 215 t.Fatalf("failed to create file
[%s]: %s", path, err) |
| 216 } |
| 217 } |
| 218 |
| 219 Convey(`Can remove the directory`, func() { |
| 220 So(RemoveAll(subDir), ShouldBeNil) |
| 221 So(isGone(subDir), ShouldBeTrue) |
| 222 }) |
| 223 |
| 224 Convey(`Can remove the directory when it is read
-only`, func() { |
| 225 // Make the directory read-only, and ass
ert that classic os.RemoveAll |
| 226 // fails. |
| 227 So(MakeReadOnly(subDir, nil), ShouldBeNi
l) |
| 228 So(os.RemoveAll(subDir), ShouldNotBeNil) |
| 229 |
| 230 So(RemoveAll(subDir), ShouldBeNil) |
| 231 So(isGone(subDir), ShouldBeTrue) |
| 232 }) |
| 233 }) |
| 234 |
| 235 Convey(`Will return nil if the target does not exist`, f
unc() { |
| 236 So(RemoveAll(filepath.Join(tdir, "dne")), Should
BeNil) |
| 237 }) |
| 238 }) |
| 239 }) |
| 240 } |
| OLD | NEW |