This file is indexed.

/usr/share/gocode/src/github.com/smartystreets/goconvey/web/server/watcher/watcher_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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package watcher

import (
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	_ "os"
	"testing"
	"time"

	. "github.com/smartystreets/goconvey/convey"
	"github.com/smartystreets/goconvey/web/server/contract"
	"github.com/smartystreets/goconvey/web/server/system"
)

func TestWatcher(t *testing.T) {
	var (
		fixture         *watcherFixture
		expectedWatches interface{}
		actualWatches   interface{}
		expectedError   interface{}
		actualError     interface{}
	)

	Convey("Subject: Watcher", t, func() {
		log.SetOutput(ioutil.Discard)
		log.SetFlags(log.LstdFlags | log.Lshortfile)
		// log.SetOutput(os.Stdout)
		fixture = newWatcherFixture()

		Convey("When initialized there should be ZERO watched folders", func() {
			So(len(fixture.watched()), ShouldEqual, 0)
			So(fixture.watcher.Root(), ShouldBeBlank)
		})

		Convey("When pointing to a root folder", func() {
			actualWatches, expectedWatches = fixture.pointToExistingRoot(goProject)

			Convey("That folder should be included as the first watched folder", func() {
				So(actualWatches, ShouldResemble, expectedWatches)
			})

			Convey("That folder should be the new root", func() {
				So(fixture.watcher.Root(), ShouldEqual, goProject)
			})
		})

		Convey("When pointing to a root folder that does not exist", func() {
			actualError, expectedError = fixture.pointToImaginaryRoot("/not/there")

			Convey("An appropriate error should be returned", func() {
				So(actualError, ShouldResemble, expectedError)
			})

			Convey("The root should not be updated", func() {
				So(fixture.watcher.Root(), ShouldBeBlank)
			})
		})

		Convey("When pointing to a root folder with nested folders", func() {
			actualWatches, expectedWatches = fixture.pointToExistingRootWithNestedFolders()

			Convey("All nested folders should be added recursively to the watched folders", func() {
				So(actualWatches, ShouldResemble, expectedWatches)
			})
		})

		Convey("When the watcher is notified of a newly created folder", func() {
			actualWatches, expectedWatches = fixture.receiveNotificationOfNewFolder()

			Convey("The folder should be included in the watched folders", func() {
				So(actualWatches, ShouldResemble, expectedWatches)
			})
		})

		Convey("When the watcher is notified of a recently deleted folder", func() {
			actualWatches, expectedWatches = fixture.receiveNotificationOfDeletedFolder()

			Convey("The folder should no longer be included in the watched folders", func() {
				So(actualWatches, ShouldResemble, expectedWatches)
			})
		})

		Convey("When a watched folder is ignored", func() {
			actualWatches, expectedWatches = fixture.ignoreWatchedFolder()

			Convey("The folder should be marked as inactive in the watched folders listing", func() {
				So(actualWatches, ShouldResemble, expectedWatches)
			})
		})

		Convey("When a folder that is not being watched is ignored", func() {
			actualWatches, expectedWatches = fixture.ignoreIrrelevantFolder()

			Convey("The request should be ignored", func() {
				So(actualWatches, ShouldResemble, expectedWatches)
			})
		})

		Convey("When a folder that does not exist is ignored", func() {
			actualWatches, expectedWatches = fixture.ignoreImaginaryFolder()

			Convey("There should be no change to the watched folders", func() {
				So(actualWatches, ShouldResemble, expectedWatches)
			})
		})

		Convey("When an ignored folder is reinstated", func() {
			actualWatches, expectedWatches = fixture.reinstateIgnoredFolder()

			Convey("The folder should be included once more in the watched folders", func() {
				So(actualWatches, ShouldResemble, expectedWatches)
			})
		})

		Convey("When an ignored folder is deleted and then reinstated", func() {
			actualWatches, expectedWatches = fixture.reinstateDeletedFolder()

			Convey("The reinstatement request should be ignored", func() {
				So(actualWatches, ShouldResemble, expectedWatches)
			})
		})

		Convey("When a folder that is not being watched is reinstated", func() {
			actualWatches, expectedWatches = fixture.reinstateIrrelevantFolder()

			Convey("The request should be ignored", func() {
				So(actualWatches, ShouldResemble, expectedWatches)
			})
		})

		Convey("Regardless of the status of the watched folders", func() {
			folders := fixture.setupSeveralFoldersWithWatcher()

			Convey("The IsWatched query method should be correct", func() {
				So(fixture.watcher.IsWatched(folders["active"]), ShouldBeTrue)
				So(fixture.watcher.IsWatched(folders["reinstated"]), ShouldBeTrue)

				So(fixture.watcher.IsWatched(folders["ignored"]), ShouldBeFalse)
				So(fixture.watcher.IsWatched(folders["deleted"]), ShouldBeFalse)
				So(fixture.watcher.IsWatched(folders["irrelevant"]), ShouldBeFalse)
			})

			Convey("The IsIgnored query method should be correct", func() {
				So(fixture.watcher.IsIgnored(folders["ignored"]), ShouldBeTrue)

				So(fixture.watcher.IsIgnored(folders["active"]), ShouldBeFalse)
				So(fixture.watcher.IsIgnored(folders["reinstated"]), ShouldBeFalse)
				So(fixture.watcher.IsIgnored(folders["deleted"]), ShouldBeFalse)
				So(fixture.watcher.IsIgnored(folders["irrelevant"]), ShouldBeFalse)
			})
		})
	})
}

