dev-install.js 6.6 KB

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