Skip to content

Commit d67f97b

Browse files
committed
tests: update override adaptor tests to use tmp-dir
1 parent d581501 commit d67f97b

File tree

1 file changed

+110
-32
lines changed

1 file changed

+110
-32
lines changed

packages/cli/test/commands.test.ts

+110-32
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { createMockLogger } from '@openfn/logger';
12
import test from 'ava';
23
import mock from 'mock-fs';
3-
import path from 'node:path';
4+
import { execSync } from 'node:child_process';
45
import fs from 'node:fs/promises';
5-
import { createMockLogger } from '@openfn/logger';
6+
import os from 'node:os';
7+
import path from 'node:path';
68

9+
import { writeFileSync } from 'node:fs';
710
import { cmd } from '../src/cli';
811
import commandParser from '../src/commands';
912
import type { Opts } from '../src/options';
@@ -93,6 +96,47 @@ async function run(command: string, job: string, options: RunOptions = {}) {
9396
}
9497
}
9598

99+
async function mockResources() {
100+
const tmpdir = await fs.mkdtemp(path.join(os.tmpdir(), 'commands-'));
101+
execSync(`cp -r ${path.resolve('test')}/__*__ ${tmpdir}`);
102+
103+
const generateJob = async (job: string) => {
104+
if (job) writeFileSync(path.join(tmpdir, '/job.js'), job);
105+
};
106+
107+
const generateOutput = async (value: string) => {
108+
if (value) writeFileSync(path.join(tmpdir, '/output.json'), value);
109+
};
110+
111+
const createNew = async (filename: string, content: string) => {
112+
const newPath = path.join(tmpdir, filename);
113+
writeFileSync(newPath, content);
114+
return newPath;
115+
};
116+
117+
return {
118+
mockPath: tmpdir,
119+
modulesPath: path.join(tmpdir, '/__modules__'),
120+
monorepoPath: path.join(tmpdir, '/__monorepo__'),
121+
repoPath: path.join(tmpdir, '__repo__'),
122+
jobPath: path.join(tmpdir, '/job.js'),
123+
outputPath: path.join(tmpdir, '/output.json'),
124+
generateJob,
125+
generateOutput,
126+
createNew,
127+
};
128+
}
129+
130+
let resMock: Awaited<ReturnType<typeof mockResources>>;
131+
132+
test.before(async () => {
133+
resMock = await mockResources();
134+
});
135+
136+
test.after(async () => {
137+
execSync(`rm -rf ${resMock.mockPath}`);
138+
});
139+
96140
test.serial('run an execution plan', async (t) => {
97141
const plan = {
98142
workflow: {
@@ -404,36 +448,48 @@ test.serial(
404448
);
405449

406450
test.serial(
407-
'override an adaptor: openfn --no-expand-adaptors -S <obj> --adaptor times-two=/modules/times-two',
451+
'override an adaptor: openfn --no-expand-adaptors -S <obj> --adaptor times-two=<path-to-module>',
408452
async (t) => {
409453
const state = JSON.stringify({ data: { count: 49.5 } });
454+
455+
await resMock.generateJob(EXPR_MOCK_ADAPTOR);
456+
410457
const result = await run(
411-
`openfn --no-expand-adaptors -S ${state} --adaptor times-two=/modules/times-two`,
412-
EXPR_MOCK_ADAPTOR
458+
`openfn ${resMock.jobPath} --no-expand-adaptors -S ${state} --adaptor times-two=${resMock.modulesPath}/times-two`,
459+
'',
460+
{ disableMock: true, outputPath: resMock.outputPath }
413461
);
462+
414463
t.assert(result.data.count === 99);
415464
}
416465
);
417466

418467
test.serial(
419-
'override adaptors: openfn --no-expand-adaptors -S <obj> --adaptors times-two=/modules/times-two',
468+
'override adaptors: openfn --no-expand-adaptors -S <obj> --adaptors times-two=<path-to-module>',
420469
async (t) => {
421470
const state = JSON.stringify({ data: { count: 49.5 } });
471+
472+
await resMock.generateJob(EXPR_MOCK_ADAPTOR);
422473
const result = await run(
423-
`openfn --no-expand-adaptors -S ${state} --adaptors times-two=/modules/times-two`,
424-
EXPR_MOCK_ADAPTOR
474+
`openfn ${resMock.jobPath} --no-expand-adaptors -S ${state} --adaptors times-two=${resMock.modulesPath}/times-two`,
475+
'',
476+
{ disableMock: true, outputPath: resMock.outputPath }
425477
);
426478
t.assert(result.data.count === 99);
427479
}
428480
);
429481

430482
test.serial(
431-
'override adaptors: openfn --no-expand-adaptors -S <obj> -a times-two=/modules/times-two',
483+
'override adaptors: openfn --no-expand-adaptors -S <obj> -a times-two=<path-to-module>',
432484
async (t) => {
433485
const state = JSON.stringify({ data: { count: 49.5 } });
486+
487+
// mock module with real filesystem
488+
await resMock.generateJob(EXPR_MOCK_ADAPTOR);
434489
const result = await run(
435-
`openfn --no-expand-adaptors -S ${state} -a times-two=/modules/times-two`,
436-
EXPR_MOCK_ADAPTOR
490+
`openfn ${resMock.jobPath} --no-expand-adaptors -S ${state} -a times-two=${resMock.modulesPath}/times-two`,
491+
'',
492+
{ disableMock: true, outputPath: resMock.outputPath }
437493
);
438494
t.assert(result.data.count === 99);
439495
}
@@ -444,11 +500,14 @@ test.serial(
444500
async (t) => {
445501
const state = JSON.stringify({ data: { count: 11 } });
446502
const job = 'export default [byTwo]';
503+
await resMock.generateJob(job);
447504
const result = await run(
448-
`openfn --no-expand-adaptors -S ${state} -a times-two --no-autoinstall`,
449-
job,
505+
`openfn ${resMock.jobPath} --no-expand-adaptors -S ${state} -a times-two --no-autoinstall`,
506+
'',
450507
{
451-
repoDir: '/repo',
508+
disableMock: true,
509+
repoDir: resMock.repoPath,
510+
outputPath: resMock.outputPath,
452511
}
453512
);
454513
t.assert(result.data.count === 22);
@@ -460,9 +519,11 @@ test.serial(
460519
async (t) => {
461520
const state = JSON.stringify({ data: { count: 22 } });
462521
const job = 'export default [byTwo]';
522+
await resMock.generateJob(job);
463523
const result = await run(
464-
`openfn -S ${state} -a times-two=/modules/times-two`,
465-
job
524+
`openfn ${resMock.jobPath} -S ${state} -a times-two=${resMock.modulesPath}/times-two`,
525+
'',
526+
{ disableMock: true, outputPath: resMock.outputPath }
466527
);
467528
t.assert(result.data.count === 44);
468529
}
@@ -472,9 +533,16 @@ test.serial(
472533
'auto-import from language-common (job): openfn job.js -a @openfn/[email protected]',
473534
async (t) => {
474535
const job = 'fn((state) => { state.data.done = true; return state; });';
475-
const result = await run('openfn -a @openfn/[email protected]', job, {
476-
repoDir: '/repo',
477-
});
536+
await resMock.generateJob(job);
537+
const result = await run(
538+
`openfn ${resMock.jobPath} -a @openfn/[email protected]`,
539+
'',
540+
{
541+
disableMock: true,
542+
repoDir: resMock.repoPath,
543+
outputPath: resMock.outputPath,
544+
}
545+
);
478546
t.true(result.data?.done);
479547
}
480548
);
@@ -492,17 +560,19 @@ test.serial(
492560
],
493561
};
494562

563+
const wfPath = await resMock.createNew(
564+
'/wf.json',
565+
JSON.stringify(workflow)
566+
);
495567
const options = {
496-
outputPath: 'output.json',
497-
expressionPath: 'wf.json',
498-
repoDir: '/repo',
568+
disableMock: true,
569+
outputPath: resMock.outputPath,
570+
expressionPath: wfPath,
571+
repoDir: resMock.repoPath,
499572
};
500573

501-
const result = await run(
502-
'openfn wf.json',
503-
JSON.stringify(workflow),
504-
options
505-
);
574+
await resMock.generateJob(JSON.stringify(workflow));
575+
const result = await run(`openfn ${wfPath}`, '', options);
506576
t.true(result.data?.done);
507577
}
508578
);
@@ -512,11 +582,15 @@ test.serial(
512582
async (t) => {
513583
const job =
514584
'alterState((state) => { /* function isn\t actually called by the mock adaptor */ throw new Error("fake adaptor") });';
585+
586+
await resMock.generateJob(job);
515587
const result = await run(
516-
'openfn -a @openfn/language-postgres --no-autoinstall',
517-
job,
588+
`openfn ${resMock.jobPath} -a @openfn/language-postgres --no-autoinstall`,
589+
'',
518590
{
519-
repoDir: '/repo',
591+
disableMock: true,
592+
repoDir: resMock.repoPath,
593+
outputPath: resMock.outputPath,
520594
}
521595
);
522596
t.assert(result === 'execute called!');
@@ -526,9 +600,13 @@ test.serial(
526600
test.serial(
527601
'load an adaptor from the monorepo env var: openfn job.js -m -a common',
528602
async (t) => {
529-
process.env.OPENFN_ADAPTORS_REPO = '/monorepo/';
603+
process.env.OPENFN_ADAPTORS_REPO = resMock.monorepoPath;
530604
const job = 'export default [alterState(() => 39)]';
531-
const result = await run('job.js -m -a common', job);
605+
await resMock.generateJob(job);
606+
const result = await run(`${resMock.jobPath} -m -a common`, '', {
607+
disableMock: true,
608+
outputPath: resMock.outputPath,
609+
});
532610
t.assert(result === 39);
533611
delete process.env.OPENFN_ADAPTORS_REPO;
534612
}

0 commit comments

Comments
 (0)