Skip to content

Commit

Permalink
Merge pull request #54 from Unibeautify/add-more-tests
Browse files Browse the repository at this point in the history
Consolidate error handling, add more tests
  • Loading branch information
stevenzeck authored Jul 25, 2018
2 parents 1259e8f + 1fbb4dc commit 8bd9ed7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 20 deletions.
6 changes: 6 additions & 0 deletions src/commands/BaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ export abstract class BaseCommand {
protected get exitCode(): number {
return process.exitCode || 0;
}

protected handleError(error: Error, exitCode: number): Promise<never> {
this.writeError(error.message);
this.exitCode = exitCode;
return Promise.reject(error);
}
}
34 changes: 14 additions & 20 deletions src/commands/BeautifyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ export class BeautifyCommand extends BaseCommand {
return setupUnibeautify().then(unibeautify => {
if (!language) {
const error = new Error("A language is required.");
this.writeError(error.message);
this.exitCode = 1;
return Promise.reject(error);
return this.handleError(error, 1);
}
return Promise.all([
this.readConfig(programArgs),
Expand All @@ -34,25 +32,22 @@ export class BeautifyCommand extends BaseCommand {
return result;
})
.catch((error: Error) => {
this.writeError(error.message);
this.exitCode = 1;
return Promise.reject(error);
return this.handleError(error, 1);
});
});
});
}

private readConfig(programArgs: IArgs): Promise<any> {
const { configFile, configJson } = programArgs;
const { configFile, configJson, filePath } = programArgs;
if (configJson) {
return this.parseConfig(configJson);
} else {
return this.configFile(configFile);
return this.configFile({ configFile, filePath });
}
}

private readText(filePath?: string): Promise<string> {
// if (this.isTerminal && filePath) {
if (filePath) {
return this.readFile(filePath);
} else {
Expand All @@ -64,14 +59,18 @@ export class BeautifyCommand extends BaseCommand {
try {
return Promise.resolve(JSON.parse(configJson));
} catch (error) {
this.writeError(error.message);
this.exitCode = 2;
return Promise.reject(error);
return this.handleError(error, 2);
}
}

private configFile(configFile?: string, filePath?: string) {
const configExplorer = cosmiconfig("unibeautify", {});
private configFile({
configFile,
filePath,
}: {
configFile?: string;
filePath?: string;
}) {
const configExplorer = cosmiconfig("unibeautify", { stopDir: filePath });
const loadConfigPromise = configFile
? configExplorer.load(configFile)
: configExplorer.search(filePath);
Expand All @@ -84,10 +83,6 @@ export class BeautifyCommand extends BaseCommand {
);
}

protected get isTerminal(): boolean {
return Boolean(this.stdin.isTTY);
}

protected readFromStdin(): Promise<string> {
return new Promise((resolve, reject) => {
let text = "";
Expand All @@ -99,8 +94,7 @@ export class BeautifyCommand extends BaseCommand {
});
this.stdin.on("error", (err: any) => {
if (err.code === "EPIPE") {
this.exitCode = 1;
return reject(err);
return this.handleError(err, 1);
}
process.emit("warning", err);
});
Expand Down
39 changes: 39 additions & 0 deletions test/commands/BeautifyCommand.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ describe("BeautifyCommand", () => {
expect(json.exitCode).toBe(0);
expect(json).toMatchSnapshot();
});
test("should find config file without specifying it", async () => {
const command = new CustomCommand();
await command.beautify({
args: [],
filePath: "test/fixtures/test1.js",
language: "JavaScript",
});
const json = command.toJSON();
expect(json.exitCode).toBe(0);
expect(json).toMatchSnapshot();
});
test("should beautify using stdin", async () => {
expect.assertions(3);
const command = new CustomCommand("const test = 'test';");
Expand All @@ -59,6 +70,7 @@ describe("BeautifyCommand", () => {
const json = command.toJSON();
expect(json.exitCode).toBe(0);
expect(json.stderr).toBe("");
// tslint:disable-next-line:quotemark
expect(json.stdout).toBe('const test = "test";\n');
});
});
Expand Down Expand Up @@ -138,6 +150,33 @@ describe("BeautifyCommand", () => {
expect(json).toMatchSnapshot("json");
});
});
test("should throw an error with invalid json", () => {
const command = new CustomCommand();
const thenCb = jest.fn();
const catchCb = jest.fn();
return command
.beautify({
args: [],
configJson: `{"JavaScript": {"beautifiers": ["ESLint"],"quotes": "double"`,
filePath: "test/fixtures/test1.js",
language: "JavaScript",
})
.then(thenCb)
.catch(catchCb)
.then(() => {
expect(thenCb).not.toBeCalled();
expect(catchCb).toHaveBeenCalled();
expect(catchCb.mock.calls).toHaveLength(1);
expect(catchCb.mock.calls[0]).toHaveLength(1);
expect(catchCb).toHaveProperty(
["mock", "calls", 0, 0, "message"],
"Unexpected end of JSON input"
);
const json = command.toJSON();
expect(json.exitCode).toBe(2);
expect(json).toMatchSnapshot("json");
});
});
});
});
});
18 changes: 18 additions & 0 deletions test/commands/__snapshots__/BeautifyCommand.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ Object {
}
`;

exports[`BeautifyCommand Beautify Errors should throw an error with invalid json: json 1`] = `
Object {
"exitCode": 2,
"stderr": "Unexpected end of JSON input
",
"stdout": "",
}
`;

exports[`BeautifyCommand Beautify should beautify using options in config file 1`] = `
Object {
"exitCode": 0,
Expand All @@ -26,3 +35,12 @@ Object {
",
}
`;

exports[`BeautifyCommand Beautify should find config file without specifying it 1`] = `
Object {
"exitCode": 0,
"stderr": "",
"stdout": "const test = \\"test\\";
",
}
`;

0 comments on commit 8bd9ed7

Please sign in to comment.