Procházet zdrojové kódy

Tests for pre-commit gulp task.

Szymon Kupś před 10 roky
rodič
revize
9b6a078de8
1 změnil soubory, kde provedl 54 přidání a 24 odebrání
  1. 54 24
      dev/tests/lint.js

+ 54 - 24
dev/tests/lint.js

@@ -7,14 +7,16 @@
 
 const sinon = require( 'sinon' );
 const gulp = require( 'gulp' );
+const gutil = require( 'gulp-util' );
 const stream = require( 'stream' );
 const Vinyl = require( 'vinyl' );
 const jshint = require( 'gulp-jshint' );
 const jscs = require( 'gulp-jscs' );
-const guppy = require( 'git-guppy' )( gulp );
 const concat = require( 'concat-stream' );
 const chai = require( 'chai' );
 const expect = chai.expect;
+const mockery = require( 'mockery' );
+const through = require( 'through2' );
 
 describe( 'lint', () => {
 	'use strict';
@@ -24,20 +26,45 @@ describe( 'lint', () => {
 		DIST_DIR: 'dist',
 		IGNORED_FILES: [ 'lib/**' ]
 	};
-	const tasks = require( '../tasks/lint/tasks' )( config );
+
 	let sandbox;
 
 	beforeEach( () => {
+		mockery.enable( {
+			warnOnReplace: false,
+			warnOnUnregistered: false
+		} );
+
+		mockery.registerMock( 'git-guppy', () => {
+			return {
+				stream() {
+					const files = [
+						new Vinyl( {
+							cwd: './',
+							path: './ckeditor.js',
+							contents: new Buffer( 'function test () {};var a;' )
+						} )
+					];
+					const fakeInputStream = new stream.Readable( { objectMode: true } );
+					fakeInputStream._read = () => {
+						fakeInputStream.push( files.pop() || null );
+					};
+
+					return fakeInputStream;
+				}
+			};
+		} );
+
 		sandbox = sinon.sandbox.create();
 	} );
 
 	afterEach( () => {
+		mockery.disable();
 		sandbox.restore();
 	} );
 
 	describe( 'lint()', () => {
 		it( 'should use jshint and jscs on source files', ( done ) => {
-			const PassThrough = stream.PassThrough;
 			const files = [
 				new Vinyl( {
 					cwd: './',
@@ -46,6 +73,9 @@ describe( 'lint', () => {
 				} )
 			];
 
+			const tasks = require( '../tasks/lint/tasks' )( config );
+			const PassThrough = stream.PassThrough;
+
 			sandbox.stub( gulp, 'src', () => {
 				const fakeInputStream = new stream.Readable( { objectMode: true } );
 				fakeInputStream._read = () => {
@@ -72,29 +102,29 @@ describe( 'lint', () => {
 
 	describe( 'pre-commit', () => {
 		it( 'should throw error when linting fails', ( done ) => {
-			//////const PassThrough = stream.PassThrough;
-			const files = [
-				new Vinyl( {
-					cwd: './',
-					path: './ckeditor.js',
-					contents: new Buffer( 'function test () {};var a;' )
-				} )
-			];
-
-			sandbox.stub( guppy, 'stream', () => {
-				console.log( 'guppy stream' );
-				const fakeInputStream = new stream.Readable( { objectMode: true } );
-				fakeInputStream._read = () => {
-					fakeInputStream.push( files.pop() || null );
-				};
+			const tasks = require( '../tasks/lint/tasks' )( config );
+			const PassThrough = stream.PassThrough;
 
-				return fakeInputStream;
+			const exitStub = sandbox.stub( process, 'exit' );
+			sandbox.stub( gutil, 'log' );
+			sandbox.stub( jscs, 'reporter', ( type ) => {
+				if ( type == 'fail' ) {
+					// Fail reporter should report error to stop linting process.
+					return through( { objectMode: true }, ( file, encoding, cb ) => {
+						cb( new Error() );
+						expect( typeof file.jscs ).to.equal( 'object' );
+						expect( typeof file.jshint ).to.equal( 'object' );
+						expect( file.jscs.success ).to.equal( false );
+						expect( file.jshint.success ).to.equal( false );
+						sinon.assert.calledOnce( exitStub );
+						done();
+					} );
+				} else {
+					return new PassThrough( { objectMode: true } );
+				}
 			} );
-
-			tasks.preCommit().pipe( concat( ( d ) => {
-				console.log( d.length );
-				done();
-			} ) );
+			sandbox.stub( jshint, 'reporter', () => new PassThrough( { objectMode: true } ) );
+			tasks.preCommit();
 		} );
 	} );
 } );