ckeditor5-dirs.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @license Copyright (c) 2003-2016, 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 sinon = require( 'sinon' );
  9. const expect = chai.expect;
  10. const path = require( 'path' );
  11. const ckeditor5Dirs = require( '../../utils/ckeditor5-dirs' );
  12. const tools = require( '../../utils/tools' );
  13. const git = require( '../../utils/git' );
  14. let sandbox;
  15. describe( 'utils', () => {
  16. beforeEach( () => {
  17. sandbox = sinon.sandbox.create();
  18. } );
  19. afterEach( () => {
  20. sandbox.restore();
  21. } );
  22. describe( 'ckeditor5dirs', () => {
  23. describe( 'getDependencies', () => {
  24. it( 'should be defined', () => expect( ckeditor5Dirs.getDependencies ).to.be.a( 'function' ) );
  25. it( 'should return null if no CKEditor5 repository is found', () => {
  26. const packageJSONDependencies = {
  27. 'plugin1': '',
  28. 'plugin2': '',
  29. 'plugin3': ''
  30. };
  31. expect( ckeditor5Dirs.getDependencies( packageJSONDependencies ) ).to.equal( null );
  32. expect( ckeditor5Dirs.getDependencies() ).to.equal( null );
  33. } );
  34. it( 'should return only ckeditor5- dependencies', () => {
  35. const packageJSONDependencies = {
  36. 'plugin1': '',
  37. 'ckeditor5-plugin-image': 'ckeditor/ckeditor5-plugin-image',
  38. 'plugin2': '',
  39. 'ckeditor5-core': 'ckeditor/ckeditor5-core'
  40. };
  41. const ckeditorDependencies = ckeditor5Dirs.getDependencies( packageJSONDependencies );
  42. expect( ckeditorDependencies ).to.be.an( 'object' );
  43. expect( ckeditorDependencies.plugin1 ).to.be.a( 'undefined' );
  44. expect( ckeditorDependencies.plugin2 ).to.be.a( 'undefined' );
  45. expect( ckeditorDependencies[ 'ckeditor5-plugin-image' ] ).to.be.a( 'string' );
  46. expect( ckeditorDependencies[ 'ckeditor5-core' ] ).to.be.a( 'string' );
  47. } );
  48. } );
  49. describe( 'getDirectories', () => {
  50. it( 'should be defined', () => expect( ckeditor5Dirs.getDirectories ).to.be.a( 'function' ) );
  51. it( 'should return only ckeditor5 directories', () => {
  52. const workspacePath = '/workspace/path';
  53. const sourceDirectories = [ 'tools', 'ckeditor5', 'ckeditor5-core', '.bin', 'ckeditor5-plugin-image' ];
  54. sandbox.stub( tools, 'getDirectories', () => sourceDirectories );
  55. const directories = ckeditor5Dirs.getDirectories( workspacePath );
  56. expect( directories.length ).to.equal( 2 );
  57. expect( directories[ 0 ] ).to.equal( 'ckeditor5-core' );
  58. expect( directories[ 1 ] ).to.equal( 'ckeditor5-plugin-image' );
  59. } );
  60. } );
  61. describe( 'getSymlinks', () => {
  62. it( 'should return CKE5 symlinks from provided path', () => {
  63. const fs = require( 'fs' );
  64. const path = 'path/to/dir';
  65. sandbox.stub( fs, 'readdirSync' ).returns( [ 'ckeditor5-core', 'ckeditor5-image', 'other-dependency' ] );
  66. sandbox.stub( tools, 'isSymlink' ).returns( true );
  67. const symlinks = ckeditor5Dirs.getSymlinks( path );
  68. expect( symlinks.length ).to.equal( 2 );
  69. expect( symlinks[ 0 ] ).to.equal( 'ckeditor5-core' );
  70. expect( symlinks[ 1 ] ).to.equal( 'ckeditor5-image' );
  71. } );
  72. } );
  73. describe( 'getDevDirectories', () => {
  74. const packageJSONDependencies = {};
  75. const workspacePath = '/workspace/path';
  76. const ckeditor5Path = path.join( workspacePath, 'ckeditor5' );
  77. const dependencies = {
  78. 'ckeditor5-plugin-image': 'ckeditor/ckeditor5-plugin-image',
  79. 'ckeditor5-core': 'ckeditor/ckeditor5-core'
  80. };
  81. const sourceDirectories = [ 'tools', 'ckeditor5', 'ckeditor5-core', '.bin', 'ckeditor5-plugin-image' ];
  82. const repositoryInfo = { name: 'ckeditor5-core' };
  83. it( 'should be defined', () => expect( ckeditor5Dirs.getDevDirectories ).to.be.a( 'function' ) );
  84. it( 'should return empty array if no dev directories were found - because of missing ckeditor5-* repos', () => {
  85. const wrongRepositoryInfo = { name: 'plugins/plugin' };
  86. sandbox.stub( ckeditor5Dirs, 'getDirectories', () => sourceDirectories );
  87. sandbox.stub( ckeditor5Dirs, 'getDependencies', () => dependencies );
  88. sandbox.stub( git, 'parseRepositoryUrl' ).returns( wrongRepositoryInfo );
  89. const directories = ckeditor5Dirs.getDevDirectories( workspacePath, packageJSONDependencies, ckeditor5Path );
  90. expect( directories ).to.be.a( 'array' );
  91. expect( directories.length ).to.equal( 0 );
  92. } );
  93. it( 'should return empty array if no dev directories were found - because of missing ckeditor5-* dirs', () => {
  94. const wrongDirectories = [ 'tools', 'ckeditor5', '.bin' ];
  95. sandbox.stub( ckeditor5Dirs, 'getDirectories', () => wrongDirectories );
  96. sandbox.stub( ckeditor5Dirs, 'getDependencies', () => dependencies );
  97. sandbox.stub( git, 'parseRepositoryUrl' ).returns( repositoryInfo );
  98. const directories = ckeditor5Dirs.getDevDirectories( workspacePath, packageJSONDependencies, ckeditor5Path );
  99. expect( directories ).to.be.a( 'array' );
  100. expect( directories.length ).to.equal( 0 );
  101. } );
  102. it( 'should return only ckeditor5 directories in development mode', () => {
  103. sandbox.stub( ckeditor5Dirs, 'getDirectories', () => sourceDirectories );
  104. sandbox.stub( ckeditor5Dirs, 'getDependencies', () => dependencies );
  105. sandbox.stub( git, 'parseRepositoryUrl' ).returns( repositoryInfo );
  106. const directories = ckeditor5Dirs.getDevDirectories( workspacePath, packageJSONDependencies, ckeditor5Path );
  107. expect( directories.length ).to.equal( 2 );
  108. expect( directories[ 0 ] ).to.deep.equal( {
  109. repositoryURL: 'ckeditor/ckeditor5-plugin-image',
  110. repositoryPath: '/workspace/path/ckeditor5/node_modules/ckeditor5-plugin-image'
  111. } );
  112. expect( directories[ 1 ] ).to.deep.equal( {
  113. repositoryURL: 'ckeditor/ckeditor5-core',
  114. repositoryPath: '/workspace/path/ckeditor5/node_modules/ckeditor5-core'
  115. } );
  116. } );
  117. it( 'should return only ckeditor5 directories in development mode, including root directory', () => {
  118. sandbox.stub( ckeditor5Dirs, 'getDirectories', () => sourceDirectories );
  119. sandbox.stub( ckeditor5Dirs, 'getDependencies', () => dependencies );
  120. sandbox.stub( git, 'parseRepositoryUrl' ).returns( repositoryInfo );
  121. const includeRoot = true;
  122. const directories = ckeditor5Dirs.getDevDirectories( workspacePath, packageJSONDependencies, ckeditor5Path, includeRoot );
  123. expect( directories.length ).to.equal( 3 );
  124. expect( directories[ 0 ] ).to.deep.equal( {
  125. repositoryURL: 'ckeditor/ckeditor5',
  126. repositoryPath: '/workspace/path/ckeditor5'
  127. } );
  128. expect( directories[ 1 ] ).to.deep.equal( {
  129. repositoryURL: 'ckeditor/ckeditor5-plugin-image',
  130. repositoryPath: '/workspace/path/ckeditor5/node_modules/ckeditor5-plugin-image'
  131. } );
  132. expect( directories[ 2 ] ).to.deep.equal( {
  133. repositoryURL: 'ckeditor/ckeditor5-core',
  134. repositoryPath: '/workspace/path/ckeditor5/node_modules/ckeditor5-core'
  135. } );
  136. } );
  137. } );
  138. } );
  139. } );