/
fixture.ts
405 lines (357 loc) · 12 KB
/
fixture.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import { joinPathFragments, readJsonFile, writeJsonFile } from "@nrwl/devkit";
import { exec, spawn } from "child_process";
import { ensureDir, ensureDirSync, readFile, remove, writeFile } from "fs-extra";
import isCI from "is-ci";
import { dirSync } from "tmp";
interface RunCommandOptions {
silenceError?: boolean;
env?: Record<string, string>;
cwd?: string;
silent?: boolean;
}
type PackageManager = "npm";
interface FixtureCreateOptions {
name: string;
packageManager: PackageManager;
runLernaInit: boolean;
initializeGit: boolean;
installDependencies: boolean;
forceDeterministicTerminalOutput?: boolean;
}
type RunCommandResult = { stdout: string; stderr: string; combinedOutput: string };
const ORIGIN_GIT = "origin.git";
const REGISTRY = "http://localhost:4872/";
export const E2E_ROOT = isCI
? dirSync({ prefix: "lerna-e2e-" }).name
: joinPathFragments("/tmp", "lerna-e2e");
ensureDirSync(E2E_ROOT);
/**
* A initialized Fixture creates an entry within /tmp/lerna-e2e for the given fixture name with the following structure:
*
* /{{ FIXTURE_NAME }}
* /origin (a bare git repo which acts as the git origin for the lerna workspace under test)
* /lerna-workspace (the lerna workspace under test, created using `lerna init`)
*
*/
export class Fixture {
private readonly fixtureRootPath = joinPathFragments(E2E_ROOT, this.name);
private readonly fixtureWorkspacePath = joinPathFragments(this.fixtureRootPath, "lerna-workspace");
private readonly fixtureOriginPath = joinPathFragments(this.fixtureRootPath, ORIGIN_GIT);
constructor(
private readonly name: string,
private readonly packageManager: PackageManager = "npm",
private readonly forceDeterministicTerminalOutput: boolean
) {}
static async create({
name,
packageManager,
runLernaInit,
initializeGit,
installDependencies,
forceDeterministicTerminalOutput,
}: FixtureCreateOptions): Promise<Fixture> {
const fixture = new Fixture(
// Make the underlying name include the package manager and be globally unique
uniq(`${name}-${packageManager}`),
packageManager,
forceDeterministicTerminalOutput || false
);
fixture.createFixtureRoot();
await fixture.createGitOrigin();
if (initializeGit) {
await fixture.gitCloneEmptyRemoteAsLernaWorkspace();
} else {
await fixture.createEmptyDirectoryForWorkspace();
}
if (runLernaInit) {
await fixture.lernaInit();
}
if (installDependencies) {
await fixture.install();
}
return fixture;
}
/**
* Tear down the entirety of the fixture including the git origin and the lerna workspace under test.
*/
async destroy(): Promise<void> {
await remove(this.fixtureRootPath);
}
/**
* Resolve the file path of a given file within the lerna workspace under test.
*/
getWorkspacePath(path?: string): string {
return path ? joinPathFragments(this.fixtureWorkspacePath, path) : this.fixtureWorkspacePath;
}
/**
* Read the contents of a given file within the lerna workspace under test.
*/
async readWorkspaceFile(file: string): Promise<string> {
return readFile(this.getWorkspacePath(file), "utf-8");
}
/**
* Execute a given command in the root of the lerna workspace under test within the fixture.
* This has been given a terse name to help with readability in the spec files.
*/
async exec(
command: string,
opts: Exclude<RunCommandOptions, "cwd"> = {
silenceError: false,
env: undefined,
}
): Promise<RunCommandResult> {
return this.execImpl(command, { ...opts, cwd: this.fixtureWorkspacePath });
}
/**
* Resolve the locally published version of lerna and run the `init` command, with an optionally
* provided arguments. Reverts useNx and useWorkspaces to false when options.keepDefaultOptions is not provided, since those options are off for most users.
*/
async lernaInit(args?: string, options?: { keepDefaultOptions: true }): Promise<RunCommandResult> {
return this.exec(
`npx --registry=http://localhost:4872/ --yes lerna@${getPublishedVersion()} init ${args || ""}`
).then((initResult) =>
options?.keepDefaultOptions ? initResult : this.revertDefaultInitOptions().then(() => initResult)
);
}
private async revertDefaultInitOptions(): Promise<void> {
await this.updateJson("lerna.json", (json) => ({
...json,
useNx: false,
useWorkspaces: false,
packages: ["packages/*"],
}));
await this.updateJson("package.json", (json) => {
const newJson = { ...json };
delete newJson.workspaces;
return newJson;
});
}
/**
* Execute the install command of the configured package manager for the current fixture.
* This has been given a terse name to help with readability in the spec files.
*/
async install(args?: string): Promise<RunCommandResult> {
switch (this.packageManager) {
case "npm":
return this.exec(`npm --registry=${REGISTRY} install${args ? ` ${args}` : ""}`);
default:
throw new Error(`Unsupported package manager: ${this.packageManager}`);
}
}
/**
* Execute a command using the locally installed instance of the lerna CLI.
* This has been given a terse name to help with readability in the spec files.
*/
async lerna(
args: string,
opts: { silenceError: boolean } = { silenceError: false }
): Promise<RunCommandResult> {
// Ensure we reference the locally installed copy of lerna
return this.exec(`npx --offline --no lerna ${args}`, { silenceError: opts.silenceError });
}
async addNxToWorkspace(): Promise<void> {
await this.updateJson("lerna.json", (json) => ({
...json,
useNx: true,
}));
writeJsonFile(this.getWorkspacePath("nx.json"), {
extends: "nx/presets/npm.json",
tasksRunnerOptions: {
default: {
runner: "nx/tasks-runners/default",
},
},
});
await this.install("-D nx@latest");
}
async addPackagesDirectory(path: string): Promise<void> {
await this.updateJson("lerna.json", (json) => ({
...json,
packages: [...(json.packages as string[]), `${path}/*`],
}));
}
async addDependencyToPackage({
packagePath,
dependencyName,
version,
}: {
packagePath: string;
dependencyName: string;
version: string;
}): Promise<void> {
await this.updateJson(`${packagePath}/package.json`, (json) => ({
...json,
dependencies: {
...(json.dependencies as Record<string, string>),
[dependencyName]: version,
},
}));
}
async addScriptsToPackage({
packagePath,
scripts,
}: {
packagePath: string;
scripts: Record<string, string>;
}): Promise<void> {
await this.updateJson(`${packagePath}/package.json`, (json) => ({
...json,
scripts: {
...(json.scripts as Record<string, string>),
...scripts,
},
}));
}
async updatePackageVersion({
packagePath,
newVersion,
}: {
packagePath: string;
newVersion: string;
}): Promise<void> {
await this.updateJson(`${packagePath}/package.json`, (json) => ({
...json,
version: newVersion,
}));
}
async createInitialGitCommit(): Promise<void> {
await this.exec("git checkout -b test-main");
await writeFile(this.getWorkspacePath(".gitignore"), "node_modules\n.DS_Store");
await this.exec("git add .gitignore");
await this.exec("git add .");
await this.exec('git commit -m "initial-commit"');
}
async updateJson(path: string, updateFn: (json: Record<string, unknown>) => Record<string, unknown>) {
const jsonPath = this.getWorkspacePath(path);
const json = readJsonFile(jsonPath) as Record<string, unknown>;
writeJsonFile(jsonPath, updateFn(json));
}
/**
* The underlying child_process implementation, not exposed to consumers of the fixture. This separation of
* "within lerna workspace vs not" is necessary because there are certain commands we need to run at the root
* of the overall fixture (e.g. around setting up the git origin).
*/
private async execImpl(
command: string,
opts: RunCommandOptions = {
silenceError: false,
env: undefined,
cwd: undefined,
}
): Promise<RunCommandResult> {
/**
* For certain commands combining independent streams for stdout and stderr is not viable,
* because it produces too much non-determinism in the final output being asserted in the test.
*
* We therefore provide a way for a fixture to opt-in to an alternative command execution implementation
* in which we simply append the stderr contents to the stdout contents in a deterministic manner.
*/
if (this.forceDeterministicTerminalOutput) {
return new Promise((resolve, reject) => {
exec(
command,
{
cwd: opts.cwd || this.fixtureRootPath,
env: {
...(opts.env || process.env),
FORCE_COLOR: "false",
},
encoding: "utf-8",
},
(err, stdout, stderr) => {
if (!opts.silenceError && err) {
reject(err);
}
resolve({
stdout: stripConsoleColors(stdout),
stderr: stripConsoleColors(stderr),
combinedOutput: stripConsoleColors(`${stdout}${stderr}`),
});
}
);
});
}
return new Promise((resolve, reject) => {
const [cmd, ...args] = command.split(" ");
let stdout = "";
let stderr = "";
let combinedOutput = "";
let error: Error | null = null;
const childProcess = spawn(cmd, args, {
shell: true,
cwd: opts.cwd || this.fixtureRootPath,
env: {
...(opts.env || process.env),
FORCE_COLOR: "false",
},
});
childProcess.stdout.setEncoding("utf8");
childProcess.stdout.on("data", (chunk) => {
stdout += chunk;
combinedOutput += chunk;
});
childProcess.stderr.setEncoding("utf8");
childProcess.stderr.on("data", (chunk) => {
stderr += chunk;
combinedOutput += chunk;
});
childProcess.on("error", (err) => {
error = err;
});
childProcess.on("close", (code) => {
if (!opts.silenceError) {
if (error) {
reject(error);
} else if (code !== 0) {
reject(new Error(stderr));
}
}
resolve({
stdout: stripConsoleColors(stdout),
stderr: stripConsoleColors(stderr),
combinedOutput: stripConsoleColors(combinedOutput),
});
});
});
}
private async createFixtureRoot(): Promise<void> {
await ensureDir(this.fixtureRootPath);
}
private async createGitOrigin(): Promise<void> {
await this.execImpl(`git init --bare ${ORIGIN_GIT}`);
}
/**
* This gives us an empty directory as our starting point (ready to run `lerna init` on)
* but with the advantage that it is already a git repository with the local origin
* correctly configured.
*/
private async gitCloneEmptyRemoteAsLernaWorkspace(): Promise<void> {
await this.execImpl(`git clone ${this.fixtureOriginPath} ${this.fixtureWorkspacePath}`);
}
/**
* This gives us an empty directory as our starting point with no git data at all, so that
* we can also test running `lerna init` under those conditions.
*/
private async createEmptyDirectoryForWorkspace(): Promise<void> {
await ensureDir(this.fixtureWorkspacePath);
}
}
/**
* Remove log colors for fail proof string search
* @param log
* @returns
*/
function stripConsoleColors(log: string): string {
// eslint-disable-next-line no-control-regex
return log.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "");
}
function getPublishedVersion(): string {
process.env.PUBLISHED_VERSION =
process.env.PUBLISHED_VERSION ||
// fallback to latest if built package is missing
"latest";
return process.env.PUBLISHED_VERSION;
}
function uniq(prefix: string) {
return `${prefix}-${Math.floor(Math.random() * 10000000)}`;
}