Skip to content

Commit 67d7e65

Browse files
authored
Merge pull request #19 from xpepermint/master
Support package.json configuration, fix return codes
2 parents 40dba78 + daf5774 commit 67d7e65

File tree

17 files changed

+161
-46
lines changed

17 files changed

+161
-46
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,25 @@ Install the [ts-node](https://www.npmjs.com/package/ts-node) NPM package then us
166166
hayspec --require ts-node/register
167167
```
168168

169+
#### Project configuration
170+
171+
Hayspec configuration options can be saved inside the `package.json` file under the the `hayspec` key.
172+
173+
```
174+
{
175+
"hayspec": {
176+
"require": [
177+
"ts-node/register"
178+
],
179+
"match": [
180+
"./src/**/*.test.*"
181+
]
182+
}
183+
}
184+
```
185+
186+
Note that these options can be overriden by providing CLI arguments.
187+
169188
## Hayspec packages
170189

171190
| Package | Description | Version

packages/hayspec-cli/package.json

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hayspec/cli",
3-
"version": "0.5.0",
3+
"version": "0.7.0",
44
"description": "CLI for Hayspec framework.",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",
@@ -11,7 +11,15 @@
1111
"clean": "rm -Rf ./dist",
1212
"transpile": "npm run clean; tsc",
1313
"prepare": "npm run transpile",
14-
"test": "npm run transpile && nyc ava"
14+
"test": "npm run transpile && nyc ava --verbose"
15+
},
16+
"hayspec": {
17+
"require": [
18+
"ts-node/register"
19+
],
20+
"match": [
21+
"./src/**/*.hay.*"
22+
]
1523
},
1624
"ava": {
1725
"compileEnhancements": false,
@@ -61,15 +69,14 @@
6169
"ava": "1.0.0-beta.6",
6270
"nyc": "^12.0.2",
6371
"ts-node": "^7.0.1",
64-
"typescript": "^3.0.1"
72+
"typescript": "^3.1.1"
6573
},
6674
"dependencies": {
67-
"@hayspec/init": "^0.5.0",
68-
"@hayspec/reporter": "^0.5.0",
69-
"@hayspec/runner": "^0.5.0",
70-
"@hayspec/spec": "^0.5.0",
75+
"@hayspec/init": "^0.7.0",
76+
"@hayspec/reporter": "^0.7.0",
77+
"@hayspec/runner": "^0.7.0",
78+
"@hayspec/spec": "^0.7.0",
7179
"inquirer": "^6.0.0",
7280
"yargs": "^11.0.0"
73-
},
74-
"gitHead": "03535c698ebd35ec94c63869f18cdde2380f739f"
81+
}
7582
}

packages/hayspec-cli/src/commands/init.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as inquirer from 'inquirer';
22
import { Generator } from '@hayspec/init';
33
import { Printer } from '@hayspec/reporter';
4+
import { getConfig } from '../lib/env';
45

56
/**
67
* Initializes project directory.
78
*/
89
export default async function (argv) {
9-
const { name, description } = argv;
10+
const { name, description } = getConfig(argv);
1011
const root = process.cwd();
1112
const printer = new Printer();
1213

@@ -52,8 +53,10 @@ export default async function (argv) {
5253
printer.colorize('gray', `$ npm test`)
5354
);
5455
printer.end();
55-
56+
process.exit(0);
57+
5658
} catch (e) {
5759
console.error(e);
60+
process.exit(2);
5861
}
5962
}

packages/hayspec-cli/src/commands/test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Runner } from '@hayspec/runner';
22
import { Spec, Stage } from '@hayspec/spec';
33
import { DefaultReporter } from '@hayspec/reporter';
4+
import { getConfig } from '../lib/env';
45

56
/**
67
* Runs tests.
78
*/
89
export default async function (argv) {
9-
const { match } = argv;
10+
const { match } = getConfig(argv);
1011
const reporter = new DefaultReporter();
1112
const stage = new Stage(reporter);
1213
const test = new Spec(stage);
@@ -18,5 +19,11 @@ export default async function (argv) {
1819
test.spec(message, result.spec);
1920
});
2021

21-
await test.perform();
22+
try {
23+
await test.perform();
24+
process.exit(reporter.failedCount ? 1 : 0);
25+
} catch (e) {
26+
console.log(e);
27+
process.exit(2);
28+
}
2229
}

packages/hayspec-cli/src/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import * as yargs from 'yargs';
22
import initHandler from './commands/init';
33
import testHandler from './commands/test';
4+
import { getConfig } from './lib/env';
45

56
/**
67
* Interface definition.
78
*/
89
const { argv } = yargs
910
.usage('Usage: $0 --help')
10-
.command('init', 'Initializes project directory.', (yargs) => yargs
11+
.command('init', 'Initializes project directory.', (yargs) => yargs
1112
.option('name', {
1213
string: true,
1314
description: 'Project name',
@@ -21,7 +22,6 @@ const { argv } = yargs
2122
.option('match', {
2223
array: true,
2324
description: 'Match pattern',
24-
default: ['./src/cro**/*.test.*'],
2525
})
2626
.option('require', {
2727
array: true,
@@ -35,6 +35,4 @@ const { argv } = yargs
3535
/**
3636
* Upgrading environment.
3737
*/
38-
if (Array.isArray(argv.require)) {
39-
argv.require.forEach((v) => require(v));
40-
}
38+
getConfig(argv).require.forEach((v) => require(v));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as pt from 'path';
2+
3+
/**
4+
* Returns package.json data.
5+
*/
6+
export function getPackage() {
7+
try {
8+
return require(pt.join(process.cwd(), 'package.json')) || {};
9+
} catch (e) {
10+
return {};
11+
}
12+
}
13+
14+
/**
15+
* Returns Hayspec options.
16+
*/
17+
export function getConfig(argv?: any) {
18+
const defaults = getPackage()['hayspec'] || {};
19+
const custom = argv || {};
20+
return {
21+
name: custom['name'] || defaults['name'] || '',
22+
description: custom['description'] || defaults['description'] || '',
23+
require: custom['require'] || defaults['require'] || [],
24+
match: custom['match'] || defaults['match'] || [],
25+
};
26+
}

packages/hayspec-cli/src/tests/assets/foo.hay.ts renamed to packages/hayspec-cli/src/tests/assets/invalid.hay.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import { Spec } from '@hayspec/spec';
33
const spec = new Spec();
44

55
spec.test('foo', async (context) => {
6-
context.is(true, true);
6+
context.true(true);
77
});
88

99
spec.test('bar', async (context) => {
10-
context.is(true, true);
11-
context.is(true, true);
12-
context.is(true, false);
10+
context.true(false);
11+
context.false(true);
1312
});
1413

1514
export default spec;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Spec } from '@hayspec/spec';
2+
3+
const spec = new Spec();
4+
5+
spec.test('foo', async (context) => {
6+
context.true(true);
7+
});
8+
9+
spec.test('bar', async (context) => {
10+
context.true(true);
11+
context.true(true);
12+
});
13+
14+
export default spec;

packages/hayspec-cli/src/tests/index.test.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,28 @@ import * as cproc from 'child_process';
44

55
const exec = util.promisify(cproc.exec);
66

7-
test('runs tests', async (t) => {
8-
const command = './bin/hayspec test --require ts-node/register --match ./src/tests/assets/**/*.hay.*';
7+
test('initializes current folder', async (t) => {
8+
const command = `mkdir -p ./node_modules/.tmp/test; cd ./node_modules/.tmp/test; ../../../bin/hayspec init --name foo --description bar; echo code: $?`;
99
const { stdout, stderr } = await exec(command);
10-
t.true(stdout.indexOf('src/tests/assets/foo.hay.ts') !== -1);
10+
t.true(stdout.indexOf('Continue by running the commands below:') !== -1);
11+
t.true(stdout.indexOf('code: 0') !== -1);
1112
t.true(stderr === '');
1213
});
1314

14-
test('initializes current folder', async (t) => {
15-
const command = `mkdir -p ./node_modules/.tmp/test; cd ./node_modules/.tmp/test; ../../../bin/hayspec init --name foo --description bar`;
15+
test('runs valid tests', async (t) => {
16+
const command = './bin/hayspec test --require ts-node/register --match ./src/tests/assets/**/valid.hay.*; echo code: $?';
1617
const { stdout, stderr } = await exec(command);
17-
t.true(stdout.indexOf('Continue by running the commands below:') !== -1);
18+
t.true(stdout.indexOf('src/tests/assets/valid.hay.ts') !== -1);
19+
t.true(stdout.indexOf('src/tests/assets/invalid.hay.ts') === -1);
20+
t.true(stdout.indexOf('code: 0') !== -1);
21+
t.true(stderr === '');
22+
});
23+
24+
test('runs invalid tests', async (t) => {
25+
const command = './bin/hayspec test --require ts-node/register --match ./src/tests/assets/**/invalid.hay.*; echo code: $?';
26+
const { stdout, stderr } = await exec(command);
27+
t.true(stdout.indexOf('src/tests/assets/valid.hay.ts') === -1);
28+
t.true(stdout.indexOf('src/tests/assets/invalid.hay.ts') !== -1);
29+
t.true(stdout.indexOf('code: 1') !== -1);
1830
t.true(stderr === '');
1931
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import test from 'ava';
2+
import { getConfig } from '../../lib/env';
3+
4+
test('method `getConfig` returns package.json hayspec configuration', async (t) => {
5+
t.deepEqual(getConfig(), {
6+
name: '',
7+
description: '',
8+
require: ['ts-node/register'],
9+
match: ['./src/**/*.hay.*'],
10+
});
11+
});
12+
13+
test('method `getConfig` merges reveived configuration', async (t) => {
14+
t.deepEqual(getConfig({
15+
extension: [],
16+
require: ['bar'],
17+
match: ['foo'],
18+
}), {
19+
name: '',
20+
description: '',
21+
require: ['bar'],
22+
match: ['foo'],
23+
});
24+
});

0 commit comments

Comments
 (0)