mirror of
https://github.com/apache/cordova-android.git
synced 2026-04-23 00:00:09 +08:00
CB-10729 Move plugin handlers tests for into platform's repo
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
*
|
||||
*
|
||||
* 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');
|
||||
|
||||
describe('common platform handler', function() {
|
||||
|
||||
describe('copyFile', function() {
|
||||
it('should throw if source path not found', function(){
|
||||
shell.rm('-rf', src);
|
||||
expect(function(){copyFile(test_dir, src, project_dir, dest);})
|
||||
.toThrow(new Error('"' + src + '" not found!'));
|
||||
});
|
||||
|
||||
it('should throw if src not in plugin directory', function(){
|
||||
shell.mkdir('-p', project_dir);
|
||||
fs.writeFileSync(non_plugin_file, 'contents', 'utf-8');
|
||||
expect(function(){copyFile(test_dir, '../non_plugin_file', project_dir, dest);}).
|
||||
toThrow(new Error('"' + non_plugin_file + '" not located within plugin!'));
|
||||
shell.rm('-rf', test_dir);
|
||||
});
|
||||
|
||||
it('should allow symlink src, if inside plugin', function(){
|
||||
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);
|
||||
});
|
||||
|
||||
it('should throw if symlink is linked to a file outside the plugin', function(){
|
||||
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;
|
||||
}
|
||||
|
||||
expect(function(){copyFile(test_dir, symlink_file, project_dir, dest);}).
|
||||
toThrow(new Error('"' + symlink_file + '" not located within plugin!'));
|
||||
shell.rm('-rf', project_dir);
|
||||
});
|
||||
|
||||
it('should throw if dest is outside the project directory', function(){
|
||||
shell.mkdir('-p', java_dir);
|
||||
fs.writeFileSync(java_file, 'contents', 'utf-8');
|
||||
expect(function(){copyFile(test_dir, java_file, project_dir, non_plugin_file);}).
|
||||
toThrow(new Error('"' + non_plugin_file + '" not located within project!'));
|
||||
shell.rm('-rf', project_dir);
|
||||
});
|
||||
|
||||
it('should call mkdir -p on target path', function(){
|
||||
shell.mkdir('-p', java_dir);
|
||||
fs.writeFileSync(java_file, 'contents', 'utf-8');
|
||||
|
||||
var s = spyOn(shell, 'mkdir').andCallThrough();
|
||||
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);
|
||||
});
|
||||
|
||||
it('should call cp source/dest paths', function(){
|
||||
shell.mkdir('-p', java_dir);
|
||||
fs.writeFileSync(java_file, 'contents', 'utf-8');
|
||||
|
||||
var s = spyOn(shell, 'cp').andCallThrough();
|
||||
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 () {
|
||||
it('should throw if target path exists', function(){
|
||||
shell.mkdir('-p', dest);
|
||||
expect(function(){copyNewFile(test_dir, src, project_dir, dest);}).
|
||||
toThrow(new Error('"' + dest + '" already exists!'));
|
||||
shell.rm('-rf', dest);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteJava', function() {
|
||||
beforeEach(function() {
|
||||
shell.mkdir('-p', java_dir);
|
||||
fs.writeFileSync(java_file, 'contents', 'utf-8');
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
shell.rm('-rf', java_dir);
|
||||
});
|
||||
|
||||
it('should call fs.unlinkSync on the provided paths', function(){
|
||||
var s = spyOn(fs, 'unlinkSync').andCallThrough();
|
||||
deleteJava(project_dir, java_file);
|
||||
expect(s).toHaveBeenCalled();
|
||||
expect(s).toHaveBeenCalledWith(path.resolve(project_dir, java_file));
|
||||
});
|
||||
|
||||
it('should delete empty directories after removing source code in a java src path hierarchy', function(){
|
||||
deleteJava(project_dir, java_file);
|
||||
expect(fs.existsSync(java_file)).not.toBe(true);
|
||||
expect(fs.existsSync(java_dir)).not.toBe(true);
|
||||
expect(fs.existsSync(path.join(src,'one'))).not.toBe(true);
|
||||
});
|
||||
|
||||
it('should never delete the top-level src directory, even if all plugins added were removed', function(){
|
||||
deleteJava(project_dir, java_file);
|
||||
expect(fs.existsSync(src)).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function ignoreEPERMonWin32(symlink_src, symlink_dest) {
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
/**
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you 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 android = common.__get__('handlers');
|
||||
var path = require('path');
|
||||
var fs = require('fs');
|
||||
var shell = require('shelljs');
|
||||
var os = require('os');
|
||||
var temp = path.join(os.tmpdir(), 'plugman');
|
||||
var plugins_dir = path.join(temp, 'cordova/plugins');
|
||||
var dummyplugin = path.join(__dirname, '../../fixtures/org.test.plugins.dummyplugin');
|
||||
var faultyplugin = path.join(__dirname, '../../fixtures/org.test.plugins.faultyplugin');
|
||||
var android_project = path.join(__dirname, '../../fixtures/android_project/*');
|
||||
|
||||
var PluginInfo = require('cordova-common').PluginInfo;
|
||||
var AndroidProject = require('../../../bin/templates/cordova/lib/AndroidProject');
|
||||
|
||||
var dummyPluginInfo = new PluginInfo(dummyplugin);
|
||||
var valid_source = dummyPluginInfo.getSourceFiles('android'),
|
||||
valid_resources = dummyPluginInfo.getResourceFiles('android'),
|
||||
valid_libs = dummyPluginInfo.getLibFiles('android');
|
||||
|
||||
var faultyPluginInfo = new PluginInfo(faultyplugin);
|
||||
var invalid_source = faultyPluginInfo.getSourceFiles('android');
|
||||
|
||||
describe('android project handler', function() {
|
||||
describe('installation', function() {
|
||||
var copyFileOrig = common.__get__('copyFile');
|
||||
var copyFileSpy = jasmine.createSpy('copyFile');
|
||||
var dummyProject;
|
||||
|
||||
beforeEach(function() {
|
||||
shell.mkdir('-p', temp);
|
||||
dummyProject = AndroidProject.getProjectFile(temp);
|
||||
common.__set__('copyFile', copyFileSpy);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
shell.rm('-rf', temp);
|
||||
common.__set__('copyFile', copyFileOrig);
|
||||
});
|
||||
|
||||
describe('of <lib-file> elements', function() {
|
||||
it('should copy files', function () {
|
||||
android['lib-file'].install(valid_libs[0], dummyPluginInfo, dummyProject);
|
||||
expect(copyFileSpy).toHaveBeenCalledWith(dummyplugin, 'src/android/TestLib.jar', temp, path.join('libs', 'TestLib.jar'), false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('of <resource-file> elements', function() {
|
||||
it('should copy files', function () {
|
||||
android['resource-file'].install(valid_resources[0], dummyPluginInfo, dummyProject);
|
||||
expect(copyFileSpy).toHaveBeenCalledWith(dummyplugin, 'android-resource.xml', temp, path.join('res', 'xml', 'dummy.xml'), false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('of <source-file> elements', function() {
|
||||
beforeEach(function() {
|
||||
shell.cp('-rf', android_project, temp);
|
||||
});
|
||||
|
||||
it('should copy stuff from one location to another by calling common.copyFile', function() {
|
||||
android['source-file'].install(valid_source[0], dummyPluginInfo, dummyProject);
|
||||
expect(copyFileSpy)
|
||||
.toHaveBeenCalledWith(dummyplugin, 'src/android/DummyPlugin.java', temp, path.join('src/com/phonegap/plugins/dummyplugin/DummyPlugin.java'), false);
|
||||
});
|
||||
|
||||
it('should throw if source file cannot be found', function() {
|
||||
common.__set__('copyFile', copyFileOrig);
|
||||
expect(function() {
|
||||
android['source-file'].install(invalid_source[0], faultyPluginInfo, dummyProject);
|
||||
}).toThrow('"' + path.resolve(faultyplugin, 'src/android/NotHere.java') + '" not found!');
|
||||
});
|
||||
|
||||
it('should throw if target file already exists', function() {
|
||||
// write out a file
|
||||
var target = path.resolve(temp, 'src/com/phonegap/plugins/dummyplugin');
|
||||
shell.mkdir('-p', target);
|
||||
target = path.join(target, 'DummyPlugin.java');
|
||||
fs.writeFileSync(target, 'some bs', 'utf-8');
|
||||
|
||||
expect(function() {
|
||||
android['source-file'].install(valid_source[0], dummyPluginInfo, dummyProject);
|
||||
}).toThrow('"' + target + '" already exists!');
|
||||
});
|
||||
});
|
||||
|
||||
describe('of <framework> elements', function() {
|
||||
|
||||
var someString = jasmine.any(String);
|
||||
|
||||
var copyNewFileOrig = common.__get__('copyNewFile');
|
||||
var copyNewFileSpy = jasmine.createSpy('copyNewFile');
|
||||
|
||||
beforeEach(function() {
|
||||
shell.cp('-rf', android_project, temp);
|
||||
|
||||
spyOn(dummyProject, 'addSystemLibrary');
|
||||
spyOn(dummyProject, 'addSubProject');
|
||||
spyOn(dummyProject, 'addGradleReference');
|
||||
common.__set__('copyNewFile', copyNewFileSpy);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
common.__set__('copyNewFile', copyNewFileOrig);
|
||||
});
|
||||
|
||||
it('should throw if framework doesn\'t have "src" attribute', function() {
|
||||
expect(function() { android.framework.install({}, dummyPluginInfo, dummyProject); }).toThrow();
|
||||
});
|
||||
|
||||
it('should install framework without "parent" attribute into project root', function() {
|
||||
var framework = {src: 'plugin-lib'};
|
||||
android.framework.install(framework, dummyPluginInfo, dummyProject);
|
||||
expect(dummyProject.addSystemLibrary).toHaveBeenCalledWith(dummyProject.projectDir, someString);
|
||||
});
|
||||
|
||||
it('should install framework with "parent" attribute into parent framework dir', function() {
|
||||
var childFramework = {src: 'plugin-lib2', parent: 'plugin-lib'};
|
||||
android.framework.install(childFramework, dummyPluginInfo, dummyProject);
|
||||
expect(dummyProject.addSystemLibrary).toHaveBeenCalledWith(path.resolve(dummyProject.projectDir, childFramework.parent), someString);
|
||||
});
|
||||
|
||||
it('should not copy anything if "custom" attribute is not set', function() {
|
||||
var framework = {src: 'plugin-lib'};
|
||||
var cpSpy = spyOn(shell, 'cp');
|
||||
android.framework.install(framework, dummyPluginInfo, dummyProject);
|
||||
expect(dummyProject.addSystemLibrary).toHaveBeenCalledWith(someString, framework.src);
|
||||
expect(cpSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should copy framework sources if "custom" attribute is set', function() {
|
||||
var framework = {src: 'plugin-lib', custom: true};
|
||||
android.framework.install(framework, dummyPluginInfo, dummyProject);
|
||||
expect(dummyProject.addSubProject).toHaveBeenCalledWith(dummyProject.projectDir, someString);
|
||||
expect(copyNewFileSpy).toHaveBeenCalledWith(dummyPluginInfo.dir, framework.src, dummyProject.projectDir, someString, false);
|
||||
});
|
||||
|
||||
it('should install gradleReference using project.addGradleReference', function() {
|
||||
var framework = {src: 'plugin-lib', custom: true, type: 'gradleReference'};
|
||||
android.framework.install(framework, dummyPluginInfo, dummyProject);
|
||||
expect(copyNewFileSpy).toHaveBeenCalledWith(dummyPluginInfo.dir, framework.src, dummyProject.projectDir, someString, false);
|
||||
expect(dummyProject.addGradleReference).toHaveBeenCalledWith(dummyProject.projectDir, someString);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('uninstallation', function() {
|
||||
|
||||
var removeFileOrig = common.__get__('removeFile');
|
||||
var deleteJavaOrig = common.__get__('deleteJava');
|
||||
|
||||
var removeFileSpy = jasmine.createSpy('removeFile');
|
||||
var deleteJavaSpy = jasmine.createSpy('deleteJava');
|
||||
var dummyProject;
|
||||
|
||||
beforeEach(function() {
|
||||
shell.mkdir('-p', temp);
|
||||
shell.mkdir('-p', plugins_dir);
|
||||
shell.cp('-rf', android_project, temp);
|
||||
AndroidProject.purgeCache();
|
||||
dummyProject = AndroidProject.getProjectFile(temp);
|
||||
common.__set__('removeFile', removeFileSpy);
|
||||
common.__set__('deleteJava', deleteJavaSpy);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
shell.rm('-rf', temp);
|
||||
common.__set__('removeFile', removeFileOrig);
|
||||
common.__set__('deleteJava', deleteJavaOrig);
|
||||
});
|
||||
|
||||
describe('of <lib-file> elements', function(done) {
|
||||
it('should remove jar files', function () {
|
||||
android['lib-file'].install(valid_libs[0], dummyPluginInfo, dummyProject);
|
||||
android['lib-file'].uninstall(valid_libs[0], dummyPluginInfo, dummyProject);
|
||||
expect(removeFileSpy).toHaveBeenCalledWith(temp, path.join('libs/TestLib.jar'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('of <resource-file> elements', function(done) {
|
||||
it('should remove files', function () {
|
||||
android['resource-file'].install(valid_resources[0], dummyPluginInfo, dummyProject);
|
||||
android['resource-file'].uninstall(valid_resources[0], dummyPluginInfo, dummyProject);
|
||||
expect(removeFileSpy).toHaveBeenCalledWith(temp, path.join('res/xml/dummy.xml'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('of <source-file> elements', function() {
|
||||
it('should remove stuff by calling common.deleteJava', function() {
|
||||
android['source-file'].install(valid_source[0], dummyPluginInfo, dummyProject);
|
||||
android['source-file'].uninstall(valid_source[0], dummyPluginInfo, dummyProject);
|
||||
expect(deleteJavaSpy).toHaveBeenCalledWith(temp, path.join('src/com/phonegap/plugins/dummyplugin/DummyPlugin.java'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('of <framework> elements', function() {
|
||||
|
||||
var someString = jasmine.any(String);
|
||||
|
||||
beforeEach(function() {
|
||||
shell.mkdir(path.join(dummyProject.projectDir, dummyPluginInfo.id));
|
||||
|
||||
spyOn(dummyProject, 'removeSystemLibrary');
|
||||
spyOn(dummyProject, 'removeSubProject');
|
||||
spyOn(dummyProject, 'removeGradleReference');
|
||||
});
|
||||
|
||||
it('should throw if framework doesn\'t have "src" attribute', function() {
|
||||
expect(function() { android.framework.uninstall({}, dummyPluginInfo, dummyProject); }).toThrow();
|
||||
});
|
||||
|
||||
it('should uninstall framework without "parent" attribute into project root', function() {
|
||||
var framework = {src: 'plugin-lib'};
|
||||
android.framework.uninstall(framework, dummyPluginInfo, dummyProject);
|
||||
expect(dummyProject.removeSystemLibrary).toHaveBeenCalledWith(dummyProject.projectDir, someString);
|
||||
});
|
||||
|
||||
it('should uninstall framework with "parent" attribute into parent framework dir', function() {
|
||||
var childFramework = {src: 'plugin-lib2', parent: 'plugin-lib'};
|
||||
android.framework.uninstall(childFramework, dummyPluginInfo, dummyProject);
|
||||
expect(dummyProject.removeSystemLibrary).toHaveBeenCalledWith(path.resolve(dummyProject.projectDir, childFramework.parent), someString);
|
||||
});
|
||||
|
||||
it('should remove framework sources if "custom" attribute is set', function() {
|
||||
var framework = {src: 'plugin-lib', custom: true};
|
||||
android.framework.uninstall(framework, dummyPluginInfo, dummyProject);
|
||||
expect(dummyProject.removeSubProject).toHaveBeenCalledWith(dummyProject.projectDir, someString);
|
||||
expect(removeFileSpy).toHaveBeenCalledWith(dummyProject.projectDir, someString);
|
||||
});
|
||||
|
||||
it('should install gradleReference using project.removeGradleReference', function() {
|
||||
var framework = {src: 'plugin-lib', custom: true, type: 'gradleReference'};
|
||||
android.framework.uninstall(framework, dummyPluginInfo, dummyProject);
|
||||
expect(removeFileSpy).toHaveBeenCalledWith(dummyProject.projectDir, someString);
|
||||
expect(dummyProject.removeGradleReference).toHaveBeenCalledWith(dummyProject.projectDir, someString);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user