Skip to content

Commit

Permalink
Merge pull request #467 from jvalue/remove-process-exit
Browse files Browse the repository at this point in the history
Remove process.exit commands
  • Loading branch information
georg-schwarz authored Nov 14, 2023
2 parents f014d7a + ebc97c9 commit f91d37e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
14 changes: 12 additions & 2 deletions libs/interpreter-lib/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
collectStartingBlocks,
createJayveeServices,
getBlocksInTopologicalSorting,
initializeWorkspace,
} from '@jvalue/jayvee-language-server';
import * as chalk from 'chalk';
import { NodeFileSystem } from 'langium/node';
Expand Down Expand Up @@ -82,16 +83,25 @@ export async function interpretModel(
', ',
)}.`,
);
process.exit(ExitCode.FAILURE);
return ExitCode.FAILURE;
}

useStdExtension();
registerDefaultConstraintExecutors();

const services = createJayveeServices(NodeFileSystem).Jayvee;
await initializeWorkspace(services);
setupJayveeServices(services, options.env);

const model = await extractAstNodeFn(services, loggerFactory);
let model: JayveeModel;
try {
model = await extractAstNodeFn(services, loggerFactory);
} catch (e) {
loggerFactory
.createLogger()
.logErr('Could not extract the AST node of the given model.');
return ExitCode.FAILURE;
}

const debugTargets = getDebugTargets(options.debugTarget);

Expand Down
27 changes: 6 additions & 21 deletions libs/interpreter-lib/src/parsing-util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ describe('Validation of parsing-util', () => {
options?: ParseHelperOptions,
) => Promise<LangiumDocument<AstNode>>;

let exitSpy: jest.SpyInstance;

let services: JayveeServices;

const logger = new CachedLogger(true, undefined, false);
Expand All @@ -39,16 +37,6 @@ describe('Validation of parsing-util', () => {
);

beforeAll(async () => {
// Mock Process.exit
exitSpy = jest
.spyOn(process, 'exit')
.mockImplementation((code?: number) => {
if (code === undefined || code === 0) {
return undefined as never;
}
throw new Error(`process.exit: ${code}`);
});

// Create language services
services = createJayveeServices(NodeFileSystem).Jayvee;

Expand All @@ -64,7 +52,6 @@ describe('Validation of parsing-util', () => {
});

afterEach(() => {
exitSpy.mockClear();
logger.clearLogs();
});

Expand All @@ -81,7 +68,7 @@ describe('Validation of parsing-util', () => {

await parseAndValidateDocument(text);

expect(exitSpy).toHaveBeenCalledTimes(0);
expect(logger.getLogs().error.length).toEqual(0);
expect(logger.getLogs().info).toHaveLength(0);
expect(logger.getLogs().error).toHaveLength(0);
expect(logger.getLogs().debug).toHaveLength(0);
Expand All @@ -97,8 +84,7 @@ describe('Validation of parsing-util', () => {
try {
await parseAndValidateDocument(text);
} catch (e) {
expect(exitSpy).toHaveBeenCalledTimes(1);
expect(exitSpy).toHaveBeenCalledWith(1);
expect(logger.getLogs().error.length).toBeGreaterThanOrEqual(1);
expect(logger.getLogs().info).toHaveLength(0);
expect(logger.getLogs().error).toHaveLength(2 * 5); // 2 calls that get formated to 5 lines each
expect(logger.getLogs().debug).toHaveLength(0);
Expand All @@ -115,8 +101,7 @@ describe('Validation of parsing-util', () => {
try {
await parseAndValidateDocument(text);
} catch (e) {
expect(exitSpy).toHaveBeenCalledTimes(1);
expect(exitSpy).toHaveBeenCalledWith(1);
expect(logger.getLogs().error.length).toEqual(1);
expect(logger.getLogs().info).toHaveLength(0);
expect(logger.getLogs().error).toHaveLength(1);
expect(logger.getLogs().debug).toHaveLength(0);
Expand All @@ -138,7 +123,7 @@ describe('Validation of parsing-util', () => {
logger,
);

expect(exitSpy).toHaveBeenCalledTimes(0);
expect(logger.getLogs().error.length).toEqual(0);
expect(logger.getLogs().info).toHaveLength(0);
expect(logger.getLogs().error).toHaveLength(0);
expect(logger.getLogs().debug).toHaveLength(0);
Expand All @@ -158,7 +143,7 @@ describe('Validation of parsing-util', () => {
logger,
);
} catch (e) {
expect(exitSpy).toHaveBeenCalledTimes(1);
expect(logger.getLogs().error.length).toEqual(1);
expect(logger.getLogs().info).toHaveLength(0);
expect(logger.getLogs().error).toHaveLength(1);
expect(logger.getLogs().error[0]).toEqual(
Expand All @@ -184,7 +169,7 @@ describe('Validation of parsing-util', () => {
logger,
);
} catch (e) {
expect(exitSpy).toHaveBeenCalledTimes(1);
expect(logger.getLogs().error.length).toEqual(1);
expect(logger.getLogs().info).toHaveLength(0);
expect(logger.getLogs().error).toHaveLength(1);
expect(logger.getLogs().error[0]).toMatch(
Expand Down
6 changes: 3 additions & 3 deletions libs/interpreter-lib/src/parsing-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export async function extractDocumentFromFile(
}: ${extensions.map((extension) => `"${extension}"`).join(',')}`;

logger.logErr(errorMessage);
process.exit(ExitCode.FAILURE);
return Promise.reject(ExitCode.FAILURE);
}

if (!fs.existsSync(fileName)) {
logger.logErr(`File ${fileName} does not exist.`);
process.exit(ExitCode.FAILURE);
return Promise.reject(ExitCode.FAILURE);
}

const document =
Expand Down Expand Up @@ -79,7 +79,7 @@ export async function validateDocument(
for (const errDiagnostic of errDiagnostics) {
logger.logLanguageServerDiagnostic(errDiagnostic, document);
}
process.exit(ExitCode.FAILURE);
return Promise.reject(ExitCode.FAILURE);
}

const nonErrDiagnostics = diagnostics.filter(
Expand Down

0 comments on commit f91d37e

Please sign in to comment.