utils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 path = require( 'path' );
  11. const fs = require( 'fs' );
  12. const mainUtils = require( '../../tasks/utils' );
  13. const gzipSize = require( 'gzip-size' );
  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 top level utils', () => {
  24. expect( utils.clean ).to.be.equal( mainUtils.clean );
  25. } );
  26. describe( 'getFileSize', () => {
  27. it( 'should return file size in bytes', () => {
  28. const filePath = 'path/to/file';
  29. const size = 1337;
  30. const statSyncMock = sandbox.stub( fs, 'statSync', () => {
  31. return { size };
  32. } );
  33. expect( utils.getFileSize( filePath ) ).to.be.equal( size );
  34. sinon.assert.calledWithExactly( statSyncMock, filePath );
  35. } );
  36. } );
  37. describe( 'getGzippedFileSize', () => {
  38. it( 'should return file size in bytes', () => {
  39. const filePath = 'path/to/file';
  40. const size = 1337;
  41. const fileContent = 'some string';
  42. const readFileSyncMock = sandbox.stub( fs, 'readFileSync', () => fileContent );
  43. const gzipSizeMock = sandbox.stub( gzipSize, 'sync', () => 1337 );
  44. expect( utils.getGzippedFileSize( filePath ) ).to.be.equal( size );
  45. sinon.assert.calledWithExactly( readFileSyncMock, filePath );
  46. sinon.assert.calledWithExactly( gzipSizeMock, fileContent );
  47. } );
  48. } );
  49. describe( 'getFilesSizeStats', () => {
  50. let size, gzippedSize;
  51. beforeEach( () => {
  52. size = 1337;
  53. gzippedSize = 543;
  54. sandbox.stub( utils, 'getFileSize', () => size );
  55. sandbox.stub( utils, 'getGzippedFileSize', () => gzippedSize );
  56. } );
  57. it( 'should returns an array with two elements', () => {
  58. const result = utils.getFilesSizeStats( [ 'sub/dir/file.js', 'other/sub/dir/file.css' ] , 'root/path' );
  59. expect( result ).to.be.an( 'array' );
  60. expect( result ).to.have.length( 2 );
  61. } );
  62. it( 'should returns list of object with files stats', () => {
  63. const result = utils.getFilesSizeStats( [ 'sub/dir/file.js', 'other/sub/dir/file.css' ] , 'root/path' );
  64. expect( result ).to.be.deep.equal( [
  65. { name: 'file.js', size, gzippedSize },
  66. { name: 'file.css', size, gzippedSize }
  67. ] );
  68. } );
  69. it( 'should get files from root directory', () => {
  70. let basenameSpy = sandbox.spy( path, 'basename' );
  71. const result = utils.getFilesSizeStats( [ 'sub/dir/file.js', 'other/sub/dir/file.css' ] , 'root/path' );
  72. expect( result[0] ).to.have.property( 'name' ).equal( 'file.js' );
  73. expect( result[1] ).to.have.property( 'name' ).equal( 'file.css' );
  74. sinon.assert.calledWithExactly( basenameSpy.firstCall, 'root/path/sub/dir/file.js' );
  75. sinon.assert.calledWithExactly( basenameSpy.secondCall, 'root/path/other/sub/dir/file.css' );
  76. } );
  77. it( 'should get files if root directory is not specified', () => {
  78. let basenameSpy = sandbox.spy( path, 'basename' );
  79. const result = utils.getFilesSizeStats( [ 'sub/dir/file.js', 'file.css' ] );
  80. expect( result[0] ).to.have.property( 'name' ).equal( 'file.js' );
  81. expect( result[1] ).to.have.property( 'name' ).equal( 'file.css' );
  82. sinon.assert.calledWithExactly( basenameSpy.firstCall, 'sub/dir/file.js' );
  83. sinon.assert.calledWithExactly( basenameSpy.secondCall, 'file.css' );
  84. } );
  85. } );
  86. } );