Skip to content

Commit

Permalink
feat(test): allow per-module specific command, close #102
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Jun 25, 2017
1 parent cb105d1 commit fa3b010
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 6 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,29 @@ You can skip a list of modules by name using `config` property in the `package.j
}
```

### Custom test command per module

Some modules are not really tested using the default `npm test` command or
whatever is passed via `--test "..."` from CLI. For example a linter module
should probably be tested using `npm run lint` command. You can set individual
test commands for each module to override the default test command. In the
`package.json` config object set "commands" object

```json
"config": {
"next-update": {
"commands": {
"git-issues": "npm run issues",
"standard": "npm run lint"
}
}
}
```

Then when `git-issues` module is checked by itself, it will run
`npm run issues` command; when module `standard` is tested by itself, the
test will use `npm run lint` command.

### Misc

* To see what has changed in the latest version of any module,
Expand Down
51 changes: 51 additions & 0 deletions __snapshots__/command-spec.js.snap-shot
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
exports['sorts by version 1'] = [
{
"name": "lodash",
"version": "1.0.1",
"from": "1.0.0",
"works": true
},
{
"name": "lodash",
"version": "1.0.2",
"from": "1.0.0",
"works": true
},
{
"name": "lodash",
"version": "1.1.0",
"from": "1.0.0",
"works": false
},
{
"name": "lodash",
"version": "1.1.1",
"from": "1.0.0",
"works": false
},
{
"name": "lodash",
"version": "1.2.0",
"from": "1.0.0",
"works": false
},
{
"name": "lodash",
"version": "1.2.1",
"from": "1.0.0",
"works": false
},
{
"name": "lodash",
"version": "1.3.0",
"from": "1.0.0",
"works": false
},
{
"name": "lodash",
"version": "1.3.1",
"from": "1.0.0",
"works": false
}
]

23 changes: 23 additions & 0 deletions docs/use.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,29 @@ You can skip a list of modules by name using `config` property in the `package.j
}
```

## Custom test command per module

Some modules are not really tested using the default `npm test` command or
whatever is passed via `--test "..."` from CLI. For example a linter module
should probably be tested using `npm run lint` command. You can set individual
test commands for each module to override the default test command. In the
`package.json` config object set "commands" object

```json
"config": {
"next-update": {
"commands": {
"git-issues": "npm run issues",
"standard": "npm run lint"
}
}
}
```

Then when `git-issues` module is checked by itself, it will run
`npm run issues` command; when module `standard` is tested by itself, the
test will use `npm run lint` command.

## Misc

* To see what has changed in the latest version of any module,
Expand Down
11 changes: 10 additions & 1 deletion src/test-module-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var quote = require('quote')
var installModule = require('./module-install')
var reportSuccess = require('./report').reportSuccess
var reportFailure = require('./report').reportFailure
const {getTestCommand} = require('./utils')
const path = require('path')
const debug = require('debug')('next-update')
var stats = require('./stats')

