mirror of
https://github.com/apache/cordova-android.git
synced 2026-04-23 00:00:09 +08:00
refactor: remove shelljs dependency (#842)
Co-authored-by: エリス <erisu@users.noreply.github.com>
This commit is contained in:
@@ -19,9 +19,8 @@
|
||||
var rewire = require('rewire');
|
||||
var common = rewire('../../../bin/templates/cordova/lib/pluginHandlers');
|
||||
var path = require('path');
|
||||
var fs = require('fs');
|
||||
var fs = require('fs-extra');
|
||||
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');
|
||||
@@ -39,22 +38,22 @@ describe('common platform handler', function () {
|
||||
|
||||
describe('copyFile', function () {
|
||||
it('Test#001 : should throw if source path not found', function () {
|
||||
shell.rm('-rf', src);
|
||||
fs.removeSync(src);
|
||||
expect(function () { copyFile(test_dir, src, project_dir, dest); })
|
||||
.toThrow(new Error('"' + src + '" not found!'));
|
||||
});
|
||||
|
||||
it('Test#002 : should throw if src not in plugin directory', function () {
|
||||
shell.mkdir('-p', project_dir);
|
||||
fs.ensureDirSync(project_dir);
|
||||
fs.writeFileSync(non_plugin_file, 'contents', 'utf-8');
|
||||
var outside_file = '../non_plugin_file';
|
||||
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 + '"'));
|
||||
shell.rm('-rf', test_dir);
|
||||
fs.removeSync(test_dir);
|
||||
});
|
||||
|
||||
it('Test#003 : should allow symlink src, if inside plugin', function () {
|
||||
shell.mkdir('-p', java_dir);
|
||||
fs.ensureDirSync(java_dir);
|
||||
fs.writeFileSync(java_file, 'contents', 'utf-8');
|
||||
|
||||
// This will fail on windows if not admin - ignore the error in that case.
|
||||
@@ -63,11 +62,11 @@ describe('common platform handler', function () {
|
||||
}
|
||||
|
||||
copyFile(test_dir, symlink_file, project_dir, dest);
|
||||
shell.rm('-rf', project_dir);
|
||||
fs.removeSync(project_dir);
|
||||
});
|
||||
|
||||
it('Test#004 : should throw if symlink is linked to a file outside the plugin', function () {
|
||||
shell.mkdir('-p', java_dir);
|
||||
fs.ensureDirSync(java_dir);
|
||||
fs.writeFileSync(non_plugin_file, 'contents', 'utf-8');
|
||||
|
||||
// This will fail on windows if not admin - ignore the error in that case.
|
||||
@@ -77,68 +76,68 @@ describe('common platform handler', function () {
|
||||
|
||||
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 + '"'));
|
||||
shell.rm('-rf', project_dir);
|
||||
fs.removeSync(project_dir);
|
||||
});
|
||||
|
||||
it('Test#005 : should throw if dest is outside the project directory', function () {
|
||||
shell.mkdir('-p', java_dir);
|
||||
fs.ensureDirSync(java_dir);
|
||||
fs.writeFileSync(java_file, 'contents', 'utf-8');
|
||||
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'));
|
||||
shell.rm('-rf', project_dir);
|
||||
fs.removeSync(project_dir);
|
||||
});
|
||||
|
||||
it('Test#006 : should call mkdir -p on target path', function () {
|
||||
shell.mkdir('-p', java_dir);
|
||||
fs.ensureDirSync(java_dir);
|
||||
fs.writeFileSync(java_file, 'contents', 'utf-8');
|
||||
|
||||
var s = spyOn(shell, 'mkdir').and.callThrough();
|
||||
var s = spyOn(fs, 'ensureDirSync').and.callThrough();
|
||||
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);
|
||||
expect(s).toHaveBeenCalledWith(path.dirname(resolvedDest));
|
||||
fs.removeSync(project_dir);
|
||||
});
|
||||
|
||||
it('Test#007 : should call cp source/dest paths', function () {
|
||||
shell.mkdir('-p', java_dir);
|
||||
fs.ensureDirSync(java_dir);
|
||||
fs.writeFileSync(java_file, 'contents', 'utf-8');
|
||||
|
||||
var s = spyOn(shell, 'cp').and.callThrough();
|
||||
var s = spyOn(fs, 'copySync').and.callThrough();
|
||||
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);
|
||||
expect(s).toHaveBeenCalledWith(java_file, resolvedDest);
|
||||
|
||||
shell.rm('-rf', project_dir);
|
||||
fs.removeSync(project_dir);
|
||||
});
|
||||
});
|
||||
|
||||
describe('copyNewFile', function () {
|
||||
it('Test#008 : should throw if target path exists', function () {
|
||||
shell.mkdir('-p', dest);
|
||||
fs.ensureDirSync(dest);
|
||||
expect(function () { copyNewFile(test_dir, src, project_dir, dest); })
|
||||
.toThrow(new Error('"' + dest + '" already exists!'));
|
||||
shell.rm('-rf', dest);
|
||||
fs.removeSync(dest);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteJava', function () {
|
||||
beforeEach(function () {
|
||||
shell.mkdir('-p', java_dir);
|
||||
fs.ensureDirSync(java_dir);
|
||||
fs.writeFileSync(java_file, 'contents', 'utf-8');
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
shell.rm('-rf', java_dir);
|
||||
fs.removeSync(java_dir);
|
||||
});
|
||||
|
||||
it('Test#009 : should call fs.unlinkSync on the provided paths', function () {
|
||||
var s = spyOn(fs, 'unlinkSync').and.callThrough();
|
||||
var s = spyOn(fs, 'removeSync').and.callThrough();
|
||||
deleteJava(project_dir, java_file);
|
||||
expect(s).toHaveBeenCalled();
|
||||
expect(s).toHaveBeenCalledWith(path.resolve(project_dir, java_file));
|
||||
|
||||
Reference in New Issue
Block a user