Skip to content

Commit

Permalink
Merge pull request #72 from Unibeautify/dependency-manager
Browse files Browse the repository at this point in the history
See #58. Improve Dependency not installed error message
  • Loading branch information
Glavin001 committed Apr 7, 2018
2 parents 390dd63 + e2220b0 commit deaa761
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/DependencyManager/Dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export abstract class Dependency {
.then(isInstalled => {
if (this.required && !isInstalled) {
throw new Error(
`Dependency "${this.name}" is required and not installed.`
[
`Dependency "${this.name}" is required and not installed.`,
...this.errors.map(error => ` - ${error.message}`),
].join("\n")
);
}
return isInstalled;
Expand Down
8 changes: 4 additions & 4 deletions src/DependencyManager/NodeDependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ export class NodeDependency extends Dependency {

// tslint:disable-next-line:no-reserved-keywords
private require(id?: string): any {
const modulePath = this.resolve(id);
const path = this.fullPath(id);
const modulePath = this.resolve(path);
if (modulePath) {
// tslint:disable-next-line:no-require-imports non-literal-require
return require(modulePath);
} else {
throw new Error(`Cannot find module ${id}`);
throw new Error(`Cannot find module ${path}`);
}
}

private resolve(file?: string): string | undefined {
const path = this.fullPath(file);
private resolve(path: string): string | undefined {
return this.resolveLocal(path) || this.resolveGlobal(path);
}

Expand Down
3 changes: 2 additions & 1 deletion test/DependencyManager/DependencyManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "../../src/DependencyManager";

test("should fail to load dependencies", async () => {
expect.assertions(1);
expect.assertions(2);
const options: DependencyOptions = {
name: "NotFound",
package: "notfound",
Expand All @@ -18,6 +18,7 @@ test("should fail to load dependencies", async () => {
expect(error.message).toMatch(
'Dependency "NotFound" is required and not installed.'
);
expect(error.message).toMatch("Cannot find module notfound/package.json");
});
});

Expand Down
6 changes: 4 additions & 2 deletions test/DependencyManager/ExecutableDependency.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from "../../src/DependencyManager";

test("should fail to load Executable dependency", async () => {
expect.assertions(3);
expect.assertions(4);
const options: DependencyOptions = {
name: "NotFound",
program: "NotFound",
Expand All @@ -17,6 +17,7 @@ test("should fail to load Executable dependency", async () => {
expect(error.message).toMatch(
'Dependency "NotFound" is required and not installed.'
);
expect(error.message).toMatch("spawn NotFound ENOENT");
expect(dependency.isInstalled).toBe(false);
expect(dependency.errors).toHaveLength(1);
});
Expand Down Expand Up @@ -279,7 +280,7 @@ describe("successfully loaded Executable dependency", () => {
});

test("should fail to parse version from text without numbers with RegExp pattern", () => {
expect.assertions(2);
expect.assertions(3);
const options: DependencyOptions = {
name: "Node",
parseVersion: /v/,
Expand All @@ -292,6 +293,7 @@ describe("successfully loaded Executable dependency", () => {
expect(error.message).toMatch(
'Dependency "Node" is required and not installed.'
);
expect(error.message).toMatch("Invalid Version:");
});
});

Expand Down
9 changes: 7 additions & 2 deletions test/DependencyManager/NodeDependency.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
} from "../../src/DependencyManager";

test("should fail to load Node dependency", async () => {
expect.assertions(3);
expect.assertions(5);
const options: DependencyOptions = {
name: "NotFound",
package: "NotFound",
package: "notfound",
type: DependencyType.Node,
};
const dependency = new NodeDependency(options);
Expand All @@ -17,8 +17,13 @@ test("should fail to load Node dependency", async () => {
expect(error.message).toMatch(
'Dependency "NotFound" is required and not installed.'
);
expect(error.message).toMatch("Cannot find module notfound/package.json");
expect(dependency.isInstalled).toBe(false);
expect(dependency.errors).toHaveLength(1);
expect(error.message).toMatch(
'Dependency "NotFound" is required and not installed.\n' +
" - Cannot find module notfound/package.json"
);
});
});

Expand Down

0 comments on commit deaa761

Please sign in to comment.