var cleanVersions = require('./registry').cleanVersions
Expand Down Expand Up @@ -106,8 +109,12 @@ function installEachTestRevert (listed, available, command, color, keep, tldr) {
verify.object(listed, 'expected listed object')
verify.array(available, 'expected array')

const packageFilename = path.resolve('./package.json')
const getCommand = _.partial(getTestCommand, packageFilename)

var checkModulesFunctions = available.map(function (nameVersion) {
var name = nameVersion.name
la(check.unemptyString(name), 'missing name', nameVersion)
var currentVersion = listed[name].version
la(check.string(currentVersion), 'cannot find current version for', name,
'among current dependencies', listed)
Expand All @@ -120,10 +127,12 @@ function installEachTestRevert (listed, available, command, color, keep, tldr) {
}
var revertFunction = installModule.bind(null, installOptions)

const testCommand = getCommand(name) || command
debug('module %s should use test command "%s"', name, testCommand)
var checkModuleFunction = testModuleVersions.bind(null, {
moduleVersions: nameVersion,
revertFunction: revertFunction,
command: command,
command: testCommand,
color: color,
currentVersion: currentVersion,
keep: keep,
Expand Down
15 changes: 14 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,21 @@ function getSkippedModules (packageFilename) {
return []
}

function getTestCommand (packageFilename, moduleName) {
la(is.unemptyString(moduleName), 'missing module name')
const config = getConfig(packageFilename)
if (!config) {
return
}
if (!config.commands) {
return
}
return config.commands[moduleName]
}

module.exports = {
name,
getConfig,
getSkippedModules
getSkippedModules,
getTestCommand
}
58 changes: 58 additions & 0 deletions test/command-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const path = require('path')
const chdir = require('chdir-promise')
const nextUpdate = require('..')
const execa = require('execa')
const snapShot = require('snap-shot')
const R = require('ramda')
const TWO_MINUTES = 120000

const testFolder = path.join(__dirname, 'test-command-map')

function prune () {
return execa.shell('npm prune')
}

function install () {
return execa.shell('npm install')
}

const flatVersionSort = R.compose(
R.sortBy(R.prop('version')),
R.flatten
)

it('sorts by version', () => {
const results = [
[ { name: 'lodash', version: '1.0.1', from: '1.0.0', works: true },
{ name: 'lodash', version: '1.1.0', from: '1.0.0', works: false },
{ name: 'lodash', version: '1.1.1', from: '1.0.0', works: false },
{ name: 'lodash', version: '1.2.0', from: '1.0.0', works: false },
{ name: 'lodash', version: '1.2.1', from: '1.0.0', works: false },
{ name: 'lodash', version: '1.3.0', from: '1.0.0', works: false },
{ name: 'lodash', version: '1.3.1', from: '1.0.0', works: false },
{ name: 'lodash', version: '1.0.2', from: '1.0.0', works: true } ]
]
snapShot(flatVersionSort(results))
})

/* global describe, beforeEach, afterEach, it */
describe('per-module configured command', () => {
beforeEach(function () {
this.timeout(TWO_MINUTES)
return chdir.to(testFolder)
.then(prune)
.then(install)
})

afterEach(chdir.back)

it('finds and uses module own test command', function () {
this.timeout(TWO_MINUTES)
const opts = {
module: 'lodash',
allow: 'minor'
}
return snapShot(nextUpdate(opts)
.then(flatVersionSort))
})
})
4 changes: 0 additions & 4 deletions test/revert-spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const path = require('path')
const fromFolder = path.join.bind(null, __dirname)
const chdir = require('chdir-promise')
const nextUpdate = require('..')
const execa = require('execa')
const snapShot = require('snap-shot')
const _ = require('lodash')
const is = require('check-more-types')
const la = require('lazy-ass')
const TWO_MINUTES = 120000

Expand Down
1 change: 1 addition & 0 deletions test/test-command-map/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=http://registry.npmjs.org/
51 changes: 51 additions & 0 deletions test/test-command-map/__snapshots__/command-spec.js.snap-shot
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
exports['finds and uses module own test command 1'] = [
{
"name": "lodash",
"version": "1.0.1",
"from": "1.0.0",
"works": true
},
{
"name": "lodash",
"version": "1.0.2",
"from": "1.0.0",
"works": true
},
{
"name": "lodash",
"version": "1.1.0",
"from": "1.0.0",
"works": false
},
{
"name": "lodash",
"version": "1.1.1",
"from": "1.0.0",
"works": false
},
{
"name": "lodash",
"version": "1.2.0",
"from": "1.0.0",
"works": false
},
{
"name": "lodash",
"version": "1.2.1",
"from": "1.0.0",
"works": false
},
{
"name": "lodash",
"version": "1.3.0",
"from": "1.0.0",
"works": false
},
{
"name": "lodash",
"version": "1.3.1",
"from": "1.0.0",
"works": false
}
]

3 changes: 3 additions & 0 deletions test/test-command-map/lodash-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const _ = require('lodash')
console.log('lodash test version %s', _.VERSION)
process.exit(_.VERSION.startsWith('1.0') ? 0 : 1)
31 changes: 31 additions & 0 deletions test/test-command-map/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "test-next-updater",
"version": "0.0.1",
"description": "Simple NPM module with dependencies for testing",
"main": "index.js",
"scripts": {
"test": "echo 'no test'",
"lodash-test": "node lodash-test.js"
},
"repository": {
"type": "git",
"url": "git@github.com:bahmutov/test-next-updater.git"
},
"keywords": [
"test",
"versions",
"updater"
],
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>",
"license": "MIT",
"dependencies": {
"lodash": "1.0.0"
},
"config": {
"next-update": {
"commands": {
"lodash": "npm run lodash-test"
}
}
}
}

0 comments on commit fa3b010

Please sign in to comment.