dev-install.js 7.1 KB

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