Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app: frontend: plugins: Modify exec and run commands #3020

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/windows/codesign.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { exec } = require('child_process');
const { execFile } = require('child_process');
const path = require('path');
const os = require('os');
const fs = require('fs');
Expand Down Expand Up @@ -145,8 +145,9 @@ function sign(esrpTool, pathToSign) {
const authJson = path.resolve(os.tmpdir(), 'Auth.json');
fs.writeFileSync(authJson, JSON.stringify(AUTH_JSON, undefined, 2));

exec(
`${esrpTool} Sign -a ${authJson} -p ${policyJson} -i ${signInputJson}`,
execFile(
esrpTool,
['Sign', '-a', authJson, '-p', policyJson, '-i', signInputJson],
(error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/make-env.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const os = require('os');
const path = require('path');
const { execSync } = require('child_process');
const { execFileSync } = require('child_process');
const { readFileSync } = require('fs');

const envFile = path.join(os.tmpdir(), 'tmpEnv');

test('Create & verify', () => {
const execFile = path.resolve(path.join(__dirname, '..', 'make-env.js'));
execSync(`node ${execFile} ${envFile}`);
execFileSync('node', [execFile, envFile]);

const envContents = readFileSync(envFile).toString();

Expand Down
4 changes: 3 additions & 1 deletion plugins/headlamp-plugin/bin/headlamp-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,10 @@ function runScriptOnPackages(packageFolder, scriptName, cmdLine, env) {

console.log(`"${folder}": ${scriptName}-ing, :${cmdLineToUse}:...`);

const [cmd, ...args] = cmdLineToUse.split(' ');

try {
child_process.execSync(cmdLineToUse, {
child_process.execFileSync(cmd, args, {
stdio: 'inherit',
encoding: 'utf8',
env: { ...process.env, ...(env || {}) },
Expand Down
58 changes: 33 additions & 25 deletions plugins/headlamp-plugin/test-headlamp-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,69 +13,69 @@ function testHeadlampPlugin() {
cleanup();

// Make a package file of headlamp-plugin we can test
run('npm install');
run('npm run build');
run('npm pack');
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`);
run('npm', ['link']);
run('node', ['bin/headlamp-plugin.js', 'create', PACKAGE_NAME, '--link']);
curDir = join('.', PACKAGE_NAME);
run(`npm install ${join('..', packedFile)}`);
run('npm', ['install', join('..', packedFile)]);

// test headlamp-plugin build
run(`node ${join('..', 'bin', 'headlamp-plugin.js')} 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`);
run('node', ['bin/headlamp-plugin.js', 'create', PACKAGE_NAME, '--link']);
curDir = PACKAGE_NAME;
run(`npm install ${join('..', packedFile)}`);
run('npm', ['install', join('..', packedFile)]);
curDir = '.';
run(`node bin/headlamp-plugin.js build ${PACKAGE_NAME}`);
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`);
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}`);
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}`);
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`);
run('node', ['bin/headlamp-plugin.js', 'create', PACKAGE_NAME, '--link']);
curDir = PACKAGE_NAME;
run(`npm install ${join('..', packedFile)}`);
run('npm run format');
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');
run('npm', ['run', 'lint']);
run('npm', ['run', 'lint-fix']);

// test type script error checks
run('npm run tsc');
run('npm', ['run', 'tsc']);

// test the storybook builds
// TODO: Reenable after storybook is fixed
Expand All @@ -90,7 +90,7 @@ function testHeadlampPlugin() {
filesToRemove.forEach(file => {
fs.rmSync(join(curDir, file), { recursive: true });
});
run(`node ${join('..', 'bin', 'headlamp-plugin.js')} upgrade --skip-package-updates`);
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'));
Expand All @@ -110,7 +110,7 @@ function testHeadlampPlugin() {
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`);
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}`);
Expand Down Expand Up @@ -143,18 +143,26 @@ function cleanup() {
.forEach(folder => fs.rmSync(folder, { recursive: true }));
}

function run(cmd) {
function run(cmd, args) {
console.log('');
console.log(`Running cmd:${cmd} inside of cwd:${curDir} abs: "${resolve(curDir)}"`);
console.log(
`Running cmd:${cmd} with args:${args.join(' ')} inside of cwd:${curDir} abs: "${resolve(
curDir
)}"`
);
console.log('');
try {
child_process.execSync(cmd, {
child_process.execFileSync(cmd, args, {
stdio: 'inherit',
cwd: curDir,
encoding: 'utf8',
});
} catch (e) {
exit(`Error: Problem running "${cmd}" inside of "${curDir}" abs: "${resolve(curDir)}"`);
exit(
`Error: Problem running "${cmd} ${args.join(' ')}" inside of "${curDir}" abs: "${resolve(
curDir
)}"`
);
}
}
function checkFileExists(fname) {
Expand Down
Loading