pulsar/spec/git-spec.coffee

275 lines
10 KiB
CoffeeScript
Raw Normal View History

temp = require 'temp'
2013-09-18 05:58:41 +04:00
Git = require '../src/git'
2013-09-20 00:51:53 +04:00
{fs} = require 'atom'
path = require 'path'
2013-09-18 05:58:41 +04:00
Task = require '../src/task'
2012-10-26 01:52:25 +04:00
describe "Git", ->
repo = null
2012-10-26 01:52:25 +04:00
2012-10-27 20:37:28 +04:00
beforeEach ->
gitPath = path.join(temp.dir, '.git')
fs.remove(gitPath) if fs.isDirectorySync(gitPath)
2012-10-27 20:37:28 +04:00
afterEach ->
repo.destroy() if repo?.repo?
describe "@open(path)", ->
it "returns null when no repository is found", ->
2013-10-21 17:49:55 +04:00
expect(Git.open(path.join(temp.dir, 'nogit.txt'))).toBeNull()
describe "new Git(path)", ->
it "throws an exception when no repository is found", ->
expect(-> new Git(path.join(temp.dir, 'nogit.txt'))).toThrow()
2012-11-03 00:24:38 +04:00
describe ".getPath()", ->
it "returns the repository path for a .git directory path", ->
repo = new Git(path.join(__dirname, 'fixtures', 'git', 'master.git', 'HEAD'))
expect(repo.getPath()).toBe path.join(__dirname, 'fixtures', 'git', 'master.git')
2012-10-26 02:20:12 +04:00
it "returns the repository path for a repository path", ->
repo = new Git(path.join(__dirname, 'fixtures', 'git', 'master.git'))
expect(repo.getPath()).toBe path.join(__dirname, 'fixtures', 'git', 'master.git')
2012-10-26 02:20:12 +04:00
describe ".isPathIgnored(path)", ->
2012-10-27 03:24:47 +04:00
it "returns true for an ignored path", ->
repo = new Git(path.join(__dirname, 'fixtures', 'git', 'ignore.git'))
expect(repo.isPathIgnored('a.txt')).toBeTruthy()
2012-10-27 03:24:47 +04:00
it "returns false for a non-ignored path", ->
repo = new Git(path.join(__dirname, 'fixtures', 'git', 'ignore.git'))
expect(repo.isPathIgnored('b.txt')).toBeFalsy()
2012-11-03 00:24:38 +04:00
describe ".isPathModified(path)", ->
[repo, filePath, newPath, originalPathText] = []
2012-11-03 00:24:38 +04:00
beforeEach ->
repo = new Git(path.join(__dirname, 'fixtures', 'git', 'working-dir'))
filePath = require.resolve('./fixtures/git/working-dir/file.txt')
newPath = path.join(__dirname, 'fixtures', 'git', 'working-dir', 'new-path.txt')
2013-09-18 05:58:41 +04:00
originalPathText = fs.read(filePath)
2012-11-03 00:24:38 +04:00
afterEach ->
2013-09-18 05:58:41 +04:00
fs.writeSync(filePath, originalPathText)
fs.remove(newPath) if fs.exists(newPath)
2012-11-03 00:24:38 +04:00
describe "when the path is unstaged", ->
it "returns false if the path has not been modified", ->
expect(repo.isPathModified(filePath)).toBeFalsy()
2012-11-03 00:24:38 +04:00
it "returns true if the path is modified", ->
2013-09-18 05:58:41 +04:00
fs.writeSync(filePath, "change")
expect(repo.isPathModified(filePath)).toBeTruthy()
2012-11-03 00:24:38 +04:00
it "returns true if the path is deleted", ->
2013-09-18 05:58:41 +04:00
fs.remove(filePath)
expect(repo.isPathModified(filePath)).toBeTruthy()
it "returns false if the path is new", ->
expect(repo.isPathModified(newPath)).toBeFalsy()
describe ".isPathNew(path)", ->
[filePath, newPath] = []
beforeEach ->
repo = new Git(path.join(__dirname, 'fixtures', 'git', 'working-dir'))
filePath = require.resolve('./fixtures/git/working-dir/file.txt')
newPath = path.join(__dirname, 'fixtures', 'git', 'working-dir', 'new-path.txt')
2013-09-18 05:58:41 +04:00
fs.writeSync(newPath, "i'm new here")
afterEach ->
2013-09-18 05:58:41 +04:00
fs.remove(newPath) if fs.exists(newPath)
describe "when the path is unstaged", ->
it "returns true if the path is new", ->
expect(repo.isPathNew(newPath)).toBeTruthy()
it "returns false if the path isn't new", ->
expect(repo.isPathNew(filePath)).toBeFalsy()
2012-11-04 02:09:43 +04:00
describe ".checkoutHead(path)", ->
[path1, path2, originalPath1Text, originalPath2Text] = []
2012-11-04 02:09:43 +04:00
beforeEach ->
repo = new Git(path.join(__dirname, 'fixtures', 'git', 'working-dir'))
path1 = require.resolve('./fixtures/git/working-dir/file.txt')
2013-09-18 05:58:41 +04:00
originalPath1Text = fs.read(path1)
path2 = require.resolve('./fixtures/git/working-dir/other.txt')
2013-09-18 05:58:41 +04:00
originalPath2Text = fs.read(path2)
2012-11-04 02:09:43 +04:00
afterEach ->
2013-09-18 05:58:41 +04:00
fs.writeSync(path1, originalPath1Text)
fs.writeSync(path2, originalPath2Text)
2012-11-04 02:09:43 +04:00
it "no longer reports a path as modified after checkout", ->
expect(repo.isPathModified(path1)).toBeFalsy()
2013-09-18 05:58:41 +04:00
fs.writeSync(path1, '')
2012-11-04 02:09:43 +04:00
expect(repo.isPathModified(path1)).toBeTruthy()
expect(repo.checkoutHead(path1)).toBeTruthy()
expect(repo.isPathModified(path1)).toBeFalsy()
it "restores the contents of the path to the original text", ->
2013-09-18 05:58:41 +04:00
fs.writeSync(path1, '')
2012-11-04 02:09:43 +04:00
expect(repo.checkoutHead(path1)).toBeTruthy()
2013-09-18 05:58:41 +04:00
expect(fs.read(path1)).toBe(originalPath1Text)
2012-11-04 02:09:43 +04:00
it "only restores the path specified", ->
2013-09-18 05:58:41 +04:00
fs.writeSync(path2, 'path 2 is edited')
2012-11-04 02:09:43 +04:00
expect(repo.isPathModified(path2)).toBeTruthy()
expect(repo.checkoutHead(path1)).toBeTruthy()
2013-09-18 05:58:41 +04:00
expect(fs.read(path2)).toBe('path 2 is edited')
2012-11-04 02:09:43 +04:00
expect(repo.isPathModified(path2)).toBeTruthy()
it "fires a status-changed event if the checkout completes successfully", ->
2013-09-18 05:58:41 +04:00
fs.writeSync(path1, '')
repo.getPathStatus(path1)
statusHandler = jasmine.createSpy('statusHandler')
repo.on 'status-changed', statusHandler
repo.checkoutHead(path1)
expect(statusHandler.callCount).toBe 1
expect(statusHandler.argsForCall[0][0..1]).toEqual [path1, 0]
repo.checkoutHead(path1)
expect(statusHandler.callCount).toBe 1
describe ".destroy()", ->
it "throws an exception when any method is called after it is called", ->
repo = new Git(require.resolve('./fixtures/git/master.git/HEAD'))
repo.destroy()
2013-06-13 20:55:42 +04:00
expect(-> repo.getShortHead()).toThrow()
2013-02-15 21:08:22 +04:00
describe ".getDiffStats(path)", ->
[filePath, originalPathText] = []
2013-02-15 21:08:22 +04:00
beforeEach ->
repo = new Git(path.join(__dirname, 'fixtures', 'git', 'working-dir'))
filePath = require.resolve('./fixtures/git/working-dir/file.txt')
2013-09-18 05:58:41 +04:00
originalPathText = fs.read(filePath)
2013-02-15 21:08:22 +04:00
afterEach ->
2013-09-18 05:58:41 +04:00
fs.writeSync(filePath, originalPathText)
2013-02-15 21:08:22 +04:00
it "returns the number of lines added and deleted", ->
expect(repo.getDiffStats(filePath)).toEqual {added: 0, deleted: 0}
2013-09-18 05:58:41 +04:00
fs.writeSync(filePath, "#{originalPathText} edited line")
expect(repo.getDiffStats(filePath)).toEqual {added: 1, deleted: 1}
2013-02-28 03:36:08 +04:00
describe ".getPathStatus(path)", ->
[filePath, originalPathText] = []
2013-02-28 03:36:08 +04:00
beforeEach ->
repo = new Git(path.join(__dirname, 'fixtures', 'git', 'working-dir'))
filePath = require.resolve('./fixtures/git/working-dir/file.txt')
2013-09-18 05:58:41 +04:00
originalPathText = fs.read(filePath)
2013-02-28 03:36:08 +04:00
afterEach ->
2013-09-18 05:58:41 +04:00
fs.writeSync(filePath, originalPathText)
2013-02-28 03:36:08 +04:00
it "trigger a status-changed event when the new status differs from the last cached one", ->
statusHandler = jasmine.createSpy("statusHandler")
repo.on 'status-changed', statusHandler
2013-09-18 05:58:41 +04:00
fs.writeSync(filePath, '')
status = repo.getPathStatus(filePath)
2013-02-28 03:36:08 +04:00
expect(statusHandler.callCount).toBe 1
2013-06-13 03:55:01 +04:00
expect(statusHandler.argsForCall[0][0..1]).toEqual [filePath, status]
2013-02-28 03:36:08 +04:00
2013-09-18 05:58:41 +04:00
fs.writeSync(filePath, 'abc')
status = repo.getPathStatus(filePath)
expect(statusHandler.callCount).toBe 1
2013-02-28 03:36:08 +04:00
describe ".refreshStatus()", ->
[newPath, modifiedPath, cleanPath, originalModifiedPathText] = []
beforeEach ->
repo = new Git(path.join(__dirname, 'fixtures', 'git', 'working-dir'))
2013-03-06 00:04:14 +04:00
modifiedPath = project.resolve('git/working-dir/file.txt')
2013-09-18 05:58:41 +04:00
originalModifiedPathText = fs.read(modifiedPath)
2013-03-06 00:04:14 +04:00
newPath = project.resolve('git/working-dir/untracked.txt')
cleanPath = project.resolve('git/working-dir/other.txt')
2013-09-18 05:58:41 +04:00
fs.writeSync(newPath, '')
afterEach ->
2013-09-18 05:58:41 +04:00
fs.writeSync(modifiedPath, originalModifiedPathText)
fs.remove(newPath) if fs.exists(newPath)
it "returns status information for all new and modified files", ->
2013-09-18 05:58:41 +04:00
fs.writeSync(modifiedPath, 'making this path modified')
statusHandler = jasmine.createSpy('statusHandler')
repo.on 'statuses-changed', statusHandler
repo.refreshStatus()
waitsFor ->
statusHandler.callCount > 0
runs ->
statuses = repo.statuses
expect(statuses[cleanPath]).toBeUndefined()
expect(repo.isStatusNew(statuses[newPath])).toBeTruthy()
expect(repo.isStatusModified(statuses[modifiedPath])).toBeTruthy()
describe "buffer events", ->
[originalContent, editSession] = []
beforeEach ->
editSession = project.openSync('sample.js')
originalContent = editSession.getText()
afterEach ->
2013-09-18 05:58:41 +04:00
fs.writeSync(editSession.getPath(), originalContent)
it "emits a status-changed event when a buffer is saved", ->
editSession.insertNewline()
statusHandler = jasmine.createSpy('statusHandler')
project.getRepo().on 'status-changed', statusHandler
editSession.save()
expect(statusHandler.callCount).toBe 1
expect(statusHandler).toHaveBeenCalledWith editSession.getPath(), 256
it "emits a status-changed event when a buffer is reloaded", ->
2013-09-18 05:58:41 +04:00
fs.writeSync(editSession.getPath(), 'changed')
statusHandler = jasmine.createSpy('statusHandler')
project.getRepo().on 'status-changed', statusHandler
editSession.getBuffer().reload()
expect(statusHandler.callCount).toBe 1
expect(statusHandler).toHaveBeenCalledWith editSession.getPath(), 256
editSession.getBuffer().reload()
expect(statusHandler.callCount).toBe 1
it "emits a status-changed event when a buffer's path changes", ->
fs.writeSync(editSession.getPath(), 'changed')
statusHandler = jasmine.createSpy('statusHandler')
project.getRepo().on 'status-changed', statusHandler
editSession.getBuffer().trigger 'path-changed'
expect(statusHandler.callCount).toBe 1
expect(statusHandler).toHaveBeenCalledWith editSession.getPath(), 256
editSession.getBuffer().trigger 'path-changed'
expect(statusHandler.callCount).toBe 1
describe "when a project is deserialized", ->
[originalContent, buffer, project2] = []
afterEach ->
2013-09-18 05:58:41 +04:00
fs.writeSync(buffer.getPath(), originalContent)
project2?.destroy()
it "subscribes to all the serialized buffers in the project", ->
project.openSync('sample.js')
project2 = deserialize(project.serialize())
buffer = project2.getBuffers()[0]
2013-10-17 22:48:22 +04:00
waitsFor ->
buffer.loaded
runs ->
originalContent = buffer.getText()
buffer.append('changes')
statusHandler = jasmine.createSpy('statusHandler')
project2.getRepo().on 'status-changed', statusHandler
buffer.save()
expect(statusHandler.callCount).toBe 1
expect(statusHandler).toHaveBeenCalledWith buffer.getPath(), 256