Skip to content

Commit fa3b010

Browse files
committed
feat(test): allow per-module specific command, close #102
1 parent cb105d1 commit fa3b010

File tree

11 files changed

+265
-6
lines changed

11 files changed

+265
-6
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,29 @@ You can skip a list of modules by name using `config` property in the `package.j
183183
}
184184
```
185185

186+
### Custom test command per module
187+
188+
Some modules are not really tested using the default `npm test` command or
189+
whatever is passed via `--test "..."` from CLI. For example a linter module
190+
should probably be tested using `npm run lint` command. You can set individual
191+
test commands for each module to override the default test command. In the
192+
`package.json` config object set "commands" object
193+
194+
```json
195+
"config": {
196+
"next-update": {
197+
"commands": {
198+
"git-issues": "npm run issues",
199+
"standard": "npm run lint"
200+
}
201+
}
202+
}
203+
```
204+
205+
Then when `git-issues` module is checked by itself, it will run
206+
`npm run issues` command; when module `standard` is tested by itself, the
207+
test will use `npm run lint` command.
208+
186209
### Misc
187210

188211
* To see what has changed in the latest version of any module,
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
exports['sorts by version 1'] = [
2+
{
3+
"name": "lodash",
4+
"version": "1.0.1",
5+
"from": "1.0.0",
6+
"works": true
7+
},
8+
{
9+
"name": "lodash",
10+
"version": "1.0.2",
11+
"from": "1.0.0",
12+
"works": true
13+
},
14+
{
15+
"name": "lodash",
16+
"version": "1.1.0",
17+
"from": "1.0.0",
18+
"works": false
19+
},
20+
{
21+
"name": "lodash",
22+
"version": "1.1.1",
23+
"from": "1.0.0",
24+
"works": false
25+
},
26+
{
27+
"name": "lodash",
28+
"version": "1.2.0",
29+
"from": "1.0.0",
30+
"works": false
31+
},
32+
{
33+
"name": "lodash",
34+
"version": "1.2.1",
35+
"from": "1.0.0",
36+
"works": false
37+
},
38+
{
39+
"name": "lodash",
40+
"version": "1.3.0",
41+
"from": "1.0.0",
42+
"works": false
43+
},
44+
{
45+
"name": "lodash",
46+
"version": "1.3.1",
47+
"from": "1.0.0",
48+
"works": false
49+
}
50+
]
51+

docs/use.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,29 @@ You can skip a list of modules by name using `config` property in the `package.j
153153
}
154154
```
155155

156+
## Custom test command per module
157+
158+
Some modules are not really tested using the default `npm test` command or
159+
whatever is passed via `--test "..."` from CLI. For example a linter module
160+
should probably be tested using `npm run lint` command. You can set individual
161+
test commands for each module to override the default test command. In the
162+
`package.json` config object set "commands" object
163+
164+
```json
165+
"config": {
166+
"next-update": {
167+
"commands": {
168+
"git-issues": "npm run issues",
169+
"standard": "npm run lint"
170+
}
171+
}
172+
}
173+
```
174+
175+
Then when `git-issues` module is checked by itself, it will run
176+
`npm run issues` command; when module `standard` is tested by itself, the
177+
test will use `npm run lint` command.
178+
156179
## Misc
157180

158181
* To see what has changed in the latest version of any module,

src/test-module-version.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ var quote = require('quote')
88
var installModule = require('./module-install')
99
var reportSuccess = require('./report').reportSuccess
1010
var reportFailure = require('./report').reportFailure
11+
const {getTestCommand} = require('./utils')
12+
const path = require('path')
13+
const debug = require('debug')('next-update')
1114
var stats = require('./stats')
1215

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

