Skip to content

Commit

Permalink
fix: wrong parsing of cli output
Browse files Browse the repository at this point in the history
Issue#: #140
  • Loading branch information
alex-gilin committed Nov 19, 2021
1 parent 451de77 commit 6c8bdf7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sap/cf-tools",
"displayName": "cf-tools",
"version": "2.0.2",
"version": "2.0.4",
"description": "Cloud Foundry API tools",
"bugs": {
"url": "https://github.com/sap-staging/cloud-foundry-tools-api.git/issues"
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class Cli {
// most probably not logged in
resolve({ "stdout": stdout, "stderr": stderr, error: Cli.CF_LOGIN_ERROR, exitCode: CF_CMD_EXIT_CODE.ERROR });
return;
} else if (stdout.includes('failed') && stdout.includes('Error:')) {
} else if (/failed.*\bError\b:/g.test(stdout)) { // lgtm [js/polynomial-redos]
// DEVXBUGS-5660
resolve({ "stdout": stdout, "stderr": stderr, error: stdout, exitCode: CF_CMD_EXIT_CODE.ERROR });
return;
Expand Down
51 changes: 51 additions & 0 deletions tests/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ describe("cli unit tests", () => {
});

it("stdout starts with FAILED and has 'Error creating request'", async () => {
execResult.on = (type: string, callback: any) => (type === "exit" ? callback(1) : {});
execResult.stdout.on = (type: string, callback: any) => (callback("FAILED and has 'Error creating request'"));
childProcessMock.expects("spawn").withExactArgs("cf", undefined, undefined).returns(execResult);
const result = await Cli.execute(undefined, undefined, token);
Expand All @@ -128,6 +129,7 @@ describe("cli unit tests", () => {
});

it("stdout command failed and has 'Error: ssome error occured'", async () => {
execResult.on = (type: string, callback: any) => (type === "exit" ? callback(1) : {});
execResult.stdout.on = (type: string, callback: any) => (callback("Request is failed. Error: it is impossible to access db."));
childProcessMock.expects("spawn").withExactArgs("cf", undefined, undefined).returns(execResult);
const mockStdin = sandbox.mock(execResult.stdin);
Expand All @@ -140,6 +142,18 @@ describe("cli unit tests", () => {
mockStdin.verify();
});

it("stdout command failed and has 'Error: ssome error occured' as a multiline output", async () => {
execResult.on = (type: string, callback: any) => (type === "exit" ? callback(0) : {});
const output = "Request is failed.\n Error: it is impossible to access db.";
execResult.stdout.on = (type: string, callback: any) => (callback(output));
childProcessMock.expects("spawn").withExactArgs("cf", undefined, undefined).returns(execResult);
const result = await Cli.execute();
expect(result.stdout).to.be.equal(output);
expect(result.stderr).to.be.empty;
expect(result.exitCode).to.be.equal(0);
expect(result.error).to.be.undefined;
});

it("stdout starts with FAILED and has 'No API endpoint set'", async () => {
const errorText = "FAILED\
No API endpoint set.";
Expand Down Expand Up @@ -171,5 +185,42 @@ describe("cli unit tests", () => {
expect(result.stdout).to.be.empty;
expect(result.exitCode).to.be.equal(-2);
});

it("stdout command succeedded but output contains instance problem description with 'failed' and 'Error:' mutiline combination", async () => {
execResult.on = (type: string, callback: any) => (type === "exit" ? callback(0) : {});
const output = `{
"last_operation": {
"state": "failed",
"description": "Status: 503; ErrorMessage: <nil>; Description: <nil>; Response Error: invalid character '<' looking for beginning of value",
"created_at": "2021-11-10T16:01:26Z"
}
}`;
execResult.stdout.on = (type: string, callback: any) => (callback(output));
childProcessMock.expects("spawn").withExactArgs("cf", undefined, undefined).returns(execResult);
const result = await Cli.execute();
expect(result.stdout).to.be.equal(output);
expect(result.stderr).to.be.empty;
expect(result.exitCode).to.be.equal(0);
expect(result.error).to.be.undefined;
});

it("stdout command succeedded but output contains instance problem description with 'failed' and 'Error:' single line combination", async () => {
execResult.on = (type: string, callback: any) => (type === "exit" ? callback(0) : {});
const output = JSON.stringify({
"last_operation": {
"state": "failed",
"description": "Status: 503; ErrorMessage: <nil>; Description: <nil>; ResponseError: invalid character '<' looking for beginning of value",
"created_at": "2021-11-10T16:01:26Z"
}
});
execResult.stdout.on = (type: string, callback: any) => (callback(output));
childProcessMock.expects("spawn").withExactArgs("cf", undefined, undefined).returns(execResult);
const result = await Cli.execute();
expect(result.stdout).to.be.equal(output);
expect(result.stderr).to.be.empty;
expect(result.exitCode).to.be.equal(0);
expect(result.error).to.be.undefined;
});

});
});

0 comments on commit 6c8bdf7

Please sign in to comment.