8
0
Просмотр исходного кода

New methods in dev tools, need in new gulp update task.

Szymon Kupś 9 лет назад
Родитель
Сommit
168fff8d0b
2 измененных файлов с 133 добавлено и 2 удалено
  1. 46 1
      dev/tasks/dev/utils/tools.js
  2. 87 1
      dev/tests/dev/tools.js

+ 46 - 1
dev/tasks/dev/utils/tools.js

@@ -52,7 +52,9 @@ module.exports = {
 	linkDirectories( source, destination ) {
 		const fs = require( 'fs' );
 		// Remove destination directory if exists.
-		if ( this.isDirectory( destination ) ) {
+		if ( this.isSymlink( destination ) ) {
+			this.removeSymlink( destination );
+		} else if ( this.isDirectory( destination ) ) {
 			this.shExec( `rm -rf ${ destination }` );
 		}
 
@@ -132,6 +134,21 @@ module.exports = {
 	},
 
 	/**
+	 * Returns true if path points to symbolic link.
+	 *
+	 * @param {String} path
+	 */
+	isSymlink( path ) {
+		const fs = require( 'fs' );
+
+		try {
+			return fs.lstatSync( path ).isSymbolicLink();
+		} catch ( e ) {}
+
+		return false;
+	},
+
+	/**
 	 * Returns all directories under specified path that match 'ckeditor5' pattern.
 	 *
 	 * @param {String} path
@@ -209,6 +226,7 @@ module.exports = {
 	 * Calls `npm uninstall <name>` command in specified path.
 	 *
 	 * @param {String} path
+	 * @param {String} name
 	 */
 	npmUninstall( path, name ) {
 		this.shExec( `cd ${ path } && npm uninstall ${ name }` );
@@ -279,5 +297,32 @@ module.exports = {
 		}
 
 		return null;
+	},
+
+	/**
+	 * Returns list of symbolic links to directories with names starting with `ckeditor5-` prefix.
+	 *
+	 * @param {String} path Path to directory,
+	 * @returns {Array} Array with directories names.
+	 */
+	getCKE5Symlinks( path ) {
+		const fs = require( 'fs' );
+		const pth = require( 'path' );
+
+		return fs.readdirSync( path ).filter( item => {
+			const fullPath = pth.join( path, item );
+
+			return dependencyRegExp.test( item ) && this.isSymlink( fullPath );
+		} );
+	},
+
+	/**
+	 * Unlinks symbolic link under specified path.
+	 *
+	 * @param {String} path
+	 */
+	removeSymlink( path ) {
+		const fs = require( 'fs' );
+		fs.unlinkSync( path );
 	}
 };

+ 87 - 1
dev/tests/dev/tools.js

@@ -76,12 +76,28 @@ describe( 'utils', () => {
 				sinon.assert.notCalled( outFn );
 				sinon.assert.notCalled( errFn );
 			} );
+
+			it( 'should not log if no output from executed command', () => {
+				const sh = require( 'shelljs' );
+				const execStub = sandbox.stub( sh, 'exec' ).returns( { code: 0, stdout: '', stderr: '' } );
+				const log = require( '../../tasks/dev/utils/log' );
+				const outFn = sandbox.spy();
+				const errFn = sandbox.spy();
+				log.configure( outFn, errFn );
+
+				tools.shExec( 'command' );
+
+				sinon.assert.calledOnce( execStub );
+				sinon.assert.notCalled( outFn );
+				sinon.assert.notCalled( errFn );
+			} );
 		} );
 
 		describe( 'linkDirectories', () => {
 			it( 'should be defined', () => expect( tools.linkDirectories ).to.be.a( 'function' ) );
 
 			it( 'should link directories', () => {
+				const isSymlinkStub = sandbox.stub( tools, 'isSymlink' ).returns( false );
 				const isDirectoryStub = sandbox.stub( tools, 'isDirectory' ).returns( false );
 				const symlinkStub = sandbox.stub( fs, 'symlinkSync' );
 				const source = '/source/dir';
@@ -90,6 +106,7 @@ describe( 'utils', () => {
 				tools.linkDirectories( source, destination );
 
 				expect( isDirectoryStub.calledOnce ).to.equal( true );
+				expect( isSymlinkStub.calledOnce ).to.equal( true );
 				expect( symlinkStub.calledOnce ).to.equal( true );
 				expect( symlinkStub.firstCall.args[ 0 ] ).to.equal( source );
 				expect( symlinkStub.firstCall.args[ 1 ] ).to.equal( destination );
@@ -97,6 +114,7 @@ describe( 'utils', () => {
 
 			it( 'should remove destination directory before linking', () => {
 				const shExecStub = sandbox.stub( tools, 'shExec' );
+				const isSymlinkStub = sandbox.stub( tools, 'isSymlink' ).returns( false );
 				const isDirectoryStub = sandbox.stub( tools, 'isDirectory' ).returns( true );
 				const symlinkStub = sandbox.stub( fs, 'symlinkSync' );
 				const source = '/source/dir';
@@ -105,10 +123,31 @@ describe( 'utils', () => {
 				tools.linkDirectories( source, destination );
 
 				expect( isDirectoryStub.calledOnce ).to.equal( true );
+				expect( isSymlinkStub.calledOnce ).to.equal( true );
 				expect( shExecStub.calledOnce ).to.equal( true );
 				expect( symlinkStub.firstCall.args[ 0 ] ).to.equal( source );
 				expect( symlinkStub.firstCall.args[ 1 ] ).to.equal( destination );
 			} );
+
+			it( 'should unlink destination directory if symlink', () => {
+				const shExecStub = sandbox.stub( tools, 'shExec' );
+				const isSymlinkStub = sandbox.stub( tools, 'isSymlink' ).returns( true );
+				const removeSymlinkStub = sandbox.stub( tools, 'removeSymlink' );
+				const isDirectoryStub = sandbox.stub( tools, 'isDirectory' ).returns( true );
+				const symlinkStub = sandbox.stub( fs, 'symlinkSync' );
+				const source = '/source/dir';
+				const destination = '/destination/dir';
+
+				tools.linkDirectories( source, destination );
+
+				expect( isDirectoryStub.notCalled ).to.equal( true );
+				expect( isSymlinkStub.calledOnce ).to.equal( true );
+				expect( shExecStub.notCalled ).to.equal( true );
+				expect( removeSymlinkStub.calledOnce ).to.equal( true );
+				expect( removeSymlinkStub.firstCall.args[ 0 ] ).to.equal( destination );
+				expect( symlinkStub.firstCall.args[ 0 ] ).to.equal( source );
+				expect( symlinkStub.firstCall.args[ 1 ] ).to.equal( destination );
+			} );
 		} );
 
 		describe( 'getCKEditorDependencies', () => {
@@ -241,6 +280,26 @@ describe( 'utils', () => {
 			} );
 		} );
 
+		describe( 'isSymlink', () => {
+			it( 'should return true if path points to symbolic link', () => {
+				const path = 'path/to/file';
+				const fs = require( 'fs' );
+				sandbox.stub( fs, 'lstatSync' ).returns( {
+					isSymbolicLink: () => true
+				} );
+
+				expect( tools.isSymlink( path ) ).to.equal( true );
+			} );
+
+			it( 'should return false if lstatSync throws', () => {
+				const path = 'path/to/file';
+				const fs = require( 'fs' );
+				sandbox.stub( fs, 'lstatSync' ).throws();
+
+				expect( tools.isSymlink( path ) ).to.equal( false );
+			} );
+		} );
+
 		describe( 'getCKE5Directories', () => {
 			it( 'should be defined', () => expect( tools.getCKE5Directories ).to.be.a( 'function' ) );
 
@@ -348,7 +407,7 @@ describe( 'utils', () => {
 				tools.npmUpdate( path );
 
 				expect( shExecStub.calledOnce ).to.equal( true );
-				expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cd ${ path } && npm update` );
+				expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cd ${ path } && npm update --dev` );
 			} );
 		} );
 
@@ -441,5 +500,32 @@ describe( 'utils', () => {
 				expect( getUrlSpy.threw( error ) ).to.equal( true );
 			} );
 		} );
+
+		describe( 'getCKE5Symlinks', () => {
+			it( 'should return CKE5 symlinks from provided path', () => {
+				const fs = require( 'fs' );
+				const path = 'path/to/dir';
+				sandbox.stub( fs, 'readdirSync' ).returns( [ 'ckeditor5-core', 'ckeditor5-image', 'other-dependency' ] );
+				sandbox.stub( tools, 'isSymlink' ).returns( true );
+
+				const symlinks = tools.getCKE5Symlinks( path );
+				expect( symlinks.length ).to.equal( 2 );
+				expect( symlinks[ 0 ] ).to.equal( 'ckeditor5-core' );
+				expect( symlinks[ 1 ] ).to.equal( 'ckeditor5-image' );
+			} );
+		} );
+
+		describe( 'removeSymlink', () => {
+			it( 'should unlink provided symlink', () => {
+				const fs = require( 'fs' );
+				const unlinkStub = sandbox.stub( fs, 'unlinkSync' );
+				const path = 'path/to/symlink';
+
+				tools.removeSymlink( path );
+
+				expect( unlinkStub.calledOnce ).to.equal( true );
+				expect( unlinkStub.firstCall.args[ 0 ] ).to.equal( path );
+			} );
+		} );
 	} );
 } );