Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] package.json helper class #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spec/fixtures/testpackage.json text eol=crlf
1 change: 1 addition & 0 deletions cordova-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ addProperty(module, 'CordovaExternalToolErrorContext', './src/CordovaError/Cordo
addProperty(module, 'PlatformJson', './src/PlatformJson');
addProperty(module, 'ConfigParser', './src/ConfigParser/ConfigParser');
addProperty(module, 'FileUpdater', './src/FileUpdater');
addProperty(module, 'PackageHelper', './src/PackageHelper');

addProperty(module, 'PluginInfo', './src/PluginInfo/PluginInfo');
addProperty(module, 'PluginInfoProvider', './src/PluginInfo/PluginInfoProvider');
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@
"ansi": "^0.3.1",
"bplist-parser": "^0.1.0",
"cross-spawn": "^6.0.5",
"deep-freeze": "^0.0.1",
"detect-indent": "^5.0.0",
"detect-newline": "^2.1.0",
"elementtree": "0.1.7",
"endent": "^1.1.1",
"fast-deep-equal": "^2.0.1",
"fs-extra": "^7.0.0",
"glob": "^7.1.2",
"minimatch": "^3.0.0",
"plist": "^3.0.1",
"q": "^1.4.1",
"stringify-package": "^1.0.0",
Copy link
Contributor

@breautek breautek Feb 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this package is deprecated now, the replacement is https://www.npmjs.com/package/@npmcli/package-json

"strip-bom": "^3.0.0",
"underscore": "^1.8.3",
"which": "^1.3.0"
"which": "^1.3.0",
"write-file-atomic": "^2.3.0"
},
"devDependencies": {
"eslint": "^5.0.0",
Expand Down
245 changes: 245 additions & 0 deletions spec/PackageHelper.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
/**
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('osenv');
const path = require('path');
const rewire = require('rewire');
const writeFileAtomic = require('write-file-atomic');
const PackageHelper = rewire('../src/PackageHelper');

describe('PackageHelper', function () {
describe('with no package.json', function () {
let tempfile;
/** @type PackageHelper */ let pkgHelper;

beforeEach(function () {
tempfile = path.join(os.tmpdir(), 'test.json');
pkgHelper = new PackageHelper(tempfile);
});

it('should have no package name', function () {
expect(pkgHelper.packageID).toBeUndefined();
});

it('should have no package version', function () {
expect(pkgHelper.version).toBeUndefined();
});

it('should have no Cordova metadata', function () {
expect(pkgHelper.cordova).toEqual({});
});

it('should have no platforms', function () {
expect(pkgHelper.cordovaPlatforms).toEqual([]);
});

it('should have no plugins', function () {
expect(pkgHelper.cordovaPlugins).toEqual([]);
});

it('should have no Cordova dependencies', function () {
expect(pkgHelper.cordovaDependencies).toEqual({});
});

it('should return no variables for a non-installed plugin', function () {
expect(pkgHelper.getPluginVariables('cordova-plugin-device')).toEqual({});
});

it('should write to the filesystem', function () {
let stub = spyOn(writeFileAtomic, 'sync');

pkgHelper.pkgfile.name = 'packageHelper-Test';
pkgHelper.writeSync();
expect(stub).toHaveBeenCalledWith(tempfile, '{\n "name": "packageHelper-Test"\n}\n');
});

it('should write to the filesystem and return a promise', function () {
let stub = jasmine.createSpy('writeFileAtomic').and.callFake(function (path, data, cb) {
expect(path).toEqual(tempfile);
expect(data).toEqual('{\n "name": "packageHelper-Test"\n}\n');

cb();
});

return PackageHelper.__with__({
writeFileAtomic: stub
})(function () {
pkgHelper.pkgfile.name = 'packageHelper-Test';
return pkgHelper.write();
});
});

it('should add a platform', function () {
pkgHelper.addPlatform('windows');

expect(pkgHelper.cordovaPlatforms).toContain('windows');
});

it('should add a plugin with no vars', function () {
pkgHelper.addPlugin('cordova-plugin-device');

expect(pkgHelper.pkgfile.cordova.plugins).toEqual({ 'cordova-plugin-device': {} });
});

it('should add a plugin with vars', function () {
pkgHelper.addPlugin('cordova-plugin-push', { FCM_TOKEN: 'foo' });

expect(pkgHelper.pkgfile.cordova.plugins).toEqual({ 'cordova-plugin-push': { 'FCM_TOKEN': 'foo' } });
});
});

