2016-02-29 23:49:17 +08:00
/ *
*
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing ,
* software distributed under the License is distributed on an
* "AS IS" BASIS , WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND , either express or implied . See the License for the
* specific language governing permissions and limitations
* under the License .
*
* /
var rewire = require ( 'rewire' ) ;
var common = rewire ( '../../../bin/templates/cordova/lib/pluginHandlers' ) ;
var path = require ( 'path' ) ;
var fs = require ( 'fs' ) ;
var osenv = require ( 'os' ) ;
var shell = require ( 'shelljs' ) ;
var test _dir = path . join ( osenv . tmpdir ( ) , 'test_plugman' ) ;
var project _dir = path . join ( test _dir , 'project' ) ;
var src = path . join ( project _dir , 'src' ) ;
var dest = path . join ( project _dir , 'dest' ) ;
var java _dir = path . join ( src , 'one' , 'two' , 'three' ) ;
var java _file = path . join ( java _dir , 'test.java' ) ;
var symlink _file = path . join ( java _dir , 'symlink' ) ;
var non _plugin _file = path . join ( osenv . tmpdir ( ) , 'non_plugin_file' ) ;
var copyFile = common . _ _get _ _ ( 'copyFile' ) ;
var deleteJava = common . _ _get _ _ ( 'deleteJava' ) ;
var copyNewFile = common . _ _get _ _ ( 'copyNewFile' ) ;
2017-06-14 02:42:20 +08:00
describe ( 'common platform handler' , function ( ) {
2016-02-29 23:49:17 +08:00
2017-06-14 02:42:20 +08:00
describe ( 'copyFile' , function ( ) {
it ( 'Test#001 : should throw if source path not found' , function ( ) {
2016-02-29 23:49:17 +08:00
shell . rm ( '-rf' , src ) ;
2017-06-14 02:42:20 +08:00
expect ( function ( ) { copyFile ( test _dir , src , project _dir , dest ) ; } )
2016-02-29 23:49:17 +08:00
. toThrow ( new Error ( '"' + src + '" not found!' ) ) ;
} ) ;
2017-06-14 02:42:20 +08:00
it ( 'Test#002 : should throw if src not in plugin directory' , function ( ) {
2016-02-29 23:49:17 +08:00
shell . mkdir ( '-p' , project _dir ) ;
fs . writeFileSync ( non _plugin _file , 'contents' , 'utf-8' ) ;
2016-05-04 07:25:48 +08:00
var outside _file = '../non_plugin_file' ;
2017-06-14 02:42:20 +08:00
expect ( function ( ) { copyFile ( test _dir , outside _file , project _dir , dest ) ; } )
. toThrow ( new Error ( 'File "' + path . resolve ( test _dir , outside _file ) + '" is located outside the plugin directory "' + test _dir + '"' ) ) ;
2016-02-29 23:49:17 +08:00
shell . rm ( '-rf' , test _dir ) ;
} ) ;
2017-06-14 02:42:20 +08:00
it ( 'Test#003 : should allow symlink src, if inside plugin' , function ( ) {
2016-02-29 23:49:17 +08:00
shell . mkdir ( '-p' , java _dir ) ;
fs . writeFileSync ( java _file , 'contents' , 'utf-8' ) ;
// This will fail on windows if not admin - ignore the error in that case.
if ( ignoreEPERMonWin32 ( java _file , symlink _file ) ) {
return ;
}
copyFile ( test _dir , symlink _file , project _dir , dest ) ;
shell . rm ( '-rf' , project _dir ) ;
} ) ;
2017-06-14 02:42:20 +08:00
it ( 'Test#004 : should throw if symlink is linked to a file outside the plugin' , function ( ) {
2016-02-29 23:49:17 +08:00
shell . mkdir ( '-p' , java _dir ) ;
fs . writeFileSync ( non _plugin _file , 'contents' , 'utf-8' ) ;
// This will fail on windows if not admin - ignore the error in that case.
if ( ignoreEPERMonWin32 ( non _plugin _file , symlink _file ) ) {
return ;
}
2017-06-14 02:42:20 +08:00
expect ( function ( ) { copyFile ( test _dir , symlink _file , project _dir , dest ) ; } )
. toThrow ( new Error ( 'File "' + path . resolve ( test _dir , symlink _file ) + '" is located outside the plugin directory "' + test _dir + '"' ) ) ;
2016-02-29 23:49:17 +08:00
shell . rm ( '-rf' , project _dir ) ;
} ) ;
2017-06-14 02:42:20 +08:00
it ( 'Test#005 : should throw if dest is outside the project directory' , function ( ) {
2016-02-29 23:49:17 +08:00
shell . mkdir ( '-p' , java _dir ) ;
fs . writeFileSync ( java _file , 'contents' , 'utf-8' ) ;
2017-06-14 02:42:20 +08:00
expect ( function ( ) { copyFile ( test _dir , java _file , project _dir , non _plugin _file ) ; } )
. toThrow ( new Error ( 'Destination "' + path . resolve ( project _dir , non _plugin _file ) + '" for source file "' + path . resolve ( test _dir , java _file ) + '" is located outside the project' ) ) ;
2016-02-29 23:49:17 +08:00
shell . rm ( '-rf' , project _dir ) ;
} ) ;
2017-06-14 02:42:20 +08:00
it ( 'Test#006 : should call mkdir -p on target path' , function ( ) {
2016-02-29 23:49:17 +08:00
shell . mkdir ( '-p' , java _dir ) ;
fs . writeFileSync ( java _file , 'contents' , 'utf-8' ) ;
2017-01-04 03:02:48 +08:00
var s = spyOn ( shell , 'mkdir' ) . and . callThrough ( ) ;
2016-02-29 23:49:17 +08:00
var resolvedDest = path . resolve ( project _dir , dest ) ;
copyFile ( test _dir , java _file , project _dir , dest ) ;
expect ( s ) . toHaveBeenCalled ( ) ;
expect ( s ) . toHaveBeenCalledWith ( '-p' , path . dirname ( resolvedDest ) ) ;
shell . rm ( '-rf' , project _dir ) ;
} ) ;
2017-06-14 02:42:20 +08:00
it ( 'Test#007 : should call cp source/dest paths' , function ( ) {
2016-02-29 23:49:17 +08:00
shell . mkdir ( '-p' , java _dir ) ;
fs . writeFileSync ( java _file , 'contents' , 'utf-8' ) ;
2017-01-04 03:02:48 +08:00
var s = spyOn ( shell , 'cp' ) . and . callThrough ( ) ;
2016-02-29 23:49:17 +08:00
var resolvedDest = path . resolve ( project _dir , dest ) ;
copyFile ( test _dir , java _file , project _dir , dest ) ;
expect ( s ) . toHaveBeenCalled ( ) ;
expect ( s ) . toHaveBeenCalledWith ( '-f' , java _file , resolvedDest ) ;
shell . rm ( '-rf' , project _dir ) ;
} ) ;
} ) ;
describe ( 'copyNewFile' , function ( ) {
2017-06-14 02:42:20 +08:00
it ( 'Test#008 : should throw if target path exists' , function ( ) {
2016-02-29 23:49:17 +08:00
shell . mkdir ( '-p' , dest ) ;
2017-06-14 02:42:20 +08:00
expect ( function ( ) { copyNewFile ( test _dir , src , project _dir , dest ) ; } )
. toThrow ( new Error ( '"' + dest + '" already exists!' ) ) ;
2016-02-29 23:49:17 +08:00
shell . rm ( '-rf' , dest ) ;
} ) ;
} ) ;
2017-06-14 02:42:20 +08:00
describe ( 'deleteJava' , function ( ) {
beforeEach ( function ( ) {
2016-02-29 23:49:17 +08:00
shell . mkdir ( '-p' , java _dir ) ;
fs . writeFileSync ( java _file , 'contents' , 'utf-8' ) ;
} ) ;
2017-06-14 02:42:20 +08:00
afterEach ( function ( ) {
2016-02-29 23:49:17 +08:00
shell . rm ( '-rf' , java _dir ) ;
} ) ;
2017-06-14 02:42:20 +08:00
it ( 'Test#009 : should call fs.unlinkSync on the provided paths' , function ( ) {
2017-01-04 03:02:48 +08:00
var s = spyOn ( fs , 'unlinkSync' ) . and . callThrough ( ) ;
2016-02-29 23:49:17 +08:00
deleteJava ( project _dir , java _file ) ;
expect ( s ) . toHaveBeenCalled ( ) ;
expect ( s ) . toHaveBeenCalledWith ( path . resolve ( project _dir , java _file ) ) ;
} ) ;
2017-06-14 02:42:20 +08:00
it ( 'Test#010 : should delete empty directories after removing source code in a java src path hierarchy' , function ( ) {
2016-02-29 23:49:17 +08:00
deleteJava ( project _dir , java _file ) ;
expect ( fs . existsSync ( java _file ) ) . not . toBe ( true ) ;
expect ( fs . existsSync ( java _dir ) ) . not . toBe ( true ) ;
2017-06-14 02:42:20 +08:00
expect ( fs . existsSync ( path . join ( src , 'one' ) ) ) . not . toBe ( true ) ;
2016-02-29 23:49:17 +08:00
} ) ;
2017-06-14 02:42:20 +08:00
it ( 'Test#011 : should never delete the top-level src directory, even if all plugins added were removed' , function ( ) {
2016-02-29 23:49:17 +08:00
deleteJava ( project _dir , java _file ) ;
expect ( fs . existsSync ( src ) ) . toBe ( true ) ;
} ) ;
} ) ;
} ) ;
2017-06-14 02:42:20 +08:00
function ignoreEPERMonWin32 ( symlink _src , symlink _dest ) {
2016-02-29 23:49:17 +08:00
try {
fs . symlinkSync ( symlink _src , symlink _dest ) ;
} catch ( e ) {
if ( process . platform === 'win32' && e . message . indexOf ( 'Error: EPERM, operation not permitted' > - 1 ) ) {
return true ;
}
throw e ;
}
return false ;
}