Skip to content

Commit

Permalink
chore: attempt to fix lavamoat tests
Browse files Browse the repository at this point in the history
- fix `test:prep` execution
- using `npm`, node/test/projects/1/node_modules will be symlinked and not under vcs
- pin keccak version
- cleanup `allow-scripts` tests (a bit of DRY, fix misuse of `path.join`, make fs cleanup more consistent)
- fix a problem in the `allow-scripts` tests where _not enough_ cleanup was happening
  • Loading branch information
boneskull committed Jul 26, 2023
1 parent 38e6235 commit 28a17c4
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 70 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -19,7 +19,7 @@
},
"scripts": {
"test": "npm --workspaces test",
"test:prep": "npm --workspaces run test:prep",
"test:prep": "lerna run test:prep",
"lint": "npm --workspaces run lint",
"lint:fix": "npm --workspaces run lint:fix",
"preversion": "npm run rebuild",
Expand Down
92 changes: 45 additions & 47 deletions packages/allow-scripts/test/index.js
@@ -1,32 +1,36 @@
const test = require('ava')
const fs = require('fs')
const path = require('path')
const { spawnSync } = require('child_process')
const fs = require('node:fs')
const path = require('node:path')
const { spawnSync } = require('node:child_process')

/**
* Path to the allow-scripts executable
*/
const ALLOW_SCRIPTS_BIN = require.resolve('../src/cli')

/**
* For fat fingers
*/
const PACKAGE_JSON = 'package.json'

test('cli - auto command', (t) => {
// set up the directories
let allowScriptsSrcRoot = path.join(__dirname, '..', 'src')
let projectRoot = path.join(__dirname, 'projects', '1')

// delete any existing package.json
fs.unlink(path.join(projectRoot, 'package.json'), err => {
if (err && err.code !== 'ENOENT') {
throw err
}
})
fs.rmSync(path.join(projectRoot, PACKAGE_JSON), { force: true });

// npm init -y
spawnSync('npm', ['init', '-y'], realisticEnvOptions(projectRoot))

// run the auto command
let cmd = path.join(allowScriptsSrcRoot, 'cli.js')
let result = spawnSync(cmd, ['auto'], realisticEnvOptions(projectRoot))
let result = spawnSync(ALLOW_SCRIPTS_BIN, ['auto'], realisticEnvOptions(projectRoot))

// forward error output for debugging
console.error(result.stderr.toString('utf-8'))

// get the package.json
const packageJsonContents = JSON.parse(fs.readFileSync(path.join(projectRoot, 'package.json'), 'utf8'))
const packageJsonContents = JSON.parse(fs.readFileSync(path.join(projectRoot, PACKAGE_JSON), 'utf8'))

// assert its contents
t.deepEqual(packageJsonContents.lavamoat, {
Expand All @@ -35,30 +39,24 @@ test('cli - auto command', (t) => {
}
})
})

test('cli - auto command with experimental bins', (t) => {
// set up the directories
let allowScriptsSrcRoot = path.join(__dirname, '..', 'src')
let projectRoot = path.join(__dirname, 'projects', '1')

// delete any existing package.json
fs.unlink(path.join(projectRoot, 'package.json'), err => {
if (err && err.code !== 'ENOENT') {
throw err
}
})
fs.rmSync(path.join(projectRoot, PACKAGE_JSON), { force: true });

// npm init -y
spawnSync('npm', ['init', '-y'], realisticEnvOptions(projectRoot))

// run the auto command
let cmd = path.join(allowScriptsSrcRoot, 'cli.js')
let result = spawnSync(cmd, ['auto', '--experimental-bins'], realisticEnvOptions(projectRoot))
let result = spawnSync(ALLOW_SCRIPTS_BIN, ['auto', '--experimental-bins'], realisticEnvOptions(projectRoot))

// forward error output for debugging
console.error(result.stderr.toString('utf-8'))

// get the package.json
const packageJsonContents = JSON.parse(fs.readFileSync(path.join(projectRoot, 'package.json'), 'utf8'))
const packageJsonContents = JSON.parse(fs.readFileSync(path.join(projectRoot, PACKAGE_JSON), 'utf8'))

// assert its contents
t.deepEqual(packageJsonContents.lavamoat, {
Expand All @@ -74,19 +72,17 @@ test('cli - auto command with experimental bins', (t) => {

test('cli - run command - good dep at the root', (t) => {
// set up the directories
let allowScriptsSrcRoot = path.join(__dirname, '..', 'src')
let projectRoot = path.join(__dirname, 'projects', '2')

// clean up from a previous run
// the force option is only here to stop rm complaining if target is missing
fs.rmSync(path.join(projectRoot, './node_modules/.bin'), {
fs.rmSync(path.join(projectRoot, 'node_modules', '.bin'), {
recursive: true,
force: true
})

// run the "run" command
let cmd = path.join(allowScriptsSrcRoot, 'cli.js')
let result = spawnSync(cmd, ['run'], realisticEnvOptions(projectRoot))
let result = spawnSync(ALLOW_SCRIPTS_BIN, ['run'], realisticEnvOptions(projectRoot))

// forward error output for debugging
console.error(result.stderr.toString('utf-8'))
Expand All @@ -109,19 +105,17 @@ test('cli - run command - good dep at the root', (t) => {
})
test('cli - run command - good dep at the root with experimental bins', (t) => {
// set up the directories
let allowScriptsSrcRoot = path.join(__dirname, '..', 'src')
let projectRoot = path.join(__dirname, 'projects', '2')

// clean up from a previous run
// the force option is only here to stop rm complaining if target is missing
fs.rmSync(path.join(projectRoot, './node_modules/.bin'), {
fs.rmSync(path.join(projectRoot, 'node_modules', '.bin'), {
recursive: true,
force: true
})

// run the "run" command
let cmd = path.join(allowScriptsSrcRoot, 'cli.js')
let result = spawnSync(cmd, ['run', '--experimental-bins'], realisticEnvOptions(projectRoot))
let result = spawnSync(ALLOW_SCRIPTS_BIN, ['run', '--experimental-bins'], realisticEnvOptions(projectRoot))

// forward error output for debugging
console.error(result.stderr.toString('utf-8'))
Expand All @@ -138,7 +132,7 @@ test('cli - run command - good dep at the root with experimental bins', (t) => {
'',
])

t.assert(fs.existsSync(path.join(projectRoot, './node_modules/.bin/good')), 'Expected a bin script to be installed in top level node_modules')
t.assert(fs.existsSync(path.join(projectRoot, 'node_modules', '.bin', 'good')), 'Expected a bin script to be installed in top level node_modules')

// note
// we could also test whether the preinstall script is
Expand All @@ -150,22 +144,25 @@ test('cli - run command - good dep at the root with experimental bins', (t) => {

test('cli - run command - good dep as a sub dep', (t) => {
// set up the directories
let allowScriptsSrcRoot = path.join(__dirname, '..', 'src')
let projectRoot = path.join(__dirname, 'projects', '3')

// clean up from a previous run
// the force option is only here to stop rm complaining if target is missing
fs.rmSync(path.join(projectRoot, 'node_modules', 'bbb', '.goodscriptworked'), { force: true })
fs.rmSync(path.join(projectRoot, 'node_modules', '.bin'), {
recursive: true,
force: true
})

// generate the bin link
spawnSync('npm', ['rebuild', 'good_dep'], realisticEnvOptions(projectRoot))

// clean up from a previous run
// the force option is only here to stop rm complaining if target is missing
fs.rmSync(path.join(projectRoot, './node_modules/bbb/.goodscriptworked'), { force: true })
// run the "run" command
let cmd = path.join(allowScriptsSrcRoot, 'cli.js')
let result = spawnSync(cmd, ['run'], realisticEnvOptions(projectRoot))
let result = spawnSync(ALLOW_SCRIPTS_BIN, ['run'], realisticEnvOptions(projectRoot))

// uncomment to forward error output for debugging
// console.error(result.stdout.toString('utf-8'))
// console.error(result.stderr.toString('utf-8'))
console.error(result.stdout.toString('utf-8'))
console.error(result.stderr.toString('utf-8'))

// assert the output
t.deepEqual(result.stdout.toString().split('\n'), [
Expand All @@ -182,26 +179,23 @@ test('cli - run command - good dep as a sub dep', (t) => {

test('cli - run command - good dep as a sub dep with experimental bins', (t) => {
// set up the directories
let allowScriptsSrcRoot = path.join(__dirname, '..', 'src')
let projectRoot = path.join(__dirname, 'projects', '3')

// clean up from a previous run
// the force option is only here to stop rm complaining if target is missing
fs.rmSync(path.join(projectRoot, './node_modules/bbb/.goodscriptworked'), { force: true })
fs.rmSync(path.join(projectRoot, './node_modules/bbb/node_modules/.bin'), {
fs.rmSync(path.join(projectRoot, 'node_modules', 'bbb', '.goodscriptworked'), { force: true })
fs.rmSync(path.join(projectRoot, 'node_modules', '.bin'), {
recursive: true,
force: true
})
// run the "run" command
let cmd = path.join(allowScriptsSrcRoot, 'cli.js')
let result = spawnSync(cmd, ['run', '--experimental-bins'], realisticEnvOptions(projectRoot))
let result = spawnSync(ALLOW_SCRIPTS_BIN, ['run', '--experimental-bins'], realisticEnvOptions(projectRoot))

// uncomment to forward error output for debugging
// console.error(result.stdout.toString('utf-8'))
// console.error(result.stderr.toString('utf-8'))


t.assert(fs.existsSync(path.join(projectRoot, './node_modules/bbb/node_modules/.bin/good')), 'Expected a nested bin script to be installed in bbb/node_modules/.bin')
t.assert(fs.existsSync(path.join(projectRoot, 'node_modules', 'bbb', 'node_modules', '.bin', 'good')), 'Expected a nested bin script to be installed in bbb/node_modules/.bin')
const errarr = result.stderr.toString().split('\n')
t.assert(errarr.every(line=>!line.includes('you shall not pass')), 'Should not have run the parent script from the nested package postinstall')
t.assert(errarr.some(line=>line.includes(`"good": "node_modules/`)), 'Expected to see instructions on how to enable a bin script1')
Expand All @@ -221,6 +215,10 @@ test('cli - run command - good dep as a sub dep with experimental bins', (t) =>

})

/**
* @param {string} projectRoot
* @returns {import('node:child_process').SpawnSyncOptions}
*/
function realisticEnvOptions(projectRoot) {
return { cwd: projectRoot, env: { ...process.env, INIT_CWD: projectRoot } }
}
}
Expand Up @@ -85,6 +85,7 @@
"globals": {
"__dirname": true,
"__filename.slice": true,
"console.warn": true,
"process.cwd": true,
"setTimeout": true
},
Expand Down
2 changes: 1 addition & 1 deletion packages/node/examples/eval-server/package.json
Expand Up @@ -9,7 +9,7 @@
"lavamoat": "^7.0.0"
},
"scripts": {
"setup": "yarn install",
"setup": "npm install",
"start": "echo 'unsafe: yarn start:node\nsafer: yarn start:lavamoat'",
"start:lavamoat": "lavamoat index.js --writeAutoConfig && lavamoat index.js",
"start:node": "node index.js"
Expand Down
2 changes: 1 addition & 1 deletion packages/node/examples/express/package.json
Expand Up @@ -14,7 +14,7 @@
"postinstall-postinstall": "^2.1.0"
},
"scripts": {
"setup": "yarn install && patch-package",
"setup": "npm install && patch-package",
"start": "echo 'node: yarn start:node\nlavamoat: yarn start:lavamoat\ncustom: yarn start:lavamoat'",
"start:node": "node index.js",
"start:lavamoat": "lavamoat index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/node/examples/todo-cli/package.json
Expand Up @@ -10,6 +10,6 @@
"readline": "^1.3.0"
},
"scripts": {
"setup": "yarn install"
"setup": "npm install"
}
}
7 changes: 0 additions & 7 deletions packages/node/test/projects/1/node_modules/a/index.js

This file was deleted.

4 changes: 0 additions & 4 deletions packages/node/test/projects/1/node_modules/a/package.json

This file was deleted.

3 changes: 0 additions & 3 deletions packages/node/test/projects/1/node_modules/b/index.js

This file was deleted.

4 changes: 0 additions & 4 deletions packages/node/test/projects/1/node_modules/b/package.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/node/test/projects/2/package.json
Expand Up @@ -4,7 +4,7 @@
"setup": "npm install && patch-package"
},
"dependencies": {
"keccak": "^3.0.0"
"keccak": "3.0.0"
},
"devDependencies": {
"patch-package": "^6.2.2"
Expand Down

0 comments on commit 28a17c4

Please sign in to comment.