diff --git a/src/cmds/test-dependant.js b/src/cmds/test-dependant.js index e88c218ea..11c0a8b0e 100644 --- a/src/cmds/test-dependant.js +++ b/src/cmds/test-dependant.js @@ -38,10 +38,15 @@ module.exports = { return deps }, default: {} + }, + scriptName: { + describe: 'The script name to run', + type: 'string', + default: 'test' } }, /** - * @param {{ repo: string; branch: string; deps: any; }} argv + * @param {{ repo: string; branch: string; deps: any; scriptName: string}} argv */ handler (argv) { const cmd = require('../test-dependant') diff --git a/src/test-dependant/index.js b/src/test-dependant/index.js index fb9dcb8e8..5651c180d 100644 --- a/src/test-dependant/index.js +++ b/src/test-dependant/index.js @@ -132,8 +132,9 @@ const upgradeDependenciesInDir = async (targetDir, deps) => { /** * @param {string} targetDir * @param {{}} deps + * @param {string} scriptName */ -const testModule = async (targetDir, deps) => { +const testModule = async (targetDir, deps, scriptName) => { const pkgPath = path.join(targetDir, 'package.json') if (!fs.existsSync(pkgPath)) { @@ -152,15 +153,15 @@ const testModule = async (targetDir, deps) => { } } - if (!modulePkg.scripts || !modulePkg.scripts.test) { - printFailureMessage(`Module ${modulePkg.name} has no tests`) + if (!modulePkg.scripts || !(scriptName in modulePkg.scripts)) { + printFailureMessage(`Module ${modulePkg.name} does not have the script ${scriptName} to run`) return } try { // run the tests without our upgrade - if they are failing, no point in continuing - await exec('npm', ['test'], { + await exec('npm', [scriptName], { cwd: targetDir }) } catch (err) { @@ -173,7 +174,7 @@ const testModule = async (targetDir, deps) => { await upgradeDependenciesInDir(targetDir, deps) // run the tests with the new deps - await exec('npm', ['test'], { + await exec('npm', [scriptName], { cwd: targetDir }) } @@ -181,17 +182,19 @@ const testModule = async (targetDir, deps) => { /** * @param {string} targetDir * @param {any} deps + * @param {string} scriptName */ -const testRepo = async (targetDir, deps) => { +const testRepo = async (targetDir, deps, scriptName) => { await installDependencies(targetDir) - await testModule(targetDir, deps) + await testModule(targetDir, deps, scriptName) } /** * @param {string} targetDir * @param {any} deps + * @param {string} scriptName */ -const testMonoRepo = async (targetDir, deps) => { +const testMonoRepo = async (targetDir, deps, scriptName) => { await installDependencies(targetDir) let lerna = path.join('node_modules', '.bin', 'lerna') @@ -221,13 +224,13 @@ const testMonoRepo = async (targetDir, deps) => { // test each package that depends on ipfs/http client for (const pattern of packages) { for await (const match of glob(targetDir, pattern)) { - await testModule(path.join(targetDir, match), deps) + await testModule(path.join(targetDir, match), deps, scriptName) } } } /** - * @param {{ repo: string; branch: string; deps: any; }} opts + * @param {{ repo: string; branch: string; deps: any; scriptName: string; }} opts */ async function testDependant (opts) { const targetDir = path.join(os.tmpdir(), `test-dependant-${Date.now()}`) @@ -244,9 +247,9 @@ async function testDependant (opts) { } if (isMonoRepo(targetDir)) { - await testMonoRepo(targetDir, opts.deps) + await testMonoRepo(targetDir, opts.deps, opts.scriptName) } else { - await testRepo(targetDir, opts.deps) + await testRepo(targetDir, opts.deps, opts.scriptName) } console.info(`Removing ${targetDir}`) diff --git a/test/dependants.js b/test/dependants.js index 0590eb6f0..a9f8cfd31 100644 --- a/test/dependants.js +++ b/test/dependants.js @@ -86,6 +86,22 @@ describe('dependants', function () { expect(output.stdout).to.include(`${diff}-dependency-version=1.0.0`) expect(output.stdout).to.include(`${diff}-dependency-version=1.0.1`) }) + + it('should run a given script in a regular project', async () => { + const diff = `derp-${Math.random()}` + + const output = await execa( + bin, + ['test-dependant', dirs.project, '--deps=it-all@1.0.1 --script-name=another-test'], + { + env: { + DISAMBIGUATOR: diff + } + } + ) + expect(output.stdout).to.include(`${diff}-dependency-version=1.0.0`) + expect(output.stdout).to.include(`${diff}-dependency-version=1.0.1`) + }) }) describe('monorepo', () => { @@ -104,5 +120,17 @@ describe('dependants', function () { expect(output.stdout).to.include(`${diff}-dependency-version=1.0.0`) expect(output.stdout).to.include(`${diff}-dependency-version=1.0.1`) }) + + it('should run a given script in a monorepo project', async () => { + const diff = `derp-${Math.random()}` + const output = await execa(bin, ['test-dependant', dirs.monorepo, '--deps=it-all@1.0.1 --script-name=another-test'], { + env: { + DISAMBIGUATOR: diff + } + }) + + expect(output.stdout).to.include(`${diff}-dependency-version=1.0.0`) + expect(output.stdout).to.include(`${diff}-dependency-version=1.0.1`) + }) }) }) diff --git a/test/fixtures/test-dependant/monorepo/packages/submodule/package.json b/test/fixtures/test-dependant/monorepo/packages/submodule/package.json index 1f333b845..e787556b1 100644 --- a/test/fixtures/test-dependant/monorepo/packages/submodule/package.json +++ b/test/fixtures/test-dependant/monorepo/packages/submodule/package.json @@ -3,7 +3,8 @@ "version": "1.0.0", "description": "", "scripts": { - "test": "node ./test.js" + "test": "node ./test.js", + "another-test": "node ./test.js" }, "author": "", "license": "ISC", diff --git a/test/fixtures/test-dependant/project/package.json b/test/fixtures/test-dependant/project/package.json index 4634ba89c..3d415a20f 100644 --- a/test/fixtures/test-dependant/project/package.json +++ b/test/fixtures/test-dependant/project/package.json @@ -5,7 +5,8 @@ "author": "", "license": "ISC", "scripts": { - "test": "node ./test.js" + "test": "node ./test.js", + "another-test": "node ./test.js" }, "dependencies": { "it-all": "1.0.0"