Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Istanbul babel plugin #73

Merged
merged 4 commits into from
Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .coverage.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
{
"include": [
"**/packages/lmieulet_meteor-coverage.js"
]
}
54 changes: 16 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A meteor package that allows you to get the statement, line, function and branch coverage of Meteor project and package.

This package uses the [istanbuljs](https://github.com/istanbuljs/istanbuljs) packages for coverage report.
This package uses the [istanbuljs](https://github.com/istanbuljs/istanbuljs) packages and the babel plugin [babel-plugin-istanbul](https://github.com/istanbuljs/babel-plugin-istanbul) for coverage report.

It's a debug only package, so it does not affect your production build.

Expand Down Expand Up @@ -71,8 +71,23 @@ Then, run the following :

```txt
meteor add lmieulet:meteor-coverage meteortesting:mocha
npm install --save-dev babel-plugin-istanbul
```

And add the [`babel-plugin-istanbul`](https://github.com/istanbuljs/babel-plugin-istanbul) to your package.json file.

```js
{
"name": "my-package",
"version": "1.0.0",
"babel": {
"plugins": [ "istanbul" ]
}
}
```

We highly recommend to wrap it in an [env option](https://babeljs.io/docs/usage/babelrc/#env-option) so you can en- and disable the file-instrumentation using an env-variable. The instrumentation is the gathering of data for code-coverage which should only be gathered when you also have this plugin enabled, which only collects it and passes it on to a reporter.

We do not have any easy tutorial to help you to setup coverage with meteortesting. For now, [use their readme](https://github.com/meteortesting/meteor-mocha#run-with-code-coverage), the [meteor-coverage-app-exemple repository](https://github.com/serut/meteor-coverage-app-exemple/tree/master/bare-exemple) and the here under legacy tutorial that worked with spacejam to try by yourself. There is not so many difference between spacejam and meteortesting. And don't try to use that solution on Windows.

### Specific setup for Meteor package
Expand All @@ -87,8 +102,6 @@ Package.onTest(function (api) {
});
```

Then, [create your configuration](#config-file) file `.coverage.json` to specify that you want to cover your package, using the `include` key.

### Configuration

Setup how you want to run your app. Actually, to let meteor-coverage start processing, you need to enable it, because it's a probe disabled by default and you need to provide the absolute path to your source folder.
Expand Down Expand Up @@ -285,45 +298,13 @@ Exemple:
```json{
"--": "Meteor app does not require any specific configuration",
"--": "If you want to instrument a package, you need to add the following",
"include": [
"**/packages/author_packageName.js"
],
"--": "If you want to, you can redefine the following:",
"exclude": {
"general": [],
"server": [
"**/node_modules/**/*.json",
"**/.?*/**",
"**/packages/!(local-test_?*.js)",
"**/+([^:]):+([^:])/**",
"**/@(test|tests|spec|specs)/**",
"**/?(*.)test?(s).?*",
"**/?(*.)spec?(s).?*",
"**/?(*.)app-test?(s).?*",
"**/?(*.)app-spec?(s).?*"
],
"client": [
"**/client/stylesheets/**",
"**/.npm/package/node_modules/**",
"**/web.browser/packages/**",
"**/.?*/**",
"**/packages/!(local-test_?*.js)",
"**/+([^:]):+([^:])/**",
"**/@(test|tests|spec|specs)/**",
"**/?(*.)test?(s).?*",
"**/?(*.)spec?(s).?*",
"**/?(*.)app-test?(s).?*",
"**/?(*.)app-spec?(s).?*"
]
},
"remapFormat": ["html", "cobertura", "clover", "json", "json-summary", "lcovonly", "teamcity", "text", "text-summary"],
"output": "./.coverage"
}
```

Details :

- Allows / Disallow is coded with the following order `include`, `exclude.general`, `exclude.(client|server)`, it is used before both instrumentation and before coverage report creation.
- The glob syntax can be found [here](http://www.linuxjournal.com/content/bash-extended-globbing).
- To create your custom config file, run the project with `COVERAGE_VERBOSE=1` env variable and use logs to see which filenames were hooked or hidden. PR welcome.
- The output folder needs to starts with a dot to exclude that folder from Meteor build.
Expand Down Expand Up @@ -371,9 +352,6 @@ The coverage is remapped to **all the available reports** (listed in the followi

```json
{
"include": [
"**/packages/lmieulet_meteor-coverage.js"
],
"remap": {
"format": ["html", "clover", "cobertura", "json", "json-summary", "lcovonly", "teamcity", "text", "text-summary"]
}
Expand Down
30 changes: 0 additions & 30 deletions conf/default-coverage.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,4 @@
{
"include": [
],
"exclude": {
"general": [],
"server": [
"**/node_modules/**/*.json",
"**/.!(meteor)*/**",
"**/packages/!(local-test_?*.js)",
"**/+([^:]):+([^:])/**",
"**/@(test|tests|spec|specs)/**",
"**/?(*.)test?(s).?*",
"**/?(*.)spec?(s).?*",
"**/?(*.)app-test?(s).?*",
"**/?(*.)app-spec?(s).?*"
],
"client": [
"**/*.json",
"**/client/stylesheets/**",
"**/.npm/package/node_modules/**",
"**/web.browser/packages/**",
"**/.?*/**",
"**/packages/!(local-test_?*.js)",
"**/+([^:]):+([^:])/**",
"**/@(test|tests|spec|specs)/**",
"**/?(*.)test?(s).?*",
"**/?(*.)spec?(s).?*",
"**/?(*.)app-test?(s).?*",
"**/?(*.)app-spec?(s).?*"
]
},
"remapFormat": ["html", "cobertura", "clover", "json", "json-summary", "lcovonly", "teamcity", "text", "text-summary"],
"output": "./.coverage"
}
6 changes: 0 additions & 6 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@ Package.describe({
});

const dependencies = {
'istanbul-lib-source-maps': '1.2.4',
'istanbul-lib-instrument': '1.10.0',
'istanbul-lib-hook': '1.2.0',
'istanbul-lib-coverage': '1.2.0',
'istanbul-lib-report': '1.1.4',
'istanbul-reports': '1.2.0',
'body-parser': '1.18.2',
'minimatch': '3.0.4',
'mkdirp': '0.5.1',
'homedir': '0.6.0',
'remap-istanbul': '0.6.4'
};

Expand Down Expand Up @@ -47,7 +42,6 @@ Package.onUse(function (api) {

Package.onTest(function (api) {
api.use('ecmascript');
api.use(['lmieulet:meteor-coverage-self-instrumenter@4.0.0'], ['server']);
api.use('http', 'client');
api.use('webapp', 'server');
api.use(['lmieulet:meteor-coverage']);
Expand Down
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
"main": "server/index.js",
"dependencies": {
"body-parser": "1.18.2",
"homedir": "0.6.0",
"istanbul-lib-coverage": "^1.2.0",
"istanbul-lib-hook": "^1.2.0",
"istanbul-lib-instrument": "^1.10.0",
"istanbul-lib-report": "^1.1.4",
"istanbul-lib-source-maps": "^1.2.4",
"istanbul-reports": "^1.2.0",
"minimatch": "3.0.4",
"mkdirp": "0.5.1",
"remap-istanbul": "0.6.4"
},
Expand Down
6 changes: 0 additions & 6 deletions server/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import path from 'path';
import mkdirp from 'mkdirp';
import Conf from './context/conf';
import Router from './router';
import Instrumenter from './services/instrumenter';
import SourceMap from './services/source-map';

export default Boot = {
startup() {
Expand All @@ -21,10 +19,6 @@ export default Boot = {
}
}
});
// Search for PUTs and check whether called from inside/outside a PUT dir
SourceMap.initialSetup();
// Start to collect coverage
Instrumenter.hookLoader();
// Connect the router to this app
new Router();
}
Expand Down
43 changes: 1 addition & 42 deletions server/context/conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ if (COVERAGE_APP_FOLDER === ENV_NOT_DEFINED) {
}
const NOT_DEFINED = '/COVERAGE/NOT/ACTIVE/';
let configuration = {
exclude: {
general: [],
server: [],
client: []
},
include: [],
output: NOT_DEFINED
};
/* istanbul ignore else */
Expand Down Expand Up @@ -53,38 +47,7 @@ if (IS_COVERAGE_ACTIVE) {
configuration = defaultConfig;
}

// Don't force to rewrite all the key of configuration.exclude,
// if they are not defined, the default conf is used.

/* istanbul ignore else */
if (configuration.exclude === undefined) {
Log.info('Loading default configuration: exclude.*');
configuration.exclude = defaultConfig.exclude;
}

/* istanbul ignore else */
if (configuration.exclude.general === undefined) {
Log.info('Loading default configuration: exclude.general');
configuration.exclude.general = defaultConfig.exclude.general;
}

/* istanbul ignore else */
if (configuration.exclude.server === undefined) {
Log.info('Loading default configuration: exclude.server');
configuration.exclude.server = defaultConfig.exclude.server;
}

/* istanbul ignore else */
if (configuration.exclude.client === undefined) {
Log.info('Loading default configuration: exclude.client');
configuration.exclude.client = defaultConfig.exclude.client;
}

/* istanbul ignore else */
if (configuration.include === undefined) {
Log.info('Loading default configuration: include');
configuration.include = defaultConfig.include || [];
}
// Don't force to rewrite all the key of configuration.

/* istanbul ignore else */
if (configuration.output === undefined) {
Expand All @@ -100,8 +63,6 @@ if (IS_COVERAGE_ACTIVE) {
}

export const COVERAGE_EXPORT_FOLDER = configuration.output;
export const exclude = configuration.exclude;
export const include = configuration.include;
export const remapFormat = configuration.remapFormat;
export const reportTypes = {
allowed: ['clover', 'cobertura', 'coverage', 'html', 'json', 'json-summary', 'lcov', 'lcovonly', 'remap', 'teamcity', 'text', 'text-lcov', 'text-summary'],
Expand All @@ -113,7 +74,5 @@ Log.info('- IS_COVERAGE_ACTIVE=', IS_COVERAGE_ACTIVE);
Log.info('- IS_COVERAGE_VERBOSE=', IS_COVERAGE_VERBOSE);
Log.info('- COVERAGE_APP_FOLDER=', COVERAGE_APP_FOLDER);
Log.info('.coverage.json values:');
Log.info('- exclude=', configuration.exclude);
Log.info('- include=', configuration.include);
Log.info('- remapFormat=', configuration.remapFormat);
Log.info('- COVERAGE_EXPORT_FOLDER=', COVERAGE_EXPORT_FOLDER);
39 changes: 0 additions & 39 deletions server/handlers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Conf from './context/conf';
import Instrumenter from './services/instrumenter';
import Core from './services/core';
import ReportService from './report/report-service';
import fs from 'fs';
Expand Down Expand Up @@ -90,48 +89,10 @@ importCoverage = function (params, req, res, next) {
}
};

instrumentClientJs = function (params, req, res, next) {
var fileurl = req.url.split('?')[0];
if (Instrumenter.shallInstrumentClientScript(fileurl)) {
var path,
pathLabel;
// Either a package
if (req.url.indexOf('/packages') === 0) {
path = '../web.browser';
pathLabel = path + fileurl;
} else if (req.url.indexOf('/app') === 0) {
// Or the app/app.js
path = '../web.browser';
pathLabel = path + fileurl;
} else {
// Or a public file
path = '../web.browser/app';
pathLabel = path + fileurl;
}
res.setHeader('Content-type', 'application/javascript');
fs.exists(path + fileurl, function (exists) {
/* istanbul ignore else */
if (!exists) return next();
fs.readFile(path + fileurl, 'utf8', function (err, fileContent) {
/* istanbul ignore else */
if (err) return next();
Instrumenter.instrumentJs(fileContent, pathLabel, function (err, data) {
/* istanbul ignore else */
if (err) throw err;
res.end(data);
});
});
});
} else {
next();
}
};

export default Handlers = {
showCoverage,
getAsset,
addClientCoverage,
instrumentClientJs,
exportFile,
importCoverage
};
10 changes: 1 addition & 9 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,14 @@ if (Conf.IS_COVERAGE_ACTIVE) {
Conf,
Router: {

},
SourceMap: {
registerSourceMap: function () {
throw 'COVERAGE_NOT_ACTIVE';
}
},
CoverageData: {

},
Instrumenter: {
hookLoader: function() {}
},
ReportService: {

}
};
}

export default library;
export default library;
4 changes: 0 additions & 4 deletions server/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import Instrumenter from './services/instrumenter';
import CoverageData from './services/coverage-data';
import SourceMap from './services/source-map';
import Conf from './context/conf';
import Router from './router';
import ReportService from './report/report-service';
Expand All @@ -11,8 +9,6 @@ Boot.startup();
export default {
Conf,
Router,
SourceMap,
CoverageData,
Instrumenter,
ReportService
};
3 changes: 0 additions & 3 deletions server/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,5 @@ export default class {
handleRequest('POST')('/coverage/client', Handlers.addClientCoverage);

handleRequest('GET')('/coverage', Handlers.showCoverage);

// inject istanbul-instruments into js files loaded by the client
handleRequest('GET')('/', Handlers.instrumentClientJs);
}
}
Loading