Skip to content

Commit 8921083

Browse files
committed
test: cover with unit and integration tests
1 parent 1e80f80 commit 8921083

File tree

27 files changed

+246
-13
lines changed

27 files changed

+246
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"lerna": "2.0.0",
3+
"version": "1.0.0",
4+
"packages": [
5+
"packages/*"
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "basic",
3+
"version": "1.0.0",
4+
"devDependencies": {
5+
"lerna": "2.0.0"
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "a",
3+
"version": "1.0.0"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "b",
3+
"version": "1.0.0"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"lerna": "2.0.0",
3+
"version": "1.0.0",
4+
"packages": [
5+
"packages/*"
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "empty",
3+
"version": "1.0.0",
4+
"devDependencies": {
5+
"lerna": "2.0.0"
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"lerna": "2.0.0",
3+
"version": "1.0.0",
4+
"packages": [
5+
"packages/*"
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "lerna-2-0",
3+
"version": "1.0.0",
4+
"devDependencies": {
5+
"lerna": "2.0.0"
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "a",
3+
"version": "1.0.0"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "b",
3+
"version": "1.0.0"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"lerna": "2.4.0",
3+
"version": "1.0.0",
4+
"packages": [
5+
"packages/*"
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "lerna-2-4",
3+
"version": "1.0.0",
4+
"devDependencies": {
5+
"lerna": "2.4.0"
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "a",
3+
"version": "1.0.0"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "b",
3+
"version": "1.0.0"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "@packages/a",
3+
"version": "1.0.0"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "@packages/b",
3+
"version": "1.0.0"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"lerna": "2.0.0",
3+
"version": "1.0.0",
4+
"packages": [
5+
"@packages/*"
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "scoped",
3+
"version": "1.0.0",
4+
"devDependencies": {
5+
"lerna": "2.0.0"
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"lerna": "2.4.0",
3+
"version": "1.0.0",
4+
"packages": [
5+
"packages/*"
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "version-mismatch",
3+
"version": "1.0.0",
4+
"devDependencies": {
5+
"lerna": "2.4.0"
6+
}
7+
}
+13-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
const Repository = require('lerna/lib/Repository');
2-
const PackageUtilities = require('lerna/lib/PackageUtilities');
1+
const importFrom = require('import-from');
32

43
module.exports = {
54
utils: {getPackages},
65
rules: {
7-
'scope-enum': () => [2, 'always', getPackages()]
6+
'scope-enum': ctx => [2, 'always', getPackages(ctx)]
87
}
98
};
109

11-
function getPackages() {
12-
const cwd = process.cwd();
10+
function getPackages(context) {
11+
const ctx = context || {};
12+
const cwd = ctx.cwd || process.cwd();
13+
14+
const Repository = importFrom(cwd, 'lerna/lib/Repository');
15+
const PackageUtilities = importFrom(cwd, 'lerna/lib/PackageUtilities');
16+
1317
const repository = new Repository(cwd);
14-
return PackageUtilities.getPackages({
18+
const packages = PackageUtilities.getPackages({
1519
packageConfigs: repository.packageConfigs,
1620
rootPath: cwd
17-
})
21+
});
22+
23+
return packages
1824
.map(pkg => pkg.name)
1925
.map(name => (name.charAt(0) === '@' ? name.split('/')[1] : name));
2026
}

@commitlint/config-lerna-scopes/package.json

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@
44
"description": "Shareable commitlint config enforcing lerna package names as scopes",
55
"scripts": {
66
"pretest": "dep-check",
7-
"test": "exit 0",
7+
"test": "ava",
88
"clean": "exit 0"
99
},
1010
"xo": false,
11+
"ava": {
12+
"files": [
13+
"test.js"
14+
],
15+
"sources": [
16+
"index.js",
17+
"fixtures/**/*"
18+
]
19+
},
1120
"repository": {
1221
"type": "git",
1322
"url": "git+https://github.com/marionebl/commitlint.git"
@@ -25,9 +34,12 @@
2534
},
2635
"homepage": "https://github.com/marionebl/commitlint#readme",
2736
"dependencies": {
37+
"import-from": "^2.1.0",
2838
"lerna": "^2.0.0"
2939
},
3040
"devDependencies": {
31-
"@commitlint/utils": "^3.1.1"
41+
"@commitlint/utils": "^3.1.1",
42+
"@commitlint/test": "^3.1.1",
43+
"ava": "^0.22.0"
3244
}
3345
}
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import {npm} from '@commitlint/test';
2+
import test from 'ava';
3+
import config from '.';
4+
5+
test('exports rules key', t => {
6+
t.true(Object.prototype.hasOwnProperty.call(config, 'rules'));
7+
});
8+
9+
test('rules hold object', t => {
10+
t.is(typeof config.rules, 'object');
11+
});
12+
13+
test('rules contain scope-enum', t => {
14+
t.true(Object.prototype.hasOwnProperty.call(config.rules, 'scope-enum'));
15+
});
16+
17+
test('scope-enum is function', t => {
18+
const {'scope-enum': fn} = config.rules;
19+
t.is(typeof fn, 'function');
20+
});
21+
22+
test('scope-enum does not throw for missing context', t => {
23+
const {'scope-enum': fn} = config.rules;
24+
t.notThrows(() => fn());
25+
});
26+
27+
test('scope-enum has expected severity', t => {
28+
const {'scope-enum': fn} = config.rules;
29+
const [severity] = fn();
30+
t.is(severity, 2);
31+
});
32+
33+
test('scope-enum has expected modifier', t => {
34+
const {'scope-enum': fn} = config.rules;
35+
const [, modifier] = fn();
36+
t.is(modifier, 'always');
37+
});
38+
39+
test('returns empty value for empty lerna repository', async t => {
40+
const {'scope-enum': fn} = config.rules;
41+
const cwd = await npm.bootstrap('fixtures/empty');
42+
const [, , value] = fn({cwd});
43+
t.deepEqual(value, []);
44+
});
45+
46+
test('returns expected value for basic lerna repository', async t => {
47+
const {'scope-enum': fn} = config.rules;
48+
const cwd = await npm.bootstrap('fixtures/basic');
49+
const [, , value] = fn({cwd});
50+
t.deepEqual(value, ['a', 'b']);
51+
});
52+
53+
test.failing(
54+
'throws for repository with .lerna vs .devDependencies.lerna mismatch',
55+
async t => {
56+
const {'scope-enum': fn} = config.rules;
57+
const cwd = await npm.bootstrap('fixtures/version-mismatch');
58+
await t.throws(() => fn({cwd}));
59+
}
60+
);
61+
62+
test('returns expected value for scoped lerna repository', async t => {
63+
const {'scope-enum': fn} = config.rules;
64+
const cwd = await npm.bootstrap('fixtures/scoped');
65+
const [, , value] = fn({cwd});
66+
t.deepEqual(value, ['a', 'b']);
67+
});
68+
69+
test('works with lerna 2.0', async t => {
70+
const {'scope-enum': fn} = config.rules;
71+
const cwd = await npm.bootstrap('fixtures/lerna-2.4');
72+
const [, , value] = fn({cwd});
73+
t.deepEqual(value, ['a', 'b']);
74+
});
75+
76+
test('works with lerna 2.4', async t => {
77+
const {'scope-enum': fn} = config.rules;
78+
const cwd = await npm.bootstrap('fixtures/lerna-2.4');
79+
const [, , value] = fn({cwd});
80+
t.deepEqual(value, ['a', 'b']);
81+
});

@commitlint/core/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@
6262
},
6363
"license": "MIT",
6464
"devDependencies": {
65-
"@commitlint/utils": "^3.1.1",
6665
"@commitlint/test": "^3.1.1",
67-
"ava": "0.22.0",
66+
"@commitlint/utils": "^3.1.1",
67+
"ava": "^0.22.0",
6868
"babel-cli": "^6.26.0",
6969
"babel-preset-commitlint": "^3.2.0",
7070
"babel-register": "^6.26.0",

@commitlint/test/src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import * as git from './git';
2+
import * as npm from './npm';
23

3-
export {git};
4+
export {git, npm};

@commitlint/test/src/npm.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as sander from '@marionebl/sander';
2+
import execa from 'execa';
3+
4+
import * as git from './git';
5+
6+
export {bootstrap};
7+
8+
async function bootstrap(fixture) {
9+
const cwd = await git.bootstrap(fixture);
10+
11+
if (await sander.exists(cwd, 'package.json')) {
12+
await execa('npm', ['install'], {cwd});
13+
}
14+
15+
return cwd;
16+
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"@commitlint/**/node_modules"
3434
],
3535
"rules": {
36-
"import/prefer-default-export": "off"
36+
"import/prefer-default-export": "off",
37+
"ava/no-ignored-test-files": "off"
3738
}
3839
},
3940
"engines": {

0 commit comments

Comments
 (0)