8
0

dev-install.js 7.2 KB

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