Skip to content

Commit

Permalink
Merge pull request #19 from xpepermint/master
Browse files Browse the repository at this point in the history
Support package.json configuration, fix return codes
  • Loading branch information
xpepermint authored Oct 4, 2018
2 parents 40dba78 + daf5774 commit 67d7e65
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 46 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,25 @@ Install the [ts-node](https://www.npmjs.com/package/ts-node) NPM package then us
hayspec --require ts-node/register
```

#### Project configuration

Hayspec configuration options can be saved inside the `package.json` file under the the `hayspec` key.

```
{
"hayspec": {
"require": [
"ts-node/register"
],
"match": [
"./src/**/*.test.*"
]
}
}
```

Note that these options can be overriden by providing CLI arguments.

## Hayspec packages

| Package | Description | Version
Expand Down
25 changes: 16 additions & 9 deletions packages/hayspec-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hayspec/cli",
"version": "0.5.0",
"version": "0.7.0",
"description": "CLI for Hayspec framework.",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand All @@ -11,7 +11,15 @@
"clean": "rm -Rf ./dist",
"transpile": "npm run clean; tsc",
"prepare": "npm run transpile",
"test": "npm run transpile && nyc ava"
"test": "npm run transpile && nyc ava --verbose"
},
"hayspec": {
"require": [
"ts-node/register"
],
"match": [
"./src/**/*.hay.*"
]
},
"ava": {
"compileEnhancements": false,
Expand Down Expand Up @@ -61,15 +69,14 @@
"ava": "1.0.0-beta.6",
"nyc": "^12.0.2",
"ts-node": "^7.0.1",
"typescript": "^3.0.1"
"typescript": "^3.1.1"
},
"dependencies": {
"@hayspec/init": "^0.5.0",
"@hayspec/reporter": "^0.5.0",
"@hayspec/runner": "^0.5.0",
"@hayspec/spec": "^0.5.0",
"@hayspec/init": "^0.7.0",
"@hayspec/reporter": "^0.7.0",
"@hayspec/runner": "^0.7.0",
"@hayspec/spec": "^0.7.0",
"inquirer": "^6.0.0",
"yargs": "^11.0.0"
},
"gitHead": "03535c698ebd35ec94c63869f18cdde2380f739f"
}
}
7 changes: 5 additions & 2 deletions packages/hayspec-cli/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as inquirer from 'inquirer';
import { Generator } from '@hayspec/init';
import { Printer } from '@hayspec/reporter';
import { getConfig } from '../lib/env';

/**
* Initializes project directory.
*/
export default async function (argv) {
const { name, description } = argv;
const { name, description } = getConfig(argv);
const root = process.cwd();
const printer = new Printer();

Expand Down Expand Up @@ -52,8 +53,10 @@ export default async function (argv) {
printer.colorize('gray', `$ npm test`)
);
printer.end();

process.exit(0);

} catch (e) {
console.error(e);
process.exit(2);
}
}
11 changes: 9 additions & 2 deletions packages/hayspec-cli/src/commands/test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Runner } from '@hayspec/runner';
import { Spec, Stage } from '@hayspec/spec';
import { DefaultReporter } from '@hayspec/reporter';
import { getConfig } from '../lib/env';

/**
* Runs tests.
*/
export default async function (argv) {
const { match } = argv;
const { match } = getConfig(argv);
const reporter = new DefaultReporter();
const stage = new Stage(reporter);
const test = new Spec(stage);
Expand All @@ -18,5 +19,11 @@ export default async function (argv) {
test.spec(message, result.spec);
});

await test.perform();
try {
await test.perform();
process.exit(reporter.failedCount ? 1 : 0);
} catch (e) {
console.log(e);
process.exit(2);
}
}
8 changes: 3 additions & 5 deletions packages/hayspec-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as yargs from 'yargs';
import initHandler from './commands/init';
import testHandler from './commands/test';
import { getConfig } from './lib/env';

/**
* Interface definition.
*/
const { argv } = yargs
.usage('Usage: $0 --help')
.command('init', 'Initializes project directory.', (yargs) => yargs
.command('init', 'Initializes project directory.', (yargs) => yargs
.option('name', {
string: true,
description: 'Project name',
Expand All @@ -21,7 +22,6 @@ const { argv } = yargs
.option('match', {
array: true,
description: 'Match pattern',
default: ['./src/cro**/*.test.*'],
})
.option('require', {
array: true,
Expand All @@ -35,6 +35,4 @@ const { argv } = yargs
/**
* Upgrading environment.
*/
if (Array.isArray(argv.require)) {
argv.require.forEach((v) => require(v));
}
getConfig(argv).require.forEach((v) => require(v));
26 changes: 26 additions & 0 deletions packages/hayspec-cli/src/lib/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as pt from 'path';

/**
* Returns package.json data.
*/
export function getPackage() {
try {
return require(pt.join(process.cwd(), 'package.json')) || {};
} catch (e) {
return {};
}
}

/**
* Returns Hayspec options.
*/
export function getConfig(argv?: any) {
const defaults = getPackage()['hayspec'] || {};
const custom = argv || {};
return {
name: custom['name'] || defaults['name'] || '',
description: custom['description'] || defaults['description'] || '',
require: custom['require'] || defaults['require'] || [],
match: custom['match'] || defaults['match'] || [],
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { Spec } from '@hayspec/spec';
const spec = new Spec();

spec.test('foo', async (context) => {
context.is(true, true);
context.true(true);
});

spec.test('bar', async (context) => {
context.is(true, true);
context.is(true, true);
context.is(true, false);
context.true(false);
context.false(true);
});

export default spec;
14 changes: 14 additions & 0 deletions packages/hayspec-cli/src/tests/assets/valid.hay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Spec } from '@hayspec/spec';

const spec = new Spec();

spec.test('foo', async (context) => {
context.true(true);
});

spec.test('bar', async (context) => {
context.true(true);
context.true(true);
});

export default spec;
24 changes: 18 additions & 6 deletions packages/hayspec-cli/src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@ import * as cproc from 'child_process';

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

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

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

test('runs invalid tests', async (t) => {
const command = './bin/hayspec test --require ts-node/register --match ./src/tests/assets/**/invalid.hay.*; echo code: $?';
const { stdout, stderr } = await exec(command);
t.true(stdout.indexOf('src/tests/assets/valid.hay.ts') === -1);
t.true(stdout.indexOf('src/tests/assets/invalid.hay.ts') !== -1);
t.true(stdout.indexOf('code: 1') !== -1);
t.true(stderr === '');
});
24 changes: 24 additions & 0 deletions packages/hayspec-cli/src/tests/lib/env.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import test from 'ava';
import { getConfig } from '../../lib/env';

test('method `getConfig` returns package.json hayspec configuration', async (t) => {
t.deepEqual(getConfig(), {
name: '',
description: '',
require: ['ts-node/register'],
match: ['./src/**/*.hay.*'],
});
});

test('method `getConfig` merges reveived configuration', async (t) => {
t.deepEqual(getConfig({
extension: [],
require: ['bar'],
match: ['foo'],
}), {
name: '',
description: '',
require: ['bar'],
match: ['foo'],
});
});
4 changes: 2 additions & 2 deletions packages/hayspec-init/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hayspec/init",
"version": "0.5.0",
"version": "0.7.0",
"description": "Project generator for Hayspec framework.",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -62,7 +62,7 @@
"ava": "1.0.0-beta.6",
"nyc": "^12.0.2",
"ts-node": "^7.0.1",
"typescript": "^3.0.1"
"typescript": "^3.1.1"
},
"dependencies": {
"fs-extra": "^6.0.1"
Expand Down
14 changes: 11 additions & 3 deletions packages/hayspec-init/src/core/structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,17 @@ export const files = [
` "version": "0.0.0",`,
` "description": "{{ description }}",`,
` "scripts": {`,
` "transpile": "tsc",`,
` "prepare": "npm run transpile",`,
` "test": "hayspec test --require ts-node/register --match './src/tests/**/*.test.ts'"`,
` "build": "tsc",`,
` "prepare": "npm run build",`,
` "test": "hayspec test"`,
` },`,
` "hayspec": {`,
` "require": [`,
` "ts-node/register"`,
` ],`,
` "match": [`,
` "./src/tests/**/*.test.ts"`,
` ]`,
` },`,
` "license": "MIT",`,
` "devDependencies": {`,
Expand Down
9 changes: 4 additions & 5 deletions packages/hayspec-reporter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hayspec/reporter",
"version": "0.5.0",
"version": "0.7.0",
"description": "Reporter for Hayspec framework.",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -62,11 +62,10 @@
"ava": "1.0.0-beta.6",
"nyc": "^12.0.2",
"ts-node": "^7.0.1",
"typescript": "^3.0.1"
"typescript": "^3.1.1"
},
"dependencies": {
"@hayspec/spec": "^0.5.0",
"@hayspec/spec": "^0.7.0",
"chalk": "^2.4.1"
},
"gitHead": "03535c698ebd35ec94c63869f18cdde2380f739f"
}
}
6 changes: 3 additions & 3 deletions packages/hayspec-reporter/src/reporters/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { Printer } from '../lib/printer';
export class DefaultReporter extends Reporter {
protected printer: Printer;
protected assertionResults: boolean[] = [];
protected passedCount: number = 0;
protected skippedCount: number = 0;
protected failedCount: number = 0;
public passedCount: number = 0;
public skippedCount: number = 0;
public failedCount: number = 0;

/**
*
Expand Down
6 changes: 3 additions & 3 deletions packages/hayspec-runner/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hayspec/runner",
"version": "0.5.0",
"version": "0.7.0",
"description": "Tests runner for Hayspec framework.",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -62,10 +62,10 @@
"ava": "1.0.0-beta.6",
"nyc": "^12.0.2",
"ts-node": "^7.0.1",
"typescript": "^3.0.1"
"typescript": "^3.1.1"
},
"dependencies": {
"@hayspec/spec": "^0.5.0",
"@hayspec/spec": "^0.7.0",
"fast-glob": "^2.2.2"
},
"gitHead": "03535c698ebd35ec94c63869f18cdde2380f739f"
Expand Down
2 changes: 1 addition & 1 deletion packages/hayspec-spec/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hayspec/spec",
"version": "0.5.0",
"version": "0.7.0",
"description": "Core logic for Hayspec framework.",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
1 change: 0 additions & 1 deletion packages/hayspec-spec/src/core/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class Reporter {
*/
public end() {
this.onEnd();
this.reset();
}

/**
Expand Down

0 comments on commit 67d7e65

Please sign in to comment.