forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixtures.ts
146 lines (115 loc) · 3.98 KB
/
fixtures.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import fs from 'fs-extra'
import _path from 'path'
import chokidar from 'chokidar'
import tempDir from 'temp-dir'
import type { ProjectFixtureDir } from './fixtureDirs'
export const root = _path.join(__dirname, '..')
const serverRoot = _path.join(__dirname, '../../packages/server/')
export { fixtureDirs, ProjectFixtureDir } from './fixtureDirs'
export const projects = _path.join(root, 'projects')
export const projectFixtures = _path.join(root, 'project-fixtures')
export const cyTmpDir = _path.join(tempDir, 'cy-projects')
const projectFixtureDirs = fs.readdirSync(projectFixtures, { withFileTypes: true }).filter((f) => f.isDirectory()).map((f) => f.name)
const safeRemove = (path) => {
try {
fs.removeSync(path)
} catch (_err) {
const err = _err as NodeJS.ErrnoException
// Windows does not like the en masse deleting of files, since the AV will hold
// a lock on files when they are written. This skips deleting if the lock is
// encountered.
if (err.code === 'EBUSY' && process.platform === 'win32') {
return console.error(`Remove failed for ${ path } due to EBUSY. Skipping on Windows.`)
}
throw err
}
}
// copy contents instead of deleting+creating new file, which can cause
// filewatchers to lose track of toFile.
const copyContents = (fromFile, toFile) => {
return Promise.all([
fs.open(toFile, 'w'),
fs.readFile(fromFile),
])
.then(([toFd, fromFileBuf]) => {
return fs.write(toFd, fromFileBuf)
.finally(() => {
return fs.close(toFd)
})
})
}
// copies all of the project fixtures
// to the cyTmpDir .projects in the root
export function scaffold () {
fs.copySync(projects, cyTmpDir)
}
/**
* Given a project name, copy the project's test files to the temp dir.
* Returns the scaffolded directory
*/
export async function scaffoldProject (project: ProjectFixtureDir): Promise<string> {
const to = _path.join(cyTmpDir, project)
const from = projectFixturePath(project)
await fs.copy(from, to)
try {
const packageJson = require(`${to}/package.json`)
const fixtureDir = packageJson.projectFixtureDirectory
if (fixtureDir) {
if (!projectFixtureDirs.includes(fixtureDir)) {
throw new Error(`Invalid project fixture directory: ${fixtureDir}, expected one of ${projectFixtureDirs}`)
}
await fs.copy(_path.join(projectFixtures, fixtureDir), to, { overwrite: false })
}
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e
}
}
return to
}
export function scaffoldWatch () {
const watchdir = _path.resolve(__dirname, '../projects')
console.log('watching files due to --no-exit', watchdir)
chokidar.watch(watchdir, {
})
.on('change', (srcFilepath, stats) => {
const tmpFilepath = _path.join(cyTmpDir, _path.relative(watchdir, srcFilepath))
return copyContents(srcFilepath, tmpFilepath)
})
.on('error', console.error)
}
// removes all of the project fixtures
// from the cyTmpDir .projects in the root
export function remove () {
safeRemove(cyTmpDir)
}
export function removeProject (name: ProjectFixtureDir) {
safeRemove(projectPath(name))
}
// Removes node_modules that might have been leftover from an initial "yarn"
// in the fixture dir
export function clearFixtureNodeModules (name: ProjectFixtureDir) {
try {
safeRemove(_path.join(projects, name, 'node_modules'))
} catch {
//
}
}
// returns the path to project fixture
// in the cyTmpDir
export function project (name: ProjectFixtureDir) {
return projectPath(name)
}
export function projectPath (name: ProjectFixtureDir) {
return _path.join(cyTmpDir, name)
}
export function get (fixture, encoding: BufferEncoding = 'utf8') {
return fs.readFileSync(_path.join(serverRoot, 'test', 'support', 'fixtures', fixture), { encoding })
}
export function path (fixture: ProjectFixtureDir) {
return _path.join(serverRoot, 'test', 'support', 'fixtures', fixture)
}
export function projectFixturePath (name: ProjectFixtureDir) {
return _path.join(projects, name)
}
export default module.exports