Jelajahi Sumber

Updated dev-boilerplate-update tests.

Szymon Kupś 10 tahun lalu
induk
melakukan
0bdff65faf

+ 13 - 9
dev/tasks/utils/dev-boilerplate-update.js

@@ -29,18 +29,22 @@ module.exports = ( ckeditor5Path, packageJSON, workspaceRoot, writeln, writeErro
 	if ( dependencies ) {
 		const directories = tools.getCKE5Directories( workspaceAbsolutePath );
 
-		for ( let dependency in dependencies ) {
-			const repositoryAbsolutePath = path.join( workspaceAbsolutePath, dependency );
+		if ( directories.length ) {
+			for ( let dependency in dependencies ) {
+				const repositoryAbsolutePath = path.join( workspaceAbsolutePath, dependency );
 
-			// Check if repository's directory already exists.
-			if ( directories.indexOf( dependency ) > -1 ) {
-				try {
-					writeln( `Updating boilerplate in ${ dependency }...` );
-					git.updateBoilerplate( repositoryAbsolutePath );
-				} catch ( error ) {
-					writeError( error );
+				// Check if repository's directory already exists.
+				if ( directories.indexOf( dependency ) > -1 ) {
+					try {
+						writeln( `Updating boilerplate in ${ dependency }...` );
+						git.updateBoilerplate( repositoryAbsolutePath );
+					} catch ( error ) {
+						writeError( error );
+					}
 				}
 			}
+		} else {
+			writeln( 'No CKEditor5 plugins in development mode.' );
 		}
 	} else {
 		writeln( 'No CKEditor5 dependencies found in package.json file.' );

+ 110 - 0
dev/tests/dev-boilerplate-update.js

@@ -0,0 +1,110 @@
+/**
+ * @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' );
+const git = require( '../tasks/utils/git' );
+const path = require( 'path' );
+
+describe( 'dev-boilerplate-update', () => {
+	const task = require( '../tasks/utils/dev-boilerplate-update' );
+	const ckeditor5Path = 'path/to/ckeditor5';
+	const workspaceRoot = '..';
+	const workspaceAbsolutePath = path.join( ckeditor5Path, workspaceRoot );
+	const emptyFn = () => {};
+
+	it( 'should update boilerplate in dev repositories', () => {
+		const dirs = [ 'ckeditor5-core', 'ckeditor5-devtest' ];
+		const getDependenciesSpy = sinon.spy( tools, 'getCKEditorDependencies' );
+		const getDirectoriesStub = sinon.stub( tools, 'getCKE5Directories' ).returns( dirs );
+		const updateStub = sinon.stub( git, 'updateBoilerplate' );
+		const json = {
+			dependencies: {
+				'ckeditor5-core': 'ckeditor/ckeditor5-core',
+				'ckeditor5-devtest': 'ckeditor/ckeditor5-devtest#new-branch',
+				'other-plugin': '1.2.3'
+			}
+		};
+
+		task( ckeditor5Path, json, workspaceRoot, emptyFn, emptyFn );
+
+		getDependenciesSpy.restore();
+		getDirectoriesStub.restore();
+		updateStub.restore();
+
+		sinon.assert.calledTwice( updateStub );
+		sinon.assert.calledWithExactly( updateStub.firstCall, path.join( workspaceAbsolutePath, dirs[ 0 ] ) );
+		sinon.assert.calledWithExactly( updateStub.secondCall, path.join( workspaceAbsolutePath, dirs[ 1 ] ) );
+	} );
+
+	it( 'should not update boilerplate when no dependencies are found', () => {
+		const getDependenciesSpy = sinon.spy( tools, 'getCKEditorDependencies' );
+		const getDirectoriesStub = sinon.stub( tools, 'getCKE5Directories' );
+		const updateStub = sinon.stub( git, 'updateBoilerplate' );
+		const json = {
+			dependencies: {
+				'other-plugin': '1.2.3'
+			}
+		};
+
+		task( ckeditor5Path, json, workspaceRoot, emptyFn, emptyFn );
+
+		getDependenciesSpy.restore();
+		getDirectoriesStub.restore();
+		updateStub.restore();
+
+		sinon.assert.notCalled( updateStub );
+	} );
+
+	it( 'should not update boilerplate when no plugins in dev mode', () => {
+		const getDependenciesSpy = sinon.spy( tools, 'getCKEditorDependencies' );
+		const getDirectoriesStub = sinon.stub( tools, 'getCKE5Directories' ).returns( [] );
+		const updateStub = sinon.stub( git, 'updateBoilerplate' );
+		const json = {
+			dependencies: {
+				'ckeditor5-devtest': 'ckeditor/ckeditor5-devtest#new-branch',
+				'other-plugin': '1.2.3'
+			}
+		};
+
+		task( ckeditor5Path, json, workspaceRoot, emptyFn, emptyFn );
+
+		getDependenciesSpy.restore();
+		getDirectoriesStub.restore();
+		updateStub.restore();
+
+		sinon.assert.notCalled( updateStub );
+	} );
+
+	it( 'should write error message when updating boilerplate is unsuccessful', () => {
+		const dirs = [ 'ckeditor5-core' ];
+		const getDependenciesSpy = sinon.spy( tools, 'getCKEditorDependencies' );
+		const getDirectoriesStub = sinon.stub( tools, 'getCKE5Directories' ).returns( dirs );
+		const error = new Error( 'Error message.' );
+		const updateStub = sinon.stub( git, 'updateBoilerplate' ).throws( error );
+		const json = {
+			dependencies: {
+				'ckeditor5-core': 'ckeditor/ckeditor5-core',
+				'ckeditor5-devtest': 'ckeditor/ckeditor5-devtest#new-branch',
+				'other-plugin': '1.2.3'
+			}
+		};
+		const writeErrorSpy = sinon.spy();
+
+		task( ckeditor5Path, json, workspaceRoot, emptyFn, writeErrorSpy );
+
+		getDependenciesSpy.restore();
+		getDirectoriesStub.restore();
+		updateStub.restore();
+
+		sinon.assert.calledOnce( updateStub );
+		sinon.assert.calledOnce( writeErrorSpy );
+		sinon.assert.calledWithExactly( writeErrorSpy, error );
+	} );
+} );

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

@@ -118,32 +118,4 @@ describe( 'dev-tasks', () => {
 			expect( spies.linkDirectories.secondCall.args[ 1 ] ).to.equal( path.join( mainRepositoryPath, 'node_modules', dirs[ 1 ] ) );
 		} );
 	} );
-
-	describe( 'dev-boilerplate-update', () => {
-		const devBoilerplateTask = require( '../tasks/utils/dev-boilerplate-update' );
-
-		it( 'should exist', () => expect( devBoilerplateTask ).to.be.a( 'function' ) );
-
-		it( 'should update boilerplate in repositories', () => {
-			const packageJSON = {
-				dependencies: {
-					'ckeditor5-core': 'ckeditor/ckeditor5-core',
-					'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
-					'non-ckeditor-plugin': 'other/plugin'
-				}
-			};
-
-			const dirs = [ 'ckeditor5-core', 'ckeditor5-plugin-devtest' ];
-			spies.getDirectories.restore();
-			spies.getDirectories = sinon.stub( tools, 'getCKE5Directories', () => dirs );
-
-			devBoilerplateTask( mainRepositoryPath, packageJSON, workspaceRoot, emptyFn, emptyFn );
-
-			expect( spies.getDependencies.calledOnce ).to.equal( true );
-			expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
-			expect( spies.updateBoilerplate.calledTwice ).to.equal( true );
-			expect( spies.updateBoilerplate.firstCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 0 ] ) );
-			expect( spies.updateBoilerplate.secondCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 1 ] ) );
-		} );
-	} );
 } );