describe('with a package.json', function () {
const packageFile = path.join(__dirname, './fixtures/testpackage.json');
/** @type PackageHelper */ let pkgHelper;

beforeEach(function () {
pkgHelper = new PackageHelper(packageFile);
});

it('should have no package name', function () {
expect(pkgHelper.packageID).toEqual('TestProject');
});

it('should have no package version', function () {
expect(pkgHelper.version).toEqual('1.0.0');
});

it('should have Cordova metadata', function () {
expect(pkgHelper.cordova).not.toEqual({});
expect(pkgHelper.cordova.platforms).not.toEqual([]);
expect(pkgHelper.cordova.plugins).not.toEqual({});
});

it('should have 2 platforms', function () {
expect(pkgHelper.cordovaPlatforms).toEqual(['android', 'cordova-amiga', 'ios']);
});

it('should have 2 plugins', function () {
expect(pkgHelper.cordovaPlugins).toEqual(['cordova-plugin-device', 'cordova-plugin-push']);
});

it('should have find Cordova dependencies in dependencies', function () {
expect(pkgHelper.cordovaDependencies['cordova-android']).toEqual('^7.0.0');
});

it('should have find Cordova dependencies in devDependencies', function () {
expect(pkgHelper.cordovaDependencies['cordova-ios']).toEqual('~4.5.0');
expect(pkgHelper.cordovaDependencies['cordova-plugin-device']).toEqual('*');
});

it('should prefer Cordova dependencies in devDependencies', function () {
expect(pkgHelper.cordovaDependencies['cordova-amiga']).toEqual('0.0.5');
});

it('should return variables for a plugin', function () {
expect(pkgHelper.getPluginVariables('cordova-plugin-device')).toEqual({});
expect(pkgHelper.getPluginVariables('cordova-plugin-push')).toEqual({ FCM_TOKEN: '1234567890abcdef' });
});

it('should not add duplicate platforms', function () {
let stub = spyOn(pkgHelper, 'write');

pkgHelper.addPlatform('android');

expect(pkgHelper.cordovaPlatforms).toContain('android');
expect(stub).not.toHaveBeenCalled();
expect(pkgHelper.modified).toBe(false);
});

it('should add a platform', function () {
pkgHelper.addPlatform('windows');

expect(pkgHelper.cordovaPlatforms).toEqual(['android', 'cordova-amiga', 'ios', 'windows']);
expect(pkgHelper.modified).toBe(true);
});

it('should add a duplicate plugin with no vars', function () {
pkgHelper.addPlugin('cordova-plugin-device');

const expected = {
'cordova-plugin-device': {},
'cordova-plugin-push': {
'FCM_TOKEN': '1234567890abcdef'
}
};

expect(pkgHelper.pkgfile.cordova.plugins).toEqual(expected);
});

it('should add a new plugin with no vars', function () {
pkgHelper.addPlugin('cordova-plugin-file');

const expected = {
'cordova-plugin-device': {},
'cordova-plugin-file': {},
'cordova-plugin-push': {
'FCM_TOKEN': '1234567890abcdef'
}
};

expect(pkgHelper.pkgfile.cordova.plugins).toEqual(expected);
expect(pkgHelper.modified).toBe(true);
});

it('should add a duplicate plugin with non-conflicting vars', function () {
pkgHelper.addPlugin('cordova-plugin-push', { FCM_TOKEN: '1234567890abcdef' });

const expected = {
'cordova-plugin-device': {},
'cordova-plugin-push': {
'FCM_TOKEN': '1234567890abcdef'
}
};

expect(pkgHelper.pkgfile.cordova.plugins).toEqual(expected);
expect(pkgHelper.modified).toBe(false);
});

it('should add a duplicate plugin with conflicting vars', function () {
pkgHelper.addPlugin('cordova-plugin-push', { FCM_TOKEN: 'hello_world' });

const expected = {
'cordova-plugin-device': {},
'cordova-plugin-push': {
'FCM_TOKEN': 'hello_world'
}
};

expect(pkgHelper.pkgfile.cordova.plugins).toEqual(expected);
expect(pkgHelper.modified).toBe(true);
});

it('should write and preserve indentation and newlines', function () {
let stub = jasmine.createSpy('writeFileAtomic').and.callFake(function (path, data, cb) {
expect(path).toEqual(packageFile);
expect(data).toMatch(/ {12}"cordova-plugin-push": {\r\n {16}"FCM_TOKEN": "hello_world"\r\n {12}},?\r\n/);

cb();
});

return PackageHelper.__with__({
writeFileAtomic: stub
})(function () {
pkgHelper.pkgfile.cordova.plugins['cordova-plugin-push']['FCM_TOKEN'] = 'hello_world';

return pkgHelper.write(true);
});
});
});
});
28 changes: 28 additions & 0 deletions spec/fixtures/testpackage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "TestProject",
"version": "1.0.0",
"author": "Apache Software Foundation",
"license": "Apache-2.0",
"dependencies": {
"cordova-amiga": "^0.0.1",
"cordova-android": "^7.0.0"
},
"devDependencies": {
"cordova-amiga": "0.0.5",
"cordova-ios": "~4.5.0",
"cordova-plugin-device": "*"
},
"cordova": {
"platforms": [
"android",
"cordova-amiga",
"ios"
],
"plugins": {
"cordova-plugin-device": {},
"cordova-plugin-push": {
"FCM_TOKEN": "1234567890abcdef"
}
}
}
}
Loading