8
0

dev-install.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. beforeEach( () => toRestore = [] );
  22. afterEach( () => {
  23. toRestore.forEach( item => item.restore() );
  24. } );
  25. it( 'should use GitHub url if provided', () => {
  26. const parseUrlSpy = sinon.spy( git, 'parseRepositoryUrl' );
  27. const isDirectoryStub = sinon.stub( tools, 'isDirectory' ).returns( false );
  28. const cloneRepositoryStub = sinon.stub( git, 'cloneRepository' );
  29. const linkDirectoriesStub = sinon.stub( tools, 'linkDirectories' );
  30. const updateJSONstub = sinon.stub( tools, 'updateJSONFile' );
  31. const npmInstallStub = sinon.stub( tools, 'npmInstall' );
  32. const checkoutStub = sinon.stub( git, 'checkout' );
  33. toRestore.push( parseUrlSpy, isDirectoryStub, cloneRepositoryStub, linkDirectoriesStub, updateJSONstub,
  34. npmInstallStub, checkoutStub );
  35. installTask( ckeditor5Path, workspacePath, repositoryUrl, () => {}, () => {} );
  36. sinon.assert.calledOnce( parseUrlSpy );
  37. sinon.assert.calledWithExactly( parseUrlSpy, repositoryUrl );
  38. const urlInfo = parseUrlSpy.firstCall.returnValue;
  39. const repositoryPath = path.join( workspaceAbsolutePath, urlInfo.name );
  40. sinon.assert.calledOnce( isDirectoryStub );
  41. sinon.assert.calledWithExactly( isDirectoryStub, repositoryPath );
  42. sinon.assert.calledOnce( cloneRepositoryStub );
  43. sinon.assert.calledWithExactly( cloneRepositoryStub, urlInfo, workspaceAbsolutePath );
  44. sinon.assert.calledOnce( checkoutStub );
  45. sinon.assert.calledWithExactly( checkoutStub, repositoryPath, urlInfo.branch );
  46. const linkPath = path.join( ckeditor5Path, 'node_modules', urlInfo.name );
  47. sinon.assert.calledOnce( linkDirectoriesStub );
  48. sinon.assert.calledWithExactly( linkDirectoriesStub, repositoryPath, linkPath );
  49. const packageJsonPath = path.join( ckeditor5Path, 'package.json' );
  50. sinon.assert.calledOnce( updateJSONstub );
  51. expect( updateJSONstub.firstCall.args[ 0 ] ).to.equal( packageJsonPath );
  52. const updateFn = updateJSONstub.firstCall.args[ 1 ];
  53. const json = updateFn( {} );
  54. expect( json.dependencies ).to.be.a( 'object' );
  55. expect( json.dependencies[ urlInfo.name ] ).to.equal( repositoryPath );
  56. sinon.assert.calledOnce( npmInstallStub );
  57. sinon.assert.calledWithExactly( npmInstallStub, ckeditor5Path );
  58. } );
  59. it( 'should use npm module name if provided', () => {
  60. const parseUrlSpy = sinon.spy( git, 'parseRepositoryUrl' );
  61. const isDirectoryStub = sinon.stub( tools, 'isDirectory' ).returns( true );
  62. const getUrlFromNpmSpy = sinon.stub( tools, 'getGitUrlFromNpm' ).returns( repositoryUrl );
  63. const cloneRepositoryStub = sinon.stub( git, 'cloneRepository' );
  64. const linkDirectoriesStub = sinon.stub( tools, 'linkDirectories' );
  65. const updateJSONstub = sinon.stub( tools, 'updateJSONFile' );
  66. const npmInstallStub = sinon.stub( tools, 'npmInstall' );
  67. const checkoutStub = sinon.stub( git, 'checkout' );
  68. toRestore.push( parseUrlSpy, isDirectoryStub, getUrlFromNpmSpy, cloneRepositoryStub, linkDirectoriesStub,
  69. updateJSONstub, npmInstallStub, checkoutStub );
  70. installTask( ckeditor5Path, workspacePath, moduleName, () => {}, () => {} );
  71. sinon.assert.calledTwice( parseUrlSpy );
  72. sinon.assert.calledWithExactly( parseUrlSpy.firstCall, moduleName );
  73. expect( parseUrlSpy.firstCall.returnValue ).to.equal( null );
  74. sinon.assert.calledOnce( getUrlFromNpmSpy );
  75. sinon.assert.calledWithExactly( getUrlFromNpmSpy, moduleName );
  76. sinon.assert.calledWithExactly( parseUrlSpy.secondCall, repositoryUrl );
  77. const urlInfo = parseUrlSpy.secondCall.returnValue;
  78. const repositoryPath = path.join( workspaceAbsolutePath, urlInfo.name );
  79. sinon.assert.calledOnce( isDirectoryStub );
  80. sinon.assert.calledWithExactly( isDirectoryStub, repositoryPath );
  81. sinon.assert.notCalled( cloneRepositoryStub );
  82. } );
  83. it( 'should throw an exception when invalid name is provided', () => {
  84. const parseUrlSpy = sinon.spy( git, 'parseRepositoryUrl' );
  85. const isDirectoryStub = sinon.stub( tools, 'isDirectory' ).returns( true );
  86. const getUrlFromNpmSpy = sinon.stub( tools, 'getGitUrlFromNpm' ).returns( null );
  87. const cloneRepositoryStub = sinon.stub( git, 'cloneRepository' );
  88. const linkDirectoriesStub = sinon.stub( tools, 'linkDirectories' );
  89. const updateJSONstub = sinon.stub( tools, 'updateJSONFile' );
  90. const npmInstallStub = sinon.stub( tools, 'npmInstall' );
  91. const checkoutStub = sinon.stub( git, 'checkout' );
  92. toRestore.push( parseUrlSpy, isDirectoryStub, getUrlFromNpmSpy, cloneRepositoryStub, linkDirectoriesStub,
  93. updateJSONstub, npmInstallStub, checkoutStub );
  94. expect( () => {
  95. installTask( ckeditor5Path, workspacePath, moduleName, () => {}, () => {} );
  96. } ).to.throw();
  97. sinon.assert.calledOnce( parseUrlSpy );
  98. sinon.assert.calledWithExactly( parseUrlSpy.firstCall, moduleName );
  99. expect( parseUrlSpy.firstCall.returnValue ).to.equal( null );
  100. } );
  101. } );