Skip to content

Commit

Permalink
refactor: test should handle the modules checks
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfouquet committed Oct 18, 2024
1 parent bee1193 commit f62a1d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
27 changes: 8 additions & 19 deletions templates/common/__test__/function.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,6 @@ import YAML from 'yaml';
*/
type WorkflowTemplate = { spec: { templates: { name: string; script: { source: string } }[] } };

/**
* List of required modules
*/
export const shimRequired: string[] = [];

/**
* Return the required module to import.
*
* @param name of the required module
* @returns
*/
function requireShim(name: string): unknown {
shimRequired.push(name);
if (name === 'node:fs') return fs;
throw new Error('Unknown import: ' + name);
}

/**
* Extract the script of the task named `taskName` from the `workflowTemplate`.
*
Expand All @@ -46,6 +29,11 @@ function getScript(workflow: WorkflowTemplate, taskName: string): string {
*/
type FunctionData = { toReplace: string; replaceWith: string };

/**
* Module
*/
type RequireShim = (requiredModule: string) => unknown;

/**
* Read the workflow YAML file and create a function from the script inside.
* Replace the data in the script and run the function.
Expand All @@ -54,18 +42,19 @@ type FunctionData = { toReplace: string; replaceWith: string };
* @param data data to replace in the script
* @param requireModule required module to import (optional)
*/
export function runTestFunction(workflowPath: string, data: FunctionData[], requireModule?: string): void {
export function runTestFunction(workflowPath: string, data: FunctionData[], requireModule?: RequireShim): void {
const wfRaw = fs.readFileSync(workflowPath, 'utf-8');
const wfTemplate = YAML.parse(wfRaw) as WorkflowTemplate;
let script = getScript(wfTemplate, 'main');

data.forEach((d) => {
script = script.replaceAll(d.toReplace, d.replaceWith);
});
// eslint-disable-next-line @typescript-eslint/no-implied-eval

if (requireModule !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-implied-eval
new Function('require', script)(requireShim);
new Function('require', script)(requireModule);
} else {
// eslint-disable-next-line @typescript-eslint/no-implied-eval
new Function(script)();
Expand Down
10 changes: 8 additions & 2 deletions templates/common/__test__/get.location.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import assert from 'node:assert';
import fs from 'node:fs';
import { describe, it } from 'node:test';

import { runTestFunction, shimRequired } from './function.helper.js';
import { runTestFunction } from './function.helper.js';

describe('get-location script template', () => {
it('should output workflow artifact location', (t) => {
const spy = t.mock.method(console, 'log');
const shimRequired: string[] = [];
runTestFunction(
'./templates/common/get.location.yml',
[
Expand All @@ -21,7 +23,11 @@ describe('get-location script template', () => {
}),
},
],
'node:fs',
(req) => {
shimRequired.push(req);
if (req === 'node:fs') return fs;
throw new Error('Failed');
},
);
assert.equal(spy.mock.callCount(), 3);

Expand Down

0 comments on commit f62a1d6

Please sign in to comment.