Skip to content

Commit

Permalink
Fix infinite loop rendering bug (#173)
Browse files Browse the repository at this point in the history
* Fix infinite loop rendering bug

* Update coveralls syntax

* Remove coveralls
  • Loading branch information
ismay authored Sep 27, 2019
1 parent ed51958 commit 58cc355
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 13 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ node_js:
- "10"
jobs:
include:
- stage: coverage
node_js: "10"
script:
- npm run test:coverage
- stage: lint
node_js: "10"
script:
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# metalsmith-in-place

[![build status][build-badge]][build-url]
[![coverage status][coverage-badge]][coverage-url]
[![greenkeeper][greenkeeper-badge]][greenkeeper-url]

> A metalsmith plugin for transforming your source files
Expand Down Expand Up @@ -133,7 +132,5 @@ There are several things that might cause you to get a `no files to process` err
[build-url]: https://travis-ci.org/metalsmith/metalsmith-in-place
[greenkeeper-badge]: https://badges.greenkeeper.io/metalsmith/metalsmith-in-place.svg
[greenkeeper-url]: https://greenkeeper.io/
[coverage-badge]: https://coveralls.io/repos/github/metalsmith/metalsmith-in-place/badge.svg?branch=master
[coverage-url]: https://coveralls.io/github/metalsmith/metalsmith-in-place?branch=master
[slack-url]: http://metalsmith-slack.herokuapp.com/
[stackoverflow-url]: http://stackoverflow.com/questions/tagged/metalsmith
10 changes: 8 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function render({ filename, files, metalsmith, settings }) {
const file = files[filename];
const engineOptions = Object.assign({}, settings.engineOptions);
const metadata = metalsmith.metadata();
const isLastExtension = extensions.length === 1;

debug(`rendering ${filename}`);

Expand All @@ -30,7 +31,7 @@ function render({ filename, files, metalsmith, settings }) {
const contents = file.contents.toString();

// If this is the last extension, replace it with a new one
if (extensions.length === 0) {
if (isLastExtension) {
debug(`last extension reached, replacing last extension with ${transform.outputFormat}`);
extensions.push(transform.outputFormat);
}
Expand All @@ -57,7 +58,12 @@ function render({ filename, files, metalsmith, settings }) {

debug(`done rendering ${filename}, renamed to ${newName}`);

// Keep rendering until there are no applicable transformers left
// Stop rendering if this was the last extension
if (isLastExtension) {
return Promise.resolve();
}

// Otherwise, keep rendering until there are no applicable transformers left
return render({ filename: newName, files, metalsmith, settings });
})
.catch(err => {
Expand Down
32 changes: 32 additions & 0 deletions lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const plugin = require('./index');
describe('metalsmith-in-place', () => {
beforeEach(() => {
jest.resetModules();
jest.restoreAllMocks();
});

it('should process a single file', done => {
Expand All @@ -28,6 +29,37 @@ describe('metalsmith-in-place', () => {
});
});

it('should stop processing after the last extension has been processed', done => {
const base = path.join(process.cwd(), 'test', 'fixtures', 'stop-processing');
const actual = path.join(base, 'build');
const expected = path.join(base, 'expected');
const metalsmith = new Metalsmith(base);

jest.doMock('./get-transformer', () => () => ({
name: 'html',
inputFormats: ['html'],
outputFormat: 'html',
render() {
return { body: 'rendered' };
},
renderAsync() {
return Promise.resolve({ body: 'rendered' });
}
}));
// eslint-disable-next-line global-require, no-shadow
const plugin = require('./index');

rimraf.sync(actual);

return metalsmith.use(plugin()).build(err => {
if (err) {
return done(err);
}
expect(() => equal(actual, expected)).not.toThrow();
return done();
});
});

it('should process multiple files', done => {
const base = path.join(process.cwd(), 'test', 'fixtures', 'multiple-files');
const actual = path.join(base, 'build');
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"precommit": "lint-staged",
"lint:js": "eslint '**/*.js'",
"lint:prettier": "prettier --list-different '**/*.js'",
"test": "jest",
"test:coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls"
"test": "jest"
},
"jest": {
"collectCoverageFrom": [
Expand All @@ -21,7 +20,6 @@
"greenkeeper": {
"ignore": [
"assert-dir-equal",
"coveralls",
"eslint",
"eslint-config-airbnb-base",
"eslint-config-prettier",
Expand All @@ -38,7 +36,6 @@
},
"devDependencies": {
"assert-dir-equal": "^1.1.0",
"coveralls": "^3.0.0",
"eslint": "^4.19.1",
"eslint-config-airbnb-base": "^13.0.0",
"eslint-config-prettier": "^2.9.0",
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/stop-processing/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rendered
1 change: 1 addition & 0 deletions test/fixtures/stop-processing/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h2 id="title">Title</h2>

0 comments on commit 58cc355

Please sign in to comment.