| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- /**
- * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* global describe, it, beforeEach, afterEach */
- 'use strict';
- const chai = require( 'chai' );
- const sinon = require( 'sinon' );
- const git = require( '../tasks/utils/git' );
- const tools = require( '../tasks/utils/tools' );
- const installTask = require( '../tasks/utils/dev-install' );
- const expect = chai.expect;
- const path = require( 'path' );
- describe( 'dev-install', () => {
- const moduleName = 'ckeditor5-core';
- const repositoryUrl = 'git@github.com:ckeditor/ckeditor5-core';
- const ckeditor5Path = '/path/to/ckeditor';
- const workspacePath = '..';
- const workspaceAbsolutePath = path.join( ckeditor5Path, workspacePath );
- const spies = {};
- beforeEach( () => {
- spies.parseUrl = sinon.spy( git, 'parseRepositoryUrl' );
- spies.isDirectory = sinon.stub( tools, 'isDirectory' );
- spies.cloneRepository = sinon.stub( git, 'cloneRepository' );
- spies.linkDirectories = sinon.stub( tools, 'linkDirectories' );
- spies.updateJSON = sinon.stub( tools, 'updateJSONFile' );
- spies.npmInstall = sinon.stub( tools, 'npmInstall' );
- spies.checkout = sinon.stub( git, 'checkout' );
- spies.getGitUrlFromNpm = sinon.stub( tools, 'getGitUrlFromNpm' );
- spies.readPackageName = sinon.stub( tools, 'readPackageName' );
- spies.npmUninstall = sinon.stub( tools, 'npmUninstall' );
- spies.installGitHooks = sinon.stub( tools, 'installGitHooks' );
- } );
- afterEach( () => {
- Object.keys( spies ).forEach( ( spy ) => spies[ spy ].restore() );
- } );
- it( 'should use GitHub URL', () => {
- spies.isDirectory.onFirstCall().returns( false );
- spies.isDirectory.onSecondCall().returns( false );
- spies.isDirectory.onThirdCall().returns( true );
- installTask( ckeditor5Path, workspacePath, repositoryUrl, () => {} );
- sinon.assert.calledThrice( spies.isDirectory );
- sinon.assert.calledOnce( spies.parseUrl );
- sinon.assert.calledWithExactly( spies.parseUrl, repositoryUrl );
- const urlInfo = spies.parseUrl.firstCall.returnValue;
- const repositoryPath = path.join( workspaceAbsolutePath, urlInfo.name );
- sinon.assert.calledWithExactly( spies.isDirectory.secondCall, repositoryPath );
- sinon.assert.calledOnce( spies.cloneRepository );
- sinon.assert.calledWithExactly( spies.cloneRepository, urlInfo, workspaceAbsolutePath );
- sinon.assert.calledOnce( spies.checkout );
- sinon.assert.calledWithExactly( spies.checkout, repositoryPath, urlInfo.branch );
- sinon.assert.calledOnce( spies.npmInstall );
- sinon.assert.calledWithExactly( spies.npmInstall, repositoryPath );
- const linkPath = path.join( ckeditor5Path, 'node_modules', urlInfo.name );
- sinon.assert.calledOnce( spies.npmUninstall );
- sinon.assert.calledWithExactly( spies.npmUninstall, ckeditor5Path, urlInfo.name );
- sinon.assert.calledOnce( spies.linkDirectories );
- sinon.assert.calledWithExactly( spies.linkDirectories, repositoryPath, linkPath );
- const packageJsonPath = path.join( ckeditor5Path, 'package.json' );
- sinon.assert.calledOnce( spies.updateJSON );
- expect( spies.updateJSON.firstCall.args[ 0 ] ).to.equal( packageJsonPath );
- const updateFn = spies.updateJSON.firstCall.args[ 1 ];
- const json = updateFn( {} );
- expect( json.dependencies ).to.be.a( 'object' );
- expect( json.dependencies[ urlInfo.name ] ).to.equal( repositoryUrl );
- sinon.assert.calledOnce( spies.installGitHooks );
- sinon.assert.calledWithExactly( spies.installGitHooks, repositoryPath );
- } );
- it( 'should use npm module name', () => {
- spies.isDirectory.onFirstCall().returns( false );
- spies.isDirectory.onSecondCall().returns( true );
- spies.getGitUrlFromNpm.returns( repositoryUrl );
- installTask( ckeditor5Path, workspacePath, moduleName, () => {} );
- sinon.assert.calledThrice( spies.isDirectory );
- sinon.assert.calledTwice( spies.parseUrl );
- sinon.assert.calledWithExactly( spies.parseUrl.firstCall, moduleName );
- expect( spies.parseUrl.firstCall.returnValue ).to.equal( null );
- sinon.assert.calledOnce( spies.getGitUrlFromNpm );
- sinon.assert.calledWithExactly( spies.getGitUrlFromNpm, moduleName );
- sinon.assert.calledWithExactly( spies.parseUrl.secondCall, repositoryUrl );
- const urlInfo = spies.parseUrl.secondCall.returnValue;
- const repositoryPath = path.join( workspaceAbsolutePath, urlInfo.name );
- sinon.assert.calledWithExactly( spies.isDirectory.secondCall, repositoryPath );
- sinon.assert.notCalled( spies.cloneRepository );
- sinon.assert.calledOnce( spies.checkout );
- sinon.assert.calledWithExactly( spies.checkout, repositoryPath, urlInfo.branch );
- sinon.assert.calledOnce( spies.npmInstall );
- sinon.assert.calledWithExactly( spies.npmInstall, repositoryPath );
- const linkPath = path.join( ckeditor5Path, 'node_modules', urlInfo.name );
- sinon.assert.calledOnce( spies.linkDirectories );
- sinon.assert.calledWithExactly( spies.linkDirectories, repositoryPath, linkPath );
- const packageJsonPath = path.join( ckeditor5Path, 'package.json' );
- sinon.assert.calledOnce( spies.updateJSON );
- expect( spies.updateJSON.firstCall.args[ 0 ] ).to.equal( packageJsonPath );
- const updateFn = spies.updateJSON.firstCall.args[ 1 ];
- const json = updateFn( {} );
- expect( json.dependencies ).to.be.a( 'object' );
- expect( json.dependencies[ urlInfo.name ] ).to.equal( repositoryUrl );
- sinon.assert.calledOnce( spies.installGitHooks );
- sinon.assert.calledWithExactly( spies.installGitHooks, repositoryPath );
- } );
- it( 'should use local relative path', () => {
- spies.isDirectory.onFirstCall().returns( true );
- spies.isDirectory.onSecondCall().returns( true );
- spies.readPackageName.returns( moduleName );
- installTask( ckeditor5Path, workspacePath, '../ckeditor5-core', () => {} );
- sinon.assert.calledThrice( spies.isDirectory );
- sinon.assert.calledOnce( spies.readPackageName );
- } );
- it( 'should use local absolute path if provided', () => {
- spies.isDirectory.onFirstCall().returns( true );
- spies.isDirectory.onSecondCall().returns( true );
- spies.readPackageName.returns( moduleName );
- installTask( ckeditor5Path, workspacePath, '/ckeditor5-core', () => {} );
- sinon.assert.calledThrice( spies.isDirectory );
- sinon.assert.calledOnce( spies.readPackageName );
- } );
- it( 'should throw an exception when invalid name is provided', () => {
- spies.isDirectory.onFirstCall().returns( false );
- spies.isDirectory.onSecondCall().returns( true );
- expect( () => {
- installTask( ckeditor5Path, workspacePath, moduleName, () => {} );
- } ).to.throw();
- sinon.assert.calledOnce( spies.parseUrl );
- sinon.assert.calledWithExactly( spies.parseUrl.firstCall, moduleName );
- expect( spies.parseUrl.firstCall.returnValue ).to.equal( null );
- } );
- it( 'should throw an exception when package.json is not present', () => {
- spies.isDirectory.onFirstCall().returns( true );
- spies.isDirectory.onSecondCall().returns( true );
- spies.readPackageName.returns( null );
- expect( () => {
- installTask( ckeditor5Path, workspacePath, moduleName, () => {} );
- } ).to.throw();
- sinon.assert.calledOnce( spies.parseUrl );
- sinon.assert.calledWithExactly( spies.parseUrl.firstCall, moduleName );
- expect( spies.parseUrl.firstCall.returnValue ).to.equal( null );
- } );
- } );
|