This repository has been archived by the owner on Jul 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.js
193 lines (175 loc) · 6.3 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
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
const commandExists = require('command-exists');
const expect = require('chai').expect;
const term = require('../index.js');
const path = require('path');
const fs = require('fs');
describe('_joinCommands()', () => {
it('should not join empty commands', () => {
expect(term._joinCommands('', '', '; ')).to.be.empty;
expect(term._joinCommands(null, null, '; ')).to.be.empty;
expect(term._joinCommands(undefined, undefined, '; ')).to.be.empty;
expect(term._joinCommands(undefined, null, '; ')).to.be.empty;
});
it('should return the command to change the working directory', () => {
let path = '/path/to/go';
let joined = 'cd "/path/to/go"';
expect(term._joinCommands(path, '', '; ')).to.equal(joined);
expect(term._joinCommands(path, null, '; ')).to.equal(joined);
});
it('should return the command', () => {
let cmd = 'foo bar';
expect(term._joinCommands('', cmd, '; ')).to.equal(cmd);
expect(term._joinCommands(null, cmd, '; ')).to.equal(cmd);
});
it('should join the commands', () => {
let cmd = 'foo bar';
let path = '/path/to/go';
let joined = 'cd "/path/to/go"; foo bar';
expect(term._joinCommands(path, cmd, '; ')).to.equal(joined);
});
});
describe('getDefaultTerminal()', () => {
it('should return the correct terminal for operating sytem', () => {
let terminal = term.getDefaultTerminal();
if (process.platform == 'darwin') {
expect(terminal).to.equal('Terminal.app');
} else if (process.platform == 'win32') {
expect(terminal).to.equal('cmd');
} else if (process.platform == 'linux') {
let terms = [
'gnome-terminal',
'konsole',
'xfce4-terminal',
'lxterminal',
'xterm',
''
];
expect(terminal).to.be.oneOf(terms);
}
});
});
describe('getWindowsCommand()', () => {
it('should return the correct command for windows', () => {
let cmd = term._getWindowsCommand('foo bar', 'C:\\path\\to\\go', 'cmd.exe');
expect(cmd).to.equal('start cmd /K "cd /d C:\\path\\to\\go & foo bar"');
});
});
describe('getLinuxCommand()', () => {
it('should return the correct command for linux', () => {
let cmd = term._getLinuxCommand('foo bar', '/path/to/go', 'konsole');
let string = `konsole -e 'bash -c "cd "/path/to/go"; foo bar; exec bash"'`
expect(cmd).to.equal(string);
});
it('should return the terminal for empty path and command', () => {
let cmd = term._getLinuxCommand('', '', 'konsole');
expect(cmd).to.equal('konsole');
});
});
describe('getDarwinCommand()', () => {
it('should return the correct command for mac', (done) => {
term._getDarwinCommand('foo bar', '/path/to/go', 'iTerm.app', (err, cmd) => {
let scriptPath = path.join(__dirname, '..', 'cmd-script.sh');
let stats = fs.statSync(scriptPath);
let script = '#!/bin/bash\ncd "/path/to/go"\nfoo bar\n/bin/bash';
expect(stats.isFile()).to.be.true;
expect(fs.readFileSync(scriptPath, {encoding: 'utf8'})).to.equal(script);
expect(cmd).to.equal(`open -a iTerm.app "${scriptPath}"`);
done(err);
});
});
it('should return the command to open a path', (done) => {
term._getDarwinCommand('', '/path/to/go', 'iTerm.app', (err, cmd) => {
expect(cmd).to.equal('open -a iTerm.app "/path/to/go"');
done(err);
});
});
it('should return the terminal for empty path and command', (done) => {
term._getDarwinCommand('', '', 'iTerm.app', (err, cmd) => {
expect(cmd).to.equal('open -a iTerm.app');
done(err);
});
});
});
describe('launchTerminal()', () => {
it('should callback an error if no terminal is given', (done) => {
term.launchTerminal('', '', '', (err) => {
expect(err).not.to.be.null;
expect(err).to.be.an('error');
done();
})
});
})
// TODO test with spies
describe('getConnectionCommand()', () => {
it('should get the right command or error', (done) => {
term.getConnectionCommand('connection-file', (err, cmd) => {
commandExists('jupyter', function(e, exist) {
if (e) throw(e);
if(exist) {
expect(err).to.be.null;
expect(cmd).to.equal('jupyter console --existing connection-file');
done();
} else {
commandExists('ipython', function(e, exist) {
if (e) throw(e);
if(exist) {
expect(err).to.be.null;
expect(cmd).to.equal('ipython console --existing connection-file');
done();
} else {
expect(err).not.to.be.null;
expect(err.message).to.equal('Could not find `jupyter` or `ipython`.');
done();
}
});
}
})
})
})
it('should get the right command for qtconsole or error', (done) => {
term.getConnectionCommand('connection-file', 'qtconsole', (err, cmd) => {
commandExists('jupyter', function(e, exist) {
if (e) throw(e);
if(exist) {
expect(err).to.be.null;
expect(cmd).to.equal('jupyter qtconsole --existing connection-file');
done();
} else {
commandExists('ipython', function(e, exist) {
if (e) throw(e);
if(exist) {
expect(err).to.be.null;
expect(cmd).to.equal('ipython qtconsole --existing connection-file');
done();
} else {
expect(err).not.to.be.null;
expect(err.message).to.equal('Could not find `jupyter` or `ipython`.');
done();
}
});
}
})
})
})
})
describe('getCmderCommand()', () => {
it('should return the correct command for cmder', (done) => {
const command = term._getCmderCommand('dir', 'C:\\Users');
expect(command).to.equal('start cmder /START "C:\\Users" /TASK "dir"');
done();
});
});
describe('getPowershellCommand()', () => {
it('should return the correct command for powershell', done => {
const command = term._getPowershellCommand('dir', 'C:\\Users');
expect(command).to.equal('start powershell -noexit -command "cd C:\\Users ; dir\"');
done();
});
});
describe('getCmdCommand()', () => {
it('should return the correct command for cmd', done => {
const command = term._getCmdCommand('dir', 'C:\\Users');
expect(command).to.equal('start cmd /K "cd /d C:\\Users & dir\"');
done();
});
});