forked from danhper/node-git-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepository-test.coffee
More file actions
273 lines (240 loc) · 10.1 KB
/
repository-test.coffee
File metadata and controls
273 lines (240 loc) · 10.1 KB
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
process.env['TMPDIR'] = '/tmp/node-git-cli'
_ = require 'underscore'
expect = require 'expect.js'
tmp = require 'tmp'
fs = require 'fs-extra'
Helpers = require './test-helpers'
gitCli = require '../src/git-cli'
Repository = require '../src/repository'
CliOption = require '../src/cli-option'
BASE_REPO_PATH = '/home/daniel/Documents/projects/node-git-cli'
unless fs.existsSync BASE_REPO_PATH
BASE_REPO_PATH = 'https://github.com/tuvistavie/node-git-cli.git'
[baseRepository, testRepository] = [null, null]
before (done) ->
if fs.existsSync(process.env['TMPDIR'])
fs.removeSync(process.env['TMPDIR'])
fs.mkdirSync(process.env['TMPDIR'])
tmp.dir (err, path) ->
Repository.clone BASE_REPO_PATH, "#{path}/node-git-cli", { bare: true }, (err, repo) ->
baseRepository = repo
done()
after ->
fs.removeSync(process.env['TMPDIR'])
describe 'Repository', ->
beforeEach (done) ->
tmp.dir (err, path) ->
Repository.clone baseRepository.workingDir(), "#{path}/node-git-cli", (err, repo) ->
testRepository = repo
done()
describe 'constructor', ->
it 'should throw error on wrong path', ->
fn = -> new Repository('/wrong/path')
expect(fn).to.throwException (e) ->
expect(e).to.be.a gitCli.BadRepositoryError
it 'should set the path', ->
path = '/path/to/.git'
repository = new Repository(path)
expect(repository.path).to.be path
describe 'workingDir', ->
it 'should return the repository working directory', ->
repository = new Repository('/path/to/.git')
expect(repository.workingDir()).to.be '/path/to'
describe '#_getOptions', ->
it 'should return "cwd"', ->
repository = new Repository('/path/to/.git')
expect(repository._getOptions()).to.eql { cwd: '/path/to' }
describe 'clone', ->
it 'should clone repository to given directory', (done) ->
tmp.dir { unsafeCleanup: true }, (err, path) ->
Repository.clone testRepository.path, "#{path}/node-git-cli", (err, repo) ->
expect(err).to.be null
expect(repo.path).to.eql "#{path}/node-git-cli/.git"
Repository.clone testRepository.path, "#{path}/node-git-cli", (err, repo) ->
expect(err).to.not.be null
done()
describe 'init', ->
it 'should init a new repository to given directory', (done) ->
tmp.dir { unsafeCleanup: true }, (err, path) ->
Repository.init "#{path}/node-git-cli", (err, repository) ->
expect(err).to.be null
expect(repository.path).to.eql "#{path}/node-git-cli/.git"
done()
describe '#status', ->
it 'get file status', (done) ->
addedFilePath = "#{testRepository.workingDir()}/foo"
editedFilePath = "#{testRepository.workingDir()}/README.md"
fs.openSync(addedFilePath, 'w')
fs.appendFileSync(editedFilePath, 'foobar')
testRepository.status (err, changes) ->
expect(err).to.be null
expect(changes).to.be.an Array
expect(changes.length).to.be 2
_.each { path: 'README.md', fullPath: editedFilePath, status: 'modified', tracked: false }, (v, k) ->
expect(changes[0][k]).to.be v
_.each { path: 'foo', fullPath: addedFilePath, status: 'added', tracked: false }, (v, k) ->
expect(changes[1][k]).to.be v
done()
describe '#add', ->
it 'should add all files by default', (done) ->
fs.openSync("#{testRepository.workingDir()}/foo", 'w')
fs.appendFileSync("#{testRepository.workingDir()}/README.md", 'foobar')
testRepository.add (err) ->
expect(err).to.be null
testRepository.status (err, changes) ->
expect(changes.length).to.be 2
_.each changes, (change) ->
expect(change.tracked).to.be true
done()
it 'shoud add given files otherwise', (done) ->
addedFilePath = "#{testRepository.workingDir()}/foo"
fs.openSync(addedFilePath, 'w')
fs.appendFileSync("#{testRepository.workingDir()}/README.md", 'foobar')
testRepository.add [addedFilePath], (err) ->
expect(err).to.be null
testRepository.status (err, changes) ->
expect(changes.length).to.be 2
expect(changes[0].tracked).to.be false
expect(changes[1].tracked).to.be true
done()
describe '#diff', ->
it 'should not return output when files are not changed', (done) ->
testRepository.diff (err, output) ->
expect(err).to.be null
expect(output).to.be.empty()
done()
it 'should return output when files are changed', (done) ->
fs.appendFileSync("#{testRepository.workingDir()}/README.md", 'foobar')
testRepository.diff (err, output) ->
expect(err).to.be null
expect(output).to.not.be.empty()
done()
describe '#diffStats', ->
it 'should return correct stats', (done) ->
fs.appendFileSync("#{testRepository.workingDir()}/README.md", 'foobar')
Helpers.removeFirstLine("#{testRepository.workingDir()}/LICENSE")
testRepository.diffStats (err, stats) ->
expect(err).to.be null
expect(stats).to.be.a Object
_.each { changedFilesNumber: 2, insertions: 1, deletions: 1}, (v, k) ->
expect(stats[k]).to.be v
done()
describe '#log', ->
it 'should return logs', (done) ->
testRepository.log (err, logs) ->
expect(err).to.be null
expect(logs).to.be.an Array
expect(logs).to.not.be.empty()
keys = ['author', 'email', 'subject', 'body', 'date', 'hash']
expect(logs[0]).to.only.have.keys keys
expect(logs[0].date).to.be.a Date
done()
describe '#commit', ->
it 'should work when files are added', (done) ->
fs.appendFileSync("#{testRepository.workingDir()}/README.md", 'foobar')
testRepository.log (err, logs) ->
logsCount = logs.length
testRepository.add (err) ->
testRepository.commit "foo'bar", (err) ->
expect(err).to.be null
testRepository.log (err, logs) ->
expect(logs.length).to.be (logsCount + 1)
done()
describe '#listRemotes', ->
it 'should list all remotes', (done) ->
testRepository.listRemotes (err, remotes) ->
expect(err).to.be null
expect(remotes).to.eql(['origin'])
done()
describe '#showRemote', ->
it 'should get remote info', (done) ->
testRepository.showRemote 'origin', (err, info) ->
expect(err).to.be null
expected =
pushUrl: baseRepository.workingDir()
fetchUrl: baseRepository.workingDir()
headBranch: 'master'
expect(info).to.eql expected
done()
describe '#currentBranch', ->
it 'should return current branch', (done) ->
testRepository.currentBranch (err, branch) ->
expect(err).to.be null
expect(branch).to.be 'master'
done()
describe '#branch', ->
it 'should list branches', (done) ->
testRepository.branch (err, branches) ->
expect(err).to.be null
expect(branches).to.eql ['master']
done()
it 'should create new branches', (done) ->
testRepository.branch 'foo', (err, branches) ->
expect(err).to.be null
testRepository.branch (err, branches) ->
expect(branches).to.eql ['foo', 'master']
done()
it 'should delete branches', (done) ->
testRepository.branch 'foo', (err, branches) ->
testRepository.branch (err, branches) ->
expect(branches).to.eql ['foo', 'master']
testRepository.branch 'foo', { D: true }, (err) ->
expect(err).to.be null
testRepository.branch (err, branches) ->
expect(branches).to.eql ['master']
done()
describe '#checkout', ->
it 'should do basic branch checkout', (done) ->
testRepository.currentBranch (err, branch) ->
testRepository.branch 'gh-pages', (err, branches) ->
expect(branch).to.be 'master'
testRepository.checkout 'gh-pages', (err) ->
expect(err).to.be null
testRepository.currentBranch (err, branch) ->
expect(branch).to.be 'gh-pages'
done()
it 'should work with -b flag', (done) ->
testRepository.checkout 'foo', { b: true }, (err) ->
expect(err).to.be null
testRepository.currentBranch (err, branch) ->
expect(branch).to.be 'foo'
done()
describe '#push', ->
it 'should push commits', (done) ->
baseRepository.log (err, logs) ->
logsCount = logs.length
fs.appendFileSync("#{testRepository.workingDir()}/README.md", 'foobar')
testRepository.commit "foo'bar", { a: true }, (err) ->
testRepository.push (err) ->
expect(err).to.be null
baseRepository.log (err, logs) ->
expect(logs.length).to.be logsCount + 1
done()
describe '#addRemote', ->
it 'should add new remote', (done) ->
testRepository.addRemote 'foo', baseRepository.path, (err) ->
expect(err).to.be null
testRepository.listRemotes (err, remotes) ->
expect(remotes).to.contain 'foo'
done()
describe '#setRemoteUrl', ->
it 'should change remote URL', (done) ->
testRepository.setRemoteUrl 'origin', 'newUrl', (err) ->
expect(err).to.be null
testRepository.showRemote 'origin', { n: true }, (err, remote) ->
expect(remote.pushUrl).to.be 'newUrl'
done()
describe '#merge', ->
it 'should merge branche', (done) ->
file = "#{testRepository.workingDir()}/README.md"
fs.appendFileSync(file, 'random string')
testRepository.branch 'newbranch', ->
testRepository.add (err) ->
testRepository.commit "new commit", (err) ->
testRepository.checkout 'newbranch', ->
expect(fs.readFileSync(file, 'utf8')).to.not.contain 'random string'
testRepository.merge 'master', ->
expect(fs.readFileSync(file, 'utf8')).to.contain 'random string'
testRepository.log (err, logs) ->
expect(logs[0].subject).to.be 'new commit'
done()