|
1 | 1 | import path from 'path';
|
2 | 2 | import test from 'ava';
|
3 | 3 | import execa from 'execa';
|
| 4 | +import {sync as bin} from 'resolve-bin'; |
| 5 | +import * as sander from 'sander'; |
4 | 6 | import stream from 'string-to-stream';
|
| 7 | +import tmp from 'tmp'; |
5 | 8 |
|
6 | 9 | const here = path.join.bind(null, __dirname);
|
| 10 | +const fix = here.bind(null, 'fixtures'); |
7 | 11 |
|
8 |
| -const SIMPLE = here('fixtures/simple'); |
9 |
| -const EXTENDS_ROOT = here('fixtures/extends-root'); |
10 |
| -const EMPTY = here('fixtures/empty'); |
11 |
| - |
12 |
| -const cli = (input = '', args = [], opts = {}) => { |
13 |
| - const c = execa(here('cli.js'), args, { |
14 |
| - capture: ['stdout'], |
15 |
| - cwd: opts.cwd |
16 |
| - }); |
17 |
| - stream(input).pipe(c.stdin); |
18 |
| - return c; |
| 12 | +const CLI = here('cli.js'); |
| 13 | +const SIMPLE = fix('simple'); |
| 14 | +const EXTENDS_ROOT = fix('extends-root'); |
| 15 | +const EMPTY = fix('empty'); |
| 16 | + |
| 17 | +const HUSKY = tmp.dirSync().name; |
| 18 | +const HUSKY_INTEGRATION = path.join(tmp.dirSync().name, 'integration'); |
| 19 | + |
| 20 | +const exec = (command, args = [], opts = {}) => { |
| 21 | + return async (input = '') => { |
| 22 | + const c = execa(command, args, { |
| 23 | + capture: ['stdout'], |
| 24 | + cwd: opts.cwd |
| 25 | + }); |
| 26 | + stream(input).pipe(c.stdin); |
| 27 | + const result = await c; |
| 28 | + if (result.code !== 0) { |
| 29 | + console.log(result.stderr); |
| 30 | + } |
| 31 | + return result; |
| 32 | + } |
19 | 33 | };
|
20 | 34 |
|
| 35 | +const cli = exec.bind(null, CLI); |
| 36 | +const git = exec.bind(null, 'git'); |
| 37 | +const mkdir = exec.bind(null, bin('mkdirp')); |
| 38 | +const npm = exec.bind(null, 'npm'); |
| 39 | +const rm = exec.bind(null, bin('rimraf')); |
| 40 | + |
21 | 41 | test('should throw when called without [input]', t => {
|
22 |
| - t.throws(cli(), /Expected a raw commit/); |
| 42 | + t.throws(cli()(), /Expected a raw commit/); |
23 | 43 | });
|
24 | 44 |
|
25 | 45 | test('should reprint input from stdin', async t => {
|
26 |
| - const actual = await cli('foo: bar', [], {cwd: EMPTY}); |
| 46 | + const actual = await cli([], {cwd: EMPTY})('foo: bar'); |
27 | 47 | t.true(actual.stdout.includes('foo: bar'));
|
28 | 48 | });
|
29 | 49 |
|
30 | 50 | test('should produce no success output with --quiet flag', async t => {
|
31 |
| - const actual = await cli('foo: bar', ['--quiet'], {cwd: EMPTY}); |
| 51 | + const actual = await cli(['--quiet'], {cwd: EMPTY})('foo: bar'); |
32 | 52 | t.is(actual.stdout, '');
|
33 | 53 | t.is(actual.stderr, '');
|
34 | 54 | });
|
35 | 55 |
|
36 | 56 | test('should produce no success output with -q flag', async t => {
|
37 |
| - const actual = await cli('foo: bar', ['-q'], {cwd: EMPTY}); |
| 57 | + const actual = await cli(['-q'], {cwd: EMPTY})('foo: bar'); |
38 | 58 | t.is(actual.stdout, '');
|
39 | 59 | t.is(actual.stderr, '');
|
40 | 60 | });
|
41 | 61 |
|
42 | 62 | test('should succeed for input from stdin without rules', async t => {
|
43 |
| - const actual = await cli('foo: bar', [], {cwd: EMPTY}); |
| 63 | + const actual = await cli([], {cwd: EMPTY})('foo: bar'); |
44 | 64 | t.is(actual.code, 0);
|
45 | 65 | });
|
46 | 66 |
|
47 | 67 | test('should fail for input from stdin with rule from rc', async t => {
|
48 |
| - const actual = await t.throws(cli('foo: bar', [], {cwd: SIMPLE})); |
| 68 | + const actual = await t.throws(cli([], {cwd: SIMPLE})('foo: bar')); |
49 | 69 | t.true(actual.stdout.includes('type must not be one of [foo]'));
|
50 | 70 | t.is(actual.code, 1);
|
51 | 71 | });
|
52 | 72 |
|
53 | 73 | test('should fail for input from stdin with rule from js', async t => {
|
54 | 74 | const actual = await t.throws(
|
55 |
| - cli('foo: bar', ['--extends', './extended'], {cwd: EXTENDS_ROOT}) |
| 75 | + cli(['--extends', './extended'], {cwd: EXTENDS_ROOT})('foo: bar') |
56 | 76 | );
|
57 | 77 | t.true(actual.stdout.includes('type must not be one of [foo]'));
|
58 | 78 | t.is(actual.code, 1);
|
59 | 79 | });
|
60 | 80 |
|
61 | 81 | test('should produce no error output with --quiet flag', async t => {
|
62 |
| - const actual = await t.throws(cli('foo: bar', ['--quiet'], {cwd: SIMPLE})); |
| 82 | + const actual = await t.throws(cli(['--quiet'], {cwd: SIMPLE})('foo: bar')); |
63 | 83 | t.is(actual.stdout, '');
|
64 | 84 | t.is(actual.stderr, '');
|
65 | 85 | t.is(actual.code, 1);
|
66 | 86 | });
|
67 | 87 |
|
68 | 88 | test('should produce no error output with -q flag', async t => {
|
69 |
| - const actual = await t.throws(cli('foo: bar', ['-q'], {cwd: SIMPLE})); |
| 89 | + const actual = await t.throws(cli(['-q'], {cwd: SIMPLE})('foo: bar')); |
70 | 90 | t.is(actual.stdout, '');
|
71 | 91 | t.is(actual.stderr, '');
|
72 | 92 | t.is(actual.code, 1);
|
73 | 93 | });
|
| 94 | + |
| 95 | +test('should work with husky commitmsg hook', async () => { |
| 96 | + const cwd = HUSKY; |
| 97 | + |
| 98 | + await init(cwd); |
| 99 | + await pkg(cwd); |
| 100 | + |
| 101 | + await npm(['install', 'husky'], {cwd})(); |
| 102 | + await git(['add', 'package.json'], {cwd})(); |
| 103 | + await git(['commit', '-m', '"chore: this should work"'], {cwd})(); |
| 104 | + |
| 105 | + await rm([HUSKY])(); |
| 106 | +}); |
| 107 | + |
| 108 | +test('should work with husky commitmsg hook in sub packages', async () => { |
| 109 | + const cwd = HUSKY_INTEGRATION; |
| 110 | + const upper = path.dirname(HUSKY_INTEGRATION); |
| 111 | + |
| 112 | + await mkdir([cwd])(); |
| 113 | + await init(upper); |
| 114 | + await pkg(cwd); |
| 115 | + |
| 116 | + await npm(['install', 'husky'], {cwd})(); |
| 117 | + await git(['add', 'package.json'], {cwd})(); |
| 118 | + |
| 119 | + await git(['commit', '-m', '"chore: this should work"'], {cwd})(); |
| 120 | + |
| 121 | + await rm([upper])(); |
| 122 | +}); |
| 123 | + |
| 124 | +async function init(cwd) { |
| 125 | + await git(['init'], {cwd})(); |
| 126 | + |
| 127 | + return Promise.all([ |
| 128 | + git(['config', 'user.email', '"[email protected]"'], {cwd })(), |
| 129 | + git(['config', 'user.name', '"commitlint"'], {cwd})() |
| 130 | + ]); |
| 131 | +} |
| 132 | + |
| 133 | +function pkg(cwd) { |
| 134 | + return sander.writeFile(cwd, 'package.json', JSON.stringify({scripts: {commitmsg: `${CLI} -e`}})); |
| 135 | +} |
0 commit comments