8
0

create-package.js 4.2 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/dev/utils/tools' );
  11. const inquiries = require( '../../tasks/dev/utils/inquiries' );
  12. const git = require( '../../tasks/dev/utils/git' );
  13. const path = require( 'path' );
  14. describe( 'dev-create-package', () => {
  15. let spies;
  16. const mainRepositoryPath = '/path/to/repository';
  17. const workspaceRoot = '..';
  18. const workspacePath = path.join( mainRepositoryPath, workspaceRoot );
  19. const packageName = 'package-name';
  20. const applicationName = 'Full application name';
  21. const packageVersion = '0.0.1';
  22. const gitHubUrl = 'ckeditor5/package-name';
  23. const packageDescription = 'Package description.';
  24. beforeEach( () => createSpies() );
  25. afterEach( () => restoreSpies() );
  26. function createSpies() {
  27. spies = {
  28. linkDirectories: sinon.stub( tools, 'linkDirectories' ),
  29. npmInstall: sinon.stub( tools, 'npmInstall' ),
  30. getPackageName: sinon.stub( inquiries, 'getPackageName' ).returns( new Promise( ( r ) => r( packageName ) ) ),
  31. getApplicationName: sinon.stub( inquiries, 'getApplicationName' ).returns( new Promise( ( r ) => r( applicationName ) ) ),
  32. getPackageVersion: sinon.stub( inquiries, 'getPackageVersion' ).returns( new Promise( ( r ) => r( packageVersion ) ) ),
  33. getPackageGitHubUrl: sinon.stub( inquiries, 'getPackageGitHubUrl' ).returns( new Promise( ( r ) => r( gitHubUrl ) ) ),
  34. getPackageDescription: sinon.stub( inquiries, 'getPackageDescription' ).returns( new Promise( ( r ) => r( packageDescription ) ) ),
  35. initializeRepository: sinon.stub( git, 'initializeRepository' ),
  36. updateJSONFile: sinon.stub( tools, 'updateJSONFile' ),
  37. copy: sinon.stub( tools, 'copyTemplateFiles' ),
  38. initialCommit: sinon.stub( git, 'initialCommit' )
  39. };
  40. }
  41. function restoreSpies() {
  42. for ( let spy in spies ) {
  43. spies[ spy ].restore();
  44. }
  45. }
  46. const packageCreateTask = require( '../../tasks/dev/tasks/create-package' );
  47. const repositoryPath = path.join( workspacePath, packageName );
  48. it( 'should exist', () => expect( packageCreateTask ).to.be.a( 'function' ) );
  49. it( 'should create a package', () => {
  50. return packageCreateTask( mainRepositoryPath, workspaceRoot ).then( () => {
  51. expect( spies.getPackageName.calledOnce ).to.equal( true );
  52. expect( spies.getApplicationName.calledOnce ).to.equal( true );
  53. expect( spies.getPackageVersion.calledOnce ).to.equal( true );
  54. expect( spies.getPackageGitHubUrl.calledOnce ).to.equal( true );
  55. expect( spies.getPackageDescription.calledOnce ).to.equal( true );
  56. expect( spies.initializeRepository.calledOnce ).to.equal( true );
  57. expect( spies.initializeRepository.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  58. expect( spies.copy.called ).to.equal( true );
  59. expect( spies.updateJSONFile.calledTwice ).to.equal( true );
  60. expect( spies.updateJSONFile.firstCall.args[ 0 ] ).to.equal( path.join( repositoryPath, 'package.json' ) );
  61. let updateFn = spies.updateJSONFile.firstCall.args[ 1 ];
  62. let json = updateFn( {} );
  63. expect( json.name ).to.equal( packageName );
  64. expect( json.version ).to.equal( packageVersion );
  65. expect( spies.updateJSONFile.secondCall.args[ 0 ] ).to.equal( path.join( mainRepositoryPath, 'package.json' ) );
  66. updateFn = spies.updateJSONFile.secondCall.args[ 1 ];
  67. json = updateFn( {} );
  68. expect( json.dependencies ).to.be.an( 'object' );
  69. expect( json.dependencies[ packageName ] ).to.equal( gitHubUrl );
  70. expect( spies.initialCommit.calledOnce ).to.equal( true );
  71. expect( spies.initialCommit.firstCall.args[ 0 ] ).to.equal( packageName );
  72. expect( spies.initialCommit.firstCall.args[ 1 ] ).to.equal( repositoryPath );
  73. expect( spies.linkDirectories.calledOnce ).to.equal( true );
  74. expect( spies.linkDirectories.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  75. expect( spies.linkDirectories.firstCall.args[ 1 ] ).to.equal( path.join( mainRepositoryPath, 'node_modules', packageName ) );
  76. expect( spies.npmInstall.calledOnce ).to.equal( true );
  77. expect( spies.npmInstall.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  78. } );
  79. } );
  80. } );