/
test.js
150 lines (104 loc) · 3.89 KB
/
test.js
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
var expect = require('chai').expect
var fs = require('fs')
var gitane = require('../index')
var os = require('os')
describe('gitane', function() {
describe('#run', function() {
it('should run the command with correct GIT_SSH environment', function(done) {
var testkey = 'testkey'
gitane.run(process.cwd(), testkey, 'env', function(err, stdout, stderr) {
expect(err).to.be.null
expect(stdout).to.match(/GIT_SSH=.*_gitane.*\.sh/)
done()
})
})
it('should run the command in the correct baseDir', function(done) {
var testkey = 'testkey'
gitane.run(os.tmpDir(), testkey, 'pwd', function(err, stdout, stderr) {
expect(err).to.be.null
expect(fs.realpathSync(stdout.trim())).to.eql(fs.realpathSync(os.tmpDir()))
done()
})
})
}),
describe('#writeFiles', function() {
it('should create a random file if none specified', function(done) {
gitane.writeFiles('testkey', null, function(err, file, keyfile) {
expect(err).to.be.null
expect(file).to.be.a('string')
expect(file).to.match(/_gitane/)
var data = fs.readFileSync(file, 'utf8')
expect(data).to.match(/exec ssh -i/)
var key = fs.readFileSync(keyfile, 'utf8')
expect(key).to.eql('testkey')
fs.unlinkSync(file)
fs.unlinkSync(keyfile)
done()
})
})
it('should use passed-in file if specified', function(done) {
var filename = "_testfile"
gitane.writeFiles('testkey', filename, function(err, file, keyfile) {
expect(file).to.eql(filename)
expect(err).to.be.null
var data = fs.readFileSync(file, 'utf8')
expect(data).to.match(/exec ssh -i/)
var key = fs.readFileSync(keyfile, 'utf8')
expect(key).to.eql('testkey')
fs.unlinkSync(file)
fs.unlinkSync(keyfile)
done()
})
})
it('should create an executable script and an 0600-mode key by default', function(done) {
var filename = "_testfile"
gitane.writeFiles('testkey', filename, function(err, file, keyfile) {
expect(file).to.eql(filename)
expect(err).to.be.null
var stats = fs.statSync(file)
// Note we must convert to octal ourselves.
expect(stats.mode.toString(8)).to.eql('100755')
fs.unlinkSync(file)
var stats = fs.statSync(keyfile)
// Note we must convert to octal ourselves.
expect(stats.mode.toString(8)).to.eql('100600')
fs.unlinkSync(keyfile)
done()
})
})
it('should create an executable script and honour keyMode param', function(done) {
var filename = "_testfile"
gitane.writeFiles('testkey', filename, 0744, function(err, file, keyfile) {
expect(file).to.eql(filename)
expect(err).to.be.null
var stats = fs.statSync(file)
// Note we must convert to octal ourselves.
expect(stats.mode.toString(8)).to.eql('100755')
fs.unlinkSync(file)
var stats = fs.statSync(keyfile)
// Note we must convert to octal ourselves.
expect(stats.mode.toString(8)).to.eql('100744')
fs.unlinkSync(keyfile)
done()
})
})
it('should support event emitter parameter for real-time updates', function(done) {
var testkey = 'testkey'
var gotStdout = false
function mockEmit(ev, data) {
console.log("emitter")
if (ev === 'stdout') {
gotStdout = true
expect(fs.realpathSync(data.trim())).to.eql(fs.realpathSync(os.tmpDir()))
}
}
var opts = {emitter: {emit:mockEmit}, baseDir:os.tmpDir(), privKey: testkey, cmd:'pwd'}
gitane.run(opts, function(err, stdout, stderr) {
expect(err).to.be.null
expect(fs.realpathSync(stdout.trim())).to.eql(fs.realpathSync(os.tmpDir()))
expect(gotStdout).to.be.true
done()
})
})
})
})