-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
ExecCommand.js
220 lines (171 loc) · 5.89 KB
/
ExecCommand.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
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
import log from "npmlog";
import path from "path";
// mocked modules
import ChildProcessUtilities from "../src/ChildProcessUtilities";
import UpdatedPackagesCollector from "../src/UpdatedPackagesCollector";
// helpers
import callsBack from "./helpers/callsBack";
import exitWithCode from "./helpers/exitWithCode";
import initFixture from "./helpers/initFixture";
// file under test
import ExecCommand from "../src/commands/ExecCommand";
jest.mock("../src/ChildProcessUtilities");
jest.mock("../src/UpdatedPackagesCollector");
// silence logs
log.level = "silent";
const calledWithArgs = () =>
ChildProcessUtilities.spawn.mock.calls[0][1];
const calledInPackages = () =>
ChildProcessUtilities.spawn.mock.calls.map((args) => path.basename(args[2].cwd));
describe("ExecCommand", () => {
beforeEach(() => {
ChildProcessUtilities.spawn = jest.fn(callsBack());
});
afterEach(() => jest.resetAllMocks());
describe("in a basic repo", () => {
let testDir;
beforeAll(() => initFixture("ExecCommand/basic").then((dir) => {
testDir = dir;
}));
it("should complain if invoked without command", (done) => {
const execCommand = new ExecCommand([], {}, testDir);
execCommand.runValidations();
execCommand.runPreparations();
execCommand.runCommand(exitWithCode(1, (err) => {
try {
expect(err.message).toBe("You must specify which command to run.");
done();
} catch (ex) {
done.fail(ex);
}
}));
});
it("passes execution error to callback", (done) => {
const err = new Error("execa error");
err.code = 1;
err.cmd = "boom";
let errorLog;
log.once("log.error", (m) => {
errorLog = m;
});
ChildProcessUtilities.spawn = jest.fn(callsBack(err));
const execCommand = new ExecCommand(["boom"], {}, testDir);
execCommand.runValidations();
execCommand.runPreparations();
execCommand.runCommand(exitWithCode(1, () => {
try {
expect(errorLog).toHaveProperty("message", "Errored while executing 'boom' in 'package-1'");
done();
} catch (ex) {
done.fail(ex);
}
}));
});
it("should filter packages with `ignore`", (done) => {
const execCommand = new ExecCommand(["ls"], {
ignore: "package-1",
}, testDir);
execCommand.runValidations();
execCommand.runPreparations();
execCommand.runCommand(exitWithCode(0, (err) => {
if (err) return done.fail(err);
try {
expect(ChildProcessUtilities.spawn).toHaveBeenCalledTimes(1);
expect(ChildProcessUtilities.spawn).lastCalledWith("ls", [], {
cwd: path.join(testDir, "packages/package-2"),
env: expect.objectContaining({
LERNA_PACKAGE_NAME: "package-2",
}),
shell: true,
}, expect.any(Function));
done();
} catch (ex) {
done.fail(ex);
}
}));
});
it("should filter packages that are not updated when onlyUpdate", (done) => {
UpdatedPackagesCollector.prototype.getUpdates = jest.fn(() => [{ package: {
name: "package-2",
location: path.join(testDir, "packages/package-2")
} }]);
const execCommand = new ExecCommand(["ls"], {
onlyUpdated: true,
}, testDir);
execCommand.runValidations();
execCommand.runPreparations();
execCommand.runCommand(exitWithCode(0, (err) => {
if (err) return done.fail(err);
try {
expect(ChildProcessUtilities.spawn).toHaveBeenCalledTimes(1);
expect(ChildProcessUtilities.spawn).lastCalledWith("ls", [], {
cwd: path.join(testDir, "packages/package-2"),
env: expect.objectContaining({
LERNA_PACKAGE_NAME: "package-2",
}),
shell: true,
}, expect.any(Function));
done();
} catch (ex) {
done.fail(ex);
}
}));
});
it("should run a command", (done) => {
const execCommand = new ExecCommand(["ls"], {}, testDir);
execCommand.runValidations();
execCommand.runPreparations();
execCommand.runCommand(exitWithCode(0, (err) => {
if (err) return done.fail(err);
try {
expect(ChildProcessUtilities.spawn).toHaveBeenCalledTimes(2);
expect(calledInPackages()).toEqual([
"package-1",
"package-2",
]);
done();
} catch (ex) {
done.fail(ex);
}
}));
});
it("should run a command with parameters", (done) => {
const execCommand = new ExecCommand(["ls", "-la"], {}, testDir);
execCommand.runValidations();
execCommand.runPreparations();
execCommand.runCommand(exitWithCode(0, (err) => {
if (err) return done.fail(err);
try {
expect(ChildProcessUtilities.spawn).toHaveBeenCalledTimes(2);
expect(calledWithArgs()).toEqual(["-la"]);
done();
} catch (ex) {
done.fail(ex);
}
}));
});
// Both of these commands should result in the same outcome
const filters = [
{ test: "runs a command for a given scope", flag: "scope", flagValue: "package-1" },
{ test: "does not run a command for ignored packages", flag: "ignore", flagValue: "package-@(2|3|4)" },
];
filters.forEach((filter) => {
it(filter.test, (done) => {
const execCommand = new ExecCommand(["ls"], {
[filter.flag]: filter.flagValue,
}, testDir);
execCommand.runValidations();
execCommand.runPreparations();
execCommand.runCommand(exitWithCode(0, (err) => {
if (err) return done.fail(err);
try {
expect(calledInPackages()).toEqual(["package-1"]);
done();
} catch (ex) {
done.fail(ex);
}
}));
});
});
});
});