| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* global describe, it, beforeEach, afterEach */
- 'use strict';
- let toRestore;
- const git = require( '../tasks/utils/git' );
- const chai = require( 'chai' );
- const sinon = require( 'sinon' );
- const tools = require( '../tasks/utils/tools' );
- const expect = chai.expect;
- describe( 'utils', () => {
- beforeEach( () => toRestore = [] );
- afterEach( () => {
- toRestore.forEach( item => item.restore() );
- } );
- describe( 'git', () => {
- describe( 'parseRepositoryUrl', () => {
- it( 'should be defined', () => expect( git.parseRepositoryUrl ).to.be.a( 'function' ) );
- it( 'should parse short GitHub URL ', () => {
- const urlInfo = git.parseRepositoryUrl( 'ckeditor/ckeditor5-core' );
- expect( urlInfo.server ).to.equal( 'https://github.com/' );
- expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core' );
- expect( urlInfo.user ).to.equal( 'ckeditor' );
- expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
- expect( urlInfo.branch ).to.equal( 'master' );
- } );
- it( 'should parse short GitHub URL with provided branch ', () => {
- const urlInfo = git.parseRepositoryUrl( 'ckeditor/ckeditor5-core#experimental' );
- expect( urlInfo.server ).to.equal( 'https://github.com/' );
- expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core' );
- expect( urlInfo.user ).to.equal( 'ckeditor' );
- expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
- expect( urlInfo.branch ).to.equal( 'experimental' );
- } );
- it( 'should parse full GitHub URL (http)', () => {
- const urlInfo = git.parseRepositoryUrl( 'http://github.com/ckeditor/ckeditor5-core.git' );
- expect( urlInfo.server ).to.equal( 'http://github.com/' );
- expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core.git' );
- expect( urlInfo.user ).to.equal( 'ckeditor' );
- expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
- expect( urlInfo.branch ).to.equal( 'master' );
- } );
- it( 'should parse full GitHub URL (http) with provided branch', () => {
- const urlInfo = git.parseRepositoryUrl( 'http://github.com/ckeditor/ckeditor5-core.git#experimental' );
- expect( urlInfo.server ).to.equal( 'http://github.com/' );
- expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core.git' );
- expect( urlInfo.user ).to.equal( 'ckeditor' );
- expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
- expect( urlInfo.branch ).to.equal( 'experimental' );
- } );
- it( 'should parse full GitHub URL (https)', () => {
- const urlInfo = git.parseRepositoryUrl( 'https://github.com/ckeditor/ckeditor5-core.git' );
- expect( urlInfo.server ).to.equal( 'https://github.com/' );
- expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core.git' );
- expect( urlInfo.user ).to.equal( 'ckeditor' );
- expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
- expect( urlInfo.branch ).to.equal( 'master' );
- } );
- it( 'should parse full GitHub URL (https) with provided branch', () => {
- const urlInfo = git.parseRepositoryUrl( 'https://github.com/ckeditor/ckeditor5-core.git#t/122' );
- expect( urlInfo.server ).to.equal( 'https://github.com/' );
- expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core.git' );
- expect( urlInfo.user ).to.equal( 'ckeditor' );
- expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
- expect( urlInfo.branch ).to.equal( 't/122' );
- } );
- it( 'should parse full GitHub URL (git)', () => {
- const urlInfo = git.parseRepositoryUrl( 'git@github.com:ckeditor/ckeditor5-core.git' );
- expect( urlInfo.server ).to.equal( 'git@github.com:' );
- expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core.git' );
- expect( urlInfo.user ).to.equal( 'ckeditor' );
- expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
- expect( urlInfo.branch ).to.equal( 'master' );
- } );
- it( 'should parse full GitHub URL (git)', () => {
- const urlInfo = git.parseRepositoryUrl( 'git://github.com/ckeditor/ckeditor5-core.git' );
- expect( urlInfo.server ).to.equal( 'git://github.com/' );
- expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core.git' );
- expect( urlInfo.user ).to.equal( 'ckeditor' );
- expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
- expect( urlInfo.branch ).to.equal( 'master' );
- } );
- it( 'should parse full GitHub URL (git) with provided branch', () => {
- const urlInfo = git.parseRepositoryUrl( 'git@github.com:ckeditor/ckeditor5-core.git#new-feature' );
- expect( urlInfo.server ).to.equal( 'git@github.com:' );
- expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core.git' );
- expect( urlInfo.user ).to.equal( 'ckeditor' );
- expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
- expect( urlInfo.branch ).to.equal( 'new-feature' );
- } );
- it( 'should return null if GitHub URL is not valid', () => {
- let urlInfo = git.parseRepositoryUrl( 'https://ckeditor.com' );
- expect( urlInfo ).to.equal( null );
- urlInfo = git.parseRepositoryUrl( 'https://github.com/info.html' );
- expect( urlInfo ).to.equal( null );
- } );
- } );
- describe( 'cloneRepository', () => {
- it( 'should be defined', () => expect( git.cloneRepository ).to.be.a( 'function' ) );
- it( 'should call clone commands', () => {
- const shExecStub = sinon.stub( tools, 'shExec' );
- const workspacePath = '/path/to/workspace/';
- const urlInfo = git.parseRepositoryUrl( 'git@github.com:ckeditor/ckeditor5-core#new-feature' );
- const cloneCommands = `cd ${ workspacePath } && git clone ${ urlInfo.server + urlInfo.repository }`;
- toRestore.push( shExecStub );
- git.cloneRepository( urlInfo, workspacePath );
- expect( shExecStub.calledOnce ).to.equal( true );
- expect( shExecStub.firstCall.args[ 0 ] ).to.equal( cloneCommands );
- } );
- } );
- describe( 'checkout', () => {
- it( 'should be defined', () => expect( git.checkout ).to.be.a( 'function' ) );
- it( 'should call checkout commands', () => {
- const shExecStub = sinon.stub( tools, 'shExec' );
- const repositoryLocation = 'path/to/repository';
- const branchName = 'branch-to-checkout';
- const checkoutCommands = `cd ${ repositoryLocation } && git checkout ${ branchName }`;
- toRestore.push( shExecStub );
- git.checkout( repositoryLocation, branchName );
- expect( shExecStub.calledOnce ).to.equal( true );
- expect( shExecStub.firstCall.args[ 0 ] ).to.equal( checkoutCommands );
- } );
- } );
- describe( 'pull', () => {
- it( 'should be defined', () => expect( git.pull ).to.be.a( 'function' ) );
- it( 'should call pull commands', () => {
- const shExecStub = sinon.stub( tools, 'shExec' );
- const repositoryLocation = 'path/to/repository';
- const branchName = 'branch-to-pull';
- const pullCommands = `cd ${ repositoryLocation } && git pull origin ${ branchName }`;
- toRestore.push( shExecStub );
- git.pull( repositoryLocation, branchName );
- expect( shExecStub.calledOnce ).to.equal( true );
- expect( shExecStub.firstCall.args[ 0 ] ).to.equal( pullCommands );
- } );
- } );
- describe( 'initializeRepository', () => {
- it( 'should be defined', () => expect( git.initializeRepository ).to.be.a( 'function' ) );
- it( 'should call initialize commands', () => {
- const shExecStub = sinon.stub( tools, 'shExec' );
- const repositoryLocation = 'path/to/repository';
- const initializeCommands = [
- `git init ${ repositoryLocation }`,
- `cd ${ repositoryLocation }`,
- `git remote add boilerplate ${ git.BOILERPLATE_REPOSITORY }`,
- `git fetch boilerplate ${ git.BOILERPLATE_BRANCH }`,
- `git merge boilerplate/${ git.BOILERPLATE_BRANCH }`
- ];
- toRestore.push( shExecStub );
- git.initializeRepository( repositoryLocation );
- expect( shExecStub.calledOnce ).to.equal( true );
- expect( shExecStub.firstCall.args[ 0 ] ).to.equal( initializeCommands.join( ' && ' ) );
- } );
- } );
- describe( 'getStatus', () => {
- it( 'should be defined', () => expect( git.getStatus ).to.be.a( 'function' ) );
- it( 'should call status command', () => {
- const shExecStub = sinon.stub( tools, 'shExec' );
- const repositoryLocation = 'path/to/repository';
- const statusCommands = `cd ${ repositoryLocation } && git status --porcelain -sb`;
- toRestore.push( shExecStub );
- git.getStatus( repositoryLocation );
- expect( shExecStub.calledOnce ).to.equal( true );
- expect( shExecStub.firstCall.args[ 0 ] ).to.equal( statusCommands );
- } );
- } );
- describe( 'updateBoilerplate', () => {
- it( 'should be defined', () => expect( git.updateBoilerplate ).to.be.a( 'function' ) );
- it( 'should fetch and merge boilerplate if remote already exists', () => {
- const shExecStub = sinon.stub( tools, 'shExec' );
- const repositoryLocation = 'path/to/repository';
- const updateCommands = [
- `cd ${ repositoryLocation }`,
- `git fetch boilerplate ${ git.BOILERPLATE_BRANCH }`,
- `git merge boilerplate/${ git.BOILERPLATE_BRANCH }`
- ];
- shExecStub.onCall( 0 ).returns( 'origin\nboilerplate' );
- toRestore.push( shExecStub );
- git.updateBoilerplate( repositoryLocation );
- expect( shExecStub.calledTwice ).to.equal( true );
- expect( shExecStub.secondCall.args[ 0 ] ).to.equal( updateCommands.join( ' && ' ) );
- } );
- it( 'should add boilerplate remote if one not exists', () => {
- const shExecStub = sinon.stub( tools, 'shExec' );
- const repositoryLocation = 'path/to/repository';
- const addRemoteCommands = `cd ${ repositoryLocation } && git remote add boilerplate ${ git.BOILERPLATE_REPOSITORY }`;
- const updateCommands = [
- `cd ${ repositoryLocation }`,
- `git fetch boilerplate ${ git.BOILERPLATE_BRANCH }`,
- `git merge boilerplate/${ git.BOILERPLATE_BRANCH }`
- ];
- shExecStub.onCall( 0 ).returns( 'origin\nnew' );
- toRestore.push( shExecStub );
- git.updateBoilerplate( repositoryLocation );
- expect( shExecStub.calledThrice ).to.equal( true );
- expect( shExecStub.secondCall.args[ 0 ] ).to.equal( addRemoteCommands );
- expect( shExecStub.thirdCall.args[ 0 ] ).to.equal( updateCommands.join( ' && ' ) );
- } );
- } );
- describe( 'initialCommit', () => {
- it( 'should be defined', () => expect( git.initialCommit ).to.be.a( 'function' ) );
- it( 'should execute commit commands', () => {
- const shExecStub = sinon.stub( tools, 'shExec' );
- const pluginName = 'ckeditor5-plugin-name';
- const repositoryPath = '/path/to/repo';
- const commitCommands = [
- `cd ${ repositoryPath }`,
- `git add .`,
- `git commit -m "Initial commit for ${ pluginName }."`
- ];
- toRestore.push( shExecStub );
- git.initialCommit( pluginName, repositoryPath );
- expect( shExecStub.calledOnce ).to.equal( true );
- expect( shExecStub.firstCall.args[ 0 ] ).to.equal( commitCommands.join( ' && ' ) );
- } );
- } );
- } );
- } );
|