Skip to content

Commit 83f57d8

Browse files
committed
Move modifyAssetLocation from build options to the config
1 parent 3a69bd3 commit 83f57d8

File tree

5 files changed

+38
-42
lines changed

5 files changed

+38
-42
lines changed

README.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ Configuration is optional. It should be put in a file at `config/coverage.js` (`
158158

159159
- `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`).
160160

161+
- `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.
162+
161163
#### Example
162164
```js
163165
module.exports = {
@@ -190,7 +192,9 @@ If you are using [`ember-cli-pretender`](https://github.com/rwjblue/ember-cli-pr
190192

191193
## Advanced customization
192194

193-
The `forceModulesToBeLoaded` can potientally cause unindented side effects when executed. You can pass custom filter fuctions that allow
195+
### `forceModulesToBeLoaded`
196+
197+
The `forceModulesToBeLoaded` function can potentially cause unintended side effects when executed. You can pass custom filter fuctions that allow
194198
you to specify which modules will be force loaded or not:
195199

196200
```js
@@ -201,28 +205,28 @@ QUnit.done(async () => {
201205
});
202206
```
203207

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

210216
```js
211-
const app = new EmberApp(defaults, {
212-
'ember-cli-code-coverage': {
213-
modifyAssetLocation(root, relativePath) {
214-
let appPath = relativePath.replace('my-project-name', 'app');
215-
216-
// here is an example of saying that `app/components/foo.js` actually
217-
// lives in `lib/inrepo/app/components/foo.js` on disk.
218-
if (fs.existsSync(path.join(root, 'lib/inrepo', appPath))) {
219-
return path.join('lib/inrepo', appPath);
220-
}
221-
222-
return false;
223-
},
217+
module.exports = {
218+
modifyAssetLocation(root, relativePath) {
219+
let appPath = relativePath.replace('my-project-name', 'app');
220+
221+
// here is an example of saying that `app/components/foo.js` actually
222+
// lives in `lib/inrepo/app/components/foo.js` on disk.
223+
if (fs.existsSync(path.join(root, 'lib', 'inrepo', appPath))) {
224+
return path.join('lib', 'inrepo', appPath);
225+
}
226+
227+
return false;
224228
},
225-
});
229+
};
226230
```
227231

228232
## Inspiration

packages/ember-cli-code-coverage/index.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@ module.exports = {
7171
};
7272
},
7373

74-
included(app) {
75-
this._super.included.apply(this, arguments);
76-
let config = app.options[this.name] || {};
77-
this.modifyAssetLocation = config && config.modifyAssetLocation;
78-
},
79-
8074
buildNamespaceMappings() {
8175
let rootNamespaceMappings = new Map();
8276
function recurse(item) {
@@ -124,19 +118,15 @@ module.exports = {
124118
attachMiddleware.serverMiddleware(startOptions.app, {
125119
configPath: this.project.configPath(),
126120
root: this.project.root,
127-
fileLookup: this.fileLookup,
128121
namespaceMappings: this.buildNamespaceMappings(),
129-
modifyAssetLocation: this.modifyAssetLocation,
130122
});
131123
},
132124

133125
testemMiddleware(app) {
134126
const config = {
135127
configPath: this.project.configPath(),
136128
root: this.project.root,
137-
fileLookup: this.fileLookup,
138129
namespaceMappings: this.buildNamespaceMappings(),
139-
modifyAssetLocation: this.modifyAssetLocation,
140130
};
141131
// if we're running `ember test --server` use the `serverMiddleware`.
142132
if (process.argv.includes('--server') || process.argv.includes('-s')) {

packages/ember-cli-code-coverage/lib/attach-middleware.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ function adjustCoverageKey(
112112
}
113113

114114
function adjustCoverage(coverage, options) {
115-
let { root, namespaceMappings, modifyAssetLocation } = options;
115+
let { root, namespaceMappings, configPath } = options;
116+
let { modifyAssetLocation } = getConfig(configPath);
117+
116118
const adjustedCoverage = Object.keys(coverage).reduce((memo, filePath) => {
117119
let relativeToProjectRoot = adjustCoverageKey(
118120
root,
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
14
module.exports = {
2-
reporters: ['lcov', 'html', 'text', 'json-summary']
5+
reporters: ['lcov', 'html', 'text', 'json-summary'],
6+
7+
modifyAssetLocation(root, relativePath) {
8+
let appPath = relativePath.replace('my-app-with-in-repo-addon', 'app');
9+
10+
if (!fs.existsSync(appPath) && fs.existsSync(path.join(root, 'lib', 'my-in-repo-addon', appPath))) {
11+
return path.join('lib', 'my-in-repo-addon', appPath);
12+
}
13+
14+
return false;
15+
}
316
};

test-packages/my-app-with-in-repo-addon/ember-cli-build.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
22

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

75
module.exports = function(defaults) {
86
let app = new EmberApp(defaults, {
@@ -11,17 +9,6 @@ module.exports = function(defaults) {
119
...require('ember-cli-code-coverage').buildBabelPlugin(),
1210
],
1311
},
14-
'ember-cli-code-coverage': {
15-
modifyAssetLocation(root, relativePath) {
16-
let appPath = relativePath.replace('my-app-with-in-repo-addon', 'app');
17-
18-
if (!fs.existsSync(appPath) && fs.existsSync(path.join(root, 'lib/my-in-repo-addon', appPath))) {
19-
return path.join('lib/my-in-repo-addon', appPath);
20-
}
21-
22-
return false;
23-
}
24-
}
2512
});
2613

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

0 commit comments

Comments
 (0)