2012-10-26 01:52:25 +04:00
|
|
|
Git = require 'git'
|
2012-10-27 20:37:28 +04:00
|
|
|
fs = require 'fs'
|
2012-10-26 01:52:25 +04:00
|
|
|
|
|
|
|
describe "Git", ->
|
|
|
|
|
2012-10-27 20:37:28 +04:00
|
|
|
beforeEach ->
|
|
|
|
fs.remove('/tmp/.git') if fs.isDirectory('/tmp/.git')
|
|
|
|
|
2013-01-09 09:15:19 +04:00
|
|
|
describe "@open(path)", ->
|
|
|
|
it "returns null when no repository is found", ->
|
2013-01-09 23:51:31 +04:00
|
|
|
expect(Git.open('/tmp/nogit.txt')).toBeNull()
|
2013-01-09 09:15:19 +04:00
|
|
|
|
2013-01-09 06:28:09 +04:00
|
|
|
describe "new Git(path)", ->
|
|
|
|
it "throws an exception when no repository is found", ->
|
|
|
|
expect(-> new Git('/tmp/nogit.txt')).toThrow()
|
|
|
|
|
2012-11-03 00:24:38 +04:00
|
|
|
describe ".getPath()", ->
|
2012-10-27 03:03:11 +04:00
|
|
|
it "returns the repository path for a .git directory path", ->
|
2012-10-26 02:20:12 +04:00
|
|
|
repo = new Git(require.resolve('fixtures/git/master.git/HEAD'))
|
|
|
|
expect(repo.getPath()).toBe require.resolve('fixtures/git/master.git') + '/'
|
|
|
|
|
|
|
|
it "returns the repository path for a repository path", ->
|
|
|
|
repo = new Git(require.resolve('fixtures/git/master.git'))
|
|
|
|
expect(repo.getPath()).toBe require.resolve('fixtures/git/master.git') + '/'
|
|
|
|
|
2012-11-03 00:24:38 +04:00
|
|
|
describe ".getHead()", ->
|
2012-10-26 01:52:25 +04:00
|
|
|
it "returns a branch name for a non-empty repository", ->
|
|
|
|
repo = new Git(require.resolve('fixtures/git/master.git'))
|
|
|
|
expect(repo.getHead()).toBe 'refs/heads/master'
|
|
|
|
|
2012-11-03 00:24:38 +04:00
|
|
|
describe ".getShortHead()", ->
|
2012-10-26 01:52:25 +04:00
|
|
|
it "returns a branch name for a non-empty repository", ->
|
|
|
|
repo = new Git(require.resolve('fixtures/git/master.git'))
|
2012-10-26 01:58:13 +04:00
|
|
|
expect(repo.getShortHead()).toBe 'master'
|
2012-10-27 03:24:47 +04:00
|
|
|
|
2012-11-03 01:54:12 +04:00
|
|
|
describe ".isPathIgnored(path)", ->
|
2012-10-27 03:24:47 +04:00
|
|
|
it "returns true for an ignored path", ->
|
|
|
|
repo = new Git(require.resolve('fixtures/git/ignore.git'))
|
2012-11-03 01:54:12 +04:00
|
|
|
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(require.resolve('fixtures/git/ignore.git'))
|
2012-11-03 01:54:12 +04:00
|
|
|
expect(repo.isPathIgnored('b.txt')).toBeFalsy()
|
2012-11-03 00:24:38 +04:00
|
|
|
|
2012-11-03 01:54:12 +04:00
|
|
|
describe ".isPathModified(path)", ->
|
|
|
|
[repo, path, newPath, originalPathText] = []
|
2012-11-03 00:24:38 +04:00
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
repo = new Git(require.resolve('fixtures/git/working-dir'))
|
|
|
|
path = require.resolve('fixtures/git/working-dir/file.txt')
|
|
|
|
newPath = fs.join(require.resolve('fixtures/git/working-dir'), 'new-path.txt')
|
2012-11-03 01:54:12 +04:00
|
|
|
originalPathText = fs.read(path)
|
2012-11-03 00:24:38 +04:00
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
fs.write(path, originalPathText)
|
|
|
|
fs.remove(newPath) if fs.exists(newPath)
|
|
|
|
|
|
|
|
describe "when the path is unstaged", ->
|
|
|
|
it "returns false if the path has not been modified", ->
|
2012-11-03 01:54:12 +04:00
|
|
|
expect(repo.isPathModified(path)).toBeFalsy()
|
2012-11-03 00:24:38 +04:00
|
|
|
|
|
|
|
it "returns true if the path is modified", ->
|
|
|
|
fs.write(path, "change")
|
2012-11-03 01:54:12 +04:00
|
|
|
expect(repo.isPathModified(path)).toBeTruthy()
|
2012-11-03 00:24:38 +04:00
|
|
|
|
|
|
|
it "returns true if the path is deleted", ->
|
|
|
|
fs.remove(path)
|
2012-11-03 01:54:12 +04:00
|
|
|
expect(repo.isPathModified(path)).toBeTruthy()
|
|
|
|
|
|
|
|
it "returns false if the path is new", ->
|
|
|
|
expect(repo.isPathModified(newPath)).toBeFalsy()
|
|
|
|
|
|
|
|
describe ".isPathNew(path)", ->
|
|
|
|
[repo, path, newPath] = []
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
repo = new Git(require.resolve('fixtures/git/working-dir'))
|
|
|
|
path = require.resolve('fixtures/git/working-dir/file.txt')
|
|
|
|
newPath = fs.join(require.resolve('fixtures/git/working-dir'), 'new-path.txt')
|
|
|
|
fs.write(newPath, "i'm new here")
|
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
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(path)).toBeFalsy()
|
2012-11-04 02:09:43 +04:00
|
|
|
|
|
|
|
describe ".checkoutHead(path)", ->
|
|
|
|
[repo, path1, path2, originalPath1Text, originalPath2Text] = []
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
repo = new Git(require.resolve('fixtures/git/working-dir'))
|
|
|
|
path1 = require.resolve('fixtures/git/working-dir/file.txt')
|
|
|
|
originalPath1Text = fs.read(path1)
|
|
|
|
path2 = require.resolve('fixtures/git/working-dir/other.txt')
|
|
|
|
originalPath2Text = fs.read(path2)
|
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
fs.write(path1, originalPath1Text)
|
|
|
|
fs.write(path2, originalPath2Text)
|
|
|
|
|
|
|
|
it "no longer reports a path as modified after checkout", ->
|
|
|
|
expect(repo.isPathModified(path1)).toBeFalsy()
|
|
|
|
fs.write(path1, '')
|
|
|
|
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", ->
|
|
|
|
fs.write(path1, '')
|
|
|
|
expect(repo.checkoutHead(path1)).toBeTruthy()
|
|
|
|
expect(fs.read(path1)).toBe(originalPath1Text)
|
|
|
|
|
|
|
|
it "only restores the path specified", ->
|
|
|
|
fs.write(path2, 'path 2 is edited')
|
|
|
|
expect(repo.isPathModified(path2)).toBeTruthy()
|
|
|
|
expect(repo.checkoutHead(path1)).toBeTruthy()
|
|
|
|
expect(fs.read(path2)).toBe('path 2 is edited')
|
|
|
|
expect(repo.isPathModified(path2)).toBeTruthy()
|
2013-01-10 05:12:15 +04:00
|
|
|
|
|
|
|
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()
|
|
|
|
expect(-> repo.getHead()).toThrow()
|
2013-02-15 21:08:22 +04:00
|
|
|
|
|
|
|
describe ".getDiffStats(path)", ->
|
|
|
|
[repo, path, originalPathText] = []
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
repo = new Git(require.resolve('fixtures/git/working-dir'))
|
|
|
|
path = require.resolve('fixtures/git/working-dir/file.txt')
|
|
|
|
originalPathText = fs.read(path)
|
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
fs.write(path, originalPathText)
|
|
|
|
|
|
|
|
it "returns the number of lines added and deleted", ->
|
|
|
|
expect(repo.getDiffStats(path)).toEqual {added: 0, deleted: 0}
|
|
|
|
fs.write(path, "#{originalPathText} edited line")
|
|
|
|
expect(repo.getDiffStats(path)).toEqual {added: 1, deleted: 1}
|