Skip to content

Commit

Permalink
Move modifyAssetLocation from build options to the config
Browse files Browse the repository at this point in the history
  • Loading branch information
robinborst95 committed Jan 19, 2022
1 parent 3a69bd3 commit 83f57d8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 42 deletions.
38 changes: 21 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ Configuration is optional. It should be put in a file at `config/coverage.js` (`

- `parallel`: Defaults to `false`. Should be set to true if parallel testing is being used for separate test runs, for example when using [ember-exam](https://github.com/trentmwillis/ember-exam) with the `--partition` flag. This will generate the coverage reports in directories suffixed with `_<random_string>` to avoid overwriting other threads reports. These reports can be joined by using the `ember coverage-merge` command (potentially as part of the [posttest hook](https://docs.npmjs.com/misc/scripts) in your `package.json`).

- `modifyAssetLocation`: Optional function that will allow you to override where a file actually lives inside of your project. See [Advanced customization](#modifyassetlocation) on how to use this function in practice.

#### Example
```js
module.exports = {
Expand Down Expand Up @@ -190,7 +192,9 @@ If you are using [`ember-cli-pretender`](https://github.com/rwjblue/ember-cli-pr

## Advanced customization

The `forceModulesToBeLoaded` can potientally cause unindented side effects when executed. You can pass custom filter fuctions that allow
### `forceModulesToBeLoaded`

The `forceModulesToBeLoaded` function can potentially cause unintended side effects when executed. You can pass custom filter fuctions that allow
you to specify which modules will be force loaded or not:

```js
Expand All @@ -201,28 +205,28 @@ QUnit.done(async () => {
});
```

### `modifyAssetLocation`

Under the hood, `ember-cli-code-coverage` attempts to "de-namespacify" paths into their real on disk location inside of
`project.root` (ie give a namespaced path like lib/inrepo/components/foo.js would live in lib/inrepo/addon/components/foo.js). It makes
some assumptions (where files live in in-repo addons vs app code for example) and sometimes those assumptions might not hold. Passing a
function `modifyAssetLocation` will allow you to override where a file actually lives inside of your project. The returned string should
be relative to your project root.
function `modifyAssetLocation` in your [configuration file](#configuration) will allow you to override where a file actually lives inside
of your project. The returned string should be relative to your project root.

```js
const app = new EmberApp(defaults, {
'ember-cli-code-coverage': {
modifyAssetLocation(root, relativePath) {
let appPath = relativePath.replace('my-project-name', 'app');

// here is an example of saying that `app/components/foo.js` actually
// lives in `lib/inrepo/app/components/foo.js` on disk.
if (fs.existsSync(path.join(root, 'lib/inrepo', appPath))) {
return path.join('lib/inrepo', appPath);
}

return false;
},
module.exports = {
modifyAssetLocation(root, relativePath) {
let appPath = relativePath.replace('my-project-name', 'app');

// here is an example of saying that `app/components/foo.js` actually
// lives in `lib/inrepo/app/components/foo.js` on disk.
if (fs.existsSync(path.join(root, 'lib', 'inrepo', appPath))) {
return path.join('lib', 'inrepo', appPath);
}

return false;
},
});
};
```

## Inspiration
Expand Down
10 changes: 0 additions & 10 deletions packages/ember-cli-code-coverage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ module.exports = {
};
},

included(app) {
this._super.included.apply(this, arguments);
let config = app.options[this.name] || {};
this.modifyAssetLocation = config && config.modifyAssetLocation;
},

buildNamespaceMappings() {
let rootNamespaceMappings = new Map();
function recurse(item) {
Expand Down Expand Up @@ -124,19 +118,15 @@ module.exports = {
attachMiddleware.serverMiddleware(startOptions.app, {
configPath: this.project.configPath(),
root: this.project.root,
fileLookup: this.fileLookup,
namespaceMappings: this.buildNamespaceMappings(),
modifyAssetLocation: this.modifyAssetLocation,
});
},

testemMiddleware(app) {
const config = {
configPath: this.project.configPath(),
root: this.project.root,
fileLookup: this.fileLookup,
namespaceMappings: this.buildNamespaceMappings(),
modifyAssetLocation: this.modifyAssetLocation,
};
// if we're running `ember test --server` use the `serverMiddleware`.
if (process.argv.includes('--server') || process.argv.includes('-s')) {
Expand Down
4 changes: 3 additions & 1 deletion packages/ember-cli-code-coverage/lib/attach-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ function adjustCoverageKey(
}

function adjustCoverage(coverage, options) {
let { root, namespaceMappings, modifyAssetLocation } = options;
let { root, namespaceMappings, configPath } = options;
let { modifyAssetLocation } = getConfig(configPath);

const adjustedCoverage = Object.keys(coverage).reduce((memo, filePath) => {
let relativeToProjectRoot = adjustCoverageKey(
root,
Expand Down
15 changes: 14 additions & 1 deletion test-packages/my-app-with-in-repo-addon/config/coverage.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
const fs = require('fs');
const path = require('path');

module.exports = {
reporters: ['lcov', 'html', 'text', 'json-summary']
reporters: ['lcov', 'html', 'text', 'json-summary'],

modifyAssetLocation(root, relativePath) {
let appPath = relativePath.replace('my-app-with-in-repo-addon', 'app');

if (!fs.existsSync(appPath) && fs.existsSync(path.join(root, 'lib', 'my-in-repo-addon', appPath))) {
return path.join('lib', 'my-in-repo-addon', appPath);
}

return false;
}
};
13 changes: 0 additions & 13 deletions test-packages/my-app-with-in-repo-addon/ember-cli-build.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

const EmberApp = require('ember-cli/lib/broccoli/ember-app');
const fs = require('fs');
const path = require('path');

module.exports = function(defaults) {
let app = new EmberApp(defaults, {
Expand All @@ -11,17 +9,6 @@ module.exports = function(defaults) {
...require('ember-cli-code-coverage').buildBabelPlugin(),
],
},
'ember-cli-code-coverage': {
modifyAssetLocation(root, relativePath) {
let appPath = relativePath.replace('my-app-with-in-repo-addon', 'app');

if (!fs.existsSync(appPath) && fs.existsSync(path.join(root, 'lib/my-in-repo-addon', appPath))) {
return path.join('lib/my-in-repo-addon', appPath);
}

return false;
}
}
});

// Use `app.import` to add additional libraries to the generated
Expand Down

0 comments on commit 83f57d8

Please sign in to comment.