dev-plugin-create.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 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. linkDirectories: sinon.stub( tools, 'linkDirectories' ),
  28. npmInstall: sinon.stub( tools, 'npmInstall' ),
  29. installGitHooks: sinon.stub( tools, 'installGitHooks' ),
  30. getPluginName: sinon.stub( inquiries, 'getPluginName' ).returns( new Promise( ( r ) => r( pluginName ) ) ),
  31. getPluginVersion: sinon.stub( inquiries, 'getPluginVersion' ).returns( new Promise( ( r ) => r( pluginVersion ) ) ),
  32. getPluginGitHubUrl: sinon.stub( inquiries, 'getPluginGitHubUrl' ).returns( new Promise( ( r ) => r( gitHubUrl ) ) ),
  33. initializeRepository: sinon.stub( git, 'initializeRepository' ),
  34. updateJSONFile: sinon.stub( tools, 'updateJSONFile' ),
  35. copyTemplateFiles: sinon.stub( tools, 'copyTemplateFiles' ),
  36. initialCommit: sinon.stub( git, 'initialCommit' )
  37. };
  38. }
  39. function restoreSpies() {
  40. for ( let spy in spies ) {
  41. spies[ spy ].restore();
  42. }
  43. }
  44. describe( 'dev-plugin-create', () => {
  45. const pluginCreateTask = require( '../tasks/utils/dev-plugin-create' );
  46. const repositoryPath = path.join( workspacePath, pluginName );
  47. it( 'should exist', () => expect( pluginCreateTask ).to.be.a( 'function' ) );
  48. it( 'should create a plugin', () => {
  49. return pluginCreateTask( mainRepositoryPath, workspaceRoot, emptyFn ).then( () => {
  50. expect( spies.getPluginName.calledOnce ).to.equal( true );
  51. expect( spies.getPluginVersion.calledOnce ).to.equal( true );
  52. expect( spies.getPluginGitHubUrl.calledOnce ).to.equal( true );
  53. expect( spies.initializeRepository.calledOnce ).to.equal( true );
  54. expect( spies.initializeRepository.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  55. expect( spies.copyTemplateFiles.calledOnce ).to.equal( true );
  56. expect( spies.copyTemplateFiles.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  57. expect( spies.updateJSONFile.calledTwice ).to.equal( true );
  58. expect( spies.updateJSONFile.firstCall.args[ 0 ] ).to.equal( path.join( repositoryPath, 'package.json' ) );
  59. let updateFn = spies.updateJSONFile.firstCall.args[ 1 ];
  60. let json = updateFn( {} );
  61. expect( json.name ).to.equal( pluginName );
  62. expect( json.version ).to.equal( pluginVersion );
  63. expect( spies.updateJSONFile.secondCall.args[ 0 ] ).to.equal( path.join( mainRepositoryPath, 'package.json' ) );
  64. updateFn = spies.updateJSONFile.secondCall.args[ 1 ];
  65. json = updateFn( {} );
  66. expect( json.dependencies ).to.be.an( 'object' );
  67. expect( json.dependencies[ pluginName ] ).to.equal( gitHubUrl );
  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. } );