Skip to content

Commit

Permalink
Command: pass explicit CWD to Repository constructor (#734)
Browse files Browse the repository at this point in the history
Avoid reliance on implicit process.cwd()

* Remove process.chdir() from test fixtures init
* test/InitCommand: speed up unit tests by replacing initFixture() with initDirName()
* test/ImportCommand: initialize both fixtures concurrently
* test/integration: use initFixture() directly
* Command: pass repository.rootPath to ExitHandler constructor
  • Loading branch information
evocateur authored Apr 6, 2017
1 parent 0cd40ec commit c9ed57a
Show file tree
Hide file tree
Showing 19 changed files with 239 additions and 214 deletions.
6 changes: 3 additions & 3 deletions src/Command.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import logger from "./logger";
const DEFAULT_CONCURRENCY = 4;

export default class Command {
constructor(input, flags) {
constructor(input, flags, cwd) {
this.input = input;
this.flags = flags;

this.lernaVersion = require("../package.json").version;
this.logger = logger;
this.repository = new Repository();
this.repository = new Repository(cwd);
this.progressBar = progressBar;
}

Expand Down Expand Up @@ -233,7 +233,7 @@ export default class Command {

_complete(err, code, callback) {
if (code !== 0) {
const exitHandler = new ExitHandler();
const exitHandler = new ExitHandler(this.repository.rootPath);
exitHandler.writeLogs(this.logger);
}

Expand Down
5 changes: 3 additions & 2 deletions src/ExitHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import path from "path";
import padEnd from "lodash/padEnd";

export default class ExitHandler {
constructor() {
constructor(cwd) {
this.cwd = cwd;
this.errorsSeen = {};
}

writeLogs(logger) {
const filePath = path.join(process.cwd(), "lerna-debug.log");
const filePath = path.join(this.cwd, "lerna-debug.log");
const fileContent = this._formatLogs(logger.logs);

FileSystemUtilities.writeFileSync(filePath, fileContent);
Expand Down
11 changes: 8 additions & 3 deletions src/Repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ import dependencyIsSatisfied from "./utils/dependencyIsSatisfied";
const DEFAULT_PACKAGE_GLOB = "packages/*";

export default class Repository {
constructor() {
// findUp returns null when not found, and path.resolve starts from process.cwd()
const lernaJsonLocation = findUp.sync("lerna.json") || path.resolve("lerna.json");
constructor(cwd) {
const lernaJsonLocation = (
// findUp returns null when not found
findUp.sync("lerna.json", { cwd }) ||

// path.resolve(".", ...) starts from process.cwd()
path.resolve((cwd || "."), "lerna.json")
);

this.rootPath = path.dirname(lernaJsonLocation);
this.lernaJsonLocation = lernaJsonLocation;
Expand Down
50 changes: 31 additions & 19 deletions test/BootstrapCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe("BootstrapCommand", () => {
}));

it("should run preinstall, postinstall and prepublish scripts", (done) => {
const bootstrapCommand = new BootstrapCommand([], {});
const bootstrapCommand = new BootstrapCommand([], {}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand Down Expand Up @@ -121,7 +121,7 @@ describe("BootstrapCommand", () => {
it("should hoist", (done) => {
const bootstrapCommand = new BootstrapCommand([], {
hoist: true
});
}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand All @@ -144,7 +144,7 @@ describe("BootstrapCommand", () => {
const bootstrapCommand = new BootstrapCommand([], {
hoist: true,
nohoist: "@test/package-1"
});
}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand Down Expand Up @@ -175,7 +175,7 @@ describe("BootstrapCommand", () => {
afterEach(resetSymlink);

it("should bootstrap packages", (done) => {
const bootstrapCommand = new BootstrapCommand([], {});
const bootstrapCommand = new BootstrapCommand([], {}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand All @@ -197,7 +197,7 @@ describe("BootstrapCommand", () => {
it("should not bootstrap ignored packages", (done) => {
const bootstrapCommand = new BootstrapCommand([], {
ignore: "package-@(3|4)"
});
}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand All @@ -218,7 +218,7 @@ describe("BootstrapCommand", () => {
it("should only bootstrap scoped packages", (done) => {
const bootstrapCommand = new BootstrapCommand([], {
scope: "package-@(3|4)"
});
}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand Down Expand Up @@ -249,7 +249,7 @@ describe("BootstrapCommand", () => {
afterEach(resetSymlink);

it("should bootstrap packages", (done) => {
const bootstrapCommand = new BootstrapCommand([], {});
const bootstrapCommand = new BootstrapCommand([], {}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand All @@ -272,7 +272,7 @@ describe("BootstrapCommand", () => {
it("should not bootstrap ignored packages", (done) => {
const bootstrapCommand = new BootstrapCommand([], {
ignore: "package-@(3|4)"
});
}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand All @@ -295,7 +295,7 @@ describe("BootstrapCommand", () => {
const bootstrapCommand = new BootstrapCommand([], {
scope: "package-2",
includeFilteredDependencies: true
});
}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand All @@ -318,7 +318,7 @@ describe("BootstrapCommand", () => {
const bootstrapCommand = new BootstrapCommand([], {
ignore: "{@test/package-1,package-@(3|4)}",
includeFilteredDependencies: true
});
}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand All @@ -345,7 +345,7 @@ describe("BootstrapCommand", () => {
}));

it("should get installed", (done) => {
const bootstrapCommand = new BootstrapCommand([], {});
const bootstrapCommand = new BootstrapCommand([], {}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand All @@ -365,10 +365,14 @@ describe("BootstrapCommand", () => {
});

describe("with external dependencies that have already been installed", () => {
beforeEach(() => initFixture("BootstrapCommand/warm"));
let testDir;

beforeEach(() => initFixture("BootstrapCommand/warm").then((dir) => {
testDir = dir;
}));

it("should not get re-installed", (done) => {
const bootstrapCommand = new BootstrapCommand([], {});
const bootstrapCommand = new BootstrapCommand([], {}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand All @@ -395,7 +399,7 @@ describe("BootstrapCommand", () => {
}));

it("should install all dependencies", (done) => {
const bootstrapCommand = new BootstrapCommand([], {});
const bootstrapCommand = new BootstrapCommand([], {}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand All @@ -415,10 +419,14 @@ describe("BootstrapCommand", () => {
});

describe("with package peerDependencies", () => {
beforeEach(() => initFixture("BootstrapCommand/peer"));
let testDir;

beforeEach(() => initFixture("BootstrapCommand/peer").then((dir) => {
testDir = dir;
}));

it("does not bootstrap peerDependencies", (done) => {
const bootstrapCommand = new BootstrapCommand([], {});
const bootstrapCommand = new BootstrapCommand([], {}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand All @@ -438,10 +446,14 @@ describe("BootstrapCommand", () => {
});

describe("zero packages", () => {
beforeEach(() => initFixture("BootstrapCommand/zero-pkgs"));
let testDir;

beforeEach(() => initFixture("BootstrapCommand/zero-pkgs").then((dir) => {
testDir = dir;
}));

it("should succeed in repositories with zero packages", (done) => {
const bootstrapCommand = new BootstrapCommand([], {});
const bootstrapCommand = new BootstrapCommand([], {}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand All @@ -458,7 +470,7 @@ describe("BootstrapCommand", () => {
}));

it("should install packages from registry", (done) => {
const bootstrapCommand = new BootstrapCommand([], {});
const bootstrapCommand = new BootstrapCommand([], {}, testDir);

bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
Expand Down
8 changes: 4 additions & 4 deletions test/CleanCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("CleanCommand", () => {
}));

it("should rm -rf the node_modules", (done) => {
const cleanCommand = new CleanCommand([], {});
const cleanCommand = new CleanCommand([], {}, testDir);

cleanCommand.runValidations();
cleanCommand.runPreparations();
Expand All @@ -72,7 +72,7 @@ describe("CleanCommand", () => {
it("should be possible to skip asking for confirmation", (done) => {
const cleanCommand = new CleanCommand([], {
yes: true
});
}, testDir);

cleanCommand.runValidations();
cleanCommand.runPreparations();
Expand All @@ -99,7 +99,7 @@ describe("CleanCommand", () => {
it(filter.test, (done) => {
const cleanCommand = new CleanCommand([], {
[filter.flag]: filter.flagValue
});
}, testDir);

cleanCommand.runValidations();
cleanCommand.runPreparations();
Expand Down Expand Up @@ -132,7 +132,7 @@ describe("CleanCommand", () => {
const cleanCommand = new CleanCommand([], {
scope: "@test/package-2",
includeFilteredDependencies: true
});
}, testDir);

cleanCommand.runValidations();
cleanCommand.runPreparations();
Expand Down
Loading

0 comments on commit c9ed57a

Please sign in to comment.