2013-09-18 05:58:41 +04:00
|
|
|
Directory = require '../src/directory'
|
2013-09-20 00:51:53 +04:00
|
|
|
{fs} = require 'atom'
|
2013-06-13 03:20:40 +04:00
|
|
|
path = require 'path'
|
2012-08-28 02:36:36 +04:00
|
|
|
|
|
|
|
describe "Directory", ->
|
|
|
|
directory = null
|
|
|
|
|
|
|
|
beforeEach ->
|
2013-09-18 02:58:08 +04:00
|
|
|
directory = new Directory(path.join(__dirname, 'fixtures'))
|
2012-08-28 02:36:36 +04:00
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
directory.off()
|
|
|
|
|
|
|
|
describe "when the contents of the directory change on disk", ->
|
|
|
|
temporaryFilePath = null
|
|
|
|
|
|
|
|
beforeEach ->
|
2013-09-18 02:58:08 +04:00
|
|
|
temporaryFilePath = path.join(__dirname, 'fixtures', 'temporary')
|
2013-11-01 00:43:44 +04:00
|
|
|
fs.removeSync(temporaryFilePath) if fs.existsSync(temporaryFilePath)
|
2012-08-28 02:36:36 +04:00
|
|
|
|
|
|
|
afterEach ->
|
2013-11-01 00:43:44 +04:00
|
|
|
fs.removeSync(temporaryFilePath) if fs.existsSync(temporaryFilePath)
|
2012-08-28 02:36:36 +04:00
|
|
|
|
2013-01-04 04:21:07 +04:00
|
|
|
it "triggers 'contents-changed' event handlers", ->
|
2012-08-28 02:36:36 +04:00
|
|
|
changeHandler = null
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
changeHandler = jasmine.createSpy('changeHandler')
|
2013-01-04 04:21:07 +04:00
|
|
|
directory.on 'contents-changed', changeHandler
|
2013-11-01 04:11:54 +04:00
|
|
|
fs.writeFileSync(temporaryFilePath, '')
|
2012-08-28 02:36:36 +04:00
|
|
|
|
|
|
|
waitsFor "first change", -> changeHandler.callCount > 0
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
changeHandler.reset()
|
2013-11-01 00:43:44 +04:00
|
|
|
fs.removeSync(temporaryFilePath)
|
2012-08-28 02:36:36 +04:00
|
|
|
|
|
|
|
waitsFor "second change", -> changeHandler.callCount > 0
|
|
|
|
|
|
|
|
describe "when the directory unsubscribes from events", ->
|
|
|
|
temporaryFilePath = null
|
|
|
|
|
|
|
|
beforeEach ->
|
2013-06-13 03:20:40 +04:00
|
|
|
temporaryFilePath = path.join(directory.path, 'temporary')
|
2013-11-01 00:43:44 +04:00
|
|
|
fs.removeSync(temporaryFilePath) if fs.existsSync(temporaryFilePath)
|
2012-08-28 02:36:36 +04:00
|
|
|
|
|
|
|
afterEach ->
|
2013-11-01 00:43:44 +04:00
|
|
|
fs.removeSync(temporaryFilePath) if fs.existsSync(temporaryFilePath)
|
2012-08-28 02:36:36 +04:00
|
|
|
|
|
|
|
it "no longer triggers events", ->
|
|
|
|
changeHandler = null
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
changeHandler = jasmine.createSpy('changeHandler')
|
2013-01-04 04:21:07 +04:00
|
|
|
directory.on 'contents-changed', changeHandler
|
2013-11-01 04:11:54 +04:00
|
|
|
fs.writeFileSync(temporaryFilePath, '')
|
2012-08-28 02:36:36 +04:00
|
|
|
|
|
|
|
waitsFor "change event", -> changeHandler.callCount > 0
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
changeHandler.reset()
|
|
|
|
directory.off()
|
|
|
|
waits 20
|
|
|
|
|
2013-11-01 00:43:44 +04:00
|
|
|
runs -> fs.removeSync(temporaryFilePath)
|
2012-08-28 02:36:36 +04:00
|
|
|
waits 20
|
|
|
|
runs -> expect(changeHandler.callCount).toBe 0
|
2013-03-26 00:49:19 +04:00
|
|
|
|
2013-11-06 04:36:51 +04:00
|
|
|
describe "on #darwin or #linux", ->
|
|
|
|
it "includes symlink information about entries", ->
|
2014-01-06 20:49:45 +04:00
|
|
|
entries = directory.getEntriesSync()
|
2013-11-06 04:36:51 +04:00
|
|
|
for entry in entries
|
|
|
|
name = entry.getBaseName()
|
|
|
|
if name is 'symlink-to-dir' or name is 'symlink-to-file'
|
|
|
|
expect(entry.symlink).toBeTruthy()
|
|
|
|
else
|
|
|
|
expect(entry.symlink).toBeFalsy()
|
2013-06-14 19:29:21 +04:00
|
|
|
|
2014-01-06 21:19:29 +04:00
|
|
|
callback = jasmine.createSpy('getEntries')
|
|
|
|
directory.getEntries(callback)
|
|
|
|
|
|
|
|
waitsFor -> callback.callCount is 1
|
|
|
|
|
|
|
|
runs ->
|
2014-01-07 01:34:02 +04:00
|
|
|
entries = callback.mostRecentCall.args[1]
|
2014-01-06 21:19:29 +04:00
|
|
|
for entry in entries
|
|
|
|
name = entry.getBaseName()
|
|
|
|
if name is 'symlink-to-dir' or name is 'symlink-to-file'
|
|
|
|
expect(entry.symlink).toBeTruthy()
|
|
|
|
else
|
|
|
|
expect(entry.symlink).toBeFalsy()
|
|
|
|
|
2013-06-14 19:29:21 +04:00
|
|
|
describe ".relativize(path)", ->
|
2013-11-06 04:36:51 +04:00
|
|
|
describe "on #darwin or #linux", ->
|
|
|
|
it "returns a relative path based on the directory's path", ->
|
|
|
|
absolutePath = directory.getPath()
|
|
|
|
expect(directory.relativize(absolutePath)).toBe ''
|
|
|
|
expect(directory.relativize(path.join(absolutePath, "b"))).toBe "b"
|
|
|
|
expect(directory.relativize(path.join(absolutePath, "b/file.coffee"))).toBe "b/file.coffee"
|
|
|
|
expect(directory.relativize(path.join(absolutePath, "file.coffee"))).toBe "file.coffee"
|
|
|
|
|
|
|
|
it "returns a relative path based on the directory's symlinked source path", ->
|
|
|
|
symlinkPath = path.join(__dirname, 'fixtures', 'symlink-to-dir')
|
|
|
|
symlinkDirectory = new Directory(symlinkPath)
|
|
|
|
realFilePath = require.resolve('./fixtures/dir/a')
|
|
|
|
expect(symlinkDirectory.relativize(symlinkPath)).toBe ''
|
|
|
|
expect(symlinkDirectory.relativize(realFilePath)).toBe 'a'
|
|
|
|
|
|
|
|
it "returns the full path if the directory's path is not a prefix of the path", ->
|
|
|
|
expect(directory.relativize('/not/relative')).toBe '/not/relative'
|
|
|
|
|
|
|
|
describe "on #win32", ->
|
|
|
|
it "returns a relative path based on the directory's path", ->
|
|
|
|
absolutePath = directory.getPath()
|
|
|
|
expect(directory.relativize(absolutePath)).toBe ''
|
|
|
|
expect(directory.relativize(path.join(absolutePath, "b"))).toBe "b"
|
|
|
|
expect(directory.relativize(path.join(absolutePath, "b/file.coffee"))).toBe "b\\file.coffee"
|
|
|
|
expect(directory.relativize(path.join(absolutePath, "file.coffee"))).toBe "file.coffee"
|
|
|
|
|
|
|
|
it "returns the full path if the directory's path is not a prefix of the path", ->
|
|
|
|
expect(directory.relativize('/not/relative')).toBe "\\not\\relative"
|
2013-06-14 19:29:21 +04:00
|
|
|
|
|
|
|
describe ".contains(path)", ->
|
|
|
|
it "returns true if the path is a child of the directory's path", ->
|
|
|
|
absolutePath = directory.getPath()
|
|
|
|
expect(directory.contains(path.join(absolutePath, "b"))).toBe true
|
2013-09-18 02:58:08 +04:00
|
|
|
expect(directory.contains(path.join(absolutePath, "b", "file.coffee"))).toBe true
|
2013-06-14 19:29:21 +04:00
|
|
|
expect(directory.contains(path.join(absolutePath, "file.coffee"))).toBe true
|
|
|
|
|
|
|
|
it "returns false if the directory's path is not a prefix of the path", ->
|
|
|
|
expect(directory.contains('/not/relative')).toBe false
|
2013-11-06 04:36:51 +04:00
|
|
|
|
|
|
|
describe "on #darwin or #linux", ->
|
|
|
|
it "returns true if the path is a child of the directory's symlinked source path", ->
|
|
|
|
symlinkPath = path.join(__dirname, 'fixtures', 'symlink-to-dir')
|
|
|
|
symlinkDirectory = new Directory(symlinkPath)
|
|
|
|
realFilePath = require.resolve('./fixtures/dir/a')
|
|
|
|
expect(symlinkDirectory.contains(realFilePath)).toBe true
|