/
app.test.js
94 lines (76 loc) · 2.93 KB
/
app.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
/* eslint-disable security/detect-child-process */
const TerminalLauncher = require('../index')
const getTerminals = require('../lib/get-terminals')
const opn = require('opn')
const childProcess = require('child_process')
jest.mock('opn')
describe('Terminal Launcher', () => {
test('launching a terminal requires a path to be provided', () => {
expect(() => {
TerminalLauncher.launchTerminal()
}).toThrow()
})
describe('Linux', () => {
beforeAll(() => {
Object.defineProperty(process, 'platform', {
value: 'linux'
})
})
test('launching a terminal calls opn with the correct params when running on linux', async () => {
const path = '/usr/local/bin/non-existent.sh'
await TerminalLauncher.launchTerminal({path})
expect(opn).toHaveBeenCalled()
const calledWithFirstArgument = opn.mock.calls[0][0]
expect(calledWithFirstArgument).toEqual(path)
})
describe('get-terminals returns array of functions', () => {
test('terminals are a list of functions', () => {
const terminals = getTerminals('mock.ext')
expect(Array.isArray(terminals)).toBeTruthy()
expect(terminals.length).toBeTruthy()
expect(typeof terminals[0] === 'function').toBeTruthy()
})
})
})
describe('Windows', () => {
beforeAll(() => {
Object.defineProperty(process, 'platform', {
value: 'win32'
})
Object.defineProperty(process, 'env', {
value: {
PATHEXT: '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC'
}
})
})
beforeEach(() => {
childProcess.exec = jest.fn(() => ({once: (type, c) => type === 'close' && c(0)}))
})
describe('launching a terminal calls child_process.exec with the correct command', () => {
test('for powershell scripts', async () => {
const path = 'C:\\script.ps1'
const expectedCommand = 'start powershell "C:\\script.ps1"'
await TerminalLauncher.launchTerminal({path})
expect(childProcess.exec).toHaveBeenCalled()
const calledWithFirstArgument = childProcess.exec.mock.calls[0][0]
expect(calledWithFirstArgument).toEqual(expectedCommand)
})
test('for bat scripts', async () => {
const path = 'C:\\script.bat'
const expectedCommand = 'start cmd /c "C:\\script.bat"'
await TerminalLauncher.launchTerminal({path})
expect(childProcess.exec).toHaveBeenCalled()
const calledWithFirstArgument = childProcess.exec.mock.calls[0][0]
expect(calledWithFirstArgument).toEqual(expectedCommand)
})
})
describe('get-terminals returns array of functions', () => {
test('terminals are a list of functions', () => {
const terminals = getTerminals('mock.ext')
expect(Array.isArray(terminals)).toBeTruthy()
expect(terminals.length).toBeTruthy()
expect(typeof terminals[0] === 'function').toBeTruthy()
})
})
})
})