8
0

tools.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /* global describe, it, beforeEach, afterEach */
  7. const chai = require( 'chai' );
  8. const sinon = require( 'sinon' );
  9. const expect = chai.expect;
  10. const tools = require( '../utils/tools' );
  11. const path = require( 'path' );
  12. let toRestore;
  13. describe( 'utils', () => {
  14. beforeEach( () => toRestore = [] );
  15. afterEach( () => {
  16. toRestore.forEach( item => item.restore() );
  17. } );
  18. describe( 'tools', () => {
  19. describe( 'linkDirectories', () => {
  20. it( 'should be defined', () => expect( tools.linkDirectories ).to.be.a( 'function' ) );
  21. it( 'should run link commands', () => {
  22. const shExecStub = sinon.stub( tools, 'shExec' );
  23. const isDirectoryStub = sinon.stub( tools, 'isDirectory' ).returns( false );
  24. const source = '/source/dir';
  25. const destination = '/destination/dir';
  26. toRestore.push( shExecStub, isDirectoryStub );
  27. tools.linkDirectories( source, destination );
  28. expect( isDirectoryStub.calledOnce ).to.equal( true );
  29. expect( shExecStub.calledOnce ).to.equal( true );
  30. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `ln -s ${ source } ${ destination }` );
  31. } );
  32. it( 'should remove destination directory before linking', () => {
  33. const shExecStub = sinon.stub( tools, 'shExec' );
  34. const isDirectoryStub = sinon.stub( tools, 'isDirectory' ).returns( true );
  35. const source = '/source/dir';
  36. const destination = '/destination/dir';
  37. toRestore.push( shExecStub, isDirectoryStub );
  38. tools.linkDirectories( source, destination );
  39. expect( isDirectoryStub.calledOnce ).to.equal( true );
  40. expect( shExecStub.calledTwice ).to.equal( true );
  41. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `rm -rf ${ destination }` );
  42. expect( shExecStub.secondCall.args[ 0 ] ).to.equal( `ln -s ${ source } ${ destination }` );
  43. } );
  44. } );
  45. describe( 'getCKEditorDependencies', () => {
  46. it( 'should be defined', () => expect( tools.getCKEditorDependencies ).to.be.a( 'function' ) );
  47. it( 'should return null if no CKEditor5 repository is found', () => {
  48. const dependencies = {
  49. 'plugin1': '',
  50. 'plugin2': '',
  51. 'plugin3': ''
  52. };
  53. expect( tools.getCKEditorDependencies( dependencies ) ).to.equal( null );
  54. } );
  55. it( 'should return only ckeditor5- dependencies', () => {
  56. const dependencies = {
  57. 'plugin1': '',
  58. 'ckeditor5-plugin-image': 'ckeditor/ckeditor5-plugin-image',
  59. 'plugin2': '',
  60. 'ckeditor5-core': 'ckeditor/ckeditor5-core'
  61. };
  62. const ckeditorDependencies = tools.getCKEditorDependencies( dependencies );
  63. expect( ckeditorDependencies ).to.be.an( 'object' );
  64. expect( ckeditorDependencies.plugin1 ).to.be.a( 'undefined' );
  65. expect( ckeditorDependencies.plugin2 ).to.be.a( 'undefined' );
  66. expect( ckeditorDependencies[ 'ckeditor5-plugin-image' ] ).to.be.a( 'string' );
  67. expect( ckeditorDependencies[ 'ckeditor5-core' ] ).to.be.a( 'string' );
  68. } );
  69. } );
  70. describe( 'getDirectories', () => {
  71. it( 'should be defined', () => expect( tools.getDirectories ).to.be.a( 'function' ) );
  72. it( 'should get directories in specified path', () => {
  73. const fs = require( 'fs' );
  74. const directories = [ 'dir1', 'dir2', 'dir3' ];
  75. const readdirSyncStub = sinon.stub( fs, 'readdirSync', () => directories );
  76. const isDirectoryStub = sinon.stub( tools, 'isDirectory' ).returns( true );
  77. const dirPath = 'path';
  78. toRestore.push( readdirSyncStub, isDirectoryStub );
  79. tools.getDirectories( dirPath );
  80. expect( readdirSyncStub.calledOnce ).to.equal( true );
  81. expect( isDirectoryStub.calledThrice ).to.equal( true );
  82. expect( isDirectoryStub.firstCall.args[ 0 ] ).to.equal( path.join( dirPath, directories[ 0 ] ) );
  83. expect( isDirectoryStub.secondCall.args[ 0 ] ).to.equal( path.join( dirPath, directories[ 1 ] ) );
  84. expect( isDirectoryStub.thirdCall.args[ 0 ] ).to.equal( path.join( dirPath, directories[ 2 ] ) );
  85. } );
  86. } );
  87. describe( 'isDirectory', () => {
  88. it( 'should be defined', () => expect( tools.isDirectory ).to.be.a( 'function' ) );
  89. it( 'should return true if path points to directory', () => {
  90. const fs = require( 'fs' );
  91. const statSyncStub = sinon.stub( fs, 'statSync', () => ( { isDirectory: () => true } ) );
  92. const path = 'path';
  93. toRestore.push( statSyncStub );
  94. const result = tools.isDirectory( path );
  95. expect( statSyncStub.calledOnce ).to.equal( true );
  96. expect( statSyncStub.firstCall.args[ 0 ] ).to.equal( path );
  97. expect( result ).to.equal( true );
  98. } );
  99. it( 'should return false if path does not point to directory', () => {
  100. const fs = require( 'fs' );
  101. const statSyncStub = sinon.stub( fs, 'statSync', () => ( { isDirectory: () => false } ) );
  102. const path = 'path';
  103. toRestore.push( statSyncStub );
  104. const result = tools.isDirectory( path );
  105. expect( statSyncStub.calledOnce ).to.equal( true );
  106. expect( statSyncStub.firstCall.args[ 0 ] ).to.equal( path );
  107. expect( result ).to.equal( false );
  108. } );
  109. it( 'should return false if statSync method throws', () => {
  110. const fs = require( 'fs' );
  111. const statSyncStub = sinon.stub( fs, 'statSync' ).throws();
  112. const path = 'path';
  113. toRestore.push( statSyncStub );
  114. const result = tools.isDirectory( path );
  115. expect( statSyncStub.calledOnce ).to.equal( true );
  116. expect( statSyncStub.firstCall.args[ 0 ] ).to.equal( path );
  117. expect( result ).to.equal( false );
  118. } );
  119. } );
  120. describe( 'getCKE5Directories', () => {
  121. it( 'should be defined', () => expect( tools.getCKE5Directories ).to.be.a( 'function' ) );
  122. it( 'should return only ckeditor5 directories', () => {
  123. const workspacePath = '/workspace/path';
  124. const sourceDirectories = [ 'tools', 'ckeditor5', 'ckeditor5-core', '.bin', 'ckeditor5-plugin-image' ];
  125. const getDirectoriesStub = sinon.stub( tools, 'getDirectories', () => sourceDirectories );
  126. toRestore.push( getDirectoriesStub );
  127. const directories = tools.getCKE5Directories( workspacePath );
  128. expect( directories.length ).equal( 2 );
  129. expect( directories[ 0 ] ).equal( 'ckeditor5-core' );
  130. expect( directories[ 1 ] ).equal( 'ckeditor5-plugin-image' );
  131. } );
  132. } );
  133. } );
  134. } );