From eddec15c41aaccfe715f89ec12cf445740e044e3 Mon Sep 17 00:00:00 2001 From: Goran Gligorin Date: Tue, 11 Apr 2017 20:38:56 +0200 Subject: [PATCH] feat(buildNumber): store or change build number in config.xml closes #18 --- src/index.js | 4 +- src/set-build-number.js | 23 +++++ test/set-build-number.test.js | 93 +++++++++++++++++++ .../configs/config.expected.xml | 39 ++++++++ test/set-build-number/configs/config.good.xml | 39 ++++++++ .../configs/config.malformed.xml | 0 .../configs/config.without.xml} | 0 test/set-version.test.js | 18 ++-- .../configs/config.expected-json.xml | 0 .../configs/config.expected.xml | 0 test/set-version/configs/config.good.xml | 39 ++++++++ test/set-version/configs/config.malformed.xml | 16 ++++ .../packages/package.good.json | 0 .../packages/package.malformed.json | 0 .../packages/package.no-version.json | 0 15 files changed, 261 insertions(+), 10 deletions(-) create mode 100644 src/set-build-number.js create mode 100644 test/set-build-number.test.js create mode 100644 test/set-build-number/configs/config.expected.xml create mode 100644 test/set-build-number/configs/config.good.xml rename test/{ => set-build-number}/configs/config.malformed.xml (100%) rename test/{configs/config.good.xml => set-build-number/configs/config.without.xml} (100%) rename test/{ => set-version}/configs/config.expected-json.xml (100%) rename test/{ => set-version}/configs/config.expected.xml (100%) create mode 100644 test/set-version/configs/config.good.xml create mode 100644 test/set-version/configs/config.malformed.xml rename test/{ => set-version}/packages/package.good.json (100%) rename test/{ => set-version}/packages/package.malformed.json (100%) rename test/{ => set-version}/packages/package.no-version.json (100%) diff --git a/src/index.js b/src/index.js index 6c1395ce..acf42799 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,9 @@ 'use strict'; import setVersion from './set-version'; +import setBuildNumber from './set-build-number'; export default { - setVersion + setVersion, + setBuildNumber }; diff --git a/src/set-build-number.js b/src/set-build-number.js new file mode 100644 index 00000000..26ae5c9f --- /dev/null +++ b/src/set-build-number.js @@ -0,0 +1,23 @@ +'use strict'; + +import fs from 'mz/fs'; +import xml2js from 'xml2js-es6-promise'; +import { Builder } from 'xml2js'; + +const xmlBuilder = new Builder(); + +function setBuildNumber(configPath, buildNumber) { + return fs.readFile(configPath, { encoding: 'UTF-8' }) + .then((xmlString) => xml2js(xmlString)) + .then((xml) => writeBuildNumber(configPath, buildNumber, xml)); +} + +function writeBuildNumber(configPath, buildNumber, xml) { + xml.widget.$['android-versionCode'] = buildNumber; + xml.widget.$['ios-CFBundleVersion'] = buildNumber; + xml.widget.$['osx-CFBundleVersion'] = buildNumber; + let newConfigData = xmlBuilder.buildObject(xml); + return fs.writeFile(configPath, newConfigData, { encoding: 'UTF-8' }); +} + +export default setBuildNumber; diff --git a/test/set-build-number.test.js b/test/set-build-number.test.js new file mode 100644 index 00000000..cd5562d6 --- /dev/null +++ b/test/set-build-number.test.js @@ -0,0 +1,93 @@ +'use strict'; + +import chai from 'chai'; +import chaiAsPromised from 'chai-as-promised'; +import fs from 'fs-promise'; + +import cordovaSetVersion from '../src/index'; + +const fileParams = { encoding: 'UTF-8' }; +const configPaths = { + COPY: './config.xml', + GOOD: './set-build-number/configs/config.good.xml', + WITHOUT: './set-build-number/configs/config.good.xml', + MALFORMED: './set-build-number/configs/config.malformed.xml', + MISSING: './set-build-number/configs/config.missing.xml', + EXPECTED: './set-build-number/configs/config.expected.xml' +}; + +chai.use(chaiAsPromised); +chai.should(); + +describe('setBuildNumber', () => { + before(() => { + process.chdir(__dirname); + }); + + it('should exist', () => { + cordovaSetVersion.should.have.property('setBuildNumber'); + }); + + it('should be a function', () => { + let type = typeof cordovaSetVersion.setBuildNumber; + type.should.equal('function'); + }); + + it('should replace existing build number attribute values', () => { + fs.copySync(configPaths.GOOD, configPaths.COPY); + + let promise = cordovaSetVersion.setBuildNumber(configPaths.COPY, 24) + .then(() => fs.readFile(configPaths.COPY, fileParams)); + + let expectedConfig = fs.readFileSync(configPaths.EXPECTED, fileParams); + + return promise.should.be.fulfilled + .then((result) => { + result.should.equal(expectedConfig); + }); + }); + + + it('should create build number attributes, when missing', () => { + fs.copySync(configPaths.WITHOUT, configPaths.COPY); + + let promise = cordovaSetVersion.setBuildNumber(configPaths.COPY, 24) + .then(() => fs.readFile(configPaths.COPY, fileParams)); + + let expectedConfig = fs.readFileSync(configPaths.EXPECTED, fileParams); + + return promise.should.be.fulfilled + .then((result) => { + result.should.equal(expectedConfig); + }); + }); + + it('should return an error about missing config.xml file', () => { + let promise = cordovaSetVersion.setBuildNumber(configPaths.MISSING, '1.0.0') + .then(() => fs.readFile(configPaths.COPY, fileParams)); + + return promise.should.be.rejected + .then((error) => { + error.should.be.instanceOf(Error); + error.should.have.property('message'); + error.message.should.contain('no such file or directory'); + error.message.should.contain(configPaths.MISSING); + }); + }); + + it('should return an error about bad config.xml file', () => { + let promise = cordovaSetVersion.setVersion(configPaths.MALFORMED, '1.0.0') + + return promise.should.be.rejected + .then((error) => { + error.should.be.instanceOf(Error); + error.message.should.not.contain('no such file or directory'); + }); + }); + + afterEach(function () { + if (fs.existsSync(configPaths.COPY)) { + fs.removeSync(configPaths.COPY); + } + }); +}); diff --git a/test/set-build-number/configs/config.expected.xml b/test/set-build-number/configs/config.expected.xml new file mode 100644 index 00000000..2f4420d4 --- /dev/null +++ b/test/set-build-number/configs/config.expected.xml @@ -0,0 +1,39 @@ + + + ionic2-rc6 + An awesome Ionic/Cordova app. + Ionic Framework Team + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/set-build-number/configs/config.good.xml b/test/set-build-number/configs/config.good.xml new file mode 100644 index 00000000..511c04bb --- /dev/null +++ b/test/set-build-number/configs/config.good.xml @@ -0,0 +1,39 @@ + + + ionic2-rc6 + An awesome Ionic/Cordova app. + Ionic Framework Team + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/configs/config.malformed.xml b/test/set-build-number/configs/config.malformed.xml similarity index 100% rename from test/configs/config.malformed.xml rename to test/set-build-number/configs/config.malformed.xml diff --git a/test/configs/config.good.xml b/test/set-build-number/configs/config.without.xml similarity index 100% rename from test/configs/config.good.xml rename to test/set-build-number/configs/config.without.xml diff --git a/test/set-version.test.js b/test/set-version.test.js index aa2160b2..fe2e2f43 100644 --- a/test/set-version.test.js +++ b/test/set-version.test.js @@ -9,18 +9,18 @@ import cordovaSetVersion from '../src/index'; const fileParams = { encoding: 'UTF-8' }; const configPaths = { COPY: './config.xml', - GOOD: './configs/config.good.xml', - MALFORMED: './configs/config.malformed.xml', - MISSING: './configs/config.missing.xml', - EXPECTED: './configs/config.expected.xml', - EXPECTED_JSON: './configs/config.expected-json.xml' + GOOD: './set-version/configs/config.good.xml', + MALFORMED: './set-version/configs/config.malformed.xml', + MISSING: './set-version/configs/config.missing.xml', + EXPECTED: './set-version/configs/config.expected.xml', + EXPECTED_JSON: './set-version/configs/config.expected-json.xml' }; const packagePaths = { COPY: './package.json', - GOOD: './packages/package.good.json', - MALFORMED: './packages/package.malformed.json', - NO_VERSION: './packages/package.no-version.json', - MISSING: './packages/package.missing.json' + GOOD: './set-version/packages/package.good.json', + MALFORMED: './set-version/packages/package.malformed.json', + NO_VERSION: './set-version/packages/package.no-version.json', + MISSING: './set-version/packages/package.missing.json' }; chai.use(chaiAsPromised); diff --git a/test/configs/config.expected-json.xml b/test/set-version/configs/config.expected-json.xml similarity index 100% rename from test/configs/config.expected-json.xml rename to test/set-version/configs/config.expected-json.xml diff --git a/test/configs/config.expected.xml b/test/set-version/configs/config.expected.xml similarity index 100% rename from test/configs/config.expected.xml rename to test/set-version/configs/config.expected.xml diff --git a/test/set-version/configs/config.good.xml b/test/set-version/configs/config.good.xml new file mode 100644 index 00000000..0541ddfe --- /dev/null +++ b/test/set-version/configs/config.good.xml @@ -0,0 +1,39 @@ + + + ionic2-rc6 + An awesome Ionic/Cordova app. + Ionic Framework Team + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/set-version/configs/config.malformed.xml b/test/set-version/configs/config.malformed.xml new file mode 100644 index 00000000..8aceecad --- /dev/null +++ b/test/set-version/configs/config.malformed.xml @@ -0,0 +1,16 @@ + + + ionic2-rc6 + An awesome Ionic/Cordova app. + Ionic Framework Team + + + + + + + + + + + diff --git a/test/packages/package.good.json b/test/set-version/packages/package.good.json similarity index 100% rename from test/packages/package.good.json rename to test/set-version/packages/package.good.json diff --git a/test/packages/package.malformed.json b/test/set-version/packages/package.malformed.json similarity index 100% rename from test/packages/package.malformed.json rename to test/set-version/packages/package.malformed.json diff --git a/test/packages/package.no-version.json b/test/set-version/packages/package.no-version.json similarity index 100% rename from test/packages/package.no-version.json rename to test/set-version/packages/package.no-version.json