Skip to content

Commit 8a4098e

Browse files
committed
[INTERNAL] commands/base.js: Add tests for error handling
1 parent c7c607a commit 8a4098e

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

test/lib/cli/commands/base.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,54 @@ test("ui5 -v", async (t) => {
1414
const {stdout} = await ui5(["-v"]);
1515
t.is(stdout, `${pkg.version} (from ${ui5Cli})`);
1616
});
17+
18+
test("Yargs error handling", async (t) => {
19+
const err = await t.throwsAsync(ui5(["invalidcomands"]));
20+
21+
const stdoutLines = err.stdout.split("\n");
22+
t.deepEqual(stdoutLines[0], "Command Failed:", "Correct first log line");
23+
// Error message itself originates from yargs and is therefore not asserted
24+
t.deepEqual(stdoutLines[stdoutLines.length - 1], `See 'ui5 --help' or 'ui5 build --help' for help`,
25+
"Correct last log line");
26+
27+
t.deepEqual(err.exitCode, 1, "Process was exited with code 1");
28+
});
29+
30+
test("Exception error handling", async (t) => {
31+
// This test depends on the init command throwing on projects that already have a ui5.yaml
32+
33+
const err = await t.throwsAsync(ui5(["init"], {
34+
cwd: path.join(__dirname, "..", "..", "..", "fixtures", "init", "application")
35+
}));
36+
37+
const stdoutLines = err.stdout.split("\n");
38+
t.deepEqual(stdoutLines[1], "⚠️ Process Failed With Error", "Correct error log");
39+
t.deepEqual(stdoutLines[3], "Error Message:", "Correct error log");
40+
t.deepEqual(stdoutLines[4], "Initialization not possible: ui5.yaml already exists", "Correct error log");
41+
t.deepEqual(stdoutLines[stdoutLines.length - 1],
42+
"For details, execute the same command again with an additional '--verbose' parameter", "Correct error log");
43+
44+
t.deepEqual(err.exitCode, 1, "Process was exited with code 1");
45+
});
46+
47+
48+
test("Exception error handling with verbose logging", async (t) => {
49+
// This test depends on the init command throwing on projects that already have a ui5.yaml
50+
51+
const err = await t.throwsAsync(ui5(["init", "--verbose"], {
52+
cwd: path.join(__dirname, "..", "..", "..", "fixtures", "init", "application")
53+
}));
54+
55+
const stdoutLines = err.stdout.split("\n");
56+
t.deepEqual(stdoutLines[1], "⚠️ Process Failed With Error", "Correct error log");
57+
t.deepEqual(stdoutLines[3], "Error Message:", "Correct error log");
58+
t.deepEqual(stdoutLines[4], "Initialization not possible: ui5.yaml already exists", "Correct error log");
59+
t.deepEqual(stdoutLines[6], "Stack Trace:", "Correct error log");
60+
t.deepEqual(stdoutLines[7], "Error: Initialization not possible: ui5.yaml already exists", "Correct error log");
61+
62+
t.deepEqual(stdoutLines[stdoutLines.length - 1], `If you think this is an issue of the UI5 Tooling, you might ` +
63+
`report it using the following URL: https://github.com/SAP/ui5-cli/issues/new`,
64+
"Correct last log line");
65+
66+
t.deepEqual(err.exitCode, 1, "Process was exited with code 1");
67+
});

test/lib/cli/commands/versions.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ const test = require("ava");
22
const sinon = require("sinon");
33
const versions = require("../../../../lib/cli/commands/versions");
44

5+
test.afterEach.always((t) => {
6+
sinon.restore();
7+
});
8+
59
test.serial("Retrieves version from package.json", (t) => {
610
const builderVersion = versions.getVersion("../../../test/fixtures/@ui5/builder");
711
const serverVersion = versions.getVersion("../../../test/fixtures/@ui5/server");
@@ -21,7 +25,6 @@ test.serial("Error: returns not installed if version was not found", (t) => {
2125
});
2226

2327
test.serial("Error: throws with error if error occurred while processing", async (t) => {
24-
sinon.stub(process, "exit");
2528
sinon.stub(versions, "getVersion").throws(new Error("Error occurred"));
2629
const error = await t.throwsAsync(versions.handler({}));
2730
t.deepEqual(error.message, "Error occurred", "Correct error message thrown");

0 commit comments

Comments
 (0)