dev-tasks.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 sinon = require( 'sinon' );
  9. const expect = chai.expect;
  10. const tools = require( '../tasks/utils/tools' );
  11. const inquiries = require( '../tasks/utils/inquiries' );
  12. const git = require( '../tasks/utils/git' );
  13. const path = require( 'path' );
  14. const emptyFn = () => { };
  15. let spies;
  16. describe( 'dev-tasks', () => {
  17. const mainRepositoryPath = '/path/to/repository';
  18. const workspaceRoot = '..';
  19. const workspacePath = path.join( mainRepositoryPath, workspaceRoot );
  20. const pluginName = 'plugin-name';
  21. const pluginVersion = '0.0.1';
  22. const gitHubUrl = 'ckeditor5/plugin-name';
  23. beforeEach( () => createSpies() );
  24. afterEach( () => restoreSpies() );
  25. function createSpies() {
  26. spies = {
  27. getDependencies: sinon.spy( tools, 'getCKEditorDependencies' ),
  28. getDirectories: sinon.stub( tools, 'getCKE5Directories', () => [] ),
  29. parseRepositoryUrl: sinon.spy( git, 'parseRepositoryUrl' ),
  30. cloneRepository: sinon.stub( git, 'cloneRepository' ),
  31. linkDirectories: sinon.stub( tools, 'linkDirectories' ),
  32. pull: sinon.stub( git, 'pull' ),
  33. checkout: sinon.stub( git, 'checkout' ),
  34. npmInstall: sinon.stub( tools, 'npmInstall' ),
  35. installGitHooks: sinon.stub( tools, 'installGitHooks' ),
  36. getPluginName: sinon.stub( inquiries, 'getPluginName' ).returns( new Promise( ( r ) => r( pluginName ) ) ),
  37. getPluginVersion: sinon.stub( inquiries, 'getPluginVersion' ).returns( new Promise( ( r ) => r( pluginVersion ) ) ),
  38. getPluginGitHubUrl: sinon.stub( inquiries, 'getPluginGitHubUrl' ).returns( new Promise( ( r ) => r( gitHubUrl ) ) ),
  39. initializeRepository: sinon.stub( git, 'initializeRepository' ),
  40. updateJSONFile: sinon.stub( tools, 'updateJSONFile' ),
  41. getStatus: sinon.stub( git, 'getStatus' ),
  42. updateBoilerplate: sinon.stub( git, 'updateBoilerplate' ),
  43. copyTemplateFiles: sinon.stub( tools, 'copyTemplateFiles' ),
  44. initialCommit: sinon.stub( git, 'initialCommit' )
  45. };
  46. }
  47. function restoreSpies() {
  48. for ( let spy in spies ) {
  49. spies[ spy ].restore();
  50. }
  51. }
  52. describe( 'dev-plugin-create', () => {
  53. const pluginCreateTask = require( '../tasks/utils/dev-plugin-create' );
  54. const repositoryPath = path.join( workspacePath, pluginName );
  55. it( 'should exist', () => expect( pluginCreateTask ).to.be.a( 'function' ) );
  56. it( 'should create a plugin', () => {
  57. return pluginCreateTask( mainRepositoryPath, workspaceRoot, emptyFn ).then( () => {
  58. expect( spies.getPluginName.calledOnce ).to.equal( true );
  59. expect( spies.getPluginVersion.calledOnce ).to.equal( true );
  60. expect( spies.getPluginGitHubUrl.calledOnce ).to.equal( true );
  61. expect( spies.initializeRepository.calledOnce ).to.equal( true );
  62. expect( spies.initializeRepository.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  63. expect( spies.copyTemplateFiles.calledOnce ).to.equal( true );
  64. expect( spies.copyTemplateFiles.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  65. expect( spies.updateJSONFile.calledTwice ).to.equal( true );
  66. expect( spies.updateJSONFile.firstCall.args[ 0 ] ).to.equal( path.join( repositoryPath, 'package.json' ) );
  67. expect( spies.updateJSONFile.secondCall.args[ 0 ] ).to.equal( path.join( mainRepositoryPath, 'package.json' ) );
  68. expect( spies.initialCommit.calledOnce ).to.equal( true );
  69. expect( spies.initialCommit.firstCall.args[ 0 ] ).to.equal( pluginName );
  70. expect( spies.initialCommit.firstCall.args[ 1 ] ).to.equal( repositoryPath );
  71. expect( spies.linkDirectories.calledOnce ).to.equal( true );
  72. expect( spies.linkDirectories.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  73. expect( spies.linkDirectories.firstCall.args[ 1 ] ).to.equal( path.join( mainRepositoryPath, 'node_modules', pluginName ) );
  74. expect( spies.npmInstall.calledOnce ).to.equal( true );
  75. expect( spies.npmInstall.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  76. expect( spies.installGitHooks.calledOnce ).to.equal( true );
  77. expect( spies.installGitHooks.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  78. } );
  79. } );
  80. } );
  81. describe( 'dev-relink', () => {
  82. const devRelinkTask = require( '../tasks/utils/dev-relink' );
  83. it( 'should exist', () => expect( devRelinkTask ).to.be.a( 'function' ) );
  84. it( 'should relink repositories', () => {
  85. const packageJSON = {
  86. dependencies: {
  87. 'ckeditor5-core': 'ckeditor/ckeditor5-core',
  88. 'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
  89. 'non-ckeditor-plugin': 'other/plugin'
  90. }
  91. };
  92. spies.getDirectories.restore();
  93. const dirs = [ 'ckeditor5-core', 'ckeditor5-plugin-devtest' ];
  94. spies.getDirectories = sinon.stub( tools, 'getCKE5Directories', () => dirs );
  95. devRelinkTask( mainRepositoryPath, packageJSON, workspaceRoot, emptyFn, emptyFn );
  96. expect( spies.getDependencies.calledOnce ).to.equal( true );
  97. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  98. expect( spies.linkDirectories.calledTwice ).to.equal( true );
  99. expect( spies.linkDirectories.firstCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 0 ] ) );
  100. expect( spies.linkDirectories.firstCall.args[ 1 ] ).to.equal( path.join( mainRepositoryPath, 'node_modules', dirs[ 0 ] ) );
  101. expect( spies.linkDirectories.secondCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 1 ] ) );
  102. expect( spies.linkDirectories.secondCall.args[ 1 ] ).to.equal( path.join( mainRepositoryPath, 'node_modules', dirs[ 1 ] ) );
  103. } );
  104. } );
  105. describe( 'dev-boilerplate-update', () => {
  106. const devBoilerplateTask = require( '../tasks/utils/dev-boilerplate-update' );
  107. it( 'should exist', () => expect( devBoilerplateTask ).to.be.a( 'function' ) );
  108. it( 'should update boilerplate in repositories', () => {
  109. const packageJSON = {
  110. dependencies: {
  111. 'ckeditor5-core': 'ckeditor/ckeditor5-core',
  112. 'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
  113. 'non-ckeditor-plugin': 'other/plugin'
  114. }
  115. };
  116. const dirs = [ 'ckeditor5-core', 'ckeditor5-plugin-devtest' ];
  117. spies.getDirectories.restore();
  118. spies.getDirectories = sinon.stub( tools, 'getCKE5Directories', () => dirs );
  119. devBoilerplateTask( mainRepositoryPath, packageJSON, workspaceRoot, emptyFn, emptyFn );
  120. expect( spies.getDependencies.calledOnce ).to.equal( true );
  121. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  122. expect( spies.updateBoilerplate.calledTwice ).to.equal( true );
  123. expect( spies.updateBoilerplate.firstCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 0 ] ) );
  124. expect( spies.updateBoilerplate.secondCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 1 ] ) );
  125. } );
  126. } );
  127. } );