-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathtest-headlamp-plugin.js
executable file
·187 lines (161 loc) · 6.13 KB
/
test-headlamp-plugin.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/env node
const USAGE = `
This tests unpublished @kinvolk/headlamp-plugin package in repo.
./test-headlamp-plugin.js
Assumes being run within the plugins/headlamp-plugin folder
`;
const PACKAGE_NAME = 'headlamp-myfancy';
function testHeadlampPlugin() {
// remove some temporary files.
cleanup();
// Make a package file of headlamp-plugin we can test
run('npm', ['install']);
run('npm', ['run', 'build']);
run('npm', ['pack']);
const packedFile = fs
.readdirSync('.')
.filter(file => file.match('kinvolk-headlamp-plugin-.*gz'))[0];
console.log('Packed headlamp-plugin package file:', packedFile);
// Use "link" to test the repo version of the headlamp-plugin tool.
run('npm', ['link']);
run('node', ['bin/headlamp-plugin.js', 'create', PACKAGE_NAME, '--link']);
curDir = join('.', PACKAGE_NAME);
run('npm', ['install', join('..', packedFile)]);
// test headlamp-plugin build
run('node', [join('..', 'bin', 'headlamp-plugin.js'), 'build']);
checkFileExists(join(PACKAGE_NAME, 'dist', 'main.js'));
// test headlamp-plugin build folder
curDir = '.';
fs.rmSync(PACKAGE_NAME, { recursive: true });
run('node', ['bin/headlamp-plugin.js', 'create', PACKAGE_NAME, '--link']);
curDir = PACKAGE_NAME;
run('npm', ['install', join('..', packedFile)]);
curDir = '.';
run('node', ['bin/headlamp-plugin.js', 'build', PACKAGE_NAME]);
checkFileExists(join(PACKAGE_NAME, 'dist', 'main.js'));
fs.writeFileSync(join(PACKAGE_NAME, 'dist', 'extra.txt'), 'All dist/ files will be copied.');
// test extraction works
run('node', ['bin/headlamp-plugin.js', 'extract', '.', '.plugins']);
checkFileExists(join('.plugins', PACKAGE_NAME, 'main.js'));
checkFileExists(join('.plugins', PACKAGE_NAME, 'package.json'));
// make sure extra files in dist/ folder are copied too
checkFileExists(join('.plugins', PACKAGE_NAME, 'extra.txt'));
// test packing works
const tmpDir = fs.mkdtempSync('headlamp-plugin-test-');
run('node', ['bin/headlamp-plugin.js', 'package', PACKAGE_NAME, tmpDir]);
checkFileExists(join(tmpDir, `${PACKAGE_NAME}-0.1.0.tar.gz`));
// extract archive and check files
const extractionFolder = join(tmpDir, 'dst');
fs.mkdirSync(extractionFolder, { recursive: true });
run('tar', ['-xzf', join(tmpDir, `${PACKAGE_NAME}-0.1.0.tar.gz`), '-C', extractionFolder]);
checkFileExists(join(extractionFolder, `${PACKAGE_NAME}`, 'main.js'));
checkFileExists(join(extractionFolder, `${PACKAGE_NAME}`, 'package.json'));
fs.rmSync(tmpDir, { recursive: true });
// test format command and that default code is formatted correctly
fs.rmSync(PACKAGE_NAME, { recursive: true });
run('node', ['bin/headlamp-plugin.js', 'create', PACKAGE_NAME, '--link']);
curDir = PACKAGE_NAME;
run('npm', ['install', join('..', packedFile)]);
run('npm', ['run', 'format']);
// test lint command and default code is lint free
run('npm', ['run', 'lint']);
run('npm', ['run', 'lint-fix']);
// test type script error checks
run('npm', ['run', 'tsc']);
// test the storybook builds
// TODO: Reenable after storybook is fixed
// run('npm run storybook-build');
// test upgrade adds missing files
const filesToRemove = [
'tsconfig.json',
join('src', 'headlamp-plugin.d.ts'),
join('.vscode', 'extensions.json'),
];
filesToRemove.forEach(file => {
fs.rmSync(join(curDir, file), { recursive: true });
});
run('node', [join('..', 'bin', 'headlamp-plugin.js'), 'upgrade', '--skip-package-updates']);
checkFileExists(join(curDir, 'tsconfig.json'));
checkFileExists(join(curDir, 'src', 'headlamp-plugin.d.ts'));
checkFileExists(join(curDir, '.vscode', 'extensions.json'));
// Does it upgrade "@kinvolk/headlamp-plugin" from an old version?
// change @kinvolk/headlamp-plugin version in package.json to an old one "^0.4.9"
const packageJsonPath = join(curDir, 'package.json');
const packageJson = fs.readFileSync(packageJsonPath, 'utf8');
const changedJson = packageJson
.split('\n')
.map(line =>
line.includes('"@kinvolk/headlamp-plugin"')
? ' "@kinvolk/headlamp-plugin": "^0.4.9"\n'
: line
)
.join('\n');
fs.writeFileSync(packageJsonPath, changedJson);
// test upgrade updates the package line, and the old version is not in there
run('node', [join('..', 'bin', 'headlamp-plugin.js'), 'upgrade']);
const oldVersion = '0.4.9';
if (fs.readFileSync(packageJsonPath, 'utf8').includes(oldVersion)) {
exit(`Error: old version still in ${packageJsonPath}`);
}
// test there are no @material-ui imports, they should be mui
if (fs.readFileSync(join(curDir, 'src', 'index.tsx'), 'utf8').includes('@material-ui')) {
exit(`Error: @material-ui imports in ${mainJsPath}`);
}
}
const fs = require('fs');
const child_process = require('child_process');
const path = require('path');
const join = path.join;
const resolve = path.resolve;
let curDir;
function cleanup() {
console.log(`Cleaning up. Removing temp files...`);
fs.readdirSync('.')
.filter(file => file.match('kinvolk-headlamp-plugin-.*gz'))
.forEach(file => fs.rmSync(file));
const foldersToRemove = [path.join('.plugins', PACKAGE_NAME), PACKAGE_NAME];
console.log('Temp foldersToRemove', foldersToRemove);
foldersToRemove
.filter(folder => fs.existsSync(folder))
.forEach(folder => fs.rmSync(folder, { recursive: true }));
}
function run(cmd, args) {
console.log('');
console.log(
`Running cmd:${cmd} with args:${args.join(' ')} inside of cwd:${curDir} abs: "${resolve(
curDir
)}"`
);
console.log('');
try {
child_process.execFileSync(cmd, args, {
stdio: 'inherit',
cwd: curDir,
encoding: 'utf8',
});
} catch (e) {
exit(
`Error: Problem running "${cmd} ${args.join(' ')}" inside of "${curDir}" abs: "${resolve(
curDir
)}"`
);
}
}
function checkFileExists(fname) {
if (!fs.existsSync(fname)) {
exit(`Error: ${fname} does not exist.`);
}
}
function exit(message) {
console.error(message);
cleanup();
process.exit(1);
}
(function () {
if (process.argv[1].includes('test-headlamp-plugin')) {
console.log(USAGE);
curDir = '.';
process.on('beforeExit', cleanup);
testHeadlampPlugin();
}
})();