From 95e9022e25e1e5b521d2376e20d137e019523ee1 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Wed, 18 Jan 2017 16:55:20 +0000 Subject: [PATCH 1/3] Implement preset --- .editorconfig | 12 +++++++++ .gitignore | 3 +++ .travis.yml | 13 +++++++++ appveyor.yml | 26 ++++++++++++++++++ espower-patterns.json | 12 +++++++++ index.js | 15 +++++++++++ license | 21 +++++++++++++++ package.json | 51 ++++++++++++++++++++++++++++++++++++ readme.md | 22 ++++++++++++++++ test.js | 61 +++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 236 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 appveyor.yml create mode 100644 espower-patterns.json create mode 100644 index.js create mode 100644 license create mode 100644 package.json create mode 100644 readme.md create mode 100644 test.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..98a761d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +indent_style = tab +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[{package.json,*.yml}] +indent_style = space +indent_size = 2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1e59e82 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +.nyc_output +coverage diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4ccb4c2 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +language: node_js +node_js: + - '7' + - '6' + - '4' +cache: + directories: + - $HOME/.npm + - node_modules +before_install: 'npm install --global npm' +before_script: 'npm prune' +script: 'npm run coverage' +after_script: 'cat ./coverage/lcov.info | ./node_modules/.bin/coveralls' diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..e2957cf --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,26 @@ +environment: + matrix: + - nodejs_version: '7' + - nodejs_version: '6' + - nodejs_version: '4' +install: + - ps: Install-Product node $env:nodejs_version + - set CI=true + - set AVA_APPVEYOR=true + - git config core.symlinks true + - git reset --hard + - npm install --global npm + - set PATH=%APPDATA%\npm;%PATH% + - npm install + - npm prune +matrix: + fast_finish: true +build: off +clone_depth: 1 +cache: + - node_modules + - '%APPDATA%\npm-cache' +test_script: + - node --version + - npm --version + - npm test diff --git a/espower-patterns.json b/espower-patterns.json new file mode 100644 index 0000000..2bcbb70 --- /dev/null +++ b/espower-patterns.json @@ -0,0 +1,12 @@ +[ + "t.truthy(value, [message])", + "t.falsy(value, [message])", + "t.true(value, [message])", + "t.false(value, [message])", + "t.is(value, expected, [message])", + "t.not(value, expected, [message])", + "t.deepEqual(value, expected, [message])", + "t.notDeepEqual(value, expected, [message])", + "t.regex(contents, regex, [message])", + "t.notRegex(contents, regex, [message])" +] diff --git a/index.js b/index.js new file mode 100644 index 0000000..9c58676 --- /dev/null +++ b/index.js @@ -0,0 +1,15 @@ +'use strict'; +const ESPOWER_PATTERNS = require('./espower-patterns.json'); + +function buildPreset(context) { + const plugins = [ + require('babel-plugin-espower/create')(context, { + embedAst: true, + patterns: ESPOWER_PATTERNS + }), + require('babel-plugin-ava-throws-helper') + ]; + + return {plugins}; +} +module.exports = buildPreset; diff --git a/license b/license new file mode 100644 index 0000000..060edd7 --- /dev/null +++ b/license @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Mark Wubben (novemberborn.net) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/package.json b/package.json new file mode 100644 index 0000000..4eb0cb9 --- /dev/null +++ b/package.json @@ -0,0 +1,51 @@ +{ + "name": "@ava/babel-preset-transform-test-files", + "version": "0.0.0", + "description": "Test file transforms as used by AVA", + "main": "index.js", + "files": [ + "index.js", + "espower-patterns.json" + ], + "scripts": { + "test": "ava", + "posttest": "xo", + "coverage": "nyc npm test" + }, + "engines": { + "node": ">=4" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/avajs/babel-preset-transform-test-files.git" + }, + "keywords": [ + "ava", + "babel-preset" + ], + "author": "Mark Wubben (https://novemberborn.net/)", + "license": "MIT", + "bugs": { + "url": "https://github.com/avajs/babel-preset-transform-test-files/issues" + }, + "homepage": "https://github.com/avajs/babel-preset-transform-test-files#readme", + "devDependencies": { + "ava": "^0.17.0", + "babel-core": "^6.21.0", + "coveralls": "^2.11.15", + "empower-core": "^0.6.1", + "nyc": "^10.1.2", + "xo": "^0.17.1" + }, + "dependencies": { + "babel-plugin-ava-throws-helper": "^1.0.0", + "babel-plugin-espower": "^2.3.2" + }, + "nyc": { + "reporter": [ + "html", + "lcov", + "text" + ] + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..cef682f --- /dev/null +++ b/readme.md @@ -0,0 +1,22 @@ +# @ava/babel-preset-transform-test-files + +A [Babel] preset for use with [AVA] test files. + +Currently contains: + +* [`babel-plugin-espower`](https://github.com/power-assert-js/babel-plugin-espower) +and the patterns that should be enhanced +* [`babel-plugin-ava-throws-helper`](https://github.com/avajs/babel-plugin-ava-throws-helper/) + +## Install + +```console +$ npm install --save @ava/babel-preset-transform-test-files +``` + +## Usage + +Add `@ava/transform-test-files` to your [Babel] presets. + +[AVA]: https://ava.li +[Babel]: https://babeljs.io/ diff --git a/test.js b/test.js new file mode 100644 index 0000000..c4f199e --- /dev/null +++ b/test.js @@ -0,0 +1,61 @@ +import {runInNewContext} from 'vm'; + +import test from 'ava'; +import * as babel from 'babel-core'; +import throwsHelper from 'babel-plugin-ava-throws-helper'; +import empower from 'empower-core'; + +import buildPreset from './index'; + +const ESPOWER_PATTERNS = require('./espower-patterns.json'); + +test('throws-helper is included', t => { + const {plugins} = buildPreset(babel); + t.true(plugins.indexOf(throwsHelper) > -1); +}); + +test('resulting preset transforms assertion patterns', t => { + const {code} = babel.transform(` + const value = 'value' + const expected = 'expected' + const contents = 'contents' + const regex = /regex/ + + // "Execute" the patterns. Hardcode them here, otherwise it's cheating. + t.truthy(value) + t.falsy(value) + t.true(value) + t.false(value) + t.is(value, expected) + t.not(value, expected) + t.deepEqual(value, expected) + t.notDeepEqual(value, expected) + t.regex(contents, regex) + t.notRegex(contents, regex) + `, { + ast: false, + babelrc: false, + presets: [buildPreset] + }); + + const appliedPatterns = []; + // Create a stub assertion object that can be enhanced using empower-core. + const assert = ESPOWER_PATTERNS + .map(p => /^t\.(.+)\(/.exec(p)[1]) + .reduce((assert, name) => { + assert[name] = () => {}; + return assert; + }, {}); + + runInNewContext(code, { + t: empower(assert, { + onSuccess({matcherSpec: {pattern}, powerAssertContext}) { + if (powerAssertContext) { // Only available if the empower plugin transformed the assertion. + appliedPatterns.push(pattern); + } + }, + patterns: ESPOWER_PATTERNS + }) + }); + t.deepEqual(appliedPatterns, ESPOWER_PATTERNS); +}); From b76a760766bf4c1d44f20b13efacbb502baaa63c Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Wed, 18 Jan 2017 17:54:54 +0000 Subject: [PATCH 2/3] Support powerAssert option to disable the espower plugin --- index.js | 13 +++++++------ readme.md | 11 ++++++++++- test.js | 10 ++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 9c58676..eb06f47 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,15 @@ 'use strict'; const ESPOWER_PATTERNS = require('./espower-patterns.json'); -function buildPreset(context) { - const plugins = [ - require('babel-plugin-espower/create')(context, { +function buildPreset(context, options) { + const plugins = []; + if (!options || options.powerAssert !== false) { + plugins.push(require('babel-plugin-espower/create')(context, { embedAst: true, patterns: ESPOWER_PATTERNS - }), - require('babel-plugin-ava-throws-helper') - ]; + })); + } + plugins.push(require('babel-plugin-ava-throws-helper')); return {plugins}; } diff --git a/readme.md b/readme.md index cef682f..a233c4d 100644 --- a/readme.md +++ b/readme.md @@ -16,7 +16,16 @@ $ npm install --save @ava/babel-preset-transform-test-files ## Usage -Add `@ava/transform-test-files` to your [Babel] presets. +Add `@ava/transform-test-files` to your [Babel] presets. You can disable +`babel-plugin-espower` by setting the `powerAssert` option to `false`: + +```json +{ + "presets": [ + ["@ava/transform-test-files", {"powerAsssert": false}] + ] +} +``` [AVA]: https://ava.li [Babel]: https://babeljs.io/ diff --git a/test.js b/test.js index c4f199e..05da51d 100644 --- a/test.js +++ b/test.js @@ -59,3 +59,13 @@ test('resulting preset transforms assertion patterns', t => { }); t.deepEqual(appliedPatterns, ESPOWER_PATTERNS); }); + +test('the espower plugin can be disabled', t => { + const expected = 't.true(value);'; + const {code} = babel.transform(expected, { + ast: false, + babelrc: false, + presets: [[require.resolve('./'), {powerAssert: false}]] + }); + t.true(code === expected); +}); From f15904fca035b7326adcd4423260aa185cdda853 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Thu, 19 Jan 2017 12:10:51 +0000 Subject: [PATCH 3/3] Expose a package hash for the plugins included in the preset The hash can be used to determine whether cache files are OK to use. --- package-hash.js | 8 ++++++++ package.json | 7 +++++-- readme.md | 3 +++ test.js | 17 +++++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 package-hash.js diff --git a/package-hash.js b/package-hash.js new file mode 100644 index 0000000..4ce2e1e --- /dev/null +++ b/package-hash.js @@ -0,0 +1,8 @@ +'use strict'; +const packageHash = require('package-hash'); + +module.exports = packageHash.sync([ + require.resolve('./package.json'), + require.resolve('babel-plugin-espower/package.json'), + require.resolve('babel-plugin-ava-throws-helper/package.json') +]); diff --git a/package.json b/package.json index 4eb0cb9..aa61bf0 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "index.js", "files": [ "index.js", - "espower-patterns.json" + "espower-patterns.json", + "package-hash.js" ], "scripts": { "test": "ava", @@ -35,11 +36,13 @@ "coveralls": "^2.11.15", "empower-core": "^0.6.1", "nyc": "^10.1.2", + "proxyquire": "^1.7.10", "xo": "^0.17.1" }, "dependencies": { "babel-plugin-ava-throws-helper": "^1.0.0", - "babel-plugin-espower": "^2.3.2" + "babel-plugin-espower": "^2.3.2", + "package-hash": "^1.2.0" }, "nyc": { "reporter": [ diff --git a/readme.md b/readme.md index a233c4d..3a637dc 100644 --- a/readme.md +++ b/readme.md @@ -27,5 +27,8 @@ Add `@ava/transform-test-files` to your [Babel] presets. You can disable } ``` +Require `@ava/babel-preset-transform-test-files/package-hash` to get a combined +hash for the installed version of the preset, as well as the plugins used. + [AVA]: https://ava.li [Babel]: https://babeljs.io/ diff --git a/test.js b/test.js index 05da51d..bd6eb06 100644 --- a/test.js +++ b/test.js @@ -4,6 +4,7 @@ import test from 'ava'; import * as babel from 'babel-core'; import throwsHelper from 'babel-plugin-ava-throws-helper'; import empower from 'empower-core'; +import proxyquire from 'proxyquire'; import buildPreset from './index'; @@ -69,3 +70,19 @@ test('the espower plugin can be disabled', t => { }); t.true(code === expected); }); + +test('computes correct package hash', t => { + t.plan(2); + + proxyquire('./package-hash', { + 'package-hash': { + sync([preset, ...plugins]) { + t.true(preset === require.resolve('./package.json')); + t.deepEqual(plugins, [ + require.resolve('babel-plugin-espower/package.json'), + require.resolve('babel-plugin-ava-throws-helper/package.json') + ]); + } + } + }); +});