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

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);
}
);

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', () => {

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);

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 () => {

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']
}
}
);
});
});

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}`);
});
});