Skip to content

Commit

Permalink
Accept non-string inputTrees
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Iaconis committed Mar 24, 2016
1 parent 5017db2 commit 6bad055
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
15 changes: 12 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint global-require: 0, consistent-return: 0 */
/* eslint global-require: 0, consistent-return: 0, no-underscore-dangle: 0 */

const Filter = require('broccoli-filter');
const CLIEngine = require('eslint').CLIEngine;
const path = require('path');

/**
* Calculates the severity of a eslint.linter.verify result
Expand Down Expand Up @@ -57,6 +58,13 @@ function filterAllIgnoredFileMessages(result) {
return resultOutput;
}

function resolveInputDirectory(inputTree) {
if (typeof inputTree === 'string') {
return inputTree;
}
return resolveInputDirectory(inputTree._inputNodes[0]);
}

/**
* Uses the content of each file in a given tree and runs eslint validation on it.
* @param {Object} inputTree Tree from broccoli.makeTree
Expand All @@ -77,7 +85,7 @@ function EslintValidationFilter(inputTree, options) {

this.cli = new CLIEngine(this.eslintOptions);

this.eslintrc = inputTree;
this.eslintrc = resolveInputDirectory(inputTree);

this.testGenerator = options.testGenerator;
if (this.testGenerator) {
Expand All @@ -95,7 +103,8 @@ EslintValidationFilter.prototype.processString = function processString(content,
'use strict'; // eslint-disable-line strict

// verify file content
const output = this.cli.executeOnText(content, this.eslintrc + '/' + relativePath);
const configPath = path.join(this.eslintrc, path.basename(relativePath));
const output = this.cli.executeOnText(content, configPath);
const filteredOutput = filterAllIgnoredFileMessages(output);

// if verification has result
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"broccoli": "^0.16.9",
"broccoli-cli": "^1.0.0",
"broccoli-merge-trees": "^1.1.1",
"broccoli-stew": "^1.2.0",
"chai": "^3.5.0",
"eslint-config-nightmare-mode": "^2.3.0",
"mocha": "^2.2.4",
Expand Down
32 changes: 21 additions & 11 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const fs = require('fs');
const expect = require('chai').expect;
const stew = require('broccoli-stew');
const find = stew.find;
const mv = stew.mv;
const runEslint = require('./helpers/run-eslint');
const FIXTURES = 'test/fixture';
const CAMELCASE = '(camelcase)';
Expand All @@ -9,18 +12,22 @@ const DOUBLEQUOTE = 'Strings must use doublequote.';
const FILEPATH = 'fixture/1.js';

describe('EslintValidationFilter', function describeEslintValidationFilter() {
it('should report errors', function shouldReportErrors() {
// lint test fixtures
const promise = runEslint(FIXTURES);
function shouldReportErrors(inputTree) {
return function _shouldReportErrors() {
// lint test fixtures
const promise = runEslint(inputTree);

return promise.then(function assertLinting({buildLog}) {
expect(buildLog, 'Used eslint validation').to.have.string(CAMELCASE);
expect(buildLog, 'Shows filepath').to.have.string(FILEPATH);
expect(buildLog, 'Used relative config - console not allowed').to.have.string(CONSOLE);
expect(buildLog, 'Used relative config - single quotes').to.not.have.string(DOUBLEQUOTE);
expect(buildLog, 'No custom rules defined').to.not.have.string(CUSTOM_RULES);
});
});
return promise.then(function assertLinting({buildLog}) {
expect(buildLog, 'Used eslint validation').to.have.string(CAMELCASE);
expect(buildLog, 'Shows filepath').to.have.string(FILEPATH);
expect(buildLog, 'Used relative config - console not allowed').to.have.string(CONSOLE);
expect(buildLog, 'Used relative config - single quotes').to.not.have.string(DOUBLEQUOTE);
expect(buildLog, 'No custom rules defined').to.not.have.string(CUSTOM_RULES);
});
};
}

it('should report errors', shouldReportErrors(FIXTURES));

it('should accept rule paths', function shouldAcceptRulePaths() {
// lint test fixtures using a custom rule
Expand Down Expand Up @@ -62,4 +69,7 @@ describe('EslintValidationFilter', function describeEslintValidationFilter() {
expect(content, 'Used the testGenerator').to.equal('test-content');
});
});

// specify test fixtures via a broccoli tree/node
it('should accept a tree/node as the input', shouldReportErrors(find(mv(FIXTURES, 'foobar/fixture'))));
});

0 comments on commit 6bad055

Please sign in to comment.