8
0
Szymon Kupś 10 лет назад
Родитель
Сommit
4cd0582fe4
3 измененных файлов с 177 добавлено и 54 удалено
  1. 3 54
      dev/tasks/dev.js
  2. 112 0
      dev/tasks/tests/devtasks.js
  3. 62 0
      dev/tasks/utils/dev-init.js

+ 3 - 54
dev/tasks/dev.js

@@ -5,70 +5,19 @@
 
 'use strict';
 
-var tools = require( './utils/tools' );
-var git = require( './utils/git' );
-var path = require( 'path' );
+const initTask = require( './utils/dev-init' );
 var ckeditor5Path = process.cwd();
 
-module.exports = grunt => {
+module.exports = ( grunt ) => {
 	const packageJSON = grunt.config.data.pkg;
 
-	/**
-	 * 1. Get CKEditor5 dependencies from package.json file.
-	 * 2. Check if any of the repositories are already present in the workspace.
-	 * 		2.1. If repository is present in the workspace, check it out to desired branch if one is provided.
-	 * 		2.2. If repository is not present in the workspace, clone it and checkout to desired branch if one is provided.
-	 * 3. Link new repository to node_modules. (do not use npm link, use standard linking instead)
-	 */
 	grunt.registerTask( 'dev-init', function() {
 		// Get workspace root relative path from configuration and convert it to absolute path.
 		let	options = {
 			workspaceRoot: '..'
 		};
-
 		options = this.options( options );
-		const workspaceAbsolutePath = path.join( ckeditor5Path, options.workspaceRoot );
-
-		// Get all CKEditor dependencies from package.json.
-		const dependencies = tools.getCKEditorDependencies( packageJSON.dependencies );
-
-		if ( dependencies ) {
-			const directories = tools.getCKE5Directories( workspaceAbsolutePath );
-
-			for ( let dependency in dependencies ) {
-				const repositoryURL = dependencies[ dependency ];
-				const urlInfo = git.parseRepositoryUrl( repositoryURL );
-				const repositoryAbsolutePath = path.join( workspaceAbsolutePath, dependency );
-
-				// Check if repository's directory already exists.
-				if ( directories.indexOf( dependency ) === -1 ) {
-					try {
-						grunt.log.writeln( `Clonning ${ repositoryURL }...` );
-						git.cloneRepository( urlInfo, workspaceAbsolutePath );
-					} catch ( error ) {
-						grunt.log.error( error );
-					}
-				}
-
-				// Check out proper branch.
-				try {
-					grunt.log.writeln( `Checking out ${ repositoryURL } to ${ urlInfo.branch }...` );
-					git.checkout( repositoryAbsolutePath, urlInfo.branch );
-				} catch ( error ) {
-					grunt.log.error( error );
-				}
-
-				// Link plugin.
-				try {
-					grunt.log.writeln( `Linking ${ repositoryURL }...` );
-					tools.linkDirectories( repositoryAbsolutePath, path.join( ckeditor5Path, 'node_modules' , dependency ) );
-				} catch ( error ) {
-					grunt.log.error( error );
-				}
-			}
-		} else {
-			grunt.log.writeln( 'No CKEditor5 dependencies found in package.json file.' );
-		}
+		initTask( ckeditor5Path, packageJSON, options, grunt.log.writeln, grunt.log.error );
 	} );
 };
 

+ 112 - 0
dev/tasks/tests/devtasks.js

@@ -0,0 +1,112 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+/* global describe, it, beforeEach, afterEach */
+const chai = require( 'chai' );
+const sinon = require( 'sinon' );
+const expect = chai.expect;
+const tools = require( '../utils/tools' );
+const git = require( '../utils/git' );
+const path = require( 'path' );
+const emptyFn = () => { };
+let spies;
+
+describe( 'dev-tasks', () => {
+	describe( 'dev-init', () => {
+		const initTask = require( '../utils/dev-init' );
+		const mainRepositoryPath = '/path/to/repository';
+		const options = {
+			workspaceRoot: '..'
+		};
+		const workspacePath = path.join( mainRepositoryPath, options.workspaceRoot );
+
+		beforeEach( () => createSpies() );
+		afterEach( () => restoreSpies() );
+
+		it( 'task should exists', () => expect( initTask ).to.be.a( 'function' ) );
+
+		it( 'performs no action when no ckeditor dependencies are found', () => {
+			const packageJSON = {
+				dependencies: {
+					'non-ckeditor-plugin': 'other/plugin'
+				}
+			};
+
+			initTask( mainRepositoryPath, packageJSON, options, emptyFn, emptyFn );
+			expect( spies.getDependencies.calledOnce ).to.equal( true );
+			expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
+			expect( spies.getDirectories.called ).to.equal( false );
+			expect( spies.parseRepositoryUrl.called ).to.equal( false );
+			expect( spies.cloneRepository.called ).to.equal( false );
+			expect( spies.checkout.called ).to.equal( false );
+			expect( spies.linkDirectories.called ).to.equal( false );
+		} );
+
+		it( 'clones repositories if no directories are found', () => {
+			const packageJSON = {
+				dependencies: {
+					'ckeditor5-core': 'ckeditor/ckeditor5-core',
+					'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
+					'non-ckeditor-plugin': 'other/plugin'
+				}
+			};
+
+			initTask( mainRepositoryPath, packageJSON, options, emptyFn, emptyFn );
+			expect( spies.getDependencies.calledOnce ).to.equal( true );
+			expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
+			expect( spies.getDirectories.calledOnce ).to.equal( true );
+			expect( spies.getDirectories.firstCall.args[ 0 ] ).to.equal( path.join( mainRepositoryPath, options.workspaceRoot ) );
+			expect( spies.parseRepositoryUrl.calledTwice ).to.equal( true );
+			expect( spies.cloneRepository.calledTwice ).to.equal( true );
+			expect( spies.cloneRepository.firstCall.args[ 0 ] ).to.equal( spies.parseRepositoryUrl.firstCall.returnValue );
+			expect( spies.cloneRepository.firstCall.args[ 1 ] ).to.equal( workspacePath );
+			expect( spies.cloneRepository.secondCall.args[ 0 ] ).to.equal( spies.parseRepositoryUrl.secondCall.returnValue );
+			expect( spies.cloneRepository.secondCall.args[ 1 ] ).to.equal( workspacePath );
+			expect( spies.checkout.calledTwice ).to.equal( true );
+			expect( spies.linkDirectories.calledTwice ).to.equal( true );
+		} );
+
+		it( 'only checks out repositories if directories are found', () => {
+			const packageJSON = {
+				dependencies: {
+					'ckeditor5-core': 'ckeditor/ckeditor5-core',
+					'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
+					'non-ckeditor-plugin': 'other/plugin'
+				}
+			};
+
+			spies.getDirectories.restore();
+			spies.getDirectories = sinon.stub( tools, 'getCKE5Directories', () => [ 'ckeditor5-core', 'ckeditor5-plugin-devtest' ] );
+
+			initTask( mainRepositoryPath, packageJSON, options, emptyFn, emptyFn );
+			expect( spies.getDependencies.calledOnce ).to.equal( true );
+			expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
+			expect( spies.getDirectories.calledOnce ).to.equal( true );
+			expect( spies.getDirectories.firstCall.args[ 0 ] ).to.equal( path.join( mainRepositoryPath, options.workspaceRoot ) );
+			expect( spies.parseRepositoryUrl.calledTwice ).to.equal( true );
+			expect( spies.cloneRepository.called ).to.equal( false );
+			expect( spies.checkout.calledTwice ).to.equal( true );
+			expect( spies.linkDirectories.calledTwice ).to.equal( true );
+		} );
+	} );
+
+	function createSpies() {
+		spies = {
+			getDependencies: sinon.spy( tools, 'getCKEditorDependencies' ),
+			getDirectories: sinon.stub( tools, 'getCKE5Directories', () => [] ),
+			parseRepositoryUrl: sinon.spy( git, 'parseRepositoryUrl' ),
+			cloneRepository: sinon.stub( git, 'cloneRepository' ),
+			linkDirectories: sinon.stub( tools, 'linkDirectories' ),
+			checkout: sinon.stub( git, 'checkout' )
+		};
+	}
+
+	function restoreSpies() {
+		for ( let spy in spies ) {
+			spies[ spy ].restore();
+		}
+	}
+} );

+ 62 - 0
dev/tasks/utils/dev-init.js

@@ -0,0 +1,62 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+var tools = require( './tools' );
+var git = require( './git' );
+var path = require( 'path' );
+
+/**
+ * 1. Get CKEditor5 dependencies from package.json file.
+ * 2. Check if any of the repositories are already present in the workspace.
+ * 		2.1. If repository is present in the workspace, check it out to desired branch if one is provided.
+ * 		2.2. If repository is not present in the workspace, clone it and checkout to desired branch if one is provided.
+ * 3. Link new repository to node_modules. (do not use npm link, use standard linking instead)
+ */
+module.exports = ( ckeditor5Path, packageJSON, options, writeln, writeError ) => {
+	const workspaceAbsolutePath = path.join( ckeditor5Path, options.workspaceRoot );
+
+	// Get all CKEditor dependencies from package.json.
+	const dependencies = tools.getCKEditorDependencies( packageJSON.dependencies );
+
+	if ( dependencies ) {
+		const directories = tools.getCKE5Directories( workspaceAbsolutePath );
+
+		for ( let dependency in dependencies ) {
+			const repositoryURL = dependencies[ dependency ];
+			const urlInfo = git.parseRepositoryUrl( repositoryURL );
+			const repositoryAbsolutePath = path.join( workspaceAbsolutePath, dependency );
+
+			// Check if repository's directory already exists.
+			if ( directories.indexOf( dependency ) === -1 ) {
+				try {
+					writeln( `Clonning ${ repositoryURL }...` );
+					git.cloneRepository( urlInfo, workspaceAbsolutePath );
+				} catch ( error ) {
+					writeError( error );
+				}
+			}
+
+			// Check out proper branch.
+			try {
+				writeln( `Checking out ${ repositoryURL } to ${ urlInfo.branch }...` );
+				git.checkout( repositoryAbsolutePath, urlInfo.branch );
+			} catch ( error ) {
+				writeError( error );
+			}
+
+			// Link plugin.
+			try {
+				writeln( `Linking ${ repositoryURL }...` );
+				tools.linkDirectories( repositoryAbsolutePath, path.join( ckeditor5Path, 'node_modules' , dependency ) );
+			} catch ( error ) {
+				writeError( error );
+			}
+		}
+	} else {
+		writeln( 'No CKEditor5 dependencies found in package.json file.' );
+	}
+};