112+
const packageFilename = path.resolve('./package.json')
113+
const getCommand = _.partial(getTestCommand, packageFilename)
114+
109115
var checkModulesFunctions = available.map(function (nameVersion) {
110116
var name = nameVersion.name
117+
la(check.unemptyString(name), 'missing name', nameVersion)
111118
var currentVersion = listed[name].version
112119
la(check.string(currentVersion), 'cannot find current version for', name,
113120
'among current dependencies', listed)
@@ -120,10 +127,12 @@ function installEachTestRevert (listed, available, command, color, keep, tldr) {
120127
}
121128
var revertFunction = installModule.bind(null, installOptions)
122129

130+
const testCommand = getCommand(name) || command
131+
debug('module %s should use test command "%s"', name, testCommand)
123132
var checkModuleFunction = testModuleVersions.bind(null, {
124133
moduleVersions: nameVersion,
125134
revertFunction: revertFunction,
126-
command: command,
135+
command: testCommand,
127136
color: color,
128137
currentVersion: currentVersion,
129138
keep: keep,

src/utils.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,21 @@ function getSkippedModules (packageFilename) {
2828
return []
2929
}
3030

31+
function getTestCommand (packageFilename, moduleName) {
32+
la(is.unemptyString(moduleName), 'missing module name')
33+
const config = getConfig(packageFilename)
34+
if (!config) {
35+
return
36+
}
37+
if (!config.commands) {
38+
return
39+
}
40+
return config.commands[moduleName]
41+
}
42+
3143
module.exports = {
3244
name,
3345
getConfig,
34-
getSkippedModules
46+
getSkippedModules,
47+
getTestCommand
3548
}

test/command-spec.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const path = require('path')
2+
const chdir = require('chdir-promise')
3+
const nextUpdate = require('..')
4+
const execa = require('execa')
5+
const snapShot = require('snap-shot')
6+
const R = require('ramda')
7+
const TWO_MINUTES = 120000
8+
9+
const testFolder = path.join(__dirname, 'test-command-map')
10+
11+
function prune () {
12+
return execa.shell('npm prune')
13+
}
14+
15+
function install () {
16+
return execa.shell('npm install')
17+
}
18+
19+
const flatVersionSort = R.compose(
20+
R.sortBy(R.prop('version')),
21+
R.flatten
22+
)
23+
24+
it('sorts by version', () => {
25+
const results = [
26+
[ { name: 'lodash', version: '1.0.1', from: '1.0.0', works: true },
27+
{ name: 'lodash', version: '1.1.0', from: '1.0.0', works: false },
28+
{ name: 'lodash', version: '1.1.1', from: '1.0.0', works: false },
29+
{ name: 'lodash', version: '1.2.0', from: '1.0.0', works: false },
30+
{ name: 'lodash', version: '1.2.1', from: '1.0.0', works: false },
31+
{ name: 'lodash', version: '1.3.0', from: '1.0.0', works: false },
32+
{ name: 'lodash', version: '1.3.1', from: '1.0.0', works: false },
33+
{ name: 'lodash', version: '1.0.2', from: '1.0.0', works: true } ]
34+
]
35+
snapShot(flatVersionSort(results))
36+
})
37+
38+
/* global describe, beforeEach, afterEach, it */
39+
describe('per-module configured command', () => {
40+
beforeEach(function () {
41+
this.timeout(TWO_MINUTES)
42+
return chdir.to(testFolder)
43+
.then(prune)
44+
.then(install)
45+
})
46+
47+
afterEach(chdir.back)
48+
49+
it('finds and uses module own test command', function () {
50+
this.timeout(TWO_MINUTES)
51+
const opts = {
52+
module: 'lodash',
53+
allow: 'minor'
54+
}
55+
return snapShot(nextUpdate(opts)
56+
.then(flatVersionSort))
57+
})
58+
})

test/revert-spec.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
const path = require('path')
22
const fromFolder = path.join.bind(null, __dirname)
33
const chdir = require('chdir-promise')
4-
const nextUpdate = require('..')
54
const execa = require('execa')
6-
const snapShot = require('snap-shot')
7-
const _ = require('lodash')
8-
const is = require('check-more-types')
95
const la = require('lazy-ass')
106
const TWO_MINUTES = 120000
117

test/test-command-map/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
registry=http://registry.npmjs.org/
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
exports['finds and uses module own test command 1'] = [
2+
{
3+
"name": "lodash",
4+
"version": "1.0.1",
5+
"from": "1.0.0",
6+
"works": true
7+
},
8+
{
9+
"name": "lodash",
10+
"version": "1.0.2",
11+
"from": "1.0.0",
12+
"works": true
13+
},
14+
{
15+
"name": "lodash",
16+
"version": "1.1.0",
17+
"from": "1.0.0",
18+
"works": false
19+
},
20+
{
21+
"name": "lodash",
22+
"version": "1.1.1",
23+
"from": "1.0.0",
24+
"works": false
25+
},
26+
{
27+
"name": "lodash",
28+
"version": "1.2.0",
29+
"from": "1.0.0",
30+
"works": false
31+
},
32+
{
33+
"name": "lodash",
34+
"version": "1.2.1",
35+
"from": "1.0.0",
36+
"works": false
37+
},
38+
{
39+
"name": "lodash",
40+
"version": "1.3.0",
41+
"from": "1.0.0",
42+
"works": false
43+
},
44+
{
45+
"name": "lodash",
46+
"version": "1.3.1",
47+
"from": "1.0.0",
48+
"works": false
49+
}
50+
]
51+

test/test-command-map/lodash-test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const _ = require('lodash')
2+
console.log('lodash test version %s', _.VERSION)
3+
process.exit(_.VERSION.startsWith('1.0') ? 0 : 1)

test/test-command-map/package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "test-next-updater",
3+
"version": "0.0.1",
4+
"description": "Simple NPM module with dependencies for testing",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo 'no test'",
8+
"lodash-test": "node lodash-test.js"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git@github.com:bahmutov/test-next-updater.git"
13+
},
14+
"keywords": [
15+
"test",
16+
"versions",
17+
"updater"
18+
],
19+
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>",
20+
"license": "MIT",
21+
"dependencies": {
22+
"lodash": "1.0.0"
23+
},
24+
"config": {
25+
"next-update": {
26+
"commands": {
27+
"lodash": "npm run lodash-test"
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)