Skip to content

Commit

Permalink
SCANNPM-39 set the exit code instead of force exiting
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-paulger-sonarsource committed Jul 16, 2024
1 parent a3def83 commit 2ed7725
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function scan(scanOptions: ScanOptions, cliArgs?: CliArgs) {
await runScan(scanOptions, cliArgs);
} catch (error) {
log(LogLevel.ERROR, `An error occurred: ${error}`);
process.exit(1);
throw error;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/scanner-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ export function runScannerEngine(
resolve();
} else {
reject(new Error(`Scanner engine failed with code ${code}`));
process.exit(code);
process.exitCode = code;
}
} else {
reject(new Error('Scanner engine exited with an unexpected state.'));
process.exit(1);
process.exitCode = 1;
}
});
});
Expand Down
22 changes: 11 additions & 11 deletions test/unit/scan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,21 @@ describe('scan', () => {
});

it('should fail if local scanner is requested but not found', async () => {
jest.spyOn(process, 'exit').mockImplementation();
jest.spyOn(java, 'serverSupportsJREProvisioning').mockResolvedValue(false);
jest.spyOn(scannerEngine, 'runScannerEngine');
jest.spyOn(scannerCli, 'runScannerCli');
jest.spyOn(sonarProcess, 'locateExecutableFromPath').mockResolvedValue(null);

await scan({ serverUrl: 'http://localhost:9000', localScannerCli: true });
await expect(
scan({ serverUrl: 'http://localhost:9000', localScannerCli: true }),
).rejects.toThrow(Error);

expect(scannerCli.downloadScannerCli).not.toHaveBeenCalled();
expect(scannerCli.runScannerCli).not.toHaveBeenCalled();
expect(logging.log).toHaveBeenCalledWith(
logging.LogLevel.ERROR,
expect.stringMatching(/SonarScanner CLI not found in PATH/),
);
expect(process.exit).toHaveBeenCalledWith(1);
});
});

Expand Down Expand Up @@ -173,26 +173,26 @@ describe('scan', () => {
});

it('should fail when skipping JRE provisioning without java in PATH', async () => {
jest.spyOn(process, 'exit').mockImplementation();
jest.spyOn(java, 'serverSupportsJREProvisioning').mockResolvedValue(true);
jest.spyOn(java, 'fetchJRE');
jest.spyOn(scannerEngine, 'runScannerEngine');
jest.spyOn(sonarProcess, 'locateExecutableFromPath').mockResolvedValue(null);

await scan({
serverUrl: 'http://localhost:9000',
options: {
[ScannerProperty.SonarScannerSkipJreProvisioning]: 'true',
},
});
await expect(
scan({
serverUrl: 'http://localhost:9000',
options: {
[ScannerProperty.SonarScannerSkipJreProvisioning]: 'true',
},
}),
).rejects.toThrow(Error);

expect(scannerEngine.runScannerEngine).not.toHaveBeenCalled();
expect(scannerCli.runScannerCli).not.toHaveBeenCalled();
expect(logging.log).toHaveBeenCalledWith(
logging.LogLevel.ERROR,
expect.stringMatching(/Java not found in PATH/),
);
expect(process.exit).toHaveBeenCalledWith(1);
});
});
});
11 changes: 4 additions & 7 deletions test/unit/scanner-engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,8 @@ describe('scanner-engine', () => {
]);
});

it('should exit process (with same code) when child process exits with code 1', async () => {
const CHILD_PROCESS_EXIT_CODE = 1;
const mockExit = jest.spyOn(process, 'exit').mockImplementation();
childProcessHandler.setExitCode(CHILD_PROCESS_EXIT_CODE);
it('should exit process (with same code) when child process exits with code 2', async () => {
childProcessHandler.setExitCode(2);

await expect(
runScannerEngine(
Expand All @@ -199,11 +197,10 @@ describe('scanner-engine', () => {
),
).rejects.toBeInstanceOf(Error);

expect(mockExit).toHaveBeenCalledWith(CHILD_PROCESS_EXIT_CODE);
expect(process.exitCode).toEqual(2);
});

it('should exit with code 1 when the child process exits with an unexpected state', async () => {
const mockExit = jest.spyOn(process, 'exit').mockImplementation();
childProcessHandler.setExitCode(null);

await expect(
Expand All @@ -215,7 +212,7 @@ describe('scanner-engine', () => {
),
).rejects.toBeInstanceOf(Error);

expect(mockExit).toHaveBeenCalledWith(1);
expect(process.exitCode).toEqual(1);
});

it('should output scanner engine output', async () => {
Expand Down

0 comments on commit 2ed7725

Please sign in to comment.