Skip to content

Commit

Permalink
cli: fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
josephjclark committed Feb 5, 2024
1 parent 9065e6a commit 161288e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 29 deletions.
2 changes: 1 addition & 1 deletion integration-tests/cli/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test.serial('openfn version', async (t) => {
test.serial('openfn test', async (t) => {
const { stdout } = await run(t.title);
t.regex(stdout, /Versions:/);
t.regex(stdout, /Running test job.../);
t.regex(stdout, /Running test expression/);
t.regex(stdout, /Result: 42/);
});

Expand Down
7 changes: 4 additions & 3 deletions integration-tests/cli/test/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import test from 'ava';
import path from 'node:path';
import run from '../src/run';
import { extractLogs, assertLog } from '../src/util';
import { stderr } from 'node:process';

const jobsPath = path.resolve('test/fixtures');

// These are all errors that will stop the CLI from even running

test.serial('job not found', async (t) => {
test.serial('expression not found', async (t) => {
const { stdout, err } = await run('openfn blah.js --log-json');
t.is(err.code, 1);

const stdlogs = extractLogs(stdout);

assertLog(t, stdlogs, /job not found/i);
assertLog(t, stdlogs, /failed to load the job from blah.js/i);
assertLog(t, stdlogs, /expression not found/i);
assertLog(t, stdlogs, /failed to load the expression from blah.js/i);
assertLog(t, stdlogs, /critical error: aborting command/i);
});

Expand Down
9 changes: 9 additions & 0 deletions integration-tests/cli/test/execute-workflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ test.serial(
}
);

// Run a new-style execution plan with custom start
test.serial(`openfn ${jobsPath}/plan.json -i`, async (t) => {
const { err } = await run(t.title);
t.falsy(err);

const out = getJSON();
t.deepEqual(out.data.userId, 1);
});

test.serial(`openfn ${jobsPath}/wf-conditional.json`, async (t) => {
const { err } = await run(t.title);
t.falsy(err);
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/cli/test/fixtures/invalid-config-path.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"jobs": [
{
"configuration": "does-not-exist.json"
"configuration": "does-not-exist.json",
"expression": "."
}
]
}
19 changes: 19 additions & 0 deletions integration-tests/cli/test/fixtures/plan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"options": {
"start": "b"
},
"workflow": {
"steps": [
{
"id": "a",
"adaptor": "common",
"expression": "fn((state) => { return state; });"
},
{
"id": "b",
"adaptor": "http",
"expression": "get('https://jsonplaceholder.typicode.com/todos/1')"
}
]
}
}
55 changes: 31 additions & 24 deletions packages/cli/src/util/load-plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ const loadPlan = async (

export default loadPlan;

// TODO this is way over simplified :(
// see load-input
const loadJson = async (workflowPath: string, logger: Logger): Promise<any> => {
let text: string;

Expand Down Expand Up @@ -96,32 +94,41 @@ const loadExpression = async (
const jobPath = options.jobPath!;

logger.debug(`Loading expression from ${jobPath}`);
const expression = await fs.readFile(jobPath, 'utf8');
const name = path.parse(jobPath).name;
try {
const expression = await fs.readFile(jobPath, 'utf8');
const name = path.parse(jobPath).name;

const step: Job = { expression };
const step: Job = { expression };

// The adaptor should have been expanded nicely already, so we don't need intervene here
if (options.adaptors) {
const [adaptor] = options.adaptors;
if (adaptor) {
step.adaptor = adaptor;
// The adaptor should have been expanded nicely already, so we don't need intervene here
if (options.adaptors) {
const [adaptor] = options.adaptors;
if (adaptor) {
step.adaptor = adaptor;
}
}
}

const wfOptions: WorkflowOptions = {};
// TODO support state props to remove?
maybeAssign(options, wfOptions, ['timeout']);
const wfOptions: WorkflowOptions = {};
// TODO support state props to remove?
maybeAssign(options, wfOptions, ['timeout']);

const plan: ExecutionPlan = {
workflow: {
name,
steps: [step],
},
options: wfOptions,
};
// call loadXPlan now so that any options can be written
return loadXPlan(plan, options, logger);
const plan: ExecutionPlan = {
workflow: {
name,
steps: [step],
},
options: wfOptions,
};
// call loadXPlan now so that any options can be written
return loadXPlan(plan, options, logger);
} catch (e) {
abort(
logger,
'Expression not found',
undefined,
`Failed to load the expression from ${jobPath}`
);
}
};

const loadOldWorkflow = async (
Expand Down Expand Up @@ -220,6 +227,7 @@ const importExpressions = async (
configurationStr,
log
);
console.log(configString);
job.configuration = JSON.parse(configString!);
}
}
Expand All @@ -234,7 +242,6 @@ const loadXPlan = async (
if (!plan.options) {
plan.options = {};
}

// Note that baseDir should be set up in the default function
await importExpressions(plan, options.baseDir!, logger);
// expand shorthand adaptors in the workflow jobs
Expand Down

0 comments on commit 161288e

Please sign in to comment.