-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateProject.ts
77 lines (70 loc) · 2.04 KB
/
createProject.ts
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
import * as fse from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import { getFixturePath } from './getFixturePath';
export type TestProject = {
path: (filePath?: string) => string;
read: (filePath: string) => Promise<string>;
readJSON: (filePath: string) => Promise<any>;
remove: () => Promise<void>;
};
/**
* Copies a fixture to a temporary directory so that tests can be isolated
* with minimal overlap and cleanup.
*/
const createProject = async (
fixtureName: string,
fixturePath: string,
): Promise<TestProject> => {
const tempFolder = await fse.promises.realpath(
await fse.promises.mkdtemp(
path.resolve(
os.tmpdir(),
`snyk-test-${fixtureName.replace(/[/\\]/g, '-')}-`,
),
),
);
const projectPath = path.resolve(tempFolder, fixtureName);
await fse.copy(fixturePath, projectPath);
return {
path: (filePath = '') => path.resolve(projectPath, filePath),
read: (filePath: string) => {
const fullFilePath = path.resolve(projectPath, filePath);
return fse.readFile(fullFilePath, 'utf-8');
},
readJSON: async (filePath: string) => {
const fullFilePath = path.resolve(projectPath, filePath);
const strJson = await fse.readFile(fullFilePath, 'utf-8');
return JSON.parse(strJson);
},
remove: () => {
return fse.remove(tempFolder);
},
};
};
/**
* Workaround until we move all fixtures to ./test/fixtures
*
* @deprecated Use createProject instead.
*/
const createProjectFromWorkspace = async (
fixtureName: string,
): Promise<TestProject> => {
return createProject(
fixtureName,
path.join(__dirname, '../../acceptance/workspaces/' + fixtureName),
);
};
/**
* Once createProjectFromWorkspace is removed, this can be "createProject".
*/
const createProjectFromFixture = async (
fixtureName: string,
): Promise<TestProject> => {
return createProject(fixtureName, getFixturePath(fixtureName));
};
export {
createProjectFromFixture as createProject,
createProjectFromFixture,
createProjectFromWorkspace,
};