-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmisc-utils.test.ts
215 lines (174 loc) · 6.74 KB
/
misc-utils.test.ts
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
import * as execaModule from 'execa';
import * as whichModule from 'which';
import {
isErrorWithCode,
isErrorWithMessage,
isErrorWithStack,
wrapError,
resolveExecutable,
runCommand,
getStdoutFromCommand,
getLinesFromCommand,
} from './misc-utils';
jest.mock('which');
jest.mock('execa');
describe('misc-utils', () => {
describe('isErrorWithCode', () => {
it('returns true if given an object with a "code" property', () => {
expect(isErrorWithCode({ code: 'some code' })).toBe(true);
});
it('returns false if given null', () => {
expect(isErrorWithCode(null)).toBe(false);
});
it('returns false if given undefined', () => {
expect(isErrorWithCode(undefined)).toBe(false);
});
it('returns false if given something that is not typeof object', () => {
expect(isErrorWithCode(12345)).toBe(false);
});
it('returns false if given an object that does not have a "code" property', () => {
expect(isErrorWithCode({})).toBe(false);
});
});
describe('isErrorWithMessage', () => {
it('returns true if given an object with a "message" property', () => {
expect(isErrorWithMessage({ message: 'some message' })).toBe(true);
});
it('returns false if given null', () => {
expect(isErrorWithMessage(null)).toBe(false);
});
it('returns false if given undefined', () => {
expect(isErrorWithMessage(undefined)).toBe(false);
});
it('returns false if given something that is not typeof object', () => {
expect(isErrorWithMessage(12345)).toBe(false);
});
it('returns false if given an object that does not have a "message" property', () => {
expect(isErrorWithMessage({})).toBe(false);
});
});
describe('isErrorWithStack', () => {
it('returns true if given an object with a "stack" property', () => {
expect(isErrorWithStack({ stack: 'some stack' })).toBe(true);
});
it('returns false if given null', () => {
expect(isErrorWithStack(null)).toBe(false);
});
it('returns false if given undefined', () => {
expect(isErrorWithStack(undefined)).toBe(false);
});
it('returns false if given something that is not typeof object', () => {
expect(isErrorWithStack(12345)).toBe(false);
});
it('returns false if given an object that does not have a "stack" property', () => {
expect(isErrorWithStack({})).toBe(false);
});
});
describe('wrapError', () => {
it('returns a new Error that links to the given Error', () => {
const originalError = new Error('oops');
const newError = wrapError('Some message', originalError);
expect(newError.message).toBe('Some message');
expect(newError.cause).toBe(originalError);
});
it('copies over any "code" property that exists on the given Error', () => {
const originalError: any = new Error('oops');
originalError.code = 'CODE';
const newError: any = wrapError('Some message', originalError);
expect(newError.code).toBe('CODE');
});
it('returns a new Error which prefixes the given message', () => {
const newError = wrapError('Some message', 'Some original message');
expect(newError.message).toBe('Some message: Some original message');
expect(newError.cause).toBeUndefined();
});
});
describe('resolveExecutable', () => {
it('returns the fullpath of the given executable as returned by "which"', async () => {
jest
.spyOn(whichModule, 'default')
.mockResolvedValue('/path/to/executable');
expect(await resolveExecutable('executable')).toBe('/path/to/executable');
});
it('returns null if the given executable cannot be found', async () => {
jest
.spyOn(whichModule, 'default')
.mockRejectedValue(new Error('not found: executable'));
expect(await resolveExecutable('executable')).toBeNull();
});
it('throws the error that "which" throws if it is not a "not found" error', async () => {
jest
.spyOn(whichModule, 'default')
.mockRejectedValue(new Error('something else'));
await expect(resolveExecutable('executable')).rejects.toThrow(
'something else',
);
});
});
describe('runCommand', () => {
it('runs the command, discarding its output', async () => {
const execaSpy = jest
.spyOn(execaModule, 'default')
// Typecast: It's difficult to provide a full return value for execa
.mockResolvedValue({ stdout: ' some output ' } as any);
const result = await runCommand('some command', ['arg1', 'arg2'], {
all: true,
});
expect(execaSpy).toHaveBeenCalledWith('some command', ['arg1', 'arg2'], {
all: true,
});
expect(result).toBeUndefined();
});
});
describe('getStdoutFromCommand', () => {
it('executes the given command and returns a version of the standard out from the command with whitespace trimmed', async () => {
const execaSpy = jest
.spyOn(execaModule, 'default')
// Typecast: It's difficult to provide a full return value for execa
.mockResolvedValue({ stdout: ' some output ' } as any);
const output = await getStdoutFromCommand(
'some command',
['arg1', 'arg2'],
{ all: true },
);
expect(execaSpy).toHaveBeenCalledWith('some command', ['arg1', 'arg2'], {
all: true,
});
expect(output).toBe('some output');
});
});
describe('getLinesFromCommand', () => {
it('executes the given command and returns the standard out from the command split into lines', async () => {
const execaSpy = jest
.spyOn(execaModule, 'default')
// Typecast: It's difficult to provide a full return value for execa
.mockResolvedValue({ stdout: 'line 1\nline 2\nline 3' } as any);
const lines = await getLinesFromCommand(
'some command',
['arg1', 'arg2'],
{ all: true },
);
expect(execaSpy).toHaveBeenCalledWith('some command', ['arg1', 'arg2'], {
all: true,
});
expect(lines).toStrictEqual(['line 1', 'line 2', 'line 3']);
});
it('does not strip leading and trailing whitespace from the output, but does remove empty lines', async () => {
const execaSpy = jest
.spyOn(execaModule, 'default')
// Typecast: It's difficult to provide a full return value for execa
.mockResolvedValue({
stdout: ' line 1\nline 2\n\n line 3 \n',
} as any);
const lines = await getLinesFromCommand(
'some command',
['arg1', 'arg2'],
{ all: true },
);
expect(execaSpy).toHaveBeenCalledWith('some command', ['arg1', 'arg2'], {
all: true,
});
expect(lines).toStrictEqual([' line 1', 'line 2', ' line 3 ']);
});
});
});