| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- /**
- * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* global describe, it, beforeEach, afterEach */
- 'use strict';
- const chai = require( 'chai' );
- const expect = chai.expect;
- const sinon = require( 'sinon' );
- const gulp = require( 'gulp' );
- const gutil = require( 'gulp-util' );
- const path = require( 'path' );
- const stream = require( 'stream' );
- const Vinyl = require( 'vinyl' );
- describe( 'build-utils', () => {
- const utils = require( '../../tasks/gulp/build/utils' );
- let sandbox;
- beforeEach( () => {
- sandbox = sinon.sandbox.create();
- } );
- afterEach( () => {
- sandbox.restore();
- } );
- describe( 'noop', () => {
- it( 'should return PassTrough stream', () => {
- const PassThrough = stream.PassThrough;
- const ret = utils.noop();
- expect( ret instanceof PassThrough ).to.equal( true );
- } );
- } );
- describe( 'dist', () => {
- it( 'should return stream created with gulp.dest', () => {
- const distDir = 'dist/';
- const format = 'amd';
- const destSpy = sandbox.spy( gulp, 'dest' );
- const stream = utils.dist( distDir, format );
- sinon.assert.calledOnce( destSpy );
- sinon.assert.calledWithExactly( destSpy, path.join( distDir, format ) );
- expect( stream ).to.equal( destSpy.firstCall.returnValue );
- } );
- } );
- describe( 'transpile', () => {
- it( 'should throw an exception when incorrect format is provided', () => {
- const transpileSpy = sandbox.spy( utils, 'transpile' );
- const format = 'incorrect-format';
- expect( () => {
- transpileSpy( format );
- } ).to.throw( Error, `Incorrect format: ${ format }` );
- } );
- it( 'should return babel transform stream', ( done ) => {
- const modulePath = '../files/utils/lib';
- const babelStream = utils.transpile( 'amd' );
- const Stream = stream.Stream;
- const appendModuleExtensionSpy = sandbox.spy( utils, 'appendModuleExtension' );
- expect( babelStream instanceof Stream ).to.equal( true );
- expect( babelStream.readable ).to.equal( true );
- expect( babelStream.writable ).to.equal( true );
- babelStream.on( 'finish', ( ) => {
- sinon.assert.calledOnce( appendModuleExtensionSpy );
- sinon.assert.calledWithExactly( appendModuleExtensionSpy, modulePath );
- expect( appendModuleExtensionSpy.firstCall.returnValue ).to.equal( modulePath + '.js' );
- done();
- } );
- babelStream.write( new Vinyl( {
- cwd: '/',
- base: '/test/',
- path: '/test/file.js',
- contents: new Buffer( `import * as lib from '${ modulePath }'` )
- } ) );
- babelStream.end();
- } );
- it( 'should report error when transpiling fails', ( done ) => {
- const babelStream = utils.transpile( 'amd' );
- const utilLogStub = sandbox.stub( gutil, 'log' );
- const consoleLogStub = sandbox.stub( console, 'log' );
- babelStream.once( 'finish', () => {
- sinon.assert.calledTwice( utilLogStub );
- sinon.assert.calledOnce( consoleLogStub );
- done();
- } );
- babelStream.write( new Vinyl( {
- cwd: '/',
- base: '/test/',
- path: '/test/file.js',
- contents: new Buffer( 'class ;' )
- } ) );
- babelStream.end();
- } );
- } );
- describe( 'getConversionStreamGenerator', () => {
- it( 'should return function that can be used for creating conversion streams', () => {
- const distDir = 'dist/';
- const formats = [ 'amd', 'cjs', 'esnext' ];
- const fn = utils.getConversionStreamGenerator( distDir );
- const streams = formats.reduce( fn, [] );
- expect( streams.length ).to.equal( formats.length );
- } );
- } );
- describe( 'pickVersionedFile', () => {
- it( 'should rename file for provided format', ( done ) => {
- const rename = utils.pickVersionedFile( 'amd' );
- rename.once( 'finish', () => {
- done();
- } );
- rename.on( 'data', ( data ) => {
- expect( data.basename ).to.equal( 'load.js' );
- } );
- rename.write( new Vinyl( {
- cwd: '/',
- base: '/test/',
- path: '/test/load__amd.js',
- contents: new Buffer( '' )
- } ) );
- rename.end();
- } );
- } );
- describe( 'unpackPackages', () => {
- it( 'should move files to correct directories', ( done ) => {
- const rename = utils.unpackPackages();
- rename.once( 'finish', () => {
- done();
- } );
- rename.on( 'data', ( data ) => {
- expect( data.path ).to.equal( 'ckeditor5-core/file.js' );
- } );
- rename.write( new Vinyl( {
- cwd: './',
- path: 'ckeditor5-core/src/file.js',
- contents: new Buffer( '' )
- } ) );
- rename.end();
- } );
- it( 'should throw error when wrong path provided 1', () => {
- const rename = utils.unpackPackages();
- expect( () => {
- rename.write( new Vinyl( {
- cwd: './',
- path: 'plugin/src/file.js',
- contents: new Buffer( '' )
- } ) );
- } ).to.throw( Error );
- } );
- it( 'should throw error when wrong path provided 2', () => {
- const rename = utils.unpackPackages();
- expect( () => {
- rename.write( new Vinyl( {
- cwd: './',
- path: 'ckeditor5-core/file.js',
- contents: new Buffer( '' )
- } ) );
- } ).to.throw( Error );
- } );
- } );
- describe( 'wrapCKEditor5Package', () => {
- it( 'should add `ckeditor5/` to a file path', ( done ) => {
- const rename = utils.wrapCKEditor5Package();
- const filePath = './test/file.js';
- const path = require( 'path' );
- rename.once( 'finish', () => {
- done();
- } );
- rename.on( 'data', ( data ) => {
- expect( data.path ).to.equal( path.join( 'ckeditor5', filePath ) );
- } );
- rename.write( new Vinyl( {
- cwd: './',
- path: filePath,
- contents: new Buffer( '' )
- } ) );
- rename.end();
- } );
- } );
- describe( 'appendModuleExtension', ( ) => {
- it( 'appends module extension when path provided', () => {
- const filePath = './path/to/file';
- const source = utils.appendModuleExtension( filePath );
- expect( source ).to.equal( filePath + '.js' );
- } );
- it( 'appends module extension when URL is provided', () => {
- const url = 'http://example.com/lib';
- const source = utils.appendModuleExtension( url );
- expect( source ).to.equal( url + '.js' );
- } );
- it( 'returns unchanged if module is provided', () => {
- const module = 'lib/module';
- const source = utils.appendModuleExtension( module );
- expect( source ).to.equal( module );
- } );
- } );
- } );
|