Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion src/cmds/test-dependant.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ module.exports = {
return deps
},
default: {}
},
scriptName: {
describe: 'The script name to run',
type: 'string'
}
},
/**
* @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
31 changes: 19 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 @@ -243,10 +246,14 @@ async function testDependant (opts) {
})
}

if (!opts.scriptName) {
opts.scriptName = 'test'
}

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