/usr/share/gocode/src/github.com/smartystreets/goconvey/web/server/watcher/scanner_test.go is in golang-github-smartystreets-goconvey-dev 1.5.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | package watcher
import (
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
"github.com/smartystreets/goconvey/web/server/contract"
"github.com/smartystreets/goconvey/web/server/system"
)
func TestScanner(t *testing.T) {
var fixture *scannerFixture
var changed bool
Convey("To begin with, the scanner is provided a contrived file system environment", t, func() {
fixture = newScannerFixture()
Convey("When we call Scan() for the first time", func() {
changed = fixture.scan()
Convey("The scanner should report a change in state", func() {
So(changed, ShouldBeTrue)
})
})
Convey("Then, on subsequent calls to Scan()", func() {
changed = fixture.scan()
Convey("When the file system has not changed in any way", func() {
Convey("The scanner should NOT report any change in state", func() {
So(fixture.scan(), ShouldBeFalse)
})
})
Convey("When a new go file is created within a watched folder", func() {
fixture.fs.Create("/root/new_stuff.go", 42, time.Now())
Convey("The scanner should report a change in state", func() {
So(fixture.scan(), ShouldBeTrue)
})
})
Convey("When a file that starts with . is created within a watched folder", func() {
fixture.fs.Create("/root/.new_stuff.go", 42, time.Now())
Convey("The scanner should not report a change in state", func() {
So(fixture.scan(), ShouldBeFalse)
})
})
Convey("When an existing go file within a watched folder has been modified", func() {
fixture.fs.Modify("/root/sub/file.go")
Convey("The scanner should report a change in state", func() {
So(fixture.scan(), ShouldBeTrue)
})
})
Convey("When an existing go file within a watched folder has been renamed", func() {
fixture.fs.Rename("/root/sub/file.go", "/root/sub/asdf.go")
Convey("The scanner should report a change in state", func() {
So(fixture.scan(), ShouldBeTrue)
})
})
Convey("When an existing go file within a watched folder has been deleted", func() {
fixture.fs.Delete("/root/sub/file.go")
Convey("The scanner should report a change in state", func() {
So(fixture.scan(), ShouldBeTrue)
})
})
Convey("When a go file is created outside any watched folders", func() {
fixture.fs.Create("/outside/new_stuff.go", 42, time.Now())
Convey("The scanner should NOT report a change in state", func() {
So(fixture.scan(), ShouldBeFalse)
})
})
Convey("When a go file is modified outside any watched folders", func() {
fixture.fs.Create("/outside/new_stuff.go", 42, time.Now())
fixture.scan() // reset
Convey("The scanner should NOT report a change in state", func() {
So(fixture.scan(), ShouldBeFalse)
})
})
Convey("When a go file is renamed outside any watched folders", func() {
fixture.fs.Create("/outside/new_stuff.go", 42, time.Now())
fixture.scan() // reset
fixture.fs.Rename("/outside/new_stuff.go", "/outside/newer_stoff.go")
Convey("The scanner should NOT report a change in state", func() {
So(fixture.scan(), ShouldBeFalse)
})
})
Convey("When a go file is deleted outside any watched folders", func() {
fixture.fs.Create("/outside/new_stuff.go", 42, time.Now())
fixture.scan() // reset
fixture.fs.Delete("/outside/new_stuff.go")
Convey("The scanner should NOT report a change in state", func() {
So(fixture.scan(), ShouldBeFalse)
})
})
Convey("When a miscellaneous file is created", func() {
fixture.fs.Create("/root/new_stuff.MISC", 42, time.Now())
Convey("The scanner should NOT report a change in state", func() {
So(fixture.scan(), ShouldBeFalse)
})
})
Convey("When a miscellaneous file is modified", func() {
fixture.fs.Create("/root/new_stuff.MISC", 42, time.Now())
fixture.scan() // reset
Convey("The scanner should NOT report a change in state", func() {
So(fixture.scan(), ShouldBeFalse)
})
})
Convey("When a miscellaneous file is renamed", func() {
fixture.fs.Create("/root/new_stuff.MISC", 42, time.Now())
fixture.scan() // reset
fixture.fs.Rename("/root/new_stuff.MISC", "/root/newer_stoff.MISC")
Convey("The scanner should NOT report a change in state", func() {
So(fixture.scan(), ShouldBeFalse)
})
})
Convey("When a miscellaneous file is deleted", func() {
fixture.fs.Create("/root/new_stuff.MISC", 42, time.Now())
fixture.scan() // reset
fixture.fs.Delete("/root/new_stuff.MISC")
Convey("The scanner should NOT report a change in state", func() {
So(fixture.scan(), ShouldBeFalse)
})
})
Convey("When a new folder is created inside a watched folder", func() {
fixture.fs.Create("/root/new", 41, time.Now())
changed := fixture.scan()
Convey("The scanner should report the change", func() {
So(changed, ShouldBeTrue)
})
Convey("The scanner should notify the watcher of the creation", func() {
So(fixture.wasCreated("/root/new"), ShouldBeTrue)
})
})
Convey("When an empty watched folder is deleted", func() {
fixture.fs.Delete("/root/sub/empty")
changed := fixture.scan()
Convey("The scanner should report the change", func() {
So(changed, ShouldBeTrue)
})
Convey("The scanner should notify the watcher of the deletion", func() {
So(fixture.wasDeleted("/root/sub/empty"), ShouldBeTrue)
})
})
Convey("When a folder is created outside any watched folders", func() {
fixture.fs.Create("/outside/asdf", 41, time.Now())
changed := fixture.scan()
Convey("The scanner should NOT report the change", func() {
So(changed, ShouldBeFalse)
})
Convey("The scanner should NOT notify the watcher of the change", func() {
So(fixture.wasCreated("/outside/asdf"), ShouldBeFalse)
})
})
Convey("When an ignored folder is deleted", func() {
fixture.watcher.Ignore("/root/sub/empty")
fixture.fs.Delete("/root/sub/empty")
changed := fixture.scan()
Convey("The scanner should report the change", func() {
So(changed, ShouldBeTrue)
})
Convey("The scanner should notify the watcher of the change", func() {
So(fixture.wasDeleted("/root/sub/empty"), ShouldBeTrue)
})
})
// Once upon a time the scanner didn't keep track of the root internally.
// This meant that when the scanner was instructed to scan a new root location
// it appeared to the scanner that many of the internally stored folders had
// been deleted becuase they were not part of the new root directory structure
// and they were reported as deletions to the watcher, which was incorrect behavior.
Convey("When the watcher has adjusted the root", func() {
fixture.fs.Create("/somewhere", 3, time.Now())
fixture.fs.Create("/somewhere/else", 3, time.Now())
fixture.watcher.Adjust("/somewhere")
Convey("And the scanner scans", func() {
changed := fixture.scan()
Convey("The scanner should report the change", func() {
So(changed, ShouldBeTrue)
})
Convey("The scanner should NOT notify the watcher of incorrect folder deletions", func() {
So(len(fixture.watcher.deleted), ShouldEqual, 0)
})
Convey("The scanner should NOT notify the watcher of incorrect folder creations", func() {
So(len(fixture.watcher.created), ShouldEqual, 0)
})
})
})
})
})
}
type scannerFixture struct {
scanner *Scanner
fs *system.FakeFileSystem
watcher *WatcherWrapper
}
func (self *scannerFixture) scan() bool {
return self.scanner.Scan()
}
func (self *scannerFixture) wasDeleted(folder string) bool {
return !self.wasCreated(folder)
}
func (self *scannerFixture) wasCreated(folder string) bool {
for _, w := range self.watcher.WatchedFolders() {
if w.Path == folder {
return true
}
}
return false
}
func newScannerFixture() *scannerFixture {
self := &scannerFixture{}
self.fs = system.NewFakeFileSystem()
self.fs.Create("/root", 0, time.Now())
self.fs.Create("/root/file.go", 1, time.Now())
self.fs.Create("/root/sub", 0, time.Now())
self.fs.Create("/root/sub/file.go", 2, time.Now())
self.fs.Create("/root/sub/empty", 0, time.Now())
self.watcher = newWatcherWrapper(NewWatcher(self.fs, system.NewFakeShell()))
self.watcher.Adjust("/root")
self.scanner = NewScanner(self.fs, self.watcher)
return self
}
/******** WatcherWrapper ********/
type WatcherWrapper struct {
inner *Watcher
created []string
deleted []string
}
func (self *WatcherWrapper) WatchedFolders() []*contract.Package {
return self.inner.WatchedFolders()
}
func (self *WatcherWrapper) Root() string {
return self.inner.Root()
}
func (self *WatcherWrapper) Adjust(root string) error {
return self.inner.Adjust(root)
}
func (self *WatcherWrapper) Deletion(folder string) {
self.deleted = append(self.deleted, folder)
self.inner.Deletion(folder)
}
func (self *WatcherWrapper) Creation(folder string) {
self.created = append(self.created, folder)
self.inner.Creation(folder)
}
func (self *WatcherWrapper) Ignore(folder string) {
self.inner.Ignore(folder)
}
func (self *WatcherWrapper) Reinstate(folder string) {
self.inner.Reinstate(folder)
}
func (self *WatcherWrapper) IsWatched(folder string) bool {
return self.inner.IsWatched(folder)
}
func (self *WatcherWrapper) IsIgnored(folder string) bool {
return self.inner.IsIgnored(folder)
}
func newWatcherWrapper(inner *Watcher) *WatcherWrapper {
self := &WatcherWrapper{}
self.inner = inner
self.created = []string{}
self.deleted = []string{}
return self
}
|