/
get-filtered-packages.test.js
192 lines (153 loc) · 6.18 KB
/
get-filtered-packages.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
"use strict";
const path = require("path");
const yargs = require("yargs/yargs");
// mocked modules
const collectUpdates = require("@lerna/collect-updates");
// helpers
const initFixture = require("@lerna-test/init-fixture")(path.resolve(__dirname, "../../command"));
const PackageGraph = require("@lerna/package-graph");
const { getPackages } = require("@lerna/project");
const getFilteredPackages = require("../lib/get-filtered-packages");
const filterOptions = require("..");
async function buildGraph(cwd) {
const packages = await getPackages(cwd);
return new PackageGraph(packages);
}
function parseOptions(...args) {
return filterOptions(
yargs()
.exitProcess(false)
.showHelpOnFail(false)
).parse(args);
}
// working dir is never mutated
let cwd;
beforeAll(async () => {
cwd = await initFixture("filtering");
});
test.each`
flag
${"--scope"}
${"--ignore"}
`("$flag requires an argument", async ({ flag }) => {
// swallow stderr during yargs execution
jest.spyOn(console, "error").mockImplementation(() => {});
try {
parseOptions(flag);
} catch (err) {
expect(err.message).toMatch("Not enough arguments");
}
expect.hasAssertions();
});
test.each`
argv | matched
${["--scope", "package-3"]} | ${[3]}
${["--scope", "package-@(1|2)"]} | ${[1, 2]}
${["--scope", "package-{3,4}"]} | ${[3, 4]}
${["--ignore", "package-3"]} | ${[1, 2, 4, 5]}
${["--ignore", "package-@(2|3|4)"]} | ${[1, 5]}
${["--ignore", "package-{1,2,5}"]} | ${[3, 4]}
${["--scope", "package-1", "--scope", "*-2"]} | ${[1, 2]}
${["--scope", "package-@(1|2)", "--ignore", "package-2"]} | ${[1]}
${["--ignore", "package-{1,2}", "--ignore", "package-3"]} | ${[4, 5]}
`("filters $argv", async ({ argv, matched }) => {
const packageGraph = await buildGraph(cwd);
const execOpts = { cwd };
const options = parseOptions(...argv);
const result = await getFilteredPackages(packageGraph, execOpts, options);
expect(result.map(node => node.name)).toEqual(matched.map(n => `package-${n}`));
});
test.each`
argv
${["--scope", "not-a-package"]}
${["--ignore", "package-*"]}
${["--scope", "package-@(1|2)", "--ignore", "package-{1,2}"]}
`("errors $argv", async ({ argv }) => {
const packageGraph = await buildGraph(cwd);
const execOpts = { cwd };
const options = parseOptions(...argv);
try {
await getFilteredPackages(packageGraph, execOpts, options);
} catch (err) {
expect(err.message).toMatch("No packages remain after filtering");
}
expect.hasAssertions();
});
test.each`
argv
${["--scope", "not-a-package", "--continue-if-no-match"]}
${["--ignore", "package-*", "--continue-if-no-match"]}
${["--scope", "package-@(1|2)", "--ignore", "package-{1,2}", "--continue-if-no-match"]}
`("no errors and no packages $argv", async ({ argv }) => {
const packageGraph = await buildGraph(cwd);
const execOpts = { cwd };
const options = parseOptions(...argv);
const result = await getFilteredPackages(packageGraph, execOpts, options);
expect(result).toHaveLength(0);
});
test("--since returns all packages if no tag is found", async () => {
const packageGraph = await buildGraph(cwd);
const execOpts = { cwd };
const options = parseOptions("--since");
const result = await getFilteredPackages(packageGraph, execOpts, options);
expect(result).toHaveLength(5);
expect(collectUpdates).toHaveBeenLastCalledWith(
expect.any(Array),
packageGraph,
execOpts,
expect.objectContaining({ since: "" })
);
});
test("--since returns packages updated since the last tag", async () => {
collectUpdates.setUpdated(cwd, "package-2", "package-3");
const packageGraph = await buildGraph(cwd);
const execOpts = { cwd };
const options = parseOptions("--since");
const result = await getFilteredPackages(packageGraph, execOpts, options);
expect(result.map(node => node.name)).toEqual(["package-2", "package-3"]);
});
test("--since <ref> should return packages updated since <ref>", async () => {
collectUpdates.setUpdated(cwd, "package-1", "package-2", "package-3");
const packageGraph = await buildGraph(cwd);
const execOpts = { cwd };
const options = parseOptions("--since", "deadbeef");
const result = await getFilteredPackages(packageGraph, execOpts, options);
expect(result.map(node => node.name)).toEqual(["package-1", "package-2", "package-3"]);
expect(collectUpdates).toHaveBeenLastCalledWith(
expect.any(Array),
packageGraph,
execOpts,
expect.objectContaining({ since: "deadbeef" })
);
});
test("--scope package-{2,3,4} --since master", async () => {
collectUpdates.setUpdated(cwd, "package-4", "package-1");
const packageGraph = await buildGraph(cwd);
const execOpts = { cwd };
const options = parseOptions("--scope", "package-{2,3,4}", "--since", "master");
const result = await getFilteredPackages(packageGraph, execOpts, options);
expect(result.map(node => node.name)).toEqual(["package-4"]);
expect(collectUpdates).toHaveBeenLastCalledWith(
// filter-packages before collect-updates
[2, 3, 4].map(n => packageGraph.get(`package-${n}`).pkg),
packageGraph,
execOpts,
expect.objectContaining({ since: "master" })
);
});
test("--include-filtered-dependents", async () => {
const packageGraph = await buildGraph(cwd);
const execOpts = { cwd };
const options = parseOptions("--scope", "package-1", "--include-filtered-dependents");
const result = await getFilteredPackages(packageGraph, execOpts, options);
expect(result.map(pkg => pkg.name)).toEqual(["package-1", "package-2", "package-5", "package-3"]);
expect(collectUpdates).not.toHaveBeenCalled();
});
test("--include-filtered-dependencies", async () => {
const packageGraph = await buildGraph(cwd);
const execOpts = { cwd };
const options = parseOptions("--scope", "package-3", "--include-filtered-dependencies");
const result = await getFilteredPackages(packageGraph, execOpts, options);
expect(result.map(pkg => pkg.name)).toEqual(["package-3", "package-2", "package-1"]);
expect(collectUpdates).not.toHaveBeenCalled();
});