diff --git a/@commitlint/config-lerna-scopes/fixtures/basic/lerna.json b/@commitlint/config-lerna-scopes/fixtures/basic/lerna.json new file mode 100644 index 0000000000..1b1247feb0 --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/basic/lerna.json @@ -0,0 +1,7 @@ +{ + "lerna": "2.0.0", + "version": "1.0.0", + "packages": [ + "packages/*" + ] +} diff --git a/@commitlint/config-lerna-scopes/fixtures/basic/package.json b/@commitlint/config-lerna-scopes/fixtures/basic/package.json new file mode 100644 index 0000000000..a216580ffb --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/basic/package.json @@ -0,0 +1,7 @@ +{ + "name": "basic", + "version": "1.0.0", + "devDependencies": { + "lerna": "2.0.0" + } +} diff --git a/@commitlint/config-lerna-scopes/fixtures/basic/packages/a/package.json b/@commitlint/config-lerna-scopes/fixtures/basic/packages/a/package.json new file mode 100644 index 0000000000..5693ad0511 --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/basic/packages/a/package.json @@ -0,0 +1,4 @@ +{ + "name": "a", + "version": "1.0.0" +} diff --git a/@commitlint/config-lerna-scopes/fixtures/basic/packages/b/package.json b/@commitlint/config-lerna-scopes/fixtures/basic/packages/b/package.json new file mode 100644 index 0000000000..e63566ecae --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/basic/packages/b/package.json @@ -0,0 +1,4 @@ +{ + "name": "b", + "version": "1.0.0" +} diff --git a/@commitlint/config-lerna-scopes/fixtures/empty/lerna.json b/@commitlint/config-lerna-scopes/fixtures/empty/lerna.json new file mode 100644 index 0000000000..1b1247feb0 --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/empty/lerna.json @@ -0,0 +1,7 @@ +{ + "lerna": "2.0.0", + "version": "1.0.0", + "packages": [ + "packages/*" + ] +} diff --git a/@commitlint/config-lerna-scopes/fixtures/empty/package.json b/@commitlint/config-lerna-scopes/fixtures/empty/package.json new file mode 100644 index 0000000000..f5df380a71 --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/empty/package.json @@ -0,0 +1,7 @@ +{ + "name": "empty", + "version": "1.0.0", + "devDependencies": { + "lerna": "2.0.0" + } +} diff --git a/@commitlint/config-lerna-scopes/fixtures/lerna-2.0/lerna.json b/@commitlint/config-lerna-scopes/fixtures/lerna-2.0/lerna.json new file mode 100644 index 0000000000..1b1247feb0 --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/lerna-2.0/lerna.json @@ -0,0 +1,7 @@ +{ + "lerna": "2.0.0", + "version": "1.0.0", + "packages": [ + "packages/*" + ] +} diff --git a/@commitlint/config-lerna-scopes/fixtures/lerna-2.0/package.json b/@commitlint/config-lerna-scopes/fixtures/lerna-2.0/package.json new file mode 100644 index 0000000000..b4fb9b8fb5 --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/lerna-2.0/package.json @@ -0,0 +1,7 @@ +{ + "name": "lerna-2-0", + "version": "1.0.0", + "devDependencies": { + "lerna": "2.0.0" + } +} diff --git a/@commitlint/config-lerna-scopes/fixtures/lerna-2.0/packages/a/package.json b/@commitlint/config-lerna-scopes/fixtures/lerna-2.0/packages/a/package.json new file mode 100644 index 0000000000..5693ad0511 --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/lerna-2.0/packages/a/package.json @@ -0,0 +1,4 @@ +{ + "name": "a", + "version": "1.0.0" +} diff --git a/@commitlint/config-lerna-scopes/fixtures/lerna-2.0/packages/b/package.json b/@commitlint/config-lerna-scopes/fixtures/lerna-2.0/packages/b/package.json new file mode 100644 index 0000000000..e63566ecae --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/lerna-2.0/packages/b/package.json @@ -0,0 +1,4 @@ +{ + "name": "b", + "version": "1.0.0" +} diff --git a/@commitlint/config-lerna-scopes/fixtures/lerna-2.4/lerna.json b/@commitlint/config-lerna-scopes/fixtures/lerna-2.4/lerna.json new file mode 100644 index 0000000000..91353a207e --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/lerna-2.4/lerna.json @@ -0,0 +1,7 @@ +{ + "lerna": "2.4.0", + "version": "1.0.0", + "packages": [ + "packages/*" + ] +} diff --git a/@commitlint/config-lerna-scopes/fixtures/lerna-2.4/package.json b/@commitlint/config-lerna-scopes/fixtures/lerna-2.4/package.json new file mode 100644 index 0000000000..5f36fd2028 --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/lerna-2.4/package.json @@ -0,0 +1,7 @@ +{ + "name": "lerna-2-4", + "version": "1.0.0", + "devDependencies": { + "lerna": "2.4.0" + } +} diff --git a/@commitlint/config-lerna-scopes/fixtures/lerna-2.4/packages/a/package.json b/@commitlint/config-lerna-scopes/fixtures/lerna-2.4/packages/a/package.json new file mode 100644 index 0000000000..5693ad0511 --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/lerna-2.4/packages/a/package.json @@ -0,0 +1,4 @@ +{ + "name": "a", + "version": "1.0.0" +} diff --git a/@commitlint/config-lerna-scopes/fixtures/lerna-2.4/packages/b/package.json b/@commitlint/config-lerna-scopes/fixtures/lerna-2.4/packages/b/package.json new file mode 100644 index 0000000000..e63566ecae --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/lerna-2.4/packages/b/package.json @@ -0,0 +1,4 @@ +{ + "name": "b", + "version": "1.0.0" +} diff --git a/@commitlint/config-lerna-scopes/fixtures/scoped/@packages/a/package.json b/@commitlint/config-lerna-scopes/fixtures/scoped/@packages/a/package.json new file mode 100644 index 0000000000..bd2d94edfe --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/scoped/@packages/a/package.json @@ -0,0 +1,4 @@ +{ + "name": "@packages/a", + "version": "1.0.0" +} diff --git a/@commitlint/config-lerna-scopes/fixtures/scoped/@packages/b/package.json b/@commitlint/config-lerna-scopes/fixtures/scoped/@packages/b/package.json new file mode 100644 index 0000000000..f0c995bc44 --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/scoped/@packages/b/package.json @@ -0,0 +1,4 @@ +{ + "name": "@packages/b", + "version": "1.0.0" +} diff --git a/@commitlint/config-lerna-scopes/fixtures/scoped/lerna.json b/@commitlint/config-lerna-scopes/fixtures/scoped/lerna.json new file mode 100644 index 0000000000..07390d2f95 --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/scoped/lerna.json @@ -0,0 +1,7 @@ +{ + "lerna": "2.0.0", + "version": "1.0.0", + "packages": [ + "@packages/*" + ] +} diff --git a/@commitlint/config-lerna-scopes/fixtures/scoped/package.json b/@commitlint/config-lerna-scopes/fixtures/scoped/package.json new file mode 100644 index 0000000000..d285920b07 --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/scoped/package.json @@ -0,0 +1,7 @@ +{ + "name": "scoped", + "version": "1.0.0", + "devDependencies": { + "lerna": "2.0.0" + } +} diff --git a/@commitlint/config-lerna-scopes/fixtures/version-mismatch/lerna.json b/@commitlint/config-lerna-scopes/fixtures/version-mismatch/lerna.json new file mode 100644 index 0000000000..91353a207e --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/version-mismatch/lerna.json @@ -0,0 +1,7 @@ +{ + "lerna": "2.4.0", + "version": "1.0.0", + "packages": [ + "packages/*" + ] +} diff --git a/@commitlint/config-lerna-scopes/fixtures/version-mismatch/package.json b/@commitlint/config-lerna-scopes/fixtures/version-mismatch/package.json new file mode 100644 index 0000000000..b77252c876 --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/version-mismatch/package.json @@ -0,0 +1,7 @@ +{ + "name": "version-mismatch", + "version": "1.0.0", + "devDependencies": { + "lerna": "2.4.0" + } +} diff --git a/@commitlint/config-lerna-scopes/index.js b/@commitlint/config-lerna-scopes/index.js index a6754dab35..ffc3cb0370 100644 --- a/@commitlint/config-lerna-scopes/index.js +++ b/@commitlint/config-lerna-scopes/index.js @@ -1,20 +1,26 @@ -const Repository = require('lerna/lib/Repository'); -const PackageUtilities = require('lerna/lib/PackageUtilities'); +const importFrom = require('import-from'); module.exports = { utils: {getPackages}, rules: { - 'scope-enum': () => [2, 'always', getPackages()] + 'scope-enum': ctx => [2, 'always', getPackages(ctx)] } }; -function getPackages() { - const cwd = process.cwd(); +function getPackages(context) { + const ctx = context || {}; + const cwd = ctx.cwd || process.cwd(); + + const Repository = importFrom(cwd, 'lerna/lib/Repository'); + const PackageUtilities = importFrom(cwd, 'lerna/lib/PackageUtilities'); + const repository = new Repository(cwd); - return PackageUtilities.getPackages({ + const packages = PackageUtilities.getPackages({ packageConfigs: repository.packageConfigs, rootPath: cwd - }) + }); + + return packages .map(pkg => pkg.name) .map(name => (name.charAt(0) === '@' ? name.split('/')[1] : name)); } diff --git a/@commitlint/config-lerna-scopes/package.json b/@commitlint/config-lerna-scopes/package.json index f23ed1fe4f..41c2cedf61 100644 --- a/@commitlint/config-lerna-scopes/package.json +++ b/@commitlint/config-lerna-scopes/package.json @@ -4,10 +4,19 @@ "description": "Shareable commitlint config enforcing lerna package names as scopes", "scripts": { "pretest": "dep-check", - "test": "exit 0", + "test": "ava", "clean": "exit 0" }, "xo": false, + "ava": { + "files": [ + "test.js" + ], + "sources": [ + "index.js", + "fixtures/**/*" + ] + }, "repository": { "type": "git", "url": "git+https://github.com/marionebl/commitlint.git" @@ -25,9 +34,12 @@ }, "homepage": "https://github.com/marionebl/commitlint#readme", "dependencies": { + "import-from": "^2.1.0", "lerna": "^2.0.0" }, "devDependencies": { - "@commitlint/utils": "^3.1.1" + "@commitlint/utils": "^3.1.1", + "@commitlint/test": "^3.1.1", + "ava": "^0.22.0" } } diff --git a/@commitlint/config-lerna-scopes/test.js b/@commitlint/config-lerna-scopes/test.js new file mode 100644 index 0000000000..30126e0655 --- /dev/null +++ b/@commitlint/config-lerna-scopes/test.js @@ -0,0 +1,81 @@ +import {npm} from '@commitlint/test'; +import test from 'ava'; +import config from '.'; + +test('exports rules key', t => { + t.true(Object.prototype.hasOwnProperty.call(config, 'rules')); +}); + +test('rules hold object', t => { + t.is(typeof config.rules, 'object'); +}); + +test('rules contain scope-enum', t => { + t.true(Object.prototype.hasOwnProperty.call(config.rules, 'scope-enum')); +}); + +test('scope-enum is function', t => { + const {'scope-enum': fn} = config.rules; + t.is(typeof fn, 'function'); +}); + +test('scope-enum does not throw for missing context', t => { + const {'scope-enum': fn} = config.rules; + t.notThrows(() => fn()); +}); + +test('scope-enum has expected severity', t => { + const {'scope-enum': fn} = config.rules; + const [severity] = fn(); + t.is(severity, 2); +}); + +test('scope-enum has expected modifier', t => { + const {'scope-enum': fn} = config.rules; + const [, modifier] = fn(); + t.is(modifier, 'always'); +}); + +test('returns empty value for empty lerna repository', async t => { + const {'scope-enum': fn} = config.rules; + const cwd = await npm.bootstrap('fixtures/empty'); + const [, , value] = fn({cwd}); + t.deepEqual(value, []); +}); + +test('returns expected value for basic lerna repository', async t => { + const {'scope-enum': fn} = config.rules; + const cwd = await npm.bootstrap('fixtures/basic'); + const [, , value] = fn({cwd}); + t.deepEqual(value, ['a', 'b']); +}); + +test.failing( + 'throws for repository with .lerna vs .devDependencies.lerna mismatch', + async t => { + const {'scope-enum': fn} = config.rules; + const cwd = await npm.bootstrap('fixtures/version-mismatch'); + await t.throws(() => fn({cwd})); + } +); + +test('returns expected value for scoped lerna repository', async t => { + const {'scope-enum': fn} = config.rules; + const cwd = await npm.bootstrap('fixtures/scoped'); + const [, , value] = fn({cwd}); + t.deepEqual(value, ['a', 'b']); +}); + +test('works with lerna 2.0', async t => { + const {'scope-enum': fn} = config.rules; + const cwd = await npm.bootstrap('fixtures/lerna-2.4'); + const [, , value] = fn({cwd}); + t.deepEqual(value, ['a', 'b']); +}); + +test('works with lerna 2.4', async t => { + const {'scope-enum': fn} = config.rules; + const cwd = await npm.bootstrap('fixtures/lerna-2.4'); + const [, , value] = fn({cwd}); + t.deepEqual(value, ['a', 'b']); +}); diff --git a/@commitlint/core/package.json b/@commitlint/core/package.json index 90c5d0aeb0..9b4e7d326e 100644 --- a/@commitlint/core/package.json +++ b/@commitlint/core/package.json @@ -62,9 +62,9 @@ }, "license": "MIT", "devDependencies": { - "@commitlint/utils": "^3.1.1", "@commitlint/test": "^3.1.1", - "ava": "0.22.0", + "@commitlint/utils": "^3.1.1", + "ava": "^0.22.0", "babel-cli": "^6.26.0", "babel-preset-commitlint": "^3.2.0", "babel-register": "^6.26.0", diff --git a/@commitlint/test/src/index.js b/@commitlint/test/src/index.js index e6c1e6ba74..f2848c67e6 100644 --- a/@commitlint/test/src/index.js +++ b/@commitlint/test/src/index.js @@ -1,3 +1,4 @@ import * as git from './git'; +import * as npm from './npm'; -export {git}; +export {git, npm}; diff --git a/@commitlint/test/src/npm.js b/@commitlint/test/src/npm.js new file mode 100644 index 0000000000..1750263005 --- /dev/null +++ b/@commitlint/test/src/npm.js @@ -0,0 +1,16 @@ +import * as sander from '@marionebl/sander'; +import execa from 'execa'; + +import * as git from './git'; + +export {bootstrap}; + +async function bootstrap(fixture) { + const cwd = await git.bootstrap(fixture); + + if (await sander.exists(cwd, 'package.json')) { + await execa('npm', ['install'], {cwd}); + } + + return cwd; +} diff --git a/package.json b/package.json index b0b3e428b9..9ab7375163 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,8 @@ "@commitlint/**/node_modules" ], "rules": { - "import/prefer-default-export": "off" + "import/prefer-default-export": "off", + "ava/no-ignored-test-files": "off" } }, "engines": {