-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
UpdatedCommand.js
207 lines (158 loc) · 6.16 KB
/
UpdatedCommand.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
import chalk from "chalk";
import execa from "execa";
import log from "npmlog";
import normalizeNewline from "normalize-newline";
import path from "path";
import touch from "touch";
// mocked or stubbed modules
import output from "../src/utils/output";
// helpers
import initFixture from "./helpers/initFixture";
import updateLernaConfig from "./helpers/updateLernaConfig";
import yargsRunner from "./helpers/yargsRunner";
// file under test
import * as commandModule from "../src/commands/UpdatedCommand";
const run = yargsRunner(commandModule);
jest.mock("../src/utils/output");
// silence logs
log.level = "silent";
// keep snapshots stable cross-platform
chalk.enabled = false;
const consoleOutput = () => output.mock.calls.map(args => normalizeNewline(args[0]));
const gitTag = cwd => execa("git", ["tag", "v1.0.0"], { cwd });
const gitAdd = cwd => execa("git", ["add", "-A"], { cwd });
const gitCommit = cwd => execa("git", ["commit", "-m", "Commit"], { cwd });
const touchFile = cwd => filePath => touch(path.join(cwd, filePath));
const setupGitChanges = async (cwd, filePaths) => {
await gitTag(cwd);
await Promise.all(filePaths.map(touchFile(cwd)));
await gitAdd(cwd);
await gitCommit(cwd);
};
describe("UpdatedCommand", async () => {
afterEach(() => jest.resetAllMocks());
/** =========================================================================
* Basic
* ======================================================================= */
describe("basic", async () => {
let testDir;
let lernaUpdated;
beforeEach(() =>
initFixture("UpdatedCommand/basic").then(dir => {
testDir = dir;
lernaUpdated = run(testDir);
})
);
it("should list changes", async () => {
await setupGitChanges(testDir, ["packages/package-2/random-file"]);
await lernaUpdated();
expect(consoleOutput()).toMatchSnapshot();
});
it("should list all packages when no tag is found", async () => {
await lernaUpdated();
expect(consoleOutput()).toMatchSnapshot();
});
it("should list changes with --force-publish *", async () => {
await setupGitChanges(testDir, ["packages/package-2/random-file"]);
await lernaUpdated("--force-publish");
expect(consoleOutput()).toMatchSnapshot();
});
it("should list changes with --force-publish [package,package]", async () => {
await setupGitChanges(testDir, ["packages/package-3/random-file"]);
await lernaUpdated("--force-publish", "package-2,package-4");
expect(consoleOutput()).toMatchSnapshot();
});
it("should list changes without ignored files", async () => {
await updateLernaConfig(testDir, {
commands: {
// "command" also supported
publish: {
ignore: ["ignored-file"],
},
},
});
await setupGitChanges(testDir, ["packages/package-2/ignored-file", "packages/package-3/random-file"]);
await lernaUpdated();
expect(consoleOutput()).toMatchSnapshot();
});
it("throws an error when --only-explicit-updates is passed", async () => {
await setupGitChanges(testDir, ["packages/package-2/random-file"]);
try {
await lernaUpdated("--only-explicit-updates");
} catch (err) {
expect(err.exitCode).toBe(1);
expect(err.message).toMatch("--only-explicit-updates");
}
});
it("should list changes in private packages", async () => {
await setupGitChanges(testDir, ["packages/package-5/random-file"]);
await lernaUpdated();
expect(consoleOutput()).toMatchSnapshot();
});
it("should return a non-zero exit code when there are no changes", async () => {
await gitTag(testDir);
const { exitCode } = await lernaUpdated();
expect(exitCode).toBe(1);
});
});
/** =========================================================================
* Circular
* ======================================================================= */
describe("circular", async () => {
let testDir;
let lernaUpdated;
beforeEach(() =>
initFixture("UpdatedCommand/circular").then(dir => {
testDir = dir;
lernaUpdated = run(testDir);
})
);
it("should list changes", async () => {
await setupGitChanges(testDir, ["packages/package-3/random-file"]);
await lernaUpdated();
expect(consoleOutput()).toMatchSnapshot();
});
it("should list changes with --force-publish *", async () => {
await setupGitChanges(testDir, ["packages/package-2/random-file"]);
await lernaUpdated("--force-publish=*");
expect(consoleOutput()).toMatchSnapshot();
});
it("should list changes with --force-publish [package,package]", async () => {
await setupGitChanges(testDir, ["packages/package-4/random-file"]);
await lernaUpdated("--force-publish", "package-2");
expect(consoleOutput()).toMatchSnapshot();
});
it("should list changes without ignored files", async () => {
await updateLernaConfig(testDir, {
command: {
// "commands" also supported
publish: {
ignore: ["ignored-file"],
},
},
});
await setupGitChanges(testDir, ["packages/package-2/ignored-file", "packages/package-3/random-file"]);
await lernaUpdated();
expect(consoleOutput()).toMatchSnapshot();
});
it("should return a non-zero exit code when there are no changes", async () => {
await gitTag(testDir);
const { exitCode } = await lernaUpdated();
expect(exitCode).toBe(1);
});
});
/** =========================================================================
* JSON Output
* ======================================================================= */
describe("with --json", async () => {
it("should list changes as a json object", async () => {
const testDir = await initFixture("UpdatedCommand/basic");
const lernaUpdated = run(testDir);
await setupGitChanges(testDir, ["packages/package-2/random-file"]);
await lernaUpdated("--json");
// Output should be a parseable string
const jsonOutput = JSON.parse(consoleOutput());
expect(jsonOutput).toMatchSnapshot();
});
});
});