type watcherFixture struct {
	watcher *Watcher
	files   *system.FakeFileSystem
	shell   *system.FakeShell
}

func (self *watcherFixture) watched() []*contract.Package {
	return self.watcher.WatchedFolders()
}

func (self *watcherFixture) pointToExistingRoot(folder string) (actual, expected interface{}) {
	self.files.Create(folder, 1, time.Now())

	self.watcher.Adjust(folder)

	actual = self.watched()
	expected = []*contract.Package{&contract.Package{Active: true, Path: goProject, Name: goPackagePrefix, Result: contract.NewPackageResult(goPackagePrefix)}}
	return
}

func (self *watcherFixture) pointToImaginaryRoot(folder string) (actual, expected interface{}) {
	actual = self.watcher.Adjust(folder)
	expected = errors.New("Directory does not exist: '/not/there'")
	return
}

func (self *watcherFixture) pointToExistingRootWithNestedFolders() (actual, expected interface{}) {
	self.files.Create(goProject, 1, time.Now())
	self.files.Create(goProject+"/sub", 2, time.Now())
	self.files.Create(goProject+"/sub2", 3, time.Now())
	self.files.Create(goProject+"/sub/subsub", 4, time.Now())

	self.watcher.Adjust(goProject)

	actual = self.watched()
	expected = []*contract.Package{
		&contract.Package{Active: true, Path: goProject, Name: goPackagePrefix, Result: contract.NewPackageResult(goPackagePrefix)},
		&contract.Package{Active: true, Path: goProject + "/sub", Name: goPackagePrefix + "/sub", Result: contract.NewPackageResult(goPackagePrefix + "/sub")},
		&contract.Package{Active: true, Path: goProject + "/sub2", Name: goPackagePrefix + "/sub2", Result: contract.NewPackageResult(goPackagePrefix + "/sub2")},
		&contract.Package{Active: true, Path: goProject + "/sub/subsub", Name: goPackagePrefix + "/sub/subsub", Result: contract.NewPackageResult(goPackagePrefix + "/sub/subsub")},
	}
	return
}

func (self *watcherFixture) pointToRootOfGoPath() {
	self.files.Create("/root/gopath", 5, time.Now())

	self.watcher.Adjust("/root/gopath")
}

func (self *watcherFixture) pointToNestedPartOfGoPath() {
	self.files.Create("/root/gopath", 5, time.Now())
	self.files.Create("/root/gopath/src/github.com/smartystreets/project", 6, time.Now())

	self.watcher.Adjust("/root/gopath/src/github.com/smartystreets/project")
}

func (self *watcherFixture) pointTo(path string) {
	self.files.Create(path, 5, time.Now())
	self.watcher.Adjust(path)
}

func (self *watcherFixture) setAmbientGoPath(path string) {
	self.shell.Setenv("GOPATH", path)
	self.files.Create(path, int64(42+len(path)), time.Now())
	self.watcher = NewWatcher(self.files, self.shell)
}

func (self *watcherFixture) receiveNotificationOfNewFolder() (actual, expected interface{}) {
	self.watcher.Creation(goProject + "/sub")

	actual = self.watched()
	expected = []*contract.Package{&contract.Package{Active: true, Path: goProject + "/sub", Name: goPackagePrefix + "/sub", Result: contract.NewPackageResult(goPackagePrefix + "/sub")}}
	return
}

func (self *watcherFixture) receiveNotificationOfDeletedFolder() (actual, expected interface{}) {
	self.watcher.Creation(goProject + "/sub2")
	self.watcher.Creation(goProject + "/sub")

	self.watcher.Deletion(goProject + "/sub")

	actual = self.watched()
	expected = []*contract.Package{&contract.Package{Active: true, Path: goProject + "/sub2", Name: goPackagePrefix + "/sub2", Result: contract.NewPackageResult(goPackagePrefix + "/sub2")}}
	return
}

func (self *watcherFixture) ignoreWatchedFolder() (actual, expected interface{}) {
	self.watcher.Creation(goProject + "/sub2")

	self.watcher.Ignore(goPackagePrefix + "/sub2")

	actual = self.watched()
	expected = []*contract.Package{&contract.Package{Active: false, Path: goProject + "/sub2", Name: goPackagePrefix + "/sub2", Result: contract.NewPackageResult(goPackagePrefix + "/sub2")}}
	return
}

func (self *watcherFixture) ignoreIrrelevantFolder() (actual, expected interface{}) {
	self.files.Create(goProject, 1, time.Now())
	self.files.Create("/something", 1, time.Now())
	self.watcher.Adjust(goProject)

	self.watcher.Ignore("/something")

	actual = self.watched()
	expected = []*contract.Package{&contract.Package{Active: true, Path: goProject, Name: goPackagePrefix, Result: contract.NewPackageResult(goPackagePrefix)}}
	return
}

func (self *watcherFixture) ignoreImaginaryFolder() (actual, expected interface{}) {
	self.files.Create(goProject, 1, time.Now())
	self.watcher.Adjust(goProject)

	self.watcher.Ignore("/not/there")

	actual = self.watched()
	expected = []*contract.Package{&contract.Package{Active: true, Path: goProject, Name: goPackagePrefix, Result: contract.NewPackageResult(goPackagePrefix)}}
	return
}

func (self *watcherFixture) reinstateIgnoredFolder() (actual, expected interface{}) {
	self.files.Create(goProject, 1, time.Now())
	self.files.Create(goProject+"/sub", 2, time.Now())
	self.watcher.Adjust(goProject)
	self.watcher.Ignore(goPackagePrefix + "/sub")

	self.watcher.Reinstate(goProject + "/sub")

	actual = self.watched()
	expected = []*contract.Package{
		&contract.Package{Active: true, Path: goProject, Name: goPackagePrefix, Result: contract.NewPackageResult(goPackagePrefix)},
		&contract.Package{Active: true, Path: goProject + "/sub", Name: goPackagePrefix + "/sub", Result: contract.NewPackageResult(goPackagePrefix + "/sub")},
	}
	return
}

func (self *watcherFixture) reinstateDeletedFolder() (actual, expected interface{}) {
	self.files.Create(goProject, 1, time.Now())
	self.files.Create(goProject+"/sub", 2, time.Now())
	self.watcher.Adjust(goProject)
	self.watcher.Ignore(goPackagePrefix + "/sub")
	self.watcher.Deletion(goProject + "/sub")

	self.watcher.Reinstate(goPackagePrefix + "/sub")

	actual = self.watched()
	expected = []*contract.Package{&contract.Package{Active: true, Path: goProject, Name: goPackagePrefix, Result: contract.NewPackageResult(goPackagePrefix)}}
	return
}

func (self *watcherFixture) reinstateIrrelevantFolder() (actual, expected interface{}) {
	self.files.Create(goProject, 1, time.Now())
	self.files.Create("/irrelevant", 2, time.Now())
	self.watcher.Adjust(goProject)

	self.watcher.Reinstate("/irrelevant")

	actual = self.watched()
	expected = []*contract.Package{&contract.Package{Active: true, Path: goProject, Name: goPackagePrefix, Result: contract.NewPackageResult(goPackagePrefix)}}
	return
}

func (self *watcherFixture) setupSeveralFoldersWithWatcher() map[string]string {
	self.files.Create(goProject, 0, time.Now())
	self.files.Create(goProject+"/active", 1, time.Now())
	self.files.Create(goProject+"/reinstated", 2, time.Now())
	self.files.Create(goProject+"/ignored", 3, time.Now())
	self.files.Create(goProject+"/deleted", 4, time.Now())
	self.files.Create("/irrelevant", 5, time.Now())

	self.watcher.Adjust(goProject)
	self.watcher.Ignore(goPackagePrefix + "/ignored")
	self.watcher.Ignore(goPackagePrefix + "/reinstated")
	self.watcher.Reinstate(goPackagePrefix + "/reinstated")
	self.watcher.Deletion(goProject + "/deleted")
	self.files.Delete(goProject + "/deleted")

	return map[string]string{
		"active":     goProject + "/active",
		"reinstated": goProject + "/reinstated",
		"ignored":    goProject + "/ignored",
		"deleted":    goProject + "/deleted",
		"irrelevant": "/irrelevant",
	}
}

func newWatcherFixture() *watcherFixture {
	self := &watcherFixture{}
	self.files = system.NewFakeFileSystem()
	self.shell = system.NewFakeShell()
	self.shell.Setenv("GOPATH", gopath)
	self.watcher = NewWatcher(self.files, self.shell)
	return self
}

const gopath = "/root/gopath"
const goPackagePrefix = "github.com/smartystreets/project"
const goProject = gopath + "/src/" + goPackagePrefix

func init() {
	fmt.Sprintf("Keeps fmt in the import list...")
}