refactor: do not infer project root from script location (#1265)

* fix(Api): do not infer project root from script location

* fix(builders): do not infer project root from script location

* fix(target): do not infer project root from script location

* test(e2e): cleanup and extend E2E tests

- Renames the file with the only existing E2E test
- Makes existing test use the API instance returned by
  `Api.createPlatform`
- Adds another test that ensures we can still require the API from
  `platformProjectPath/cordova/Api.js`

* fix(check_reqs): do not infer project root from script location
This commit is contained in:
Raphael von der Grün
2021-07-13 08:51:20 +02:00
committed by GitHub
parent 70a1eff705
commit 1f0ea173b0
14 changed files with 142 additions and 124 deletions
+74
View File
@@ -0,0 +1,74 @@
/*
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.
*/
const os = require('os');
const fs = require('fs-extra');
const path = require('path');
const { EventEmitter } = require('events');
const { ConfigParser, PluginInfoProvider } = require('cordova-common');
const Api = require('../../bin/templates/cordova/Api');
function makeTempDir () {
const tmpDirTemplate = path.join(os.tmpdir(), 'cordova-android-test-');
return fs.realpathSync(fs.mkdtempSync(tmpDirTemplate));
}
async function makeProject (projectPath) {
const configXmlPath = path.join(__dirname, '../../bin/templates/project/res/xml/config.xml');
const config = new ConfigParser(configXmlPath);
config.setPackageName('io.cordova.testapp');
config.setName('TestApp');
const noopEvents = new EventEmitter();
return Api.createPlatform(projectPath, config, {}, noopEvents);
}
describe('E2E', function () {
let tmpDir, projectPath, api;
beforeEach(async () => {
tmpDir = makeTempDir();
projectPath = path.join(tmpDir, 'project');
api = await makeProject(projectPath);
});
afterEach(() => {
fs.removeSync(tmpDir);
});
it('loads the API from a project directory', async () => {
// Allow test project to find the `cordova-android` module
fs.ensureSymlinkSync(
path.join(__dirname, '../..'),
path.join(tmpDir, 'node_modules/cordova-android'),
'junction'
);
expect(() => {
require(path.join(projectPath, 'cordova/Api.js'));
}).not.toThrow();
});
it('adds a plugin with framework', async () => {
const fakePluginPath = path.join(__dirname, 'fixtures/cordova-plugin-fake');
const pluginInfo = new PluginInfoProvider().get(fakePluginPath);
await expectAsync(api.addPlugin(pluginInfo)).toBeResolved();
});
});
-72
View File
@@ -1,72 +0,0 @@
/*
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.
*/
const os = require('os');
const fs = require('fs-extra');
const path = require('path');
const { EventEmitter } = require('events');
const { ConfigParser, PluginInfoProvider } = require('cordova-common');
const Api = require('../../bin/templates/cordova/Api');
const fakePluginPath = path.join(__dirname, 'fixtures/cordova-plugin-fake');
const configXmlPath = path.join(__dirname, '../../bin/templates/project/res/xml/config.xml');
describe('plugin add', function () {
let tmpDir;
beforeEach(() => {
const tmpDirTemplate = path.join(os.tmpdir(), 'cordova-android-test-');
tmpDir = fs.realpathSync(fs.mkdtempSync(tmpDirTemplate));
});
afterEach(() => {
fs.removeSync(tmpDir);
});
it('Test#001 : create project and add a plugin with framework', function () {
const projectname = 'testpluginframework';
const projectid = 'com.test.plugin.framework';
const config = new ConfigParser(configXmlPath);
config.setPackageName(projectid);
config.setName(projectname);
const projectPath = path.join(tmpDir, projectname);
const pluginInfo = new PluginInfoProvider().get(fakePluginPath);
const noopEvents = new EventEmitter();
return Promise.resolve()
.then(() => Api.createPlatform(projectPath, config, {}, noopEvents))
.then(() => {
// Allow test project to find the `cordova-android` module
fs.ensureSymlinkSync(
path.join(__dirname, '../..'),
path.join(projectPath, 'node_modules/cordova-android'),
'junction'
);
// We need to use the API from the project or some paths break
// TODO remove this and use the API instance returned from
// createPlatform once we fixed the platform
const Api = require(path.join(projectPath, 'cordova/Api.js'));
const api = new Api('android', projectPath, noopEvents);
return api.addPlugin(pluginInfo);
});
}, 90000);
});
+3 -5
View File
@@ -43,10 +43,8 @@ describe('ProjectBuilder', () => {
expect(builder.root).toBe(rootDir);
});
it('should set the project directory to three folders up', () => {
ProjectBuilder.__set__('__dirname', 'projecttest/platforms/android/app');
builder = new ProjectBuilder();
expect(builder.root).toMatch(/projecttest$/);
it('should throw if project directory is missing', () => {
expect(() => new ProjectBuilder()).toThrowError(TypeError);
});
});
@@ -224,7 +222,7 @@ describe('ProjectBuilder', () => {
return builder.build({}).then(
() => fail('Unexpectedly resolved'),
error => {
expect(checkReqsSpy.check_android_target).toHaveBeenCalled();
expect(checkReqsSpy.check_android_target).toHaveBeenCalledWith(rootDir);
expect(error).toBe(testError);
}
);
+3 -1
View File
@@ -31,8 +31,10 @@ describe('builders', () => {
describe('getBuilder', () => {
it('should return an instance of ProjectBuilder when gradle is requested', () => {
const newBuilder = builders.getBuilder();
const root = 'FakeProjectRoot';
const newBuilder = builders.getBuilder(root);
expect(newBuilder).toEqual(jasmine.any(ProjectBuilder));
expect(newBuilder.root).toBe(root);
});
it('should throw an error if a builder cannot be instantiated', () => {
+5 -4
View File
@@ -262,6 +262,7 @@ describe('check_reqs', function () {
});
describe('get_target', function () {
const projectRoot = 'fakeProjectRoot';
var ConfigParser;
var getPreferenceSpy;
beforeEach(function () {
@@ -273,7 +274,7 @@ describe('check_reqs', function () {
});
it('should retrieve DEFAULT_TARGET_API', function () {
var target = check_reqs.get_target();
var target = check_reqs.get_target(projectRoot);
expect(target).toBeDefined();
expect(target).toContain('android-' + DEFAULT_TARGET_API);
});
@@ -282,7 +283,7 @@ describe('check_reqs', function () {
spyOn(fs, 'existsSync').and.returnValue(true);
getPreferenceSpy.and.returnValue(String(DEFAULT_TARGET_API + 1));
var target = check_reqs.get_target();
var target = check_reqs.get_target(projectRoot);
expect(getPreferenceSpy).toHaveBeenCalledWith('android-targetSdkVersion', 'android');
expect(target).toBe('android-' + (DEFAULT_TARGET_API + 1));
@@ -292,7 +293,7 @@ describe('check_reqs', function () {
spyOn(fs, 'existsSync').and.returnValue(true);
getPreferenceSpy.and.returnValue('android-99');
var target = check_reqs.get_target();
var target = check_reqs.get_target(projectRoot);
expect(getPreferenceSpy).toHaveBeenCalledWith('android-targetSdkVersion', 'android');
expect(target).toBe('android-' + DEFAULT_TARGET_API);
@@ -305,7 +306,7 @@ describe('check_reqs', function () {
getPreferenceSpy.and.returnValue(String(DEFAULT_TARGET_API - 1));
var target = check_reqs.get_target();
var target = check_reqs.get_target(projectRoot);
expect(getPreferenceSpy).toHaveBeenCalledWith('android-targetSdkVersion', 'android');
expect(target).toBe('android-' + DEFAULT_TARGET_API);
+1 -1
View File
@@ -787,7 +787,7 @@ describe('prepare', () => {
gradlePropertiesParserSpy = spyOn(GradlePropertiesParser.prototype, 'configure');
api = new Api();
api = new Api('android', cordovaProject.root);
});
it('runs without arguments', async () => {
+15 -3
View File
@@ -56,7 +56,8 @@ describe('run', () => {
run.__set__({
target: targetSpyObj,
emulator: emulatorSpyObj
emulator: emulatorSpyObj,
AndroidManifest: class {}
});
const builder = builders.getBuilder('FakeRootPath');
@@ -66,14 +67,25 @@ describe('run', () => {
});
// run needs `this` to behave like an Api instance
run.run = run.run.bind({ _builder: builder });
run.run = run.run.bind({
_builder: builder,
locations: { manifest: 'FakeManifestPath' }
});
});
it('should install on target after build', () => {
const AndroidManifest = run.__get__('AndroidManifest');
return run.run().then(() => {
expect(targetSpyObj.install).toHaveBeenCalledWith(
resolvedTarget,
{ apkPaths: ['fake.apk'], buildType: 'debug' }
{
manifest: jasmine.any(AndroidManifest),
buildResults: {
buildType: 'debug',
apkPaths: ['fake.apk']
}
}
);
});
});
+13 -18
View File
@@ -226,25 +226,20 @@ describe('target', () => {
});
describe('install', () => {
let AndroidManifestSpy;
let AndroidManifestFns;
let AndroidManifestGetActivitySpy;
let AdbSpy;
let buildSpy;
let installTarget;
let installTarget, manifest, appSpec;
beforeEach(() => {
installTarget = { id: 'emulator-5556', type: 'emulator', arch: 'atari' };
manifest = jasmine.createSpyObj('manifestStub', ['getPackageId', 'getActivity']);
manifest.getActivity.and.returnValue(jasmine.createSpyObj('Activity', ['getName']));
appSpec = { manifest, buildResults: {} };
buildSpy = jasmine.createSpyObj('build', ['findBestApkForArchitecture']);
target.__set__('build', buildSpy);
AndroidManifestFns = jasmine.createSpyObj('AndroidManifestFns', ['getPackageId', 'getActivity']);
AndroidManifestGetActivitySpy = jasmine.createSpyObj('getActivity', ['getName']);
AndroidManifestFns.getActivity.and.returnValue(AndroidManifestGetActivitySpy);
AndroidManifestSpy = jasmine.createSpy('AndroidManifest').and.returnValue(AndroidManifestFns);
target.__set__('AndroidManifest', AndroidManifestSpy);
AdbSpy = jasmine.createSpyObj('Adb', ['shell', 'start', 'install', 'uninstall']);
AdbSpy.shell.and.returnValue(Promise.resolve());
AdbSpy.start.and.returnValue(Promise.resolve());
@@ -257,7 +252,7 @@ describe('target', () => {
});
it('should install to the passed target', () => {
return target.install(installTarget, {}).then(() => {
return target.install(installTarget, appSpec).then(() => {
expect(AdbSpy.install.calls.argsFor(0)[0]).toBe(installTarget.id);
});
});
@@ -272,7 +267,7 @@ describe('target', () => {
const apkPath = 'my/apk/path/app.apk';
buildSpy.findBestApkForArchitecture.and.returnValue(apkPath);
return target.install(installTarget, buildResults).then(() => {
return target.install(installTarget, { manifest, buildResults }).then(() => {
expect(buildSpy.findBestApkForArchitecture).toHaveBeenCalledWith(buildResults, installTarget.arch);
expect(AdbSpy.install.calls.argsFor(0)[1]).toBe(apkPath);
@@ -285,7 +280,7 @@ describe('target', () => {
Promise.resolve()
);
return target.install(installTarget, {}).then(() => {
return target.install(installTarget, appSpec).then(() => {
expect(AdbSpy.install).toHaveBeenCalledTimes(2);
expect(AdbSpy.uninstall).toHaveBeenCalled();
});
@@ -295,7 +290,7 @@ describe('target', () => {
const errorMsg = 'Failure: Failed to install';
AdbSpy.install.and.rejectWith(new CordovaError(errorMsg));
return target.install(installTarget, {}).then(
return target.install(installTarget, appSpec).then(
() => fail('Unexpectedly resolved'),
err => {
expect(err).toEqual(jasmine.any(CordovaError));
@@ -305,7 +300,7 @@ describe('target', () => {
});
it('should unlock the screen on device', () => {
return target.install(installTarget, {}).then(() => {
return target.install(installTarget, appSpec).then(() => {
expect(AdbSpy.shell).toHaveBeenCalledWith(installTarget.id, 'input keyevent 82');
});
});
@@ -313,10 +308,10 @@ describe('target', () => {
it('should start the newly installed app on the device', () => {
const packageId = 'unittestapp';
const activityName = 'TestActivity';
AndroidManifestFns.getPackageId.and.returnValue(packageId);
AndroidManifestGetActivitySpy.getName.and.returnValue(activityName);
manifest.getPackageId.and.returnValue(packageId);
manifest.getActivity().getName.and.returnValue(activityName);
return target.install(installTarget, {}).then(() => {
return target.install(installTarget, appSpec).then(() => {
expect(AdbSpy.start).toHaveBeenCalledWith(installTarget.id, `${packageId}/.${activityName}`);
});
});