lint.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global describe, it, beforeEach, afterEach */
  6. const sinon = require( 'sinon' );
  7. const gulp = require( 'gulp' );
  8. const gutil = require( 'gulp-util' );
  9. const stream = require( 'stream' );
  10. const Vinyl = require( 'vinyl' );
  11. const jshint = require( 'gulp-jshint' );
  12. const jscs = require( 'gulp-jscs' );
  13. const concat = require( 'concat-stream' );
  14. const chai = require( 'chai' );
  15. const expect = chai.expect;
  16. const mockery = require( 'mockery' );
  17. const through = require( 'through2' );
  18. describe( 'lint', () => {
  19. 'use strict';
  20. const config = {
  21. ROOT_DIR: '.',
  22. BUILD_DIR: 'build',
  23. IGNORED_FILES: [ 'lib/**' ]
  24. };
  25. let sandbox;
  26. beforeEach( () => {
  27. mockery.enable( {
  28. warnOnReplace: false,
  29. warnOnUnregistered: false
  30. } );
  31. mockery.registerMock( 'git-guppy', () => {
  32. return {
  33. stream() {
  34. const files = [
  35. new Vinyl( {
  36. cwd: './',
  37. path: './ckeditor.js',
  38. contents: new Buffer( 'function test () {};var a;' )
  39. } )
  40. ];
  41. const fakeInputStream = new stream.Readable( { objectMode: true } );
  42. fakeInputStream._read = () => {
  43. fakeInputStream.push( files.pop() || null );
  44. };
  45. return fakeInputStream;
  46. }
  47. };
  48. } );
  49. sandbox = sinon.sandbox.create();
  50. } );
  51. afterEach( () => {
  52. mockery.disable();
  53. sandbox.restore();
  54. } );
  55. describe( 'lint()', () => {
  56. it( 'should use jshint and jscs on source files', ( done ) => {
  57. const files = [
  58. new Vinyl( {
  59. cwd: './',
  60. path: './ckeditor.js',
  61. contents: new Buffer( 'function test () {};var a;' )
  62. } )
  63. ];
  64. const tasks = require( '../tasks/lint/tasks' )( config );
  65. const PassThrough = stream.PassThrough;
  66. sandbox.stub( gulp, 'src', () => {
  67. const fakeInputStream = new stream.Readable( { objectMode: true } );
  68. fakeInputStream._read = () => {
  69. fakeInputStream.push( files.pop() || null );
  70. };
  71. return fakeInputStream;
  72. } );
  73. sandbox.stub( jscs, 'reporter', () => new PassThrough( { objectMode: true } ) );
  74. sandbox.stub( jshint, 'reporter', () => new PassThrough( { objectMode: true } ) );
  75. tasks.lint().pipe( concat( ( data ) => {
  76. expect( data.length ).to.equal( 1 );
  77. const file = data[ 0 ];
  78. expect( typeof file.jscs ).to.equal( 'object' );
  79. expect( typeof file.jshint ).to.equal( 'object' );
  80. expect( file.jscs.success ).to.equal( false );
  81. expect( file.jshint.success ).to.equal( false );
  82. done();
  83. } ) );
  84. } );
  85. } );
  86. describe( 'lintStaged', () => {
  87. it( 'should throw error when linting fails', ( done ) => {
  88. const tasks = require( '../tasks/lint/tasks' )( config );
  89. const PassThrough = stream.PassThrough;
  90. const exitStub = sandbox.stub( process, 'exit' );
  91. sandbox.stub( gutil, 'log' );
  92. sandbox.stub( jscs, 'reporter', ( type ) => {
  93. if ( type == 'fail' ) {
  94. // Fail reporter should report error to stop linting process.
  95. return through( { objectMode: true }, ( file, encoding, cb ) => {
  96. cb( new Error() );
  97. expect( typeof file.jscs ).to.equal( 'object' );
  98. expect( typeof file.jshint ).to.equal( 'object' );
  99. expect( file.jscs.success ).to.equal( false );
  100. expect( file.jshint.success ).to.equal( false );
  101. sinon.assert.calledOnce( exitStub );
  102. done();
  103. } );
  104. } else {
  105. return new PassThrough( { objectMode: true } );
  106. }
  107. } );
  108. sandbox.stub( jshint, 'reporter', () => new PassThrough( { objectMode: true } ) );
  109. tasks.lintStaged();
  110. } );
  111. } );
  112. } );