Explorar el Código

Changed dev-init task to use dev-install on each dependency. Updated tests.

Szymon Kupś hace 10 años
padre
commit
dcfdffa551
Se han modificado 4 ficheros con 64 adiciones y 123 borrados
  1. 1 1
      dev/tasks/dev.js
  2. 6 44
      dev/tasks/utils/dev-init.js
  3. 57 0
      dev/tests/dev-init.js
  4. 0 78
      dev/tests/dev-tasks.js

+ 1 - 1
dev/tasks/dev.js

@@ -20,7 +20,7 @@ module.exports = ( grunt ) => {
 	const workspaceRoot = grunt.config.data.workspaceRoot;
 
 	grunt.registerTask( 'dev-init', function() {
-		initTask( ckeditor5Path, packageJSON, workspaceRoot, grunt.log.writeln, grunt.log.error );
+		initTask( installTask, ckeditor5Path, packageJSON, workspaceRoot, grunt.log.writeln );
 	} );
 
 	grunt.registerTask( 'dev-plugin-create', function() {

+ 6 - 44
dev/tasks/utils/dev-init.js

@@ -6,66 +6,28 @@
 'use strict';
 
 const tools = require( './tools' );
-const git = require( './git' );
-const 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. Pull changes from remote branch.
- * 4. Link each new repository to node_modules. (do not use npm link, use standard linking instead)
- * 5. Run `npm install` in each repository.
- * 6. Install Git hooks in each repository.
+ * 2. Run install task on each dependency.
  *
+ * @param {Function} installTask Install task to use on each dependency.
  * @param {String} ckeditor5Path Path to main CKEditor5 repository.
  * @param {Object} packageJSON Parsed package.json file from CKEditor5 repository.
  * @param {String} workspaceRoot Relative path to workspace root.
  * @param {Function} writeln Function for log output.
- * @param {Function} writeError Function of error output
  */
-module.exports = ( ckeditor5Path, packageJSON, workspaceRoot, writeln, writeError ) => {
-	const workspaceAbsolutePath = path.join( ckeditor5Path, workspaceRoot );
-
+module.exports = ( installTask, ckeditor5Path, packageJSON, workspaceRoot, writeln ) => {
 	// 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.
-			try {
-				if ( directories.indexOf( dependency ) === -1 ) {
-					writeln( `Clonning ${ repositoryURL }...` );
-					git.cloneRepository( urlInfo, workspaceAbsolutePath );
-				}
-
-				// Check out proper branch.
-				writeln( `Checking out ${ repositoryURL } to ${ urlInfo.branch }...` );
-				git.checkout( repositoryAbsolutePath, urlInfo.branch );
-
-				writeln( `Pulling changes to ${ repositoryURL } ${ urlInfo.branch }...` );
-				git.pull( repositoryAbsolutePath, urlInfo.branch );
-
-				writeln( `Linking ${ repositoryURL }...` );
-				tools.linkDirectories( repositoryAbsolutePath, path.join( ckeditor5Path, 'node_modules' , dependency ) );
-
-				writeln( `Running npm install in ${ repositoryURL }.` );
-				tools.npmInstall( repositoryAbsolutePath );
-
-				writeln( `Installing Git hooks in ${ repositoryURL }.` );
-				tools.installGitHooks( repositoryAbsolutePath );
-			} catch ( error ) {
-				writeError( error );
-			}
+			writeln( `\x1b[1m\x1b[36m${ dependency }\x1b[0m` );
+			installTask( ckeditor5Path, workspaceRoot, repositoryURL, writeln );
 		}
 	} else {
-		writeln( 'No CKEditor5 dependencies found in package.json file.' );
+		writeln( 'No CKEditor5 dependencies (ckeditor5-) found in package.json file.' );
 	}
 };

+ 57 - 0
dev/tests/dev-init.js

@@ -0,0 +1,57 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* global describe, it */
+
+'use strict';
+
+const sinon = require( 'sinon' );
+const tools = require( '../tasks/utils/tools' );
+
+describe( 'dev-init', () => {
+	const initTask = require( '../tasks/utils/dev-init' );
+	const ckeditor5Path = 'path/to/ckeditor5';
+	const workspaceRoot = '..';
+	const emptyFn = () => {};
+
+	it( 'should get all ckedtior5- dependencies and execute dev-install on them', () => {
+		const getDependenciesSpy = sinon.spy( tools, 'getCKEditorDependencies' );
+		const installSpy = sinon.spy();
+		const JSON = {
+			dependencies: {
+				'ckeditor5-core': 'ckeditor/ckeditor5-code',
+				'non-ckeditor-plugin': '^2.0.0',
+				'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest'
+			}
+		};
+		const deps = JSON.dependencies;
+
+		initTask( installSpy, ckeditor5Path, JSON, workspaceRoot, emptyFn );
+
+		getDependenciesSpy.restore();
+
+		sinon.assert.calledOnce( getDependenciesSpy );
+		sinon.assert.calledWithExactly( getDependenciesSpy, deps );
+		sinon.assert.calledTwice( installSpy );
+		sinon.assert.calledWithExactly( installSpy.firstCall, ckeditor5Path, workspaceRoot, deps[ 'ckeditor5-core' ], emptyFn );
+		sinon.assert.calledWithExactly( installSpy.secondCall, ckeditor5Path, workspaceRoot, deps[ 'ckeditor5-plugin-devtest' ], emptyFn );
+	} );
+
+	it( 'should not call dev-install if no ckedtior5- dependencies', () => {
+		const getDependenciesSpy = sinon.spy( tools, 'getCKEditorDependencies' );
+		const installSpy = sinon.spy();
+		const JSON = {
+			dependencies: {}
+		};
+
+		initTask( installSpy, ckeditor5Path, JSON, workspaceRoot, emptyFn );
+
+		getDependenciesSpy.restore();
+
+		sinon.assert.calledOnce( getDependenciesSpy );
+		sinon.assert.calledWithExactly( getDependenciesSpy, JSON.dependencies );
+		sinon.assert.notCalled( installSpy );
+	} );
+} );

+ 0 - 78
dev/tests/dev-tasks.js

@@ -58,84 +58,6 @@ describe( 'dev-tasks', () => {
 		}
 	}
 
-	describe( 'dev-init', () => {
-		const initTask = require( '../tasks/utils/dev-init' );
-
-		it( 'task should exist', () => 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, workspaceRoot, 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.pull.called ).to.equal( false );
-			expect( spies.npmInstall.called ).to.equal( false );
-			expect( spies.installGitHooks.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, workspaceRoot, 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, 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.pull.calledTwice ).to.equal( true );
-			expect( spies.linkDirectories.calledTwice ).to.equal( true );
-			expect( spies.npmInstall.calledTwice ).to.equal( true );
-			expect( spies.installGitHooks.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, workspaceRoot, 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, workspaceRoot ) );
-			expect( spies.parseRepositoryUrl.calledTwice ).to.equal( true );
-			expect( spies.cloneRepository.called ).to.equal( false );
-			expect( spies.checkout.calledTwice ).to.equal( true );
-			expect( spies.pull.calledTwice ).to.equal( true );
-			expect( spies.linkDirectories.calledTwice ).to.equal( true );
-			expect( spies.npmInstall.calledTwice ).to.equal( true );
-			expect( spies.installGitHooks.calledTwice ).to.equal( true );
-		} );
-	} );
-
 	describe( 'dev-plugin-create', () => {
 		const pluginCreateTask = require( '../tasks/utils/dev-plugin-create' );
 		const repositoryPath = path.join( workspacePath, pluginName );