Skip to content
Merged
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: 6 additions & 1 deletion src/cmds/test-dependant.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
27 changes: 15 additions & 12 deletions src/test-dependant/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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) {
Expand All @@ -173,25 +174,27 @@ 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
})
}

/**
* @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')
Expand Down Expand Up @@ -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()}`)
Expand All @@ -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}`)
Expand Down
28 changes: 28 additions & 0 deletions test/dependants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '[email protected] --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', () => {
Expand All @@ -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, '[email protected] --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`)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/test-dependant/project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down