-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfixtures.ts
More file actions
83 lines (67 loc) · 2.19 KB
/
fixtures.ts
File metadata and controls
83 lines (67 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import fs from 'fs';
import { Inquirerer } from 'inquirerer';
import { ParsedArgs } from 'minimist';
import os from 'os';
import path from 'path';
import { DEFAULT_TEMPLATE_REPO } from '@pgpmjs/core';
import { commands } from '../src/commands';
import { setupTests, TestEnvironment } from './cli';
import { withInitDefaults } from './init-argv';
const { mkdtempSync, rmSync, cpSync } = fs;
export const FIXTURES_PATH = path.resolve(__dirname, '../../../__fixtures__');
export const getFixturePath = (...paths: string[]) =>
path.join(FIXTURES_PATH, ...paths);
export class TestFixture {
readonly tempDir: string;
readonly tempFixtureDir: string;
readonly getFixturePath: (...paths: string[]) => string;
private environment: TestEnvironment;
constructor(...fixturePath: string[]) {
this.tempDir = mkdtempSync(path.join(os.tmpdir(), 'launchql-test-'));
if (fixturePath.length > 0) {
const originalFixtureDir = getFixturePath(...fixturePath);
this.tempFixtureDir = path.join(this.tempDir, ...fixturePath);
cpSync(originalFixtureDir, this.tempFixtureDir, { recursive: true });
} else {
this.tempFixtureDir = this.tempDir;
}
this.getFixturePath = (...paths: string[]) =>
path.join(this.tempFixtureDir, ...paths);
this.environment = setupTests()();
}
fixturePath(...paths: string[]) {
return path.join(this.tempFixtureDir, ...paths);
}
cleanup() {
rmSync(this.tempDir, { recursive: true, force: true });
}
async runCmd(argv: ParsedArgs) {
const {
mockInput,
mockOutput,
writeResults,
transformResults
} = this.environment;
// Default to remote templates and deterministic identity answers for init
// flows so tests are stable and do not rely on local machine config.
argv = withInitDefaults(argv, DEFAULT_TEMPLATE_REPO);
const prompter = new Inquirerer({
input: mockInput,
output: mockOutput,
noTty: true
});
const result = await commands(argv, prompter, {
noTty: true,
input: mockInput,
output: mockOutput,
version: '1.0.0',
minimistOpts: {}
});
return {
result,
argv,
writeResults,
transformResults
};
}
}