utils.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 gutil = require( 'gulp-util' );
  11. const path = require( 'path' );
  12. const fs = require( 'fs' );
  13. const mainUtils = require( '../../tasks/utils' );
  14. describe( 'bundle-utils', () => {
  15. const utils = require( '../../tasks/bundle/utils' );
  16. let sandbox;
  17. beforeEach( () => {
  18. sandbox = sinon.sandbox.create();
  19. } );
  20. afterEach( () => {
  21. sandbox.restore();
  22. } );
  23. it( 'should be extended by level utils', () => {
  24. expect( utils.clean ).to.be.equal( mainUtils.clean );
  25. } );
  26. describe( 'getFileSize', () => {
  27. it( 'should return human readable file size', () => {
  28. let filePath = 'path/to/file';
  29. let statSyncMock = sandbox.stub( fs, 'statSync', () => {
  30. return { size: 102400 };
  31. } );
  32. expect( utils.getFileSize( filePath ) ).to.be.equal( '100 KB' );
  33. sinon.assert.calledWithExactly( statSyncMock, filePath );
  34. } );
  35. } );
  36. describe( 'logFilesSize', () => {
  37. let gutilLogSpy;
  38. beforeEach( () => {
  39. gutilLogSpy = sandbox.stub( gutil, 'log' );
  40. sandbox.stub( utils, 'getFileSize', () => '1 MB' );
  41. } );
  42. it( 'should log only files base name with file size separate by new line character', () => {
  43. const expected = gutil.colors.green( `\nfile1.js: 1 MB\nfile2.js: 1 MB` );
  44. utils.logFilesSize( [ 'sub/dir/file1.js', 'other/sub/dir/file2.js' ] , 'root/path' );
  45. sinon.assert.calledWithExactly( gutilLogSpy, expected );
  46. } );
  47. it( 'should get files from root directory', () => {
  48. let basenameSpy = sandbox.spy( path, 'basename' );
  49. utils.logFilesSize( [ 'sub/dir/file1.js', 'other/sub/dir/file2.js' ] , 'root/path' );
  50. sinon.assert.calledWithExactly( basenameSpy.firstCall, 'root/path/sub/dir/file1.js' );
  51. sinon.assert.calledWithExactly( basenameSpy.secondCall, 'root/path/other/sub/dir/file2.js' );
  52. } );
  53. it( 'should get files if root directory is not specified', () => {
  54. let basenameSpy = sandbox.spy( path, 'basename' );
  55. utils.logFilesSize( [ 'sub/dir/file1.js', 'file2.js' ] );
  56. sinon.assert.calledWithExactly( basenameSpy.firstCall, 'sub/dir/file1.js' );
  57. sinon.assert.calledWithExactly( basenameSpy.secondCall, 'file2.js' );
  58. } );
  59. } );
  60. } );