From 387a2ba1a896c687a505c22a5cba54c798de6c33 Mon Sep 17 00:00:00 2001 From: billybonks Date: Thu, 15 Mar 2018 07:39:03 +0800 Subject: [PATCH 1/2] update tests --- jest-test.config.js | 4 ++-- tests/__snapshots__/test.js.snap | 19 +++++++++++++++++++ tests/test.js | 28 +++------------------------- 3 files changed, 24 insertions(+), 27 deletions(-) create mode 100644 tests/__snapshots__/test.js.snap diff --git a/jest-test.config.js b/jest-test.config.js index 51501c5..35a4993 100644 --- a/jest-test.config.js +++ b/jest-test.config.js @@ -1,8 +1,8 @@ module.exports = { displayName: 'test', - testRegex: '.*(test.js)', + testRegex: '.*(.test.js)', watchPathIgnorePatterns: ['/tmp'], - testPathIgnorePatterns: ['tmp', 'node_modules'], + testPathIgnorePatterns: ['tmp', 'node_modules', '/__snapshots__/'], coverageDirectory: 'coverage', coverageReporters: ['lcov'] }; diff --git a/tests/__snapshots__/test.js.snap b/tests/__snapshots__/test.js.snap new file mode 100644 index 0000000..63d210f --- /dev/null +++ b/tests/__snapshots__/test.js.snap @@ -0,0 +1,19 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Broccoli StyleLint Plugin Generated Tests generates correct failing test string 1`] = ` +"QUnit.module('Stylelint'); +QUnit.test('has-errors.scss should pass stylelint', function(assert) { + assert.expect(1); + assert.ok(false, '1:15 Unexpected empty block (block-no-empty)\\\\n6:10 Expected \\\\\\\\\\"#000000\\\\\\\\\\" to be \\\\\\\\\\"black\\\\\\\\\\" (color-named)'); +}); +" +`; + +exports[`Broccoli StyleLint Plugin Generated Tests generates correct passing test string 1`] = ` +"QUnit.module('Stylelint'); +QUnit.test('no-errors.scss should pass stylelint', function(assert) { + assert.expect(1); + assert.ok(false, 'no-errors.scss should pass stylelint'); +}); +" +`; diff --git a/tests/test.js b/tests/test.js index 068759f..3641199 100644 --- a/tests/test.js +++ b/tests/test.js @@ -96,7 +96,7 @@ describe('Broccoli StyleLint Plugin', function() { }); it('should log when log=true', co.wrap(function *() { - yield buildAndLint('tests/fixtures/has-errors', {disableConsoleLogging:false, console: fakeConsole}); + yield buildAndLint('tests/fixtures/has-errors', {log:true, console: fakeConsole}); expect(fakeConsole.log).toHaveBeenCalled(); })); @@ -114,11 +114,6 @@ describe('Broccoli StyleLint Plugin', function() { let linter = StyleLinter.create('', options); expect(linter.testGenerator).toEqual(require('../lib/test-generator')); }); - - it('uses old geneator if testingFramework key is not present', function() { - let linter = StyleLinter.create('', {}); - expect(linter.testGenerator).toEqual(require('../lib/test-generator-old')); - }); }); @@ -147,12 +142,6 @@ describe('Broccoli StyleLint Plugin', function() { expect(linter.linterConfig).toEqual(linterConfig); }); - it('accepts a custom logger', co.wrap(function *() { - var spy = jest.fn(); - yield buildAndLint('tests/fixtures/test-generation', {linterConfig:{syntax:'scss'}, log:true, consoleLogger:spy}); - expect(spy).toHaveBeenCalled(); - })); - describe('Tests', function () { describe('Generate Tests', function(){ @@ -223,24 +212,13 @@ describe('Broccoli StyleLint Plugin', function() { })); it('generates correct failing test string', co.wrap(function *() { - var testAssertion = 'module(\'Style Lint\');\n'+ - 'test(\'has-errors.scss should pass stylelint\', function() {\n'+ - ' ok(false, \'1:15 Unexpected empty block (block-no-empty)\');\n'+ - ' ok(false, \'6:10 Expected \\"#000000\\" to be \\"black\\" (color-named)\');\n'+ - '});\n'; - let results = yield buildAndLint('tests/fixtures/has-errors', {testFailingFiles:true}); - return expect(readTestFile(walkTestsOutputReadableTree(results))).toBe(testAssertion); + return expect(readTestFile(walkTestsOutputReadableTree(results))).toMatchSnapshot(); })); it('generates correct passing test string', co.wrap(function *() { - var passedTestAssertion = 'module(\'Style Lint\');\n'+ - 'test(\'no-errors.scss should pass stylelint\', function() {\n'+ - ' ok(\'true , no-errors.scss passed stylelint\');\n'+ - '});\n'; - let results = yield buildAndLint('tests/fixtures/no-errors', {testPassingFiles:true}); - return expect(readTestFile(walkTestsOutputReadableTree(results))).toBe(passedTestAssertion); + return expect(readTestFile(walkTestsOutputReadableTree(results))).toMatchSnapshot(); })); }); }); From be1d88189a5409a49286aed109e5192d9f4ad2cd Mon Sep 17 00:00:00 2001 From: billybonks Date: Thu, 15 Mar 2018 07:39:49 +0800 Subject: [PATCH 2/2] remove deprecations --- index.js | 59 +++++++++------------------------------ lib/test-generator-old.js | 20 ------------- 2 files changed, 13 insertions(+), 66 deletions(-) delete mode 100644 lib/test-generator-old.js diff --git a/index.js b/index.js index cbf8ad7..fdd2d34 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,6 @@ const stylelint = require('stylelint'); const path = require('path'); const broccoliNodeInfo = require('broccoli-node-info'); const chalk = require('chalk'); -const oldGenerator = require('./lib/test-generator-old'); const FACTORY_METHOD_USED = Symbol('create() factory method was used'); //Copied from stylelint, until style lint ignores files properly via node api @@ -66,6 +65,7 @@ class StyleLinter extends Filter { * @class */ constructor(inputNodes, options) { + super(inputNodes, options); this.options = options || {linterConfig:{}}; @@ -77,24 +77,8 @@ class StyleLinter extends Filter { 'calling Stylelint() directly or using new Stylelint()'); } - if(options.consoleLogger){ - console.warn('After 2.0 release "consoleLogger" propety will be removed in favour of stylelint formatter option'); - } - this.compileOptions(options); this.setSyntax(this.linterConfig); - - if(typeof options['disableConsoleLogging'] !== 'undefined'){ - console.warn('After 2.0 release "disableConsoleLogging" propety will be removed in favour of "log"'); - this.log = !options['disableConsoleLogging']; - } - - //TODO:remove this deprecation on v2 release - if(this.testGenerator === oldGenerator){ - console.warn(`After 2.0 the default test generator will be removed in favour of a standard one, To migrate specify the "testingFramework" option in 2.0 it will default to qunit`); - } - - } static create(inputNode, _options){ @@ -105,18 +89,13 @@ class StyleLinter extends Filter { } compileOptions(options){ - if(options.testingFramework){ - options.testGenerator = require('./lib/test-generator'); - } - /* Used to extract and delete options from input hash */ const availableOptions = [{name: 'onError'}, {name: 'disableTestGeneration'}, - {name: 'testingFramework'}, + {name: 'testingFramework', default:'qunit'}, {name: 'testFailingFiles'}, {name: 'testPassingFiles'}, - {name: 'testGenerator', default: oldGenerator}, - {name: 'consoleLogger', default: StyleLinter.prototype.consoleLogger}, + {name: 'testGenerator', default: require('./lib/test-generator')}, {name: 'linterConfig', default: {}}, {name: 'log', default: true}, {name: 'console', default: console}]; @@ -127,7 +106,6 @@ class StyleLinter extends Filter { let defaultValue = option.default || this[name]; this[name] = typeof options[name] === 'undefined' ? defaultValue : options[name]; } - if(typeof this.testFailingFiles === 'undefined' && typeof this.testPassingFiles === 'undefined' && typeof this.disableTestGeneration === 'undefined'){ this.testFailingFiles = true; this.testPassingFiles = true; @@ -136,9 +114,11 @@ class StyleLinter extends Filter { this.testPassingFiles = typeof this.testPassingFiles === 'undefined' ? !this.disableTestGeneration : this.testPassingFiles; } + if(this.testPassingFiles || this.testFailingFiles){ + this.targetExtension = 'stylelint-test.js' ; + } this.linterConfig = Object.assign({formatter: 'string'}, this.linterConfig); this.linterConfig.files = null; - } /** * Sets the, file extensions that the broccoli plugin must parse @@ -161,13 +141,7 @@ class StyleLinter extends Filter { config.syntax = ''; } extensions.push(targetExtension); - if(this.testPassingFiles || this.testFailingFiles) { - targetExtension = 'stylelint-test.js'; - } else { - console.warn('After 2.0 release if not tests are disabled, broccoli-stylelint will not return the original files'); - } this.extensions = extensions; - this.targetExtension = targetExtension; } /** Filter Class Overrides **/ @@ -197,11 +171,13 @@ class StyleLinter extends Filter { //sets the value to relative path otherwise it would be absolute path results = self.processResults(results, relativePath); if(results.errored && self.testFailingFiles) { - results.output = self.testGenerator(relativePath, results); + results.output = self.testGenerator(relativePath, results, this.testingFramework); + return results; } else if(!results.errored && self.testPassingFiles) { - results.output = self.testGenerator(relativePath); + results.output = self.testGenerator(relativePath, results, this.testingFramework); + return results; } - return results; + return ''; }).catch(err => { console.error(chalk.red('======= Something went wrong running stylelint =======')); if(err.code === 78){ @@ -224,14 +200,14 @@ class StyleLinter extends Filter { * - Generate tests * @override */ - postProcess(results, relativePath) { + postProcess(results) { if(results) { if(results.errored){ if(this.onError) { this.onError(results); } if(this.log) { - this.consoleLogger(results, relativePath); + this.console.log(results.log); } } return results; @@ -268,15 +244,6 @@ class StyleLinter extends Filter { return results; } - /** - * @method consoleLogger - * - * custom console logger - */ - consoleLogger(results) { - this.console.log(results.log); - } - } module.exports = StyleLinter; diff --git a/lib/test-generator-old.js b/lib/test-generator-old.js deleted file mode 100644 index 5acc725..0000000 --- a/lib/test-generator-old.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict'; - -const escapeString = require('js-string-escape'); - -module.exports = function(relativePath, errors) { - let assertions = []; - let module = 'module(\'Style Lint\');\n'; - let test = 'test(\'' + relativePath + ' should pass stylelint\', function() {\n'; - if(!errors){ - let assertion = ' ok(\'true , '+relativePath+' passed stylelint\');'; - return module+test+assertion+'\n});\n'; - } else { - for(let i = 0; i < errors.warnings.length; i++){ - let warning = errors.warnings[i]; - let index = warning.line+':'+warning.column; - assertions.push(' ok(' + false + ', \''+index +' '+escapeString(warning.text)+'\');'); - } - return module+test+assertions.join('\n')+'\n});\n'; - } -};