Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --parse-only flag #492

Merged
merged 10 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/interpreter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ program
undefined,
)
.option(
'--parse-only',
'-po, --parse-only',
'Only parses the model without running it. Exits with 0 if the model is valid, with 1 otherwise.',
false,
)
Expand Down
63 changes: 58 additions & 5 deletions apps/interpreter/src/parse-only.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,75 @@
//
// SPDX-License-Identifier: AGPL-3.0-only

import * as fs from 'node:fs/promises';
import * as os from 'os';
import * as path from 'path';
import * as process from 'process';

import { RunOptions } from '@jvalue/jayvee-interpreter-lib';
import {
clearBlockExecutorRegistry,
clearConstraintExecutorRegistry,
} from '@jvalue/jayvee-execution/test';
import {
RunOptions,
interpretModel,
interpretString,
} from '@jvalue/jayvee-interpreter-lib';

import { runAction } from './run-action';

jest.mock('@jvalue/jayvee-interpreter-lib', () => {
const original: object = jest.requireActual('@jvalue/jayvee-interpreter-lib'); // Step 2.
joluj marked this conversation as resolved.
Show resolved Hide resolved
return {
...original,
interpretModel: jest.fn(),
interpretString: jest.fn(),
};
});

describe('Parse Only', () => {
const baseDir = path.resolve(__dirname, '../../../example/');
const pathToValidModel = path.resolve(baseDir, 'cars.jv');

const defaultOptions: RunOptions = {
env: new Map<string, string>(),
debug: false,
debugGranularity: 'minimal',
debugTarget: undefined,
parseOnly: true,
};

let tempFile: string | undefined = undefined;

afterEach(async () => {
if (tempFile != null) {
await fs.rm(tempFile);
// eslint-disable-next-line require-atomic-updates
tempFile = undefined;
}
});

afterEach(() => {
// Assert that model is not executed
expect(interpretString).not.toBeCalled();
expect(interpretModel).not.toBeCalled();
});

beforeEach(() => {
jest.clearAllMocks();
jest.spyOn(process, 'exit').mockImplementation(() => {
throw new Error();
});

// Reset jayvee specific stuff
clearBlockExecutorRegistry();
clearConstraintExecutorRegistry();
});

it('should exit with 0 on a valid option', async () => {
await expect(
runAction(path.resolve(baseDir, 'cars.jv'), {
runAction(pathToValidModel, {
...defaultOptions,
joluj marked this conversation as resolved.
Show resolved Hide resolved
parseOnly: true,
}),
).rejects.toBeDefined();

Expand All @@ -38,13 +79,25 @@ describe('Parse Only', () => {
});

it('should exit with 1 on error', async () => {
joluj marked this conversation as resolved.
Show resolved Hide resolved
const validModel = (await fs.readFile(pathToValidModel)).toString();

tempFile = path.resolve(
os.tmpdir(),
// E.g. "0.gn6v6ra9575" -> "gn6v6ra9575.jv"
Math.random().toString(36).substring(2) + '.jv',
);

// Write a partial valid model in that file
joluj marked this conversation as resolved.
Show resolved Hide resolved
await fs.writeFile(tempFile, validModel.substring(validModel.length / 2));

await expect(
runAction(path.resolve(baseDir, 'cars.jv'), {
runAction(tempFile, {
...defaultOptions,
parseOnly: true,
}),
).rejects.toBeDefined();

expect(process.exit).toBeCalledTimes(1);
expect(process.exit).toHaveBeenCalledWith(0);
expect(process.exit).toHaveBeenCalledWith(1);
});
});
2 changes: 1 addition & 1 deletion apps/interpreter/src/run-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function runAction(
);
if (options.parseOnly === true) {
const { model, services } = await parseModel(extractAstNodeFn, options);
const exitCode = model && services ? 0 : 1;
const exitCode = model != null && services != null ? 0 : 1;
process.exit(exitCode);
}
const exitCode = await interpretModel(extractAstNodeFn, options);
Expand Down
Loading