-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathinit-test-fixtures.ts
154 lines (148 loc) · 4.78 KB
/
init-test-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
147
148
149
150
151
152
153
154
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as cpy from 'cpy';
import {
BUILD_DIRECTORY,
statP,
spawnP,
readFileP,
writeFileP,
mkdirP,
} from './utils';
import {readdir} from 'fs';
import {promisify} from 'util';
import * as semver from 'semver';
const readdirP: (path: string) => Promise<string[]> = promisify(readdir);
export async function initTestFixtures(installPlugins: boolean) {
// Copy fixtures to build directory
const fixtureDirectories = ['./test/fixtures'];
for (const fixtureDirectory of fixtureDirectories) {
await cpy(`${fixtureDirectory}/**`, BUILD_DIRECTORY, {
parents: true,
});
}
if (!installPlugins) {
return;
}
// Run `npm install` for package fixtures
const packageFixtures = JSON.parse(
(await readFileP('./test/fixtures/plugin-fixtures.json', 'utf8')) as string
);
await mkdirP('./build/test/plugins/fixtures').catch((e: {code?: string}) => {
// it's OK if this directory already exists
if (e.code !== 'EEXIST') {
throw e;
}
});
for (const packageName in packageFixtures) {
const packageDirectory = `./build/test/plugins/fixtures/${packageName}`;
let fixtureExists = true;
try {
await statP(packageDirectory);
} catch {
fixtureExists = false;
}
/**
* This is the general approach:
*
* if package supports this Node version:
* if fixtures don't already exist for this package:
* create fixtures
* else
* if gRPC module binary exists but for a different Node version:
* reinstall gRPC
* else
* skip install
* else
* skip install
*/
const packageFixture = packageFixtures[packageName];
const supportedNodeVersions =
(packageFixture.engines && packageFixture.engines.node) || '*';
if (semver.satisfies(process.version, supportedNodeVersions)) {
if (!fixtureExists) {
await mkdirP(packageDirectory);
await writeFileP(
`${packageDirectory}/package.json`,
JSON.stringify(
Object.assign(
{
name: packageName,
version: '1.0.0',
main: 'index.js',
},
packageFixture
),
null,
2
)
);
const mainModule =
packageFixture._mainModule ||
Object.keys(packageFixture.dependencies)[0];
await writeFileP(
`${packageDirectory}/index.js`,
`module.exports = require('${mainModule}');\n`
);
console.log(`npm install in ${packageDirectory}`);
await spawnP('npm', ['install', '--reinstall'], {
cwd: packageDirectory,
});
} else {
// Conditionally re-install if gRPC module binary exists but for a different Node version
let reinstallGrpc = !fixtureExists;
const extBinDirectory = `${packageDirectory}/node_modules/grpc/src/node/extension_binary`;
try {
await statP(extBinDirectory);
const files = await readdirP(extBinDirectory);
const modulesVersions = files
.map(file => file.match(/^node-v([0-9]+)-/))
.filter(x => x)
.map(matches => matches![1]);
if (
!modulesVersions.some(
version => version === process.versions.modules
)
) {
reinstallGrpc = true;
}
} catch {
// 🤷♂️
}
if (reinstallGrpc) {
console.log(
`Re-installing gRPC in ${packageDirectory} because of stale gRPC binary`
);
const grpcPackageJson = JSON.parse(
(await readFileP(
`${packageDirectory}/node_modules/grpc/package.json`,
'utf8'
)) as string
);
await spawnP('npm', ['install', `grpc@${grpcPackageJson.version}`], {
cwd: packageDirectory,
});
} else {
console.log(
`Skipping npm install in ${packageDirectory} since node_modules already exists`
);
}
}
} else {
console.log(
`Skipping npm install in ${packageDirectory} since package not supported in Node ${process.version}`
);
}
}
}