8
0

dev-install.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. } );
  35. afterEach( () => {
  36. toRestore.forEach( item => item.restore() );
  37. Object.keys( spies ).forEach( ( spy ) => spies[ spy ].restore() );
  38. } );
  39. it( 'should use GitHub URL', () => {
  40. spies.isDirectory.onFirstCall().returns( false );
  41. spies.isDirectory.onSecondCall().returns( false );
  42. spies.isDirectory.onThirdCall().returns( true );
  43. installTask( ckeditor5Path, workspacePath, repositoryUrl, () => {} );
  44. sinon.assert.calledThrice( spies.isDirectory );
  45. sinon.assert.calledOnce( spies.parseUrl );
  46. sinon.assert.calledWithExactly( spies.parseUrl, repositoryUrl );
  47. const urlInfo = spies.parseUrl.firstCall.returnValue;
  48. const repositoryPath = path.join( workspaceAbsolutePath, urlInfo.name );
  49. sinon.assert.calledWithExactly( spies.isDirectory.secondCall, repositoryPath );
  50. sinon.assert.calledOnce( spies.cloneRepository );
  51. sinon.assert.calledWithExactly( spies.cloneRepository, urlInfo, workspaceAbsolutePath );
  52. sinon.assert.calledOnce( spies.checkout );
  53. sinon.assert.calledWithExactly( spies.checkout, repositoryPath, urlInfo.branch );
  54. sinon.assert.calledOnce( spies.npmInstall );
  55. sinon.assert.calledWithExactly( spies.npmInstall, repositoryPath );
  56. const linkPath = path.join( ckeditor5Path, 'node_modules', urlInfo.name );
  57. sinon.assert.calledOnce( spies.npmUninstall );
  58. sinon.assert.calledWithExactly( spies.npmUninstall, ckeditor5Path, urlInfo.name );
  59. sinon.assert.calledOnce( spies.linkDirectories );
  60. sinon.assert.calledWithExactly( spies.linkDirectories, repositoryPath, linkPath );
  61. const packageJsonPath = path.join( ckeditor5Path, 'package.json' );
  62. sinon.assert.calledOnce( spies.updateJSON );
  63. expect( spies.updateJSON.firstCall.args[ 0 ] ).to.equal( packageJsonPath );
  64. const updateFn = spies.updateJSON.firstCall.args[ 1 ];
  65. const json = updateFn( {} );
  66. expect( json.dependencies ).to.be.a( 'object' );
  67. expect( json.dependencies[ urlInfo.name ] ).to.equal( repositoryUrl );
  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. } );
  100. it( 'should use local relative path', () => {
  101. spies.isDirectory.onFirstCall().returns( true );
  102. spies.isDirectory.onSecondCall().returns( true );
  103. spies.readPackageName.returns( moduleName );
  104. installTask( ckeditor5Path, workspacePath, '../ckeditor5-core', () => {} );
  105. sinon.assert.calledThrice( spies.isDirectory );
  106. sinon.assert.calledOnce( spies.readPackageName );
  107. } );
  108. it( 'should use local absolute path if provided', () => {
  109. spies.isDirectory.onFirstCall().returns( true );
  110. spies.isDirectory.onSecondCall().returns( true );
  111. spies.readPackageName.returns( moduleName );
  112. installTask( ckeditor5Path, workspacePath, '/ckeditor5-core', () => {} );
  113. sinon.assert.calledThrice( spies.isDirectory );
  114. sinon.assert.calledOnce( spies.readPackageName );
  115. } );
  116. it( 'should throw an exception when invalid name is provided', () => {
  117. spies.isDirectory.onFirstCall().returns( false );
  118. spies.isDirectory.onSecondCall().returns( true );
  119. expect( () => {
  120. installTask( ckeditor5Path, workspacePath, moduleName, () => {} );
  121. } ).to.throw();
  122. sinon.assert.calledOnce( spies.parseUrl );
  123. sinon.assert.calledWithExactly( spies.parseUrl.firstCall, moduleName );
  124. expect( spies.parseUrl.firstCall.returnValue ).to.equal( null );
  125. } );
  126. it( 'should throw an exception when package.json is not present', () => {
  127. spies.isDirectory.onFirstCall().returns( true );
  128. spies.isDirectory.onSecondCall().returns( true );
  129. spies.readPackageName.returns( null );
  130. expect( () => {
  131. installTask( ckeditor5Path, workspacePath, moduleName, () => {} );
  132. } ).to.throw();
  133. sinon.assert.calledOnce( spies.parseUrl );
  134. sinon.assert.calledWithExactly( spies.parseUrl.firstCall, moduleName );
  135. expect( spies.parseUrl.firstCall.returnValue ).to.equal( null );
  136. } );
  137. } );