8
0

utils.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. expect( () => {
  47. transpileSpy( format );
  48. } ).to.throw( Error, `Incorrect format: ${ format }` );
  49. } );
  50. it( 'should return babel transform stream', ( done ) => {
  51. const modulePath = '../files/utils/lib';
  52. const babelStream = utils.transpile( 'amd' );
  53. const Stream = stream.Stream;
  54. const appendModuleExtensionSpy = sandbox.spy( utils, 'appendModuleExtension' );
  55. expect( babelStream instanceof Stream ).to.equal( true );
  56. expect( babelStream.readable ).to.equal( true );
  57. expect( babelStream.writable ).to.equal( true );
  58. babelStream.on( 'finish', ( ) => {
  59. sinon.assert.calledOnce( appendModuleExtensionSpy );
  60. sinon.assert.calledWithExactly( appendModuleExtensionSpy, modulePath );
  61. expect( appendModuleExtensionSpy.firstCall.returnValue ).to.equal( modulePath + '.js' );
  62. done();
  63. } );
  64. babelStream.write( new Vinyl( {
  65. cwd: '/',
  66. base: '/test/',
  67. path: '/test/file.js',
  68. contents: new Buffer( `import * as lib from '${ modulePath }'` )
  69. } ) );
  70. babelStream.end();
  71. } );
  72. it( 'should report error when transpiling fails', ( done ) => {
  73. const babelStream = utils.transpile( 'amd' );
  74. const utilLogStub = sandbox.stub( gutil, 'log' );
  75. const consoleLogStub = sandbox.stub( console, 'log' );
  76. babelStream.once( 'finish', () => {
  77. sinon.assert.calledTwice( utilLogStub );
  78. sinon.assert.calledOnce( consoleLogStub );
  79. done();
  80. } );
  81. babelStream.write( new Vinyl( {
  82. cwd: '/',
  83. base: '/test/',
  84. path: '/test/file.js',
  85. contents: new Buffer( 'class ;' )
  86. } ) );
  87. babelStream.end();
  88. } );
  89. } );
  90. describe( 'getConversionStreamGenerator', () => {
  91. it( 'should return function that can be used for creating conversion streams', () => {
  92. const distDir = 'dist/';
  93. const formats = [ 'amd', 'cjs', 'esnext' ];
  94. const fn = utils.getConversionStreamGenerator( distDir );
  95. const streams = formats.reduce( fn, [] );
  96. expect( streams.length ).to.equal( formats.length );
  97. } );
  98. } );
  99. describe( 'pickVersionedFile', () => {
  100. it( 'should rename file for provided format', ( done ) => {
  101. const rename = utils.pickVersionedFile( 'amd' );
  102. rename.once( 'finish', () => {
  103. done();
  104. } );
  105. rename.on( 'data', ( data ) => {
  106. expect( data.basename ).to.equal( 'load.js' );
  107. } );
  108. rename.write( new Vinyl( {
  109. cwd: '/',
  110. base: '/test/',
  111. path: '/test/load__amd.js',
  112. contents: new Buffer( '' )
  113. } ) );
  114. rename.end();
  115. } );
  116. } );
  117. describe( 'unpackPackages', () => {
  118. it( 'should move files to correct directories', ( done ) => {
  119. const rename = utils.unpackPackages();
  120. rename.once( 'finish', () => {
  121. done();
  122. } );
  123. rename.on( 'data', ( data ) => {
  124. expect( data.path ).to.equal( 'ckeditor5-core/file.js' );
  125. } );
  126. rename.write( new Vinyl( {
  127. cwd: './',
  128. path: 'ckeditor5-core/src/file.js',
  129. contents: new Buffer( '' )
  130. } ) );
  131. rename.end();
  132. } );
  133. it( 'should throw error when wrong path provided 1', () => {
  134. const rename = utils.unpackPackages();
  135. expect( () => {
  136. rename.write( new Vinyl( {
  137. cwd: './',
  138. path: 'plugin/src/file.js',
  139. contents: new Buffer( '' )
  140. } ) );
  141. } ).to.throw( Error );
  142. } );
  143. it( 'should throw error when wrong path provided 2', () => {
  144. const rename = utils.unpackPackages();
  145. expect( () => {
  146. rename.write( new Vinyl( {
  147. cwd: './',
  148. path: 'ckeditor5-core/file.js',
  149. contents: new Buffer( '' )
  150. } ) );
  151. } ).to.throw( Error );
  152. } );
  153. } );
  154. describe( 'wrapCKEditor5Package', () => {
  155. it( 'should add `ckeditor5/` to a file path', ( done ) => {
  156. const rename = utils.wrapCKEditor5Package();
  157. const filePath = './test/file.js';
  158. const path = require( 'path' );
  159. rename.once( 'finish', () => {
  160. done();
  161. } );
  162. rename.on( 'data', ( data ) => {
  163. expect( data.path ).to.equal( path.join( 'ckeditor5', filePath ) );
  164. } );
  165. rename.write( new Vinyl( {
  166. cwd: './',
  167. path: filePath,
  168. contents: new Buffer( '' )
  169. } ) );
  170. rename.end();
  171. } );
  172. } );
  173. describe( 'appendModuleExtension', ( ) => {
  174. it( 'appends module extension when path provided', () => {
  175. const filePath = './path/to/file';
  176. const source = utils.appendModuleExtension( filePath );
  177. expect( source ).to.equal( filePath + '.js' );
  178. } );
  179. it( 'appends module extension when URL is provided', () => {
  180. const url = 'http://example.com/lib';
  181. const source = utils.appendModuleExtension( url );
  182. expect( source ).to.equal( url + '.js' );
  183. } );
  184. it( 'returns unchanged if module is provided', () => {
  185. const module = 'lib/module';
  186. const source = utils.appendModuleExtension( module );
  187. expect( source ).to.equal( module );
  188. } );
  189. } );
  190. } );