8
0

tasks.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global describe, it, beforeEach, afterEach */
  6. 'use strict';
  7. const mockery = require( 'mockery' );
  8. const sinon = require( 'sinon' );
  9. const Vinyl = require( 'vinyl' );
  10. const utils = require( '../../tasks/gulp/build/utils' );
  11. const babel = require( 'babel-core' );
  12. const chai = require( 'chai' );
  13. const expect = chai.expect;
  14. const gutil = require( 'gulp-util' );
  15. const gulp = require( 'gulp' );
  16. const path = require( 'path' );
  17. const through = require( 'through2' );
  18. describe( 'build-tasks', () => {
  19. let sandbox, tasks;
  20. const config = {
  21. ROOT_DIR: '.',
  22. DIST_DIR: 'dist'
  23. };
  24. beforeEach( () => {
  25. mockery.enable( {
  26. warnOnReplace: false,
  27. warnOnUnregistered: false
  28. } );
  29. sandbox = sinon.sandbox.create();
  30. mockery.registerMock( 'minimist', () => {
  31. return {
  32. formats: 'amd',
  33. watch: false
  34. };
  35. } );
  36. tasks = require( '../../tasks/gulp/build/tasks' )( config );
  37. } );
  38. afterEach( () => {
  39. mockery.disable();
  40. sandbox.restore();
  41. } );
  42. describe( 'packages', () => {
  43. it( 'should return stream with correct packages as src', () => {
  44. const fs = require( 'fs' );
  45. const readDirStub = sandbox.stub( fs, 'readdirSync', () => [ 'ckeditor5-core', 'ckeditor5-toolbar' ] );
  46. const statStub = sandbox.stub( fs, 'lstatSync', () => {
  47. return {
  48. isDirectory() {
  49. return true;
  50. },
  51. isSymbolicLink() {
  52. return false;
  53. }
  54. };
  55. } );
  56. const gulpSrcSpy = sandbox.spy( gulp, 'src' );
  57. tasks.src.packages( false );
  58. sinon.assert.calledOnce( readDirStub );
  59. sinon.assert.calledTwice( gulpSrcSpy );
  60. sinon.assert.calledTwice( statStub );
  61. expect( gulpSrcSpy.firstCall.args[ 0 ] ).to.equal( path.join( 'node_modules', 'ckeditor5-core', '@(src|tests)', '**', '*' ) );
  62. expect( gulpSrcSpy.secondCall.args[ 0 ] ).to.equal( path.join( 'node_modules', 'ckeditor5-toolbar', '@(src|tests)', '**', '*' ) );
  63. } );
  64. it( 'should skip files and resolve symbolic links', () => {
  65. const fs = require( 'fs' );
  66. const readDirStub = sandbox.stub( fs, 'readdirSync', () => [ 'ckeditor5-file.js' ] );
  67. const statStub = sandbox.stub( fs, 'lstatSync', () => {
  68. return {
  69. isDirectory() {
  70. return false;
  71. },
  72. isSymbolicLink() {
  73. return true;
  74. }
  75. };
  76. } );
  77. const realPathStub = sandbox.stub( fs, 'realpathSync', () => '/real/path' );
  78. const gulpSrcSpy = sandbox.spy( gulp, 'src' );
  79. tasks.src.packages( false );
  80. sinon.assert.calledOnce( readDirStub );
  81. sinon.assert.calledOnce( realPathStub );
  82. sinon.assert.notCalled( gulpSrcSpy );
  83. sinon.assert.calledTwice( statStub );
  84. } );
  85. } );
  86. describe( 'build', () => {
  87. it( 'should return build stream', ( done ) => {
  88. const code = 'export default {};';
  89. sandbox.stub( gutil, 'log' );
  90. const build = tasks.build;
  91. const stream = require( 'stream' );
  92. const files = [
  93. new Vinyl( {
  94. cwd: './',
  95. path: './src/file.js',
  96. contents: new Buffer( code )
  97. } )
  98. ];
  99. let written = 0;
  100. // Stub input stream.
  101. sandbox.stub( tasks.src, 'all', () => {
  102. const fakeInputStream = new stream.Readable( { objectMode: true } );
  103. fakeInputStream._read = () => {
  104. fakeInputStream.push( files.pop() || null );
  105. };
  106. return fakeInputStream;
  107. } );
  108. // Stub output stream.
  109. sandbox.stub( utils, 'dist', () => {
  110. return through( { objectMode: true }, ( file, encoding, cb ) => {
  111. written++;
  112. const result = babel.transform( code, { plugins: [ 'transform-es2015-modules-amd' ] } );
  113. // Check if provided code was transformed by babel.
  114. expect( file.contents.toString() ).to.equal( result.code );
  115. cb( null, file );
  116. } );
  117. } );
  118. const conversionStream = build();
  119. conversionStream.on( 'finish', () => {
  120. expect( written ).to.equal( 1 );
  121. done();
  122. } );
  123. } );
  124. } );
  125. } );