Skip to content

Commit

Permalink
refactor: Synchronize import ChildProcessUtilities -> childProcess
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Dec 11, 2020
1 parent a02e12e commit 4acff59
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 108 deletions.
6 changes: 3 additions & 3 deletions commands/create/index.js
Expand Up @@ -13,7 +13,7 @@ const pReduce = require("p-reduce");
const slash = require("slash");

const { Command } = require("@lerna/command");
const ChildProcessUtilities = require("@lerna/child-process");
const childProcess = require("@lerna/child-process");
const npmConf = require("@lerna/npm-conf");
const { ValidationError } = require("@lerna/validation-error");
const { builtinNpmrc } = require("./lib/builtin-npmrc");
Expand Down Expand Up @@ -178,7 +178,7 @@ class CreateCommand extends Command {
}

gitConfig(prop) {
return ChildProcessUtilities.execSync("git", ["config", "--get", prop], this.execOpts);
return childProcess.execSync("git", ["config", "--get", prop], this.execOpts);
}

collectExternalVersions() {
Expand Down Expand Up @@ -358,7 +358,7 @@ class CreateCommand extends Command {

setRepository() {
try {
const url = ChildProcessUtilities.execSync("git", ["remote", "get-url", "origin"], this.execOpts);
const url = childProcess.execSync("git", ["remote", "get-url", "origin"], this.execOpts);

this.conf.set("repository", url);
} catch (err) {
Expand Down
6 changes: 3 additions & 3 deletions commands/diff/__tests__/diff-command.test.js
Expand Up @@ -6,7 +6,7 @@ const path = require("path");
const { getPackages } = require("@lerna/project");

// mocked modules
const ChildProcessUtilities = require("@lerna/child-process");
const childProcess = require("@lerna/child-process");

// helpers
const initFixture = require("@lerna-test/init-fixture")(__dirname);
Expand All @@ -23,7 +23,7 @@ expect.addSnapshotSerializer(require("@lerna-test/serialize-git-sha"));

describe("DiffCommand", () => {
// overwrite spawn so we get piped stdout, not inherited
ChildProcessUtilities.spawn = jest.fn((...args) => execa(...args));
childProcess.spawn = jest.fn((...args) => execa(...args));

it("should diff packages from the first commit", async () => {
const cwd = await initFixture("basic");
Expand Down Expand Up @@ -102,7 +102,7 @@ describe("DiffCommand", () => {
it("should error when git diff exits non-zero", async () => {
const cwd = await initFixture("basic");

ChildProcessUtilities.spawn.mockImplementationOnce(() => {
childProcess.spawn.mockImplementationOnce(() => {
const nonZero = new Error("An actual non-zero, not git diff pager SIGPIPE");
nonZero.exitCode = 1;

Expand Down
4 changes: 2 additions & 2 deletions commands/diff/index.js
@@ -1,6 +1,6 @@
"use strict";

const ChildProcessUtilities = require("@lerna/child-process");
const childProcess = require("@lerna/child-process");
const { Command } = require("@lerna/command");
const { ValidationError } = require("@lerna/validation-error");
const { getLastCommit } = require("./lib/get-last-commit");
Expand Down Expand Up @@ -49,7 +49,7 @@ class DiffCommand extends Command {
}

execute() {
return ChildProcessUtilities.spawn("git", this.args, this.execOpts).catch((err) => {
return childProcess.spawn("git", this.args, this.execOpts).catch((err) => {
if (err.exitCode) {
// quitting the diff viewer is not an error
throw err;
Expand Down
29 changes: 14 additions & 15 deletions commands/exec/__tests__/exec-command.test.js
Expand Up @@ -5,7 +5,7 @@ const fs = require("fs-extra");
const globby = require("globby");

// mocked modules
const ChildProcessUtilities = require("@lerna/child-process");
const childProcess = require("@lerna/child-process");

// helpers
const initFixture = require("@lerna-test/init-fixture")(__dirname);
Expand All @@ -16,20 +16,19 @@ const { normalizeRelativeDir } = require("@lerna-test/normalize-relative-dir");
const lernaExec = require("@lerna-test/command-runner")(require("../command"));

// assertion helpers
const calledInPackages = () =>
ChildProcessUtilities.spawn.mock.calls.map(([, , opts]) => path.basename(opts.cwd));
const calledInPackages = () => childProcess.spawn.mock.calls.map(([, , opts]) => path.basename(opts.cwd));

const execInPackagesStreaming = (testDir) =>
ChildProcessUtilities.spawnStreaming.mock.calls.reduce((arr, [command, params, opts, prefix]) => {
childProcess.spawnStreaming.mock.calls.reduce((arr, [command, params, opts, prefix]) => {
const dir = normalizeRelativeDir(testDir, opts.cwd);
arr.push([dir, command, `(prefix: ${prefix})`].concat(params).join(" "));
return arr;
}, []);

describe("ExecCommand", () => {
// TODO: it's very suspicious that mockResolvedValue() doesn't work here
ChildProcessUtilities.spawn = jest.fn(() => Promise.resolve({ exitCode: 0 }));
ChildProcessUtilities.spawnStreaming = jest.fn(() => Promise.resolve({ exitCode: 0 }));
childProcess.spawn = jest.fn(() => Promise.resolve({ exitCode: 0 }));
childProcess.spawnStreaming = jest.fn(() => Promise.resolve({ exitCode: 0 }));

afterEach(() => {
process.exitCode = undefined;
Expand All @@ -50,7 +49,7 @@ describe("ExecCommand", () => {
});

it("rejects with execution error", async () => {
ChildProcessUtilities.spawn.mockImplementationOnce((cmd, args) => {
childProcess.spawn.mockImplementationOnce((cmd, args) => {
const boom = new Error("execution error");

boom.failed = true;
Expand All @@ -72,7 +71,7 @@ describe("ExecCommand", () => {
});

it("should ignore execution errors with --no-bail", async () => {
ChildProcessUtilities.spawn.mockImplementationOnce((cmd, args, { pkg }) => {
childProcess.spawn.mockImplementationOnce((cmd, args, { pkg }) => {
const boom = new Error(pkg.name);

boom.failed = true;
Expand All @@ -88,8 +87,8 @@ describe("ExecCommand", () => {
// command doesn't throw, but it _does_ set exitCode
expect(process.exitCode).toBe(456);

expect(ChildProcessUtilities.spawn).toHaveBeenCalledTimes(2);
expect(ChildProcessUtilities.spawn).toHaveBeenLastCalledWith(
expect(childProcess.spawn).toHaveBeenCalledTimes(2);
expect(childProcess.spawn).toHaveBeenLastCalledWith(
"boom",
["--shaka", "--lakka"],
expect.objectContaining({
Expand All @@ -101,8 +100,8 @@ describe("ExecCommand", () => {
it("should filter packages with `ignore`", async () => {
await lernaExec(testDir)("ls", "--ignore", "package-1");

expect(ChildProcessUtilities.spawn).toHaveBeenCalledTimes(1);
expect(ChildProcessUtilities.spawn).toHaveBeenLastCalledWith("ls", [], {
expect(childProcess.spawn).toHaveBeenCalledTimes(1);
expect(childProcess.spawn).toHaveBeenLastCalledWith("ls", [], {
cwd: path.join(testDir, "packages/package-2"),
pkg: expect.objectContaining({
name: "package-2",
Expand All @@ -120,15 +119,15 @@ describe("ExecCommand", () => {
it("should run a command", async () => {
await lernaExec(testDir)("ls");

expect(ChildProcessUtilities.spawn).toHaveBeenCalledTimes(2);
expect(childProcess.spawn).toHaveBeenCalledTimes(2);
expect(calledInPackages()).toEqual(["package-1", "package-2"]);
});

it("should run a command with parameters", async () => {
await lernaExec(testDir)("ls", "--", "-la");

expect(ChildProcessUtilities.spawn).toHaveBeenCalledTimes(2);
expect(ChildProcessUtilities.spawn).toHaveBeenLastCalledWith("ls", ["-la"], expect.any(Object));
expect(childProcess.spawn).toHaveBeenCalledTimes(2);
expect(childProcess.spawn).toHaveBeenLastCalledWith("ls", ["-la"], expect.any(Object));
});

it("runs a command for a given scope", async () => {
Expand Down
11 changes: 3 additions & 8 deletions commands/exec/index.js
Expand Up @@ -2,7 +2,7 @@

const pMap = require("p-map");

const ChildProcessUtilities = require("@lerna/child-process");
const childProcess = require("@lerna/child-process");
const { Command } = require("@lerna/command");
const { Profiler } = require("@lerna/profiler");
const { runTopologically } = require("@lerna/run-topologically");
Expand Down Expand Up @@ -164,16 +164,11 @@ class ExecCommand extends Command {
}

runCommandInPackageStreaming(pkg) {
return ChildProcessUtilities.spawnStreaming(
this.command,
this.args,
this.getOpts(pkg),
this.prefix && pkg.name
);
return childProcess.spawnStreaming(this.command, this.args, this.getOpts(pkg), this.prefix && pkg.name);
}

runCommandInPackageCapturing(pkg) {
return ChildProcessUtilities.spawn(this.command, this.args, this.getOpts(pkg));
return childProcess.spawn(this.command, this.args, this.getOpts(pkg));
}
}

Expand Down
10 changes: 5 additions & 5 deletions commands/import/index.js
Expand Up @@ -5,7 +5,7 @@ const fs = require("fs-extra");
const path = require("path");
const pMapSeries = require("p-map-series");

const ChildProcessUtilities = require("@lerna/child-process");
const childProcess = require("@lerna/child-process");
const { Command } = require("@lerna/command");
const { promptConfirmation } = require("@lerna/prompt");
const { ValidationError } = require("@lerna/validation-error");
Expand Down Expand Up @@ -138,11 +138,11 @@ class ImportCommand extends Command {
}

execSync(cmd, args) {
return ChildProcessUtilities.execSync(cmd, args, this.execOpts);
return childProcess.execSync(cmd, args, this.execOpts);
}

externalExecSync(cmd, args) {
return ChildProcessUtilities.execSync(cmd, args, this.externalExecOpts);
return childProcess.execSync(cmd, args, this.externalExecOpts);
}

createPatchForCommit(sha) {
Expand Down Expand Up @@ -226,7 +226,7 @@ class ImportCommand extends Command {
//
// Fall back to three-way merge, which can help with duplicate commits
// due to merge history.
const proc = ChildProcessUtilities.exec("git", procArgs, this.execOpts);
const proc = childProcess.exec("git", procArgs, this.execOpts);

proc.stdin.end(patch);

Expand All @@ -241,7 +241,7 @@ class ImportCommand extends Command {
tracker.completeWork(1);

// Automatically skip empty commits
return ChildProcessUtilities.exec("git", ["am", "--skip"], this.execOpts);
return childProcess.exec("git", ["am", "--skip"], this.execOpts);
}

err.sha = sha;
Expand Down
20 changes: 10 additions & 10 deletions commands/version/__tests__/git-commit.test.js
Expand Up @@ -4,43 +4,43 @@ jest.mock("temp-write");
jest.mock("@lerna/child-process");

const { EOL } = require("os");
const { sync: mockWrite } = require("temp-write");
const { exec: mockExec } = require("@lerna/child-process");
const tempWrite = require("temp-write");
const childProcess = require("@lerna/child-process");
const { gitCommit } = require("../lib/git-commit");

describe("git commit", () => {
mockExec.mockResolvedValue();
mockWrite.mockReturnValue("temp-file-path");
childProcess.exec.mockResolvedValue();
tempWrite.sync.mockReturnValue("temp-file-path");

test("--message", async () => {
const opts = { cwd: "message" };
await gitCommit("subject", {}, opts);
expect(mockExec).toHaveBeenLastCalledWith("git", ["commit", "-m", "subject"], opts);
expect(childProcess.exec).toHaveBeenLastCalledWith("git", ["commit", "-m", "subject"], opts);
});

test("--message <multiline>", async () => {
const message = `subject${EOL}${EOL}body`;
const opts = { cwd: "multi-line" };
await gitCommit(message, {}, opts);
expect(mockWrite).toHaveBeenLastCalledWith(message, "lerna-commit.txt");
expect(mockExec).toHaveBeenLastCalledWith("git", ["commit", "-F", "temp-file-path"], opts);
expect(tempWrite.sync).toHaveBeenLastCalledWith(message, "lerna-commit.txt");
expect(childProcess.exec).toHaveBeenLastCalledWith("git", ["commit", "-F", "temp-file-path"], opts);
});

test("--amend", async () => {
const opts = { cwd: "no-edit" };
await gitCommit("whoops", { amend: true }, opts);
expect(mockExec).toHaveBeenLastCalledWith("git", ["commit", "--amend", "--no-edit"], opts);
expect(childProcess.exec).toHaveBeenLastCalledWith("git", ["commit", "--amend", "--no-edit"], opts);
});

test("--no-commit-hooks", async () => {
const opts = { cwd: "no-verify" };
await gitCommit("yolo", { commitHooks: false }, opts);
expect(mockExec).toHaveBeenLastCalledWith("git", ["commit", "--no-verify", "-m", "yolo"], opts);
expect(childProcess.exec).toHaveBeenLastCalledWith("git", ["commit", "--no-verify", "-m", "yolo"], opts);
});

test("--sign-git-commit", async () => {
const opts = { cwd: "signed" };
await gitCommit("nice", { signGitCommit: true }, opts);
expect(mockExec).toHaveBeenLastCalledWith("git", ["commit", "--gpg-sign", "-m", "nice"], opts);
expect(childProcess.exec).toHaveBeenLastCalledWith("git", ["commit", "--gpg-sign", "-m", "nice"], opts);
});
});
10 changes: 5 additions & 5 deletions commands/version/__tests__/git-tag.test.js
Expand Up @@ -2,19 +2,19 @@

jest.mock("@lerna/child-process");

const { exec: mockExec } = require("@lerna/child-process");
const childProcess = require("@lerna/child-process");
const { gitTag } = require("../lib/git-tag");

describe("gitTag", () => {
mockExec.mockResolvedValue();
childProcess.exec.mockResolvedValue();

it("creates an annotated git tag", async () => {
const tag = "v1.2.3";
const opts = { cwd: "default" };

await gitTag(tag, {}, opts);

expect(mockExec).toHaveBeenLastCalledWith("git", ["tag", tag, "-m", tag], opts);
expect(childProcess.exec).toHaveBeenLastCalledWith("git", ["tag", tag, "-m", tag], opts);
});

it("signs the tag when configured", async () => {
Expand All @@ -23,7 +23,7 @@ describe("gitTag", () => {

await gitTag(tag, { signGitTag: true }, opts);

expect(mockExec).toHaveBeenLastCalledWith("git", ["tag", tag, "-m", tag, "--sign"], opts);
expect(childProcess.exec).toHaveBeenLastCalledWith("git", ["tag", tag, "-m", tag, "--sign"], opts);
});

it("forces the tag when configured", async () => {
Expand All @@ -32,6 +32,6 @@ describe("gitTag", () => {

await gitTag(tag, { forceGitTag: true }, opts);

expect(mockExec).toHaveBeenLastCalledWith("git", ["tag", tag, "-m", tag, "--force"], opts);
expect(childProcess.exec).toHaveBeenLastCalledWith("git", ["tag", tag, "-m", tag, "--force"], opts);
});
});

0 comments on commit 4acff59

Please sign in to comment.