diff --git a/libs/interpreter-lib/src/interpreter.ts b/libs/interpreter-lib/src/interpreter.ts index 32893a082..e63bac142 100644 --- a/libs/interpreter-lib/src/interpreter.ts +++ b/libs/interpreter-lib/src/interpreter.ts @@ -28,6 +28,7 @@ import { collectStartingBlocks, createJayveeServices, getBlocksInTopologicalSorting, + initializeWorkspace, } from '@jvalue/jayvee-language-server'; import * as chalk from 'chalk'; import { NodeFileSystem } from 'langium/node'; @@ -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); diff --git a/libs/interpreter-lib/src/parsing-util.spec.ts b/libs/interpreter-lib/src/parsing-util.spec.ts index 7f8ab9abf..6bd157b0c 100644 --- a/libs/interpreter-lib/src/parsing-util.spec.ts +++ b/libs/interpreter-lib/src/parsing-util.spec.ts @@ -27,8 +27,6 @@ describe('Validation of parsing-util', () => { options?: ParseHelperOptions, ) => Promise>; - let exitSpy: jest.SpyInstance; - let services: JayveeServices; const logger = new CachedLogger(true, undefined, false); @@ -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; @@ -64,7 +52,6 @@ describe('Validation of parsing-util', () => { }); afterEach(() => { - exitSpy.mockClear(); logger.clearLogs(); }); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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( @@ -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( diff --git a/libs/interpreter-lib/src/parsing-util.ts b/libs/interpreter-lib/src/parsing-util.ts index a08029a09..0953f417c 100644 --- a/libs/interpreter-lib/src/parsing-util.ts +++ b/libs/interpreter-lib/src/parsing-util.ts @@ -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 = @@ -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(