2013-09-24 02:46:57 +04:00
|
|
|
temp = require 'temp'
|
|
|
|
fstream = require 'fstream'
|
2013-09-18 05:58:41 +04:00
|
|
|
Project = require '../src/project'
|
2014-02-24 05:09:05 +04:00
|
|
|
_ = require 'underscore-plus'
|
|
|
|
fs = require 'fs-plus'
|
2013-06-13 02:26:09 +04:00
|
|
|
path = require 'path'
|
2013-09-18 05:58:41 +04:00
|
|
|
BufferedProcess = require '../src/buffered-process'
|
2012-08-28 02:36:36 +04:00
|
|
|
|
|
|
|
describe "Project", ->
|
|
|
|
beforeEach ->
|
2015-01-10 01:54:54 +03:00
|
|
|
atom.project.setPaths([atom.project.getDirectories()[0]?.resolve('dir')])
|
2012-08-28 02:36:36 +04:00
|
|
|
|
2013-08-29 05:37:40 +04:00
|
|
|
describe "serialization", ->
|
2013-08-30 05:24:51 +04:00
|
|
|
deserializedProject = null
|
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
deserializedProject?.destroy()
|
|
|
|
|
2014-01-04 03:23:23 +04:00
|
|
|
it "does not include unretained buffers in the serialized state", ->
|
2014-04-24 03:50:25 +04:00
|
|
|
waitsForPromise ->
|
|
|
|
atom.project.bufferForPath('a')
|
2013-11-21 04:45:26 +04:00
|
|
|
|
2014-04-24 03:50:25 +04:00
|
|
|
runs ->
|
|
|
|
expect(atom.project.getBuffers().length).toBe 1
|
|
|
|
deserializedProject = atom.project.testSerialization()
|
|
|
|
expect(deserializedProject.getBuffers().length).toBe 0
|
2013-08-29 05:37:40 +04:00
|
|
|
|
2013-11-19 21:08:24 +04:00
|
|
|
it "listens for destroyed events on deserialized buffers and removes them when they are destroyed", ->
|
2014-04-24 03:50:25 +04:00
|
|
|
waitsForPromise ->
|
|
|
|
atom.project.open('a')
|
2013-11-19 21:08:24 +04:00
|
|
|
|
2014-04-24 03:50:25 +04:00
|
|
|
runs ->
|
|
|
|
expect(atom.project.getBuffers().length).toBe 1
|
|
|
|
deserializedProject = atom.project.testSerialization()
|
|
|
|
|
|
|
|
expect(deserializedProject.getBuffers().length).toBe 1
|
|
|
|
deserializedProject.getBuffers()[0].destroy()
|
|
|
|
expect(deserializedProject.getBuffers().length).toBe 0
|
2013-11-19 21:08:24 +04:00
|
|
|
|
2015-01-15 22:51:11 +03:00
|
|
|
|
2015-01-15 22:53:39 +03:00
|
|
|
it "does not deserialize buffers when their path is a directory that exists", ->
|
2015-01-15 22:51:11 +03:00
|
|
|
pathToOpen = path.join(temp.mkdirSync(), 'file.txt')
|
|
|
|
|
|
|
|
waitsForPromise ->
|
|
|
|
atom.project.open(pathToOpen)
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
expect(atom.project.getBuffers().length).toBe 1
|
|
|
|
fs.mkdirSync(pathToOpen)
|
|
|
|
deserializedProject = atom.project.testSerialization()
|
2015-01-16 00:08:16 +03:00
|
|
|
expect(deserializedProject.getBuffers().length).toBe 0
|
|
|
|
|
|
|
|
it "does not deserialize buffers when their path is inaccessible", ->
|
|
|
|
pathToOpen = path.join(temp.mkdirSync(), 'file.txt')
|
|
|
|
fs.writeFileSync(pathToOpen, '')
|
|
|
|
|
|
|
|
waitsForPromise ->
|
|
|
|
atom.project.open(pathToOpen)
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
expect(atom.project.getBuffers().length).toBe 1
|
|
|
|
fs.chmodSync(pathToOpen, '000')
|
|
|
|
deserializedProject = atom.project.testSerialization()
|
2015-01-15 22:51:11 +03:00
|
|
|
expect(deserializedProject.getBuffers().length).toBe 0
|
|
|
|
|
2014-04-08 21:52:19 +04:00
|
|
|
describe "when an editor is saved and the project has no path", ->
|
2013-02-28 03:21:42 +04:00
|
|
|
it "sets the project's path to the saved file's parent directory", ->
|
2013-10-04 00:22:05 +04:00
|
|
|
tempFile = temp.openSync().path
|
2014-10-01 20:37:27 +04:00
|
|
|
atom.project.setPaths([])
|
|
|
|
expect(atom.project.getPaths()[0]).toBeUndefined()
|
2014-04-24 03:50:25 +04:00
|
|
|
editor = null
|
2013-02-28 03:21:42 +04:00
|
|
|
|
2014-04-24 03:50:25 +04:00
|
|
|
waitsForPromise ->
|
|
|
|
atom.project.open().then (o) -> editor = o
|
2014-02-13 04:56:00 +04:00
|
|
|
|
2014-04-24 03:50:25 +04:00
|
|
|
runs ->
|
|
|
|
editor.saveAs(tempFile)
|
2014-10-01 20:37:27 +04:00
|
|
|
expect(atom.project.getPaths()[0]).toBe path.dirname(tempFile)
|
2013-05-15 06:23:25 +04:00
|
|
|
|
2015-01-09 04:10:59 +03:00
|
|
|
describe "when a watch error is thrown from the TextBuffer", ->
|
|
|
|
editor = null
|
|
|
|
beforeEach ->
|
|
|
|
waitsForPromise ->
|
|
|
|
atom.project.open(require.resolve('./fixtures/dir/a')).then (o) -> editor = o
|
|
|
|
|
|
|
|
it "creates a warning notification", ->
|
|
|
|
atom.notifications.onDidAddNotification noteSpy = jasmine.createSpy()
|
|
|
|
|
2015-01-14 23:46:31 +03:00
|
|
|
error = new Error('SomeError')
|
|
|
|
error.eventType = 'resurrect'
|
2015-01-09 04:10:59 +03:00
|
|
|
editor.buffer.emitter.emit 'will-throw-watch-error',
|
|
|
|
handle: jasmine.createSpy()
|
2015-01-14 23:46:31 +03:00
|
|
|
error: error
|
2015-01-09 04:10:59 +03:00
|
|
|
|
|
|
|
expect(noteSpy).toHaveBeenCalled()
|
|
|
|
|
|
|
|
notification = noteSpy.mostRecentCall.args[0]
|
|
|
|
expect(notification.getType()).toBe 'warning'
|
|
|
|
expect(notification.getDetail()).toBe 'SomeError'
|
2015-01-14 23:46:31 +03:00
|
|
|
expect(notification.getMessage()).toContain '`resurrect`'
|
2015-01-15 02:17:55 +03:00
|
|
|
expect(notification.getMessage()).toContain 'fixtures/dir/a'
|
2015-01-09 04:10:59 +03:00
|
|
|
|
2014-02-13 04:56:00 +04:00
|
|
|
describe ".open(path)", ->
|
2014-04-08 21:52:19 +04:00
|
|
|
[absolutePath, newBufferHandler] = []
|
2014-02-13 04:56:00 +04:00
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
absolutePath = require.resolve('./fixtures/dir/a')
|
|
|
|
newBufferHandler = jasmine.createSpy('newBufferHandler')
|
|
|
|
atom.project.on 'buffer-created', newBufferHandler
|
2013-05-15 06:23:25 +04:00
|
|
|
|
2014-02-13 04:56:00 +04:00
|
|
|
describe "when given an absolute path that isn't currently open", ->
|
2014-04-08 21:52:19 +04:00
|
|
|
it "returns a new edit session for the given path and emits 'buffer-created'", ->
|
2014-02-13 04:56:00 +04:00
|
|
|
editor = null
|
|
|
|
waitsForPromise ->
|
|
|
|
atom.project.open(absolutePath).then (o) -> editor = o
|
|
|
|
|
|
|
|
runs ->
|
2013-11-20 03:22:47 +04:00
|
|
|
expect(editor.buffer.getPath()).toBe absolutePath
|
|
|
|
expect(newBufferHandler).toHaveBeenCalledWith editor.buffer
|
2013-05-15 06:23:25 +04:00
|
|
|
|
2014-02-13 04:56:00 +04:00
|
|
|
describe "when given a relative path that isn't currently opened", ->
|
2014-04-08 21:52:19 +04:00
|
|
|
it "returns a new edit session for the given path (relative to the project root) and emits 'buffer-created'", ->
|
2014-02-13 04:56:00 +04:00
|
|
|
editor = null
|
|
|
|
waitsForPromise ->
|
|
|
|
atom.project.open(absolutePath).then (o) -> editor = o
|
|
|
|
|
|
|
|
runs ->
|
2013-11-20 03:22:47 +04:00
|
|
|
expect(editor.buffer.getPath()).toBe absolutePath
|
|
|
|
expect(newBufferHandler).toHaveBeenCalledWith editor.buffer
|
2013-05-15 06:23:25 +04:00
|
|
|
|
2014-02-13 04:56:00 +04:00
|
|
|
describe "when passed the path to a buffer that is currently opened", ->
|
2014-04-08 21:52:19 +04:00
|
|
|
it "returns a new edit session containing currently opened buffer", ->
|
2014-02-13 04:56:00 +04:00
|
|
|
editor = null
|
2014-04-24 03:50:25 +04:00
|
|
|
|
2014-02-13 04:56:00 +04:00
|
|
|
waitsForPromise ->
|
|
|
|
atom.project.open(absolutePath).then (o) -> editor = o
|
|
|
|
|
|
|
|
runs ->
|
2013-05-15 06:23:25 +04:00
|
|
|
newBufferHandler.reset()
|
2014-04-24 03:50:25 +04:00
|
|
|
|
|
|
|
waitsForPromise ->
|
|
|
|
atom.project.open(absolutePath).then ({buffer}) ->
|
|
|
|
expect(buffer).toBe editor.buffer
|
|
|
|
|
|
|
|
waitsForPromise ->
|
|
|
|
atom.project.open('a').then ({buffer}) ->
|
|
|
|
expect(buffer).toBe editor.buffer
|
|
|
|
expect(newBufferHandler).not.toHaveBeenCalled()
|
2013-05-15 06:23:25 +04:00
|
|
|
|
2014-02-13 04:56:00 +04:00
|
|
|
describe "when not passed a path", ->
|
2014-04-08 21:52:19 +04:00
|
|
|
it "returns a new edit session and emits 'buffer-created'", ->
|
2014-02-13 04:56:00 +04:00
|
|
|
editor = null
|
|
|
|
waitsForPromise ->
|
|
|
|
atom.project.open().then (o) -> editor = o
|
|
|
|
|
|
|
|
runs ->
|
2013-11-20 03:22:47 +04:00
|
|
|
expect(editor.buffer.getPath()).toBeUndefined()
|
|
|
|
expect(newBufferHandler).toHaveBeenCalledWith(editor.buffer)
|
2013-05-15 06:23:25 +04:00
|
|
|
|
2013-10-08 00:47:20 +04:00
|
|
|
it "returns number of read bytes as progress indicator", ->
|
2015-01-10 01:54:54 +03:00
|
|
|
filePath = atom.project.getDirectories()[0]?.resolve 'a'
|
2013-10-08 00:47:20 +04:00
|
|
|
totalBytes = 0
|
2013-11-21 03:35:49 +04:00
|
|
|
promise = atom.project.open(filePath)
|
2013-10-08 00:47:20 +04:00
|
|
|
promise.progress (bytesRead) -> totalBytes = bytesRead
|
|
|
|
|
|
|
|
waitsForPromise ->
|
|
|
|
promise
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
expect(totalBytes).toBe fs.statSync(filePath).size
|
|
|
|
|
2013-10-15 21:58:33 +04:00
|
|
|
describe ".bufferForPath(path)", ->
|
2013-10-03 03:17:49 +04:00
|
|
|
[buffer] = []
|
|
|
|
beforeEach ->
|
|
|
|
waitsForPromise ->
|
2013-11-21 03:35:49 +04:00
|
|
|
atom.project.bufferForPath("a").then (o) ->
|
2013-10-03 03:17:49 +04:00
|
|
|
buffer = o
|
|
|
|
buffer.retain()
|
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
buffer.release()
|
|
|
|
|
|
|
|
describe "when opening a previously opened path", ->
|
|
|
|
it "does not create a new buffer", ->
|
|
|
|
waitsForPromise ->
|
2013-11-21 03:35:49 +04:00
|
|
|
atom.project.bufferForPath("a").then (anotherBuffer) ->
|
2013-10-03 03:17:49 +04:00
|
|
|
expect(anotherBuffer).toBe buffer
|
|
|
|
|
|
|
|
waitsForPromise ->
|
2013-11-21 03:35:49 +04:00
|
|
|
atom.project.bufferForPath("b").then (anotherBuffer) ->
|
2013-10-03 03:17:49 +04:00
|
|
|
expect(anotherBuffer).not.toBe buffer
|
|
|
|
|
|
|
|
it "creates a new buffer if the previous buffer was destroyed", ->
|
|
|
|
buffer.release()
|
|
|
|
|
|
|
|
waitsForPromise ->
|
2013-11-21 03:35:49 +04:00
|
|
|
atom.project.bufferForPath("b").then (anotherBuffer) ->
|
2013-10-03 03:17:49 +04:00
|
|
|
expect(anotherBuffer).not.toBe buffer
|
|
|
|
|
2014-11-27 19:11:21 +03:00
|
|
|
describe ".setPaths(path)", ->
|
2012-08-28 02:36:36 +04:00
|
|
|
describe "when path is a file", ->
|
|
|
|
it "sets its path to the files parent directory and updates the root directory", ->
|
2014-10-01 20:37:27 +04:00
|
|
|
atom.project.setPaths([require.resolve('./fixtures/dir/a')])
|
|
|
|
expect(atom.project.getPaths()[0]).toEqual path.dirname(require.resolve('./fixtures/dir/a'))
|
2014-11-26 01:54:06 +03:00
|
|
|
expect(atom.project.getDirectories()[0].path).toEqual path.dirname(require.resolve('./fixtures/dir/a'))
|
2012-08-28 02:36:36 +04:00
|
|
|
|
|
|
|
describe "when path is a directory", ->
|
|
|
|
it "sets its path to the directory and updates the root directory", ->
|
2013-09-18 05:58:41 +04:00
|
|
|
directory = fs.absolute(path.join(__dirname, 'fixtures', 'dir', 'a-dir'))
|
2014-10-01 20:37:27 +04:00
|
|
|
atom.project.setPaths([directory])
|
|
|
|
expect(atom.project.getPaths()[0]).toEqual directory
|
2014-11-26 01:54:06 +03:00
|
|
|
expect(atom.project.getDirectories()[0].path).toEqual directory
|
2012-08-28 02:36:36 +04:00
|
|
|
|
|
|
|
describe "when path is null", ->
|
|
|
|
it "sets its path and root directory to null", ->
|
2014-10-01 20:37:27 +04:00
|
|
|
atom.project.setPaths([])
|
|
|
|
expect(atom.project.getPaths()[0]?).toBeFalsy()
|
2014-11-26 01:54:06 +03:00
|
|
|
expect(atom.project.getDirectories()[0]?).toBeFalsy()
|
2012-08-28 02:36:36 +04:00
|
|
|
|
2014-09-02 21:47:05 +04:00
|
|
|
it "normalizes the path to remove consecutive slashes, ., and .. segments", ->
|
2014-10-01 20:37:27 +04:00
|
|
|
atom.project.setPaths(["#{require.resolve('./fixtures/dir/a')}#{path.sep}b#{path.sep}#{path.sep}.."])
|
|
|
|
expect(atom.project.getPaths()[0]).toEqual path.dirname(require.resolve('./fixtures/dir/a'))
|
2014-11-26 01:54:06 +03:00
|
|
|
expect(atom.project.getDirectories()[0].path).toEqual path.dirname(require.resolve('./fixtures/dir/a'))
|
2014-09-02 21:47:05 +04:00
|
|
|
|
2013-11-27 02:24:48 +04:00
|
|
|
describe ".eachBuffer(callback)", ->
|
|
|
|
beforeEach ->
|
|
|
|
atom.project.bufferForPathSync('a')
|
|
|
|
|
|
|
|
it "invokes the callback for existing buffer", ->
|
|
|
|
count = 0
|
|
|
|
count = 0
|
|
|
|
callbackBuffer = null
|
|
|
|
callback = (buffer) ->
|
|
|
|
callbackBuffer = buffer
|
|
|
|
count++
|
|
|
|
atom.project.eachBuffer(callback)
|
|
|
|
expect(count).toBe 1
|
|
|
|
expect(callbackBuffer).toBe atom.project.getBuffers()[0]
|
|
|
|
|
|
|
|
it "invokes the callback for new buffers", ->
|
|
|
|
count = 0
|
|
|
|
callbackBuffer = null
|
|
|
|
callback = (buffer) ->
|
|
|
|
callbackBuffer = buffer
|
|
|
|
count++
|
|
|
|
|
|
|
|
atom.project.eachBuffer(callback)
|
|
|
|
count = 0
|
|
|
|
callbackBuffer = null
|
|
|
|
atom.project.bufferForPathSync(require.resolve('./fixtures/sample.txt'))
|
|
|
|
expect(count).toBe 1
|
|
|
|
expect(callbackBuffer).toBe atom.project.getBuffers()[1]
|