Bladeren bron

Added tests to task tools. Updated comments.

Szymon Kupś 10 jaren geleden
bovenliggende
commit
3f4280ee35
2 gewijzigde bestanden met toevoegingen van 66 en 0 verwijderingen
  1. 50 0
      dev/tasks/tests/tools.js
  2. 16 0
      dev/tasks/utils/tools.js

+ 50 - 0
dev/tasks/tests/tools.js

@@ -162,5 +162,55 @@ describe( 'utils', () => {
 				expect( directories[ 1 ] ).equal( 'ckeditor5-plugin-image' );
 			} );
 		} );
+
+		describe( 'updateJSONFile', () => {
+			it( 'should be defined', () => expect( tools.updateJSONFile ).to.be.a( 'function' ) );
+			it( 'should read, update and save JSON file', () => {
+				const path = 'path/to/file.json';
+				const fs = require( 'fs' );
+				const readFileStub = sinon.stub( fs, 'readFileSync', () => '{}' );
+				const modifiedJSON = { modified: true };
+				const writeFileStub = sinon.stub( fs, 'writeFileSync' );
+				toRestore.push( readFileStub, writeFileStub );
+
+				tools.updateJSONFile( path, () => {
+					return modifiedJSON;
+				} );
+
+				expect( readFileStub.calledOnce ).to.equal( true );
+				expect( readFileStub.firstCall.args[ 0 ] ).to.equal( path );
+				expect( writeFileStub.calledOnce ).to.equal( true );
+				expect( writeFileStub.firstCall.args[ 0 ] ).to.equal( path );
+				expect( writeFileStub.firstCall.args[ 1 ] ).to.equal( JSON.stringify( modifiedJSON, null, 2 ) );
+			} );
+		} );
+
+		describe( 'npmInstall', () => {
+			it( 'should be defined', () => expect( tools.npmInstall ).to.be.a( 'function' ) );
+			it( 'should execute npm install command', () => {
+				const shExecStub = sinon.stub( tools, 'shExec' );
+				const path = '/path/to/repository';
+				toRestore.push( shExecStub );
+
+				tools.npmInstall( path );
+
+				expect( shExecStub.calledOnce ).to.equal( true );
+				expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cd ${ path } && npm install` );
+			} );
+		} );
+
+		describe( 'installGitHooks', () => {
+			it( 'should be defined', () => expect( tools.installGitHooks ).to.be.a( 'function' ) );
+			it( 'should execute grunt githooks command', () => {
+				const shExecStub = sinon.stub( tools, 'shExec' );
+				const path = '/path/to/repository';
+				toRestore.push( shExecStub );
+
+				tools.installGitHooks( path );
+
+				expect( shExecStub.calledOnce ).to.equal( true );
+				expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cd ${ path } && grunt githooks` );
+			} );
+		} );
 	} );
 } );

+ 16 - 0
dev/tasks/utils/tools.js

@@ -221,6 +221,12 @@ module.exports = {
 		} );
 	},
 
+	/**
+	 * Updates JSON file under specified path.
+	 * @param {String} path Path to file on disk.
+	 * @param {Function} updateFunction Function that will be called with parsed JSON object. It should return
+	 * modified JSON object to save.
+	 */
 	updateJSONFile( path, updateFunction ) {
 		const fs = require( 'fs' );
 
@@ -231,10 +237,20 @@ module.exports = {
 		fs.writeFileSync( path, JSON.stringify( json, null, 2 ), 'utf-8' );
 	},
 
+	/**
+	 * Calls `npm install` command in specified path.
+	 *
+	 * @param {String} path
+	 */
 	npmInstall( path ) {
 		this.shExec( `cd ${ path } && npm install` );
 	},
 
+	/**
+	 * Installs Git hooks in specified repository.
+	 *
+	 * @param {String} path
+	 */
 	installGitHooks( path ) {
 		this.shExec( `cd ${ path } && grunt githooks` );
 	}