-
Notifications
You must be signed in to change notification settings - Fork 915
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): determine git root correctly in sub directories (#64)
* test(cli): add failing integration test * fix: determine git root correctly from sub directories * fixup! make git initializing work fixes #62
- Loading branch information
Showing
5 changed files
with
117 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,135 @@ | ||
import path from 'path'; | ||
import test from 'ava'; | ||
import execa from 'execa'; | ||
import {sync as bin} from 'resolve-bin'; | ||
import * as sander from 'sander'; | ||
import stream from 'string-to-stream'; | ||
import tmp from 'tmp'; | ||
|
||
const here = path.join.bind(null, __dirname); | ||
const fix = here.bind(null, 'fixtures'); | ||
|
||
const SIMPLE = here('fixtures/simple'); | ||
const EXTENDS_ROOT = here('fixtures/extends-root'); | ||
const EMPTY = here('fixtures/empty'); | ||
|
||
const cli = (input = '', args = [], opts = {}) => { | ||
const c = execa(here('cli.js'), args, { | ||
capture: ['stdout'], | ||
cwd: opts.cwd | ||
}); | ||
stream(input).pipe(c.stdin); | ||
return c; | ||
const CLI = here('cli.js'); | ||
const SIMPLE = fix('simple'); | ||
const EXTENDS_ROOT = fix('extends-root'); | ||
const EMPTY = fix('empty'); | ||
|
||
const HUSKY = tmp.dirSync().name; | ||
const HUSKY_INTEGRATION = path.join(tmp.dirSync().name, 'integration'); | ||
|
||
const exec = (command, args = [], opts = {}) => { | ||
return async (input = '') => { | ||
const c = execa(command, args, { | ||
capture: ['stdout'], | ||
cwd: opts.cwd | ||
}); | ||
stream(input).pipe(c.stdin); | ||
const result = await c; | ||
if (result.code !== 0) { | ||
console.log(result.stderr); | ||
} | ||
return result; | ||
} | ||
}; | ||
|
||
const cli = exec.bind(null, CLI); | ||
const git = exec.bind(null, 'git'); | ||
const mkdir = exec.bind(null, bin('mkdirp')); | ||
const npm = exec.bind(null, 'npm'); | ||
const rm = exec.bind(null, bin('rimraf')); | ||
|
||
test('should throw when called without [input]', t => { | ||
t.throws(cli(), /Expected a raw commit/); | ||
t.throws(cli()(), /Expected a raw commit/); | ||
}); | ||
|
||
test('should reprint input from stdin', async t => { | ||
const actual = await cli('foo: bar', [], {cwd: EMPTY}); | ||
const actual = await cli([], {cwd: EMPTY})('foo: bar'); | ||
t.true(actual.stdout.includes('foo: bar')); | ||
}); | ||
|
||
test('should produce no success output with --quiet flag', async t => { | ||
const actual = await cli('foo: bar', ['--quiet'], {cwd: EMPTY}); | ||
const actual = await cli(['--quiet'], {cwd: EMPTY})('foo: bar'); | ||
t.is(actual.stdout, ''); | ||
t.is(actual.stderr, ''); | ||
}); | ||
|
||
test('should produce no success output with -q flag', async t => { | ||
const actual = await cli('foo: bar', ['-q'], {cwd: EMPTY}); | ||
const actual = await cli(['-q'], {cwd: EMPTY})('foo: bar'); | ||
t.is(actual.stdout, ''); | ||
t.is(actual.stderr, ''); | ||
}); | ||
|
||
test('should succeed for input from stdin without rules', async t => { | ||
const actual = await cli('foo: bar', [], {cwd: EMPTY}); | ||
const actual = await cli([], {cwd: EMPTY})('foo: bar'); | ||
t.is(actual.code, 0); | ||
}); | ||
|
||
test('should fail for input from stdin with rule from rc', async t => { | ||
const actual = await t.throws(cli('foo: bar', [], {cwd: SIMPLE})); | ||
const actual = await t.throws(cli([], {cwd: SIMPLE})('foo: bar')); | ||
t.true(actual.stdout.includes('type must not be one of [foo]')); | ||
t.is(actual.code, 1); | ||
}); | ||
|
||
test('should fail for input from stdin with rule from js', async t => { | ||
const actual = await t.throws( | ||
cli('foo: bar', ['--extends', './extended'], {cwd: EXTENDS_ROOT}) | ||
cli(['--extends', './extended'], {cwd: EXTENDS_ROOT})('foo: bar') | ||
); | ||
t.true(actual.stdout.includes('type must not be one of [foo]')); | ||
t.is(actual.code, 1); | ||
}); | ||
|
||
test('should produce no error output with --quiet flag', async t => { | ||
const actual = await t.throws(cli('foo: bar', ['--quiet'], {cwd: SIMPLE})); | ||
const actual = await t.throws(cli(['--quiet'], {cwd: SIMPLE})('foo: bar')); | ||
t.is(actual.stdout, ''); | ||
t.is(actual.stderr, ''); | ||
t.is(actual.code, 1); | ||
}); | ||
|
||
test('should produce no error output with -q flag', async t => { | ||
const actual = await t.throws(cli('foo: bar', ['-q'], {cwd: SIMPLE})); | ||
const actual = await t.throws(cli(['-q'], {cwd: SIMPLE})('foo: bar')); | ||
t.is(actual.stdout, ''); | ||
t.is(actual.stderr, ''); | ||
t.is(actual.code, 1); | ||
}); | ||
|
||
test('should work with husky commitmsg hook', async () => { | ||
const cwd = HUSKY; | ||
|
||
await init(cwd); | ||
await pkg(cwd); | ||
|
||
await npm(['install', 'husky'], {cwd})(); | ||
await git(['add', 'package.json'], {cwd})(); | ||
await git(['commit', '-m', '"chore: this should work"'], {cwd})(); | ||
|
||
await rm([HUSKY])(); | ||
}); | ||
|
||
test('should work with husky commitmsg hook in sub packages', async () => { | ||
const cwd = HUSKY_INTEGRATION; | ||
const upper = path.dirname(HUSKY_INTEGRATION); | ||
|
||
await mkdir([cwd])(); | ||
await init(upper); | ||
await pkg(cwd); | ||
|
||
await npm(['install', 'husky'], {cwd})(); | ||
await git(['add', 'package.json'], {cwd})(); | ||
|
||
await git(['commit', '-m', '"chore: this should work"'], {cwd})(); | ||
|
||
await rm([upper])(); | ||
}); | ||
|
||
async function init(cwd) { | ||
await git(['init'], {cwd})(); | ||
|
||
return Promise.all([ | ||
git(['config', 'user.email', '"[email protected]"'], {cwd})(), | ||
git(['config', 'user.name', '"commitlint"'], {cwd})() | ||
]); | ||
} | ||
|
||
function pkg(cwd) { | ||
return sander.writeFile(cwd, 'package.json', JSON.stringify({scripts: {commitmsg: `${CLI} -e`}})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"scripts":{"commitmsg":"commitlint -e"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters