8
0

utils.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 chai = require( 'chai' );
  8. const expect = chai.expect;
  9. const sinon = require( 'sinon' );
  10. const gulp = require( 'gulp' );
  11. const gutil = require( 'gulp-util' );
  12. const path = require( 'path' );
  13. const stream = require( 'stream' );
  14. const Vinyl = require( 'vinyl' );
  15. describe( 'build-utils', () => {
  16. const utils = require( '../../tasks/gulp/build/utils' );
  17. let sandbox;
  18. beforeEach( () => {
  19. sandbox = sinon.sandbox.create();
  20. } );
  21. afterEach( () => {
  22. sandbox.restore();
  23. } );
  24. describe( 'noop', () => {
  25. it( 'should return PassTrough stream', () => {
  26. const PassThrough = stream.PassThrough;
  27. const ret = utils.noop();
  28. expect( ret instanceof PassThrough ).to.equal( true );
  29. } );
  30. } );
  31. describe( 'dist', () => {
  32. it( 'should return stream created with gulp.dest', () => {
  33. const distDir = 'dist/';
  34. const format = 'amd';
  35. const destSpy = sandbox.spy( gulp, 'dest' );
  36. const stream = utils.dist( distDir, format );
  37. sinon.assert.calledOnce( destSpy );
  38. sinon.assert.calledWithExactly( destSpy, path.join( distDir, format ) );
  39. expect( stream ).to.equal( destSpy.firstCall.returnValue );
  40. } );
  41. } );
  42. describe( 'transpile', () => {
  43. it( 'should throw an exception when incorrect format is provided', () => {
  44. const transpileSpy = sandbox.spy( utils, 'transpile' );
  45. const format = 'incorrect-format';
  46. let error;
  47. try {
  48. transpileSpy( format );
  49. } catch ( e ) {
  50. error = e;
  51. }
  52. sinon.assert.threw( transpileSpy, error );
  53. expect( error.message ).to.equal( `Incorrect format: ${ format }` );
  54. } );
  55. it( 'should return babel transform stream', ( done ) => {
  56. const modulePath = '../files/utils/lib';
  57. const babelStream = utils.transpile( 'amd' );
  58. const Stream = stream.Stream;
  59. const appendModuleExtensionSpy = sandbox.spy( utils, 'appendModuleExtension' );
  60. expect( babelStream instanceof Stream ).to.equal( true );
  61. expect( babelStream.readable ).to.equal( true );
  62. expect( babelStream.writable ).to.equal( true );
  63. babelStream.on( 'finish', ( ) => {
  64. sinon.assert.calledOnce( appendModuleExtensionSpy );
  65. sinon.assert.calledWithExactly( appendModuleExtensionSpy, modulePath );
  66. expect( appendModuleExtensionSpy.firstCall.returnValue ).to.equal( modulePath + '.js' );
  67. done();
  68. } );
  69. babelStream.write( new Vinyl( {
  70. cwd: '/',
  71. base: '/test/',
  72. path: '/test/file.js',
  73. contents: new Buffer( `import * as lib from '${ modulePath }'` )
  74. } ) );
  75. babelStream.end();
  76. } );
  77. it( 'should report error when transpiling fails', ( done ) => {
  78. const babelStream = utils.transpile( 'amd' );
  79. const utilLogStub = sandbox.stub( gutil, 'log' );
  80. const consoleLogStub = sandbox.stub( console, 'log' );
  81. babelStream.once( 'finish', () => {
  82. sinon.assert.calledTwice( utilLogStub );
  83. sinon.assert.calledOnce( consoleLogStub );
  84. done();
  85. } );
  86. babelStream.write( new Vinyl( {
  87. cwd: '/',
  88. base: '/test/',
  89. path: '/test/file.js',
  90. contents: new Buffer( 'class ;' )
  91. } ) );
  92. babelStream.end();
  93. } );
  94. } );
  95. describe( 'getConversionStreamGenerator', () => {
  96. it( 'should return function that can be used for creating conversion streams', () => {
  97. const distDir = 'dist/';
  98. const formats = [ 'amd', 'cjs', 'esnext' ];
  99. const fn = utils.getConversionStreamGenerator( distDir );
  100. const streams = formats.reduce( fn, [] );
  101. expect( streams.length ).to.equal( formats.length );
  102. } );
  103. } );
  104. describe( 'pickVersionedFile', () => {
  105. it( 'should rename file for provided format', ( done ) => {
  106. const rename = utils.pickVersionedFile( 'amd' );
  107. rename.once( 'finish', () => {
  108. done();
  109. } );
  110. rename.on( 'data', ( data ) => {
  111. expect( data.basename ).to.equal( 'load.js' );
  112. } );
  113. rename.write( new Vinyl( {
  114. cwd: '/',
  115. base: '/test/',
  116. path: '/test/load__amd.js',
  117. contents: new Buffer( '' )
  118. } ) );
  119. rename.end();
  120. } );
  121. } );
  122. describe( 'unpackPackages', () => {
  123. it( 'should move files to correct directories', ( done ) => {
  124. const rename = utils.unpackPackages();
  125. rename.once( 'finish', () => {
  126. done();
  127. } );
  128. rename.on( 'data', ( data ) => {
  129. expect( data.path ).to.equal( 'ckeditor5-core/file.js' );
  130. } );
  131. rename.write( new Vinyl( {
  132. cwd: './',
  133. path: 'ckeditor5-core/src/file.js',
  134. contents: new Buffer( '' )
  135. } ) );
  136. rename.end();
  137. } );
  138. } );
  139. describe( 'wrapCKEditor5Package', () => {
  140. it( 'should add `ckeditor5/` to a file path', ( done ) => {
  141. const rename = utils.wrapCKEditor5Package();
  142. const filePath = './test/file.js';
  143. const path = require( 'path' );
  144. rename.once( 'finish', () => {
  145. done();
  146. } );
  147. rename.on( 'data', ( data ) => {
  148. expect( data.path ).to.equal( path.join( 'ckeditor5', filePath ) );
  149. } );
  150. rename.write( new Vinyl( {
  151. cwd: './',
  152. path: filePath,
  153. contents: new Buffer( '' )
  154. } ) );
  155. rename.end();
  156. } );
  157. } );
  158. describe( 'appendModuleExtension', ( ) => {
  159. it( 'appends module extension when path provided', () => {
  160. const filePath = './path/to/file';
  161. const source = utils.appendModuleExtension( filePath );
  162. expect( source ).to.equal( filePath + '.js' );
  163. } );
  164. it( 'appends module extension when URL is provided', () => {
  165. const url = 'http://example.com/lib';
  166. const source = utils.appendModuleExtension( url );
  167. expect( source ).to.equal( url + '.js' );
  168. } );
  169. it( 'returns unchanged if module is provided', () => {
  170. const module = 'lib/module';
  171. const source = utils.appendModuleExtension( module );
  172. expect( source ).to.equal( module );
  173. } );
  174. } );
  175. } );