install.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 installTask = require( '../../tasks/dev/tasks/install' );
  10. const expect = chai.expect;
  11. const path = require( 'path' );
  12. const { tools, git } = require( 'ckeditor5-dev-utils' );
  13. describe( 'dev-install', () => {
  14. const moduleName = 'ckeditor5-core';
  15. const repositoryUrl = 'git@github.com:ckeditor/ckeditor5-core';
  16. const ckeditor5Path = '/path/to/ckeditor';
  17. const workspacePath = '..';
  18. const workspaceAbsolutePath = path.join( ckeditor5Path, workspacePath );
  19. const spies = {};
  20. beforeEach( () => {
  21. spies.parseUrl = sinon.spy( git, 'parseRepositoryUrl' );
  22. spies.isDirectory = sinon.stub( tools, 'isDirectory' );
  23. spies.cloneRepository = sinon.stub( git, 'cloneRepository' );
  24. spies.linkDirectories = sinon.stub( tools, 'linkDirectories' );
  25. spies.updateJSON = sinon.stub( tools, 'updateJSONFile' );
  26. spies.npmInstall = sinon.stub( tools, 'npmInstall' );
  27. spies.checkout = sinon.stub( git, 'checkout' );
  28. spies.getGitUrlFromNpm = sinon.stub( tools, 'getGitUrlFromNpm' );
  29. spies.readPackageName = sinon.stub( tools, 'readPackageName' );
  30. } );
  31. afterEach( () => {
  32. Object.keys( spies ).forEach( ( spy ) => spies[ spy ].restore() );
  33. } );
  34. it( 'should use GitHub URL', () => {
  35. spies.isDirectory.onFirstCall().returns( false );
  36. spies.isDirectory.onSecondCall().returns( false );
  37. spies.isDirectory.onThirdCall().returns( true );
  38. installTask( ckeditor5Path, workspacePath, repositoryUrl );
  39. sinon.assert.calledTwice( spies.isDirectory );
  40. sinon.assert.calledOnce( spies.parseUrl );
  41. sinon.assert.calledWithExactly( spies.parseUrl, repositoryUrl );
  42. const urlInfo = spies.parseUrl.firstCall.returnValue;
  43. const repositoryPath = path.join( workspaceAbsolutePath, urlInfo.name );
  44. sinon.assert.calledWithExactly( spies.isDirectory.secondCall, repositoryPath );
  45. sinon.assert.calledOnce( spies.cloneRepository );
  46. sinon.assert.calledWithExactly( spies.cloneRepository, urlInfo, workspaceAbsolutePath );
  47. sinon.assert.calledOnce( spies.checkout );
  48. sinon.assert.calledWithExactly( spies.checkout, repositoryPath, urlInfo.branch );
  49. sinon.assert.calledOnce( spies.npmInstall );
  50. sinon.assert.calledWithExactly( spies.npmInstall, repositoryPath );
  51. const linkPath = path.join( ckeditor5Path, 'node_modules', urlInfo.name );
  52. sinon.assert.calledOnce( spies.linkDirectories );
  53. sinon.assert.calledWithExactly( spies.linkDirectories, repositoryPath, linkPath );
  54. const packageJsonPath = path.join( ckeditor5Path, 'package.json' );
  55. sinon.assert.calledOnce( spies.updateJSON );
  56. expect( spies.updateJSON.firstCall.args[ 0 ] ).to.equal( packageJsonPath );
  57. const updateFn = spies.updateJSON.firstCall.args[ 1 ];
  58. const json = updateFn( {} );
  59. expect( json.dependencies ).to.be.a( 'object' );
  60. expect( json.dependencies[ urlInfo.name ] ).to.equal( repositoryUrl );
  61. } );
  62. it( 'should use npm module name', () => {
  63. spies.isDirectory.onFirstCall().returns( false );
  64. spies.isDirectory.onSecondCall().returns( true );
  65. spies.getGitUrlFromNpm.returns( repositoryUrl );
  66. installTask( ckeditor5Path, workspacePath, moduleName );
  67. sinon.assert.calledTwice( spies.isDirectory );
  68. sinon.assert.calledTwice( spies.parseUrl );
  69. sinon.assert.calledWithExactly( spies.parseUrl.firstCall, moduleName );
  70. expect( spies.parseUrl.firstCall.returnValue ).to.equal( null );
  71. sinon.assert.calledOnce( spies.getGitUrlFromNpm );
  72. sinon.assert.calledWithExactly( spies.getGitUrlFromNpm, moduleName );
  73. sinon.assert.calledWithExactly( spies.parseUrl.secondCall, repositoryUrl );
  74. const urlInfo = spies.parseUrl.secondCall.returnValue;
  75. const repositoryPath = path.join( workspaceAbsolutePath, urlInfo.name );
  76. sinon.assert.calledWithExactly( spies.isDirectory.secondCall, repositoryPath );
  77. sinon.assert.notCalled( spies.cloneRepository );
  78. sinon.assert.calledOnce( spies.checkout );
  79. sinon.assert.calledWithExactly( spies.checkout, repositoryPath, urlInfo.branch );
  80. sinon.assert.calledOnce( spies.npmInstall );
  81. sinon.assert.calledWithExactly( spies.npmInstall, repositoryPath );
  82. const linkPath = path.join( ckeditor5Path, 'node_modules', urlInfo.name );
  83. sinon.assert.calledOnce( spies.linkDirectories );
  84. sinon.assert.calledWithExactly( spies.linkDirectories, repositoryPath, linkPath );
  85. const packageJsonPath = path.join( ckeditor5Path, 'package.json' );
  86. sinon.assert.calledOnce( spies.updateJSON );
  87. expect( spies.updateJSON.firstCall.args[ 0 ] ).to.equal( packageJsonPath );
  88. const updateFn = spies.updateJSON.firstCall.args[ 1 ];
  89. const json = updateFn( {} );
  90. expect( json.dependencies ).to.be.a( 'object' );
  91. expect( json.dependencies[ urlInfo.name ] ).to.equal( repositoryUrl );
  92. } );
  93. it( 'should use local relative path', () => {
  94. spies.isDirectory.onFirstCall().returns( true );
  95. spies.isDirectory.onSecondCall().returns( true );
  96. spies.readPackageName.returns( moduleName );
  97. installTask( ckeditor5Path, workspacePath, '../ckeditor5-core' );
  98. sinon.assert.calledTwice( spies.isDirectory );
  99. sinon.assert.calledOnce( spies.readPackageName );
  100. } );
  101. it( 'should use local absolute path if provided', () => {
  102. spies.isDirectory.onFirstCall().returns( true );
  103. spies.isDirectory.onSecondCall().returns( true );
  104. spies.readPackageName.returns( moduleName );
  105. installTask( ckeditor5Path, workspacePath, '/ckeditor5-core' );
  106. sinon.assert.calledTwice( spies.isDirectory );
  107. sinon.assert.calledOnce( spies.readPackageName );
  108. } );
  109. it( 'should throw an exception when invalid name is provided', () => {
  110. spies.isDirectory.onFirstCall().returns( false );
  111. spies.isDirectory.onSecondCall().returns( true );
  112. expect( () => {
  113. installTask( ckeditor5Path, workspacePath, moduleName );
  114. } ).to.throw();
  115. sinon.assert.calledOnce( spies.parseUrl );
  116. sinon.assert.calledWithExactly( spies.parseUrl.firstCall, moduleName );
  117. expect( spies.parseUrl.firstCall.returnValue ).to.equal( null );
  118. } );
  119. it( 'should throw an exception when package.json is not present', () => {
  120. spies.isDirectory.onFirstCall().returns( true );
  121. spies.isDirectory.onSecondCall().returns( true );
  122. spies.readPackageName.returns( null );
  123. expect( () => {
  124. installTask( ckeditor5Path, workspacePath, moduleName );
  125. } ).to.throw();
  126. sinon.assert.calledOnce( spies.parseUrl );
  127. sinon.assert.calledWithExactly( spies.parseUrl.firstCall, moduleName );
  128. expect( spies.parseUrl.firstCall.returnValue ).to.equal( null );
  129. } );
  130. } );