|
| 1 | +import { |
| 2 | + CliError, |
| 3 | + ExitCodeError, |
| 4 | + getCliError, |
| 5 | +} from "../../../src/codeql-cli/cli-errors"; |
| 6 | +import { EOL } from "os"; |
| 7 | + |
| 8 | +describe("getCliError", () => { |
| 9 | + it("returns an error with an unknown error", () => { |
| 10 | + const error = new Error("foo"); |
| 11 | + |
| 12 | + expect(getCliError(error, undefined, "bar", ["baz"])).toEqual( |
| 13 | + new CliError("foo", undefined, error, "bar", ["baz"]), |
| 14 | + ); |
| 15 | + }); |
| 16 | + |
| 17 | + it("returns an error with an unknown error with stderr", () => { |
| 18 | + const error = new Error("foo"); |
| 19 | + |
| 20 | + expect(getCliError(error, "Something failed", "bar", ["baz"])).toEqual( |
| 21 | + new CliError("Something failed", "Something failed", error, "bar", [ |
| 22 | + "baz", |
| 23 | + ]), |
| 24 | + ); |
| 25 | + }); |
| 26 | + |
| 27 | + it("returns an error with an unknown error with stderr", () => { |
| 28 | + const error = new Error("foo"); |
| 29 | + |
| 30 | + expect(getCliError(error, "Something failed", "bar", ["baz"])).toEqual( |
| 31 | + new CliError("Something failed", "Something failed", error, "bar", [ |
| 32 | + "baz", |
| 33 | + ]), |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it("returns an error with an exit code error with unhandled exit code", () => { |
| 38 | + const error = new ExitCodeError(99); // OOM |
| 39 | + |
| 40 | + expect(getCliError(error, "OOM!", "bar", ["baz"])).toEqual( |
| 41 | + new CliError("OOM!", "OOM!", error, "bar", ["baz"]), |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it("returns an error with an exit code error with handled exit code without string", () => { |
| 46 | + const error = new ExitCodeError(2); |
| 47 | + |
| 48 | + expect(getCliError(error, "Something happened!", "bar", ["baz"])).toEqual( |
| 49 | + new CliError("Something happened!", "Something happened!", error, "bar", [ |
| 50 | + "baz", |
| 51 | + ]), |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it("returns an error with a user code error with identifying string", () => { |
| 56 | + const error = new ExitCodeError(2); |
| 57 | + const stderr = `Something happened!${EOL}A fatal error occurred: The query did not run successfully.${EOL}The correct columns were not present.`; |
| 58 | + |
| 59 | + expect(getCliError(error, stderr, "bar", ["baz"])).toEqual( |
| 60 | + new CliError( |
| 61 | + `A fatal error occurred: The query did not run successfully.${EOL}The correct columns were not present.`, |
| 62 | + stderr, |
| 63 | + error, |
| 64 | + "bar", |
| 65 | + ["baz"], |
| 66 | + ), |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns an error with a user code error with cancelled string", () => { |
| 71 | + const error = new ExitCodeError(2); |
| 72 | + const stderr = `Running query...${EOL}Something is happening...${EOL}Computation was cancelled: Cancelled by user`; |
| 73 | + |
| 74 | + expect(getCliError(error, stderr, "bar", ["baz"])).toEqual( |
| 75 | + new CliError(stderr, stderr, error, "bar", ["baz"]), |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + it("returns an error with a cancelled error with identifying string", () => { |
| 80 | + const error = new ExitCodeError(98); |
| 81 | + const stderr = `Running query...${EOL}Something is happening...${EOL}Computation was cancelled: Cancelled by user`; |
| 82 | + |
| 83 | + expect(getCliError(error, stderr, "bar", ["baz"])).toEqual( |
| 84 | + new CliError( |
| 85 | + "Computation was cancelled: Cancelled by user", |
| 86 | + stderr, |
| 87 | + error, |
| 88 | + "bar", |
| 89 | + ["baz"], |
| 90 | + ), |
| 91 | + ); |
| 92 | + }); |
| 93 | +}); |
0 commit comments