From b9b962993f0d31c5d17328aa867f764dbe450a0a Mon Sep 17 00:00:00 2001 From: Georg Schwarz Date: Tue, 30 Jul 2024 09:27:46 +0200 Subject: [PATCH] Make interpreter work with absolute paths --- apps/interpreter/src/current-dir.ts | 11 --------- .../src/examples-smoke-test.spec.ts | 23 ++++-------------- apps/interpreter/src/parse-only.spec.ts | 24 ++++++------------- apps/interpreter/src/run-action.ts | 11 +++++---- 4 files changed, 17 insertions(+), 52 deletions(-) delete mode 100644 apps/interpreter/src/current-dir.ts diff --git a/apps/interpreter/src/current-dir.ts b/apps/interpreter/src/current-dir.ts deleted file mode 100644 index dede587b..00000000 --- a/apps/interpreter/src/current-dir.ts +++ /dev/null @@ -1,11 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg -// -// SPDX-License-Identifier: AGPL-3.0-only - -/** - * Gets the current directory path. - * We use this method for emulating tests in other directory to configure the correct working directory. - */ -export function getCurrentDir(): string { - return process.cwd(); -} diff --git a/apps/interpreter/src/examples-smoke-test.spec.ts b/apps/interpreter/src/examples-smoke-test.spec.ts index d0bc03ec..4320718e 100644 --- a/apps/interpreter/src/examples-smoke-test.spec.ts +++ b/apps/interpreter/src/examples-smoke-test.spec.ts @@ -40,21 +40,6 @@ vi.mock('sqlite3', () => { }; }); -// simulate as if we were starting the jv cli in the example dir -vi.mock('./current-dir', () => { - const currentDirMock = () => - path.join( - path.dirname(fileURLToPath(import.meta.url)), // relative to this test file - '..', - '..', - '..', - 'example', - ); - return { - getCurrentDir: currentDirMock, - }; -}); - describe('jv example smoke tests', () => { const defaultOptions: RunOptions = { pipeline: '.*', @@ -105,7 +90,7 @@ describe('jv example smoke tests', () => { }); sqliteLoaderMock.setup(); - await runAction('cars.jv', { + await runAction('example/cars.jv', { ...defaultOptions, }); @@ -137,7 +122,7 @@ describe('jv example smoke tests', () => { postgresLoaderMock.setup(); sqliteLoaderMock.setup(); - await runAction('electric-vehicles.jv', { + await runAction('example/electric-vehicles.jv', { ...defaultOptions, env: new Map([ ['DB_HOST', 'mock'], @@ -200,7 +185,7 @@ describe('jv example smoke tests', () => { }); sqliteLoaderMock.setup(); - await runAction('gtfs-rt.jv', { + await runAction('example/gtfs-rt.jv', { ...defaultOptions, }); @@ -228,7 +213,7 @@ describe('jv example smoke tests', () => { }); sqliteLoaderMock.setup(); - await runAction('gtfs-static.jv', { + await runAction('example/gtfs-static.jv', { ...defaultOptions, }); diff --git a/apps/interpreter/src/parse-only.spec.ts b/apps/interpreter/src/parse-only.spec.ts index 3b018136..5a304a62 100644 --- a/apps/interpreter/src/parse-only.spec.ts +++ b/apps/interpreter/src/parse-only.spec.ts @@ -22,21 +22,11 @@ const interpreterMock: JayveeInterpreter = { vi.stubGlobal('DefaultJayveeInterpreter', interpreterMock); const dirPathOfThisTest = path.dirname(fileURLToPath(import.meta.url)); -const pathExamplesRelativeToThisTest = path.join('..', '..', '..', 'example'); - -// simulate as if we were starting the jv cli in the example dir -vi.mock('./current-dir', () => { - const currentDirMock = () => - path.join(dirPathOfThisTest, pathExamplesRelativeToThisTest); - return { - getCurrentDir: currentDirMock, - }; -}); +const pathProjectRootRelativeToThisTest = path.join('..', '..', '..'); describe('Parse Only', () => { - const pathToValidModelFromExamplesDir = 'cars.jv'; - const pathToInvalidModelFromExamplesDir = path.join( - '..', + const pathToValidModelFromProjectRoot = path.join('example', 'cars.jv'); + const pathToInvalidModelFromProjectRoot = path.join( 'apps', 'interpreter', 'test', @@ -68,7 +58,7 @@ describe('Parse Only', () => { it('should exit with 0 on a valid option', async () => { await expect( - runAction(pathToValidModelFromExamplesDir, { + runAction(pathToValidModelFromProjectRoot, { ...defaultOptions, parseOnly: true, }), @@ -81,13 +71,13 @@ describe('Parse Only', () => { it('should exit with 1 on error', async () => { const modelPathRelativeToThisTest = path.join( dirPathOfThisTest, - pathExamplesRelativeToThisTest, - pathToInvalidModelFromExamplesDir, + pathProjectRootRelativeToThisTest, + pathToInvalidModelFromProjectRoot, ); expect(fs.existsSync(modelPathRelativeToThisTest)).toBe(true); await expect( - runAction(pathToInvalidModelFromExamplesDir, { + runAction(pathToInvalidModelFromProjectRoot, { ...defaultOptions, parseOnly: true, }), diff --git a/apps/interpreter/src/run-action.ts b/apps/interpreter/src/run-action.ts index ee23123e..ad4d718d 100644 --- a/apps/interpreter/src/run-action.ts +++ b/apps/interpreter/src/run-action.ts @@ -17,7 +17,6 @@ import { type JayveeServices, } from '@jvalue/jayvee-language-server'; -import { getCurrentDir } from './current-dir'; import { parsePipelineMatcherRegExp, parseRunOptions } from './run-options'; export async function runAction( @@ -35,9 +34,9 @@ export async function runAction( return process.exit(ExitCode.FAILURE); } - const currentDir = getCurrentDir(); + const currentDir = process.cwd(); const workingDir = currentDir; - const absoluteFilePath = path.join(currentDir, filePath); + const filePathRelativeToCurrentDir = path.relative(currentDir, filePath); const interpreter = new DefaultJayveeInterpreter({ pipelineMatcher: (pipelineDefinition) => @@ -49,10 +48,12 @@ export async function runAction( }).addWorkspace(workingDir); if (options.parseOnly === true) { - return await runParseOnly(absoluteFilePath, interpreter); + return await runParseOnly(filePathRelativeToCurrentDir, interpreter); } - const exitCode = await interpreter.interpretFile(absoluteFilePath); + const exitCode = await interpreter.interpretFile( + filePathRelativeToCurrentDir, + ); process.exit(exitCode); }