diff --git a/src/cli.js b/src/cli.js index 22e97998..77ac862d 100755 --- a/src/cli.js +++ b/src/cli.js @@ -35,8 +35,8 @@ const options = { const cli = meow(options); -const filename = cli.input[0] || null; +const configPath = cli.input[0] || null; const version = cli.flags.version || null; const buildNumber = +cli.flags.buildNumber || null; -cordovaSetVersion(filename, version, buildNumber); +cordovaSetVersion({ configPath, version, buildNumber }); diff --git a/src/index.js b/src/index.js index 261a5b29..703721b8 100644 --- a/src/index.js +++ b/src/index.js @@ -7,48 +7,6 @@ const readFile = promisify(fs.readFile); const writeFile = promisify(fs.writeFile); const xmlBuilder = new Builder(); -const DefaultConfigPath = './config.xml'; - -function parse1Argument(arg) { - if (typeof arg === 'string' && arg.indexOf('.xml') < 0) { - return [null, arg, null]; - } - - if (typeof arg === 'number') { - return [null, null, arg]; - } - - return [arg, null, null]; -} - -function parse2Arguments(arg1, arg2) { - const arg1IsString = typeof arg1 === 'string'; - const arg1IsStringXml = arg1IsString && arg1.indexOf('.xml') >= 0; - const arg2IsNumber = typeof arg2 === 'number'; - - if (arg2IsNumber && (arg1IsStringXml || !arg1IsString)) { - return [arg1, null, arg2]; - } - - if (arg1IsString && !arg1IsStringXml) { - return [null, arg1, arg2]; - } - - return [arg1, arg2, null]; -} - -function parseArguments(...args) { - switch (args.length) { - case 0: - return [null, null, null]; - case 1: - return parse1Argument(args[0]); - case 2: - return parse2Arguments(args[0], args[1]); - default: - return args; - } -} function checkTypeErrors(configPath, version, buildNumber) { if (typeof configPath !== 'string') { @@ -105,25 +63,19 @@ function setAttributes(xml, version, buildNumber) { * @param {string} [version] * @param {number} [buildNumber] */ -async function cordovaSetVersion(...args) { - let [configPath, version, buildNumber] = parseArguments(...args); +async function cordovaSetVersion({ configPath, version, buildNumber } = {}) { + const cPath = configPath || './config.xml'; - configPath = configPath || DefaultConfigPath; - version = version || null; - buildNumber = buildNumber || null; + checkTypeErrors(cPath, version, buildNumber); - checkTypeErrors(configPath, version, buildNumber); + const currentConfig = await getXml(cPath); - let xml = await getXml(configPath); - - if (!version && !buildNumber) { - version = await getVersionFromPackage(version); - } + const v = !version && !buildNumber ? await getVersionFromPackage(version) : version; - xml = setAttributes(xml, version, buildNumber); + const newConfig = setAttributes(currentConfig, v, buildNumber); - const newData = xmlBuilder.buildObject(xml); - return writeFile(configPath, newData, { encoding: 'UTF-8' }); + const newData = xmlBuilder.buildObject(newConfig); + return writeFile(cPath, newData, { encoding: 'UTF-8' }); } export default cordovaSetVersion; diff --git a/test/index-tests/build-number.js b/test/index-tests/build-number.js index ef2e7ee4..9f90c62d 100644 --- a/test/index-tests/build-number.js +++ b/test/index-tests/build-number.js @@ -5,11 +5,11 @@ import cordovaSetVersion from '../../src'; import { tempConfigFile, entryConfigFiles, expectedXmlFiles } from '../configs'; function buildNumberTest() { - describe('(buildNumber)', () => { + describe('({ buildNumber })', () => { it('should override existing buildNumber and preserve existing version', async () => { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); - await cordovaSetVersion(86); + await cordovaSetVersion({ buildNumber: 86 }); expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_VERSION_AND_BUILD)); }); @@ -17,7 +17,7 @@ function buildNumberTest() { it('should add buildNumber and preserve existing version', async () => { fs.copySync(entryConfigFiles.VERSION_AND_NO_BUILD, tempConfigFile); - await cordovaSetVersion(86); + await cordovaSetVersion({ buildNumber: 86 }); expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_VERSION_AND_NO_BUILD)); }); @@ -25,7 +25,7 @@ function buildNumberTest() { it('should override existing buildNumber and not add version', async () => { fs.copySync(entryConfigFiles.NO_VERSION_AND_BUILD, tempConfigFile); - await cordovaSetVersion(86); + await cordovaSetVersion({ buildNumber: 86 }); expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_NO_VERSION_AND_BUILD)); }); @@ -33,53 +33,53 @@ function buildNumberTest() { it('should add buildNumber and not add version', async () => { fs.copySync(entryConfigFiles.NO_VERSION_AND_NO_BUILD, tempConfigFile); - await cordovaSetVersion(86); + await cordovaSetVersion({ buildNumber: 86 }); expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_NO_VERSION_AND_NO_BUILD)); }); - it('should return an error about configPath type', async () => { + it('should return an error about buildNumber type', async () => { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); try { - await cordovaSetVersion({}); + await cordovaSetVersion({ buildNumber: {} }); } catch (error) { expect(error).not.toBeNil(); - expect(error.message).toContain('configPath'); + expect(error.message).toContain('buildNumber'); expect(error.message).toContain('must be a'); } }); - it('should return an error about missing config file', async () => { + it('should return an error about buildNumber type', async () => { + fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); + try { - await cordovaSetVersion(86); + await cordovaSetVersion({ buildNumber: 86.2 }); } catch (error) { expect(error).not.toBeNil(); - expect(error.message).toContain('no such file or directory'); - expect(error.message).toContain('config.xml'); + expect(error.message).toContain('buildNumber'); + expect(error.message).toContain('must be a'); } }); - it('should return an error about malformed config file', async () => { - fs.copySync(entryConfigFiles.MALFORMED, tempConfigFile); - + it('should return an error about missing config file', async () => { try { - await cordovaSetVersion(86); + await cordovaSetVersion({ buildNumber: 86 }); } catch (error) { expect(error).not.toBeNil(); - expect(error.message).not.toContain('no such file or directory'); + expect(error.message).toContain('no such file or directory'); + expect(error.message).toContain('config.xml'); } }); - it('should return an error about buildNumber type', async () => { + it('should return an error about malformed config file', async () => { fs.copySync(entryConfigFiles.MALFORMED, tempConfigFile); try { - await cordovaSetVersion(86.2); + await cordovaSetVersion({ buildNumber: 86 }); } catch (error) { expect(error).not.toBeNil(); - expect(error.message).toContain('buildNumber'); - expect(error.message).toContain('must be an'); + expect(error.message).not.toContain('no such file or directory'); } }); }); diff --git a/test/index-tests/config-path-build-number.js b/test/index-tests/config-path-build-number.js index 59dda5ec..23bca0f3 100644 --- a/test/index-tests/config-path-build-number.js +++ b/test/index-tests/config-path-build-number.js @@ -5,11 +5,11 @@ import cordovaSetVersion from '../../src'; import { tempProvidedConfigFile, entryConfigFiles, expectedXmlFiles } from '../configs'; function configPathBuildNumberTest() { - describe('(configPath, buildNumber)', () => { + describe('({ configPath, buildNumber })', () => { it('should override existing buildNumber and preserve existing version', async () => { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); - await cordovaSetVersion(tempProvidedConfigFile, 86); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, buildNumber: 86 }); expect(readFile(tempProvidedConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_VERSION_AND_BUILD)); }); @@ -17,7 +17,7 @@ function configPathBuildNumberTest() { it('should add buildNumber and preserve existing version', async () => { fs.copySync(entryConfigFiles.VERSION_AND_NO_BUILD, tempProvidedConfigFile); - await cordovaSetVersion(tempProvidedConfigFile, 86); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, buildNumber: 86 }); expect(readFile(tempProvidedConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_VERSION_AND_NO_BUILD)); }); @@ -25,7 +25,7 @@ function configPathBuildNumberTest() { it('should override existing buildNumber and not add version', async () => { fs.copySync(entryConfigFiles.NO_VERSION_AND_BUILD, tempProvidedConfigFile); - await cordovaSetVersion(tempProvidedConfigFile, 86); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, buildNumber: 86 }); expect(readFile(tempProvidedConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_NO_VERSION_AND_BUILD)); }); @@ -33,7 +33,7 @@ function configPathBuildNumberTest() { it('should add buildNumber and not add version', async () => { fs.copySync(entryConfigFiles.NO_VERSION_AND_NO_BUILD, tempProvidedConfigFile); - await cordovaSetVersion(tempProvidedConfigFile, 86); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, buildNumber: 86 }); expect(readFile(tempProvidedConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_NO_VERSION_AND_NO_BUILD)); }); @@ -42,7 +42,7 @@ function configPathBuildNumberTest() { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); try { - await cordovaSetVersion({}, 86); + await cordovaSetVersion({ configPath: {}, buildNumber: 86 }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('configPath'); @@ -50,9 +50,21 @@ function configPathBuildNumberTest() { } }); + it('should return an error about buildNumber type', async () => { + fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); + + try { + await cordovaSetVersion({ configPath: tempProvidedConfigFile, buildNumber: {} }); + } catch (error) { + expect(error).not.toBeNil(); + expect(error.message).toContain('buildNumber'); + expect(error.message).toContain('must be a'); + } + }); + it('should return an error about missing config file', async () => { try { - await cordovaSetVersion(tempProvidedConfigFile, 86); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, buildNumber: 86 }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('no such file or directory'); @@ -64,7 +76,7 @@ function configPathBuildNumberTest() { fs.copySync(entryConfigFiles.MALFORMED, tempProvidedConfigFile); try { - await cordovaSetVersion(tempProvidedConfigFile, 86); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, buildNumber: 86 }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).not.toContain('no such file or directory'); diff --git a/test/index-tests/config-path-version-build-number.js b/test/index-tests/config-path-version-build-number.js index 837bace5..f7c74485 100644 --- a/test/index-tests/config-path-version-build-number.js +++ b/test/index-tests/config-path-version-build-number.js @@ -5,11 +5,11 @@ import cordovaSetVersion from '../../src'; import { tempProvidedConfigFile, entryConfigFiles, expectedXmlFiles } from '../configs'; function configPathVersionBuildNumberTest() { - describe('(configPath, version, buildNumber)', () => { + describe('({ configPath, version, buildNumber })', () => { it('should override both existing version and buildNumber', async () => { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); - await cordovaSetVersion(tempProvidedConfigFile, '2.4.9', 86); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: 86 }); expect(readFile(tempProvidedConfigFile)).toBe( readFile(expectedXmlFiles.VERSION_AND_BUILD_TO_VERSION_AND_BUILD), @@ -19,7 +19,7 @@ function configPathVersionBuildNumberTest() { it('should override existing version and add buildNumber', async () => { fs.copySync(entryConfigFiles.VERSION_AND_NO_BUILD, tempProvidedConfigFile); - await cordovaSetVersion(tempProvidedConfigFile, '2.4.9', 86); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: 86 }); expect(readFile(tempProvidedConfigFile)).toBe( readFile(expectedXmlFiles.VERSION_AND_BUILD_TO_VERSION_AND_NO_BUILD), @@ -29,7 +29,7 @@ function configPathVersionBuildNumberTest() { it('should add version and override existing buildNumber', async () => { fs.copySync(entryConfigFiles.NO_VERSION_AND_BUILD, tempProvidedConfigFile); - await cordovaSetVersion(tempProvidedConfigFile, '2.4.9', 86); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: 86 }); expect(readFile(tempProvidedConfigFile)).toBe( readFile(expectedXmlFiles.VERSION_AND_BUILD_TO_NO_VERSION_AND_BUILD), @@ -39,7 +39,7 @@ function configPathVersionBuildNumberTest() { it('should add version and buildNumber', async () => { fs.copySync(entryConfigFiles.NO_VERSION_AND_NO_BUILD, tempProvidedConfigFile); - await cordovaSetVersion(tempProvidedConfigFile, '2.4.9', 86); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: 86 }); expect(readFile(tempProvidedConfigFile)).toBe( readFile(expectedXmlFiles.VERSION_AND_BUILD_TO_NO_VERSION_AND_NO_BUILD), @@ -50,7 +50,7 @@ function configPathVersionBuildNumberTest() { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); try { - await cordovaSetVersion({}, '2.4.9', 86); + await cordovaSetVersion({ configPath: {}, version: '2.4.9', buildNumber: 86 }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('configPath'); @@ -62,7 +62,7 @@ function configPathVersionBuildNumberTest() { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); try { - await cordovaSetVersion(tempProvidedConfigFile, {}, 86); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: {}, buildNumber: 86 }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('version'); @@ -74,7 +74,7 @@ function configPathVersionBuildNumberTest() { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); try { - await cordovaSetVersion(tempProvidedConfigFile, '2.4.9', {}); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: {} }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('buildNumber'); @@ -84,7 +84,7 @@ function configPathVersionBuildNumberTest() { it('should return an error about missing config file', async () => { try { - await cordovaSetVersion(tempProvidedConfigFile, '2.4.9', 86); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: 86 }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('no such file or directory'); @@ -96,7 +96,7 @@ function configPathVersionBuildNumberTest() { fs.copySync(entryConfigFiles.MALFORMED, tempProvidedConfigFile); try { - await cordovaSetVersion(tempProvidedConfigFile, '2.4.9', 86); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: 86 }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).not.toContain('no such file or directory'); diff --git a/test/index-tests/config-path-version.js b/test/index-tests/config-path-version.js index 724093df..1ac2f826 100644 --- a/test/index-tests/config-path-version.js +++ b/test/index-tests/config-path-version.js @@ -12,11 +12,11 @@ import { } from '../configs'; function configPathVersionTest() { - describe('(configPath, version)', () => { + describe('({ configPath, version })', () => { it('should override existing version and preserve existing buildNumber', async () => { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); - await cordovaSetVersion(tempProvidedConfigFile, '2.4.9'); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9' }); expect(readFile(tempProvidedConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_TO_VERSION_AND_BUILD)); }); @@ -24,7 +24,7 @@ function configPathVersionTest() { it('should override existing version and not add buildNumber', async () => { fs.copySync(entryConfigFiles.VERSION_AND_NO_BUILD, tempProvidedConfigFile); - await cordovaSetVersion(tempProvidedConfigFile, '2.4.9'); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9' }); expect(readFile(tempProvidedConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_TO_VERSION_AND_NO_BUILD)); }); @@ -32,7 +32,7 @@ function configPathVersionTest() { it('should add version and preserve existing buildNumber', async () => { fs.copySync(entryConfigFiles.NO_VERSION_AND_BUILD, tempProvidedConfigFile); - await cordovaSetVersion(tempProvidedConfigFile, '2.4.9'); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9' }); expect(readFile(tempProvidedConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_TO_NO_VERSION_AND_BUILD)); }); @@ -40,7 +40,7 @@ function configPathVersionTest() { it('should add version and not add buildNumber', async () => { fs.copySync(entryConfigFiles.NO_VERSION_AND_NO_BUILD, tempProvidedConfigFile); - await cordovaSetVersion(tempProvidedConfigFile, '2.4.9'); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9' }); expect(readFile(tempProvidedConfigFile)).toBe( readFile(expectedXmlFiles.VERSION_TO_NO_VERSION_AND_NO_BUILD), @@ -51,7 +51,7 @@ function configPathVersionTest() { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); try { - await cordovaSetVersion({}, '2.4.9'); + await cordovaSetVersion({ configPath: {}, version: '2.4.9' }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('configPath'); @@ -64,7 +64,7 @@ function configPathVersionTest() { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); try { - await cordovaSetVersion(tempProvidedConfigFile, {}); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: {} }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('version'); @@ -74,7 +74,7 @@ function configPathVersionTest() { it('should return an error about missing config file', async () => { try { - await cordovaSetVersion(tempProvidedConfigFile, '2.4.9'); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9' }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('no such file or directory'); @@ -86,7 +86,7 @@ function configPathVersionTest() { fs.copySync(entryConfigFiles.MALFORMED, tempProvidedConfigFile); try { - await cordovaSetVersion(tempProvidedConfigFile, '2.4.9'); + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9' }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).not.toContain('no such file or directory'); @@ -94,11 +94,11 @@ function configPathVersionTest() { }); }); - describe('(pluginConfigPath, version)', () => { + describe('({ pluginConfigPath, version })', () => { it('should override existing version', async () => { fs.copySync(entryPluginConfigFiles.VERSION, tempProvidedPluginConfigFile); - await cordovaSetVersion(tempProvidedPluginConfigFile, '2.4.9'); + await cordovaSetVersion({ configPath: tempProvidedPluginConfigFile, version: '2.4.9' }); expect(readFile(tempProvidedPluginConfigFile)).toBe(readFile(expectedPluginXmlFiles.VERSION_TO_VERSION)); }); @@ -106,7 +106,7 @@ function configPathVersionTest() { it('should add version', async () => { fs.copySync(entryPluginConfigFiles.NO_VERSION, tempProvidedPluginConfigFile); - await cordovaSetVersion(tempProvidedPluginConfigFile, '2.4.9'); + await cordovaSetVersion({ configPath: tempProvidedPluginConfigFile, version: '2.4.9' }); expect(readFile(tempProvidedPluginConfigFile)).toBe(readFile(expectedPluginXmlFiles.VERSION_TO_NO_VERSION)); }); @@ -115,7 +115,7 @@ function configPathVersionTest() { fs.copySync(entryPluginConfigFiles.VERSION, tempProvidedPluginConfigFile); try { - await cordovaSetVersion({}, '2.4.9'); + await cordovaSetVersion({ configPath: {}, version: '2.4.9' }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('configPath'); @@ -128,7 +128,7 @@ function configPathVersionTest() { fs.copySync(entryPluginConfigFiles.VERSION, tempProvidedPluginConfigFile); try { - await cordovaSetVersion(tempProvidedPluginConfigFile, {}); + await cordovaSetVersion({ configPath: tempProvidedPluginConfigFile, version: {} }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('version'); @@ -138,7 +138,7 @@ function configPathVersionTest() { it('should return an error about missing config file', async () => { try { - await cordovaSetVersion(tempProvidedPluginConfigFile, '2.4.9'); + await cordovaSetVersion({ configPath: tempProvidedPluginConfigFile, version: '2.4.9' }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('no such file or directory'); @@ -150,7 +150,7 @@ function configPathVersionTest() { fs.copySync(entryPluginConfigFiles.MALFORMED, tempProvidedPluginConfigFile); try { - await cordovaSetVersion(tempProvidedPluginConfigFile, '2.4.9'); + await cordovaSetVersion({ configPath: tempProvidedPluginConfigFile, version: '2.4.9' }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).not.toContain('no such file or directory'); diff --git a/test/index-tests/config-path.js b/test/index-tests/config-path.js index cb8950af..e5c544d3 100644 --- a/test/index-tests/config-path.js +++ b/test/index-tests/config-path.js @@ -13,12 +13,12 @@ import { import { tempPackageFile, entryPackageFiles } from '../packages'; function configPathTest() { - describe('(configPath)', () => { + describe('({ configPath })', () => { it('should override existing version and preserve existing buildNumber', async () => { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); fs.copySync(entryPackageFiles.GOOD, tempPackageFile); - await cordovaSetVersion(tempProvidedConfigFile); + await cordovaSetVersion({ configPath: tempProvidedConfigFile }); expect(readFile(tempProvidedConfigFile)).toBe( readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD), @@ -29,7 +29,7 @@ function configPathTest() { fs.copySync(entryConfigFiles.VERSION_AND_NO_BUILD, tempProvidedConfigFile); fs.copySync(entryPackageFiles.GOOD, tempPackageFile); - await cordovaSetVersion(tempProvidedConfigFile); + await cordovaSetVersion({ configPath: tempProvidedConfigFile }); expect(readFile(tempProvidedConfigFile)).toBe( readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_NO_BUILD), @@ -40,7 +40,7 @@ function configPathTest() { fs.copySync(entryConfigFiles.NO_VERSION_AND_BUILD, tempProvidedConfigFile); fs.copySync(entryPackageFiles.GOOD, tempPackageFile); - await cordovaSetVersion(tempProvidedConfigFile); + await cordovaSetVersion({ configPath: tempProvidedConfigFile }); expect(readFile(tempProvidedConfigFile)).toBe( readFile(expectedXmlFiles.PACKAGE_VERSION_TO_NO_VERSION_AND_BUILD), @@ -51,7 +51,7 @@ function configPathTest() { fs.copySync(entryConfigFiles.NO_VERSION_AND_NO_BUILD, tempProvidedConfigFile); fs.copySync(entryPackageFiles.GOOD, tempPackageFile); - await cordovaSetVersion(tempProvidedConfigFile); + await cordovaSetVersion({ configPath: tempProvidedConfigFile }); expect(readFile(tempProvidedConfigFile)).toBe( readFile(expectedXmlFiles.PACKAGE_VERSION_TO_NO_VERSION_AND_NO_BUILD), @@ -62,7 +62,7 @@ function configPathTest() { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); try { - await cordovaSetVersion({}); + await cordovaSetVersion({ configPath: {} }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('configPath'); @@ -72,7 +72,7 @@ function configPathTest() { it('should return an error about missing config file', async () => { try { - await cordovaSetVersion(tempProvidedConfigFile); + await cordovaSetVersion({ configPath: tempProvidedConfigFile }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('no such file or directory'); @@ -85,7 +85,7 @@ function configPathTest() { fs.copySync(entryPackageFiles.GOOD, tempPackageFile); try { - await cordovaSetVersion(tempProvidedConfigFile); + await cordovaSetVersion({ configPath: tempProvidedConfigFile }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).not.toContain('no such file or directory'); @@ -95,7 +95,7 @@ function configPathTest() { it('should return an error about missing package file', async () => { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); try { - await cordovaSetVersion(tempProvidedConfigFile); + await cordovaSetVersion({ configPath: tempProvidedConfigFile }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('no such file or directory'); @@ -108,7 +108,7 @@ function configPathTest() { fs.copySync(entryPackageFiles.MALFORMED, tempPackageFile); try { - await cordovaSetVersion(tempProvidedConfigFile); + await cordovaSetVersion({ configPath: tempProvidedConfigFile }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).not.toContain('no such file or directory'); @@ -116,12 +116,12 @@ function configPathTest() { }); }); - describe('(pluginConfigPath)', () => { + describe('({ pluginConfigPath })', () => { it('should override existing version', async () => { fs.copySync(entryPluginConfigFiles.VERSION, tempProvidedPluginConfigFile); fs.copySync(entryPackageFiles.GOOD, tempPackageFile); - await cordovaSetVersion(tempProvidedPluginConfigFile); + await cordovaSetVersion({ configPath: tempProvidedPluginConfigFile }); expect(readFile(tempProvidedPluginConfigFile)).toBe( readFile(expectedPluginXmlFiles.PACKAGE_VERSION_TO_VERSION), @@ -132,7 +132,7 @@ function configPathTest() { fs.copySync(entryPluginConfigFiles.NO_VERSION, tempProvidedPluginConfigFile); fs.copySync(entryPackageFiles.GOOD, tempPackageFile); - await cordovaSetVersion(tempProvidedPluginConfigFile); + await cordovaSetVersion({ configPath: tempProvidedPluginConfigFile }); expect(readFile(tempProvidedPluginConfigFile)).toBe( readFile(expectedPluginXmlFiles.PACKAGE_VERSION_TO_NO_VERSION), @@ -143,7 +143,7 @@ function configPathTest() { fs.copySync(entryPluginConfigFiles.VERSION, tempProvidedPluginConfigFile); try { - await cordovaSetVersion({}); + await cordovaSetVersion({ configPath: {} }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('configPath'); @@ -153,7 +153,7 @@ function configPathTest() { it('should return an error about missing config file', async () => { try { - await cordovaSetVersion(tempProvidedPluginConfigFile); + await cordovaSetVersion({ configPath: tempProvidedPluginConfigFile }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('no such file or directory'); @@ -166,7 +166,7 @@ function configPathTest() { fs.copySync(entryPackageFiles.GOOD, tempPackageFile); try { - await cordovaSetVersion(tempProvidedPluginConfigFile); + await cordovaSetVersion({ configPath: tempProvidedPluginConfigFile }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).not.toContain('no such file or directory'); @@ -176,7 +176,7 @@ function configPathTest() { it('should return an error about missing package file', async () => { fs.copySync(entryPluginConfigFiles.VERSION, tempProvidedPluginConfigFile); try { - await cordovaSetVersion(tempProvidedPluginConfigFile); + await cordovaSetVersion({ configPath: tempProvidedPluginConfigFile }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('no such file or directory'); @@ -189,7 +189,7 @@ function configPathTest() { fs.copySync(entryPackageFiles.MALFORMED, tempPackageFile); try { - await cordovaSetVersion(tempProvidedPluginConfigFile); + await cordovaSetVersion({ configPath: tempProvidedPluginConfigFile }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).not.toContain('no such file or directory'); diff --git a/test/index-tests/nulls.js b/test/index-tests/nulls.js index fec71af6..78962096 100644 --- a/test/index-tests/nulls.js +++ b/test/index-tests/nulls.js @@ -7,117 +7,136 @@ import { tempPackageFile, entryPackageFiles } from '../packages'; function nullsTest() { describe('nulls', () => { - it('(configPath, null)', async () => { + beforeEach(() => { + fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); fs.copySync(entryPackageFiles.GOOD, tempPackageFile); + }); - await cordovaSetVersion(tempProvidedConfigFile, null); + it('({})', async () => { + await cordovaSetVersion({}); - expect(readFile(tempProvidedConfigFile)).toBe( - readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD), - ); + expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD)); }); - it('(configPath, null, null)', async () => { - fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); - fs.copySync(entryPackageFiles.GOOD, tempPackageFile); + it('({ buildNumber: null })', async () => { + await cordovaSetVersion({ buildNumber: null }); - await cordovaSetVersion(tempProvidedConfigFile, null, null); - - expect(readFile(tempProvidedConfigFile)).toBe( - readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD), - ); + expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD)); }); - it('(configPath, version, null)', async () => { - fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); - - await cordovaSetVersion(tempProvidedConfigFile, '2.4.9', null); + it('({ version: null })', async () => { + await cordovaSetVersion({ buildNumber: null }); - expect(readFile(tempProvidedConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_TO_VERSION_AND_BUILD)); + expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD)); }); - it('(configPath, null, buildNumber)', async () => { - fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempProvidedConfigFile); - - await cordovaSetVersion(tempProvidedConfigFile, null, 86); + it('({ version: null, buildNumber: null })', async () => { + await cordovaSetVersion({ version: null, buildNumber: null }); - expect(readFile(tempProvidedConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_VERSION_AND_BUILD)); + expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD)); }); - it('(version, null)', async () => { - fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); + it('({ version: null, buildNumber: 86 })', async () => { + await cordovaSetVersion({ version: null, buildNumber: 86 }); - await cordovaSetVersion('2.4.9', null); - - expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_TO_VERSION_AND_BUILD)); + expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_VERSION_AND_BUILD)); }); - it('(null, version)', async () => { - fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); - - await cordovaSetVersion(null, '2.4.9'); + it("({ version: '2.4.9', buildNumber: null })", async () => { + await cordovaSetVersion({ version: '2.4.9', buildNumber: null }); expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_TO_VERSION_AND_BUILD)); }); - it('(null, version, null)', async () => { - fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); + it('({ configPath: null })', async () => { + await cordovaSetVersion({ configPath: null }); - await cordovaSetVersion(null, '2.4.9', null); + expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD)); + }); - expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_TO_VERSION_AND_BUILD)); + it('({ configPath: null, buildNumber: null })', async () => { + await cordovaSetVersion({ configPath: null, buildNumber: null }); + + expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD)); }); - it('(null, version, buildNumber)', async () => { - fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); + it('({ configPath: null, buildNumber: 86 })', async () => { + await cordovaSetVersion({ configPath: null, buildNumber: 86 }); - await cordovaSetVersion(null, '2.4.9', 86); + expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_VERSION_AND_BUILD)); + }); - expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_AND_BUILD_TO_VERSION_AND_BUILD)); + it('({ configPath: null, version: null })', async () => { + await cordovaSetVersion({ configPath: null, version: null }); + + expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD)); }); - it('(null, buildNumber)', async () => { - fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); + it('({ configPath: null, version: null, buildNumber: null })', async () => { + await cordovaSetVersion({ configPath: null, version: null, buildNumber: null }); + + expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD)); + }); - await cordovaSetVersion(null, 86); + it('({ configPath: null, version: null, buildNumber: 86 })', async () => { + await cordovaSetVersion({ configPath: null, version: null, buildNumber: 86 }); expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_VERSION_AND_BUILD)); }); - it('(null, null, buildNumber)', async () => { - fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); + it("({ configPath: null, version: '2.4.9' })", async () => { + await cordovaSetVersion({ configPath: null, version: '2.4.9' }); - await cordovaSetVersion(null, null, 86); + expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_TO_VERSION_AND_BUILD)); + }); - expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_VERSION_AND_BUILD)); + it("({ configPath: null, version: '2.4.9', buildNumber: null })", async () => { + await cordovaSetVersion({ configPath: null, version: '2.4.9', buildNumber: null }); + + expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_TO_VERSION_AND_BUILD)); }); - it('(null)', async () => { - fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); - fs.copySync(entryPackageFiles.GOOD, tempPackageFile); + it("({ configPath: null, version: '2.4.9', buildNumber: 86 })", async () => { + await cordovaSetVersion({ configPath: null, version: '2.4.9', buildNumber: 86 }); - await cordovaSetVersion(null); + expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_AND_BUILD_TO_VERSION_AND_BUILD)); + }); - expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD)); + it('({ configPath: tempProvidedConfigFile, buildNumber: null })', async () => { + await cordovaSetVersion({ configPath: tempProvidedConfigFile, buildNumber: null }); + + expect(readFile(tempProvidedConfigFile)).toBe( + readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD), + ); }); - it('(null, null)', async () => { - fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); - fs.copySync(entryPackageFiles.GOOD, tempPackageFile); + it('({ configPath: tempProvidedConfigFile, version: null })', async () => { + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: null }); + + expect(readFile(tempProvidedConfigFile)).toBe( + readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD), + ); + }); - await cordovaSetVersion(null, null); + it('({ configPath: tempProvidedConfigFile, version: null, buildNumber: null })', async () => { + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: null, buildNumber: null }); - expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD)); + expect(readFile(tempProvidedConfigFile)).toBe( + readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD), + ); }); - it('(null, null, null)', async () => { - fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); - fs.copySync(entryPackageFiles.GOOD, tempPackageFile); + it('({ configPath: tempProvidedConfigFile, version: null, buildNumber: 86 })', async () => { + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: null, buildNumber: 86 }); - await cordovaSetVersion(null, null, null); + expect(readFile(tempProvidedConfigFile)).toBe(readFile(expectedXmlFiles.BUILD_TO_VERSION_AND_BUILD)); + }); - expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.PACKAGE_VERSION_TO_VERSION_AND_BUILD)); + it("({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: null })", async () => { + await cordovaSetVersion({ configPath: tempProvidedConfigFile, version: '2.4.9', buildNumber: null }); + + expect(readFile(tempProvidedConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_TO_VERSION_AND_BUILD)); }); }); } diff --git a/test/index-tests/version-build-number.js b/test/index-tests/version-build-number.js index 8f464e83..402fe034 100644 --- a/test/index-tests/version-build-number.js +++ b/test/index-tests/version-build-number.js @@ -5,11 +5,11 @@ import cordovaSetVersion from '../../src'; import { tempConfigFile, entryConfigFiles, expectedXmlFiles } from '../configs'; function versionBuildNumberTest() { - describe('(version, buildNumber)', () => { + describe('({ version, buildNumber })', () => { it('should override both existing version and buildNumber', async () => { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); - await cordovaSetVersion('2.4.9', 86); + await cordovaSetVersion({ version: '2.4.9', buildNumber: 86 }); expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_AND_BUILD_TO_VERSION_AND_BUILD)); }); @@ -17,7 +17,7 @@ function versionBuildNumberTest() { it('should override existing version and add buildNumber', async () => { fs.copySync(entryConfigFiles.VERSION_AND_NO_BUILD, tempConfigFile); - await cordovaSetVersion('2.4.9', 86); + await cordovaSetVersion({ version: '2.4.9', buildNumber: 86 }); expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_AND_BUILD_TO_VERSION_AND_NO_BUILD)); }); @@ -25,7 +25,7 @@ function versionBuildNumberTest() { it('should add version and override existing buildNumber', async () => { fs.copySync(entryConfigFiles.NO_VERSION_AND_BUILD, tempConfigFile); - await cordovaSetVersion('2.4.9', 86); + await cordovaSetVersion({ version: '2.4.9', buildNumber: 86 }); expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_AND_BUILD_TO_NO_VERSION_AND_BUILD)); }); @@ -33,21 +33,21 @@ function versionBuildNumberTest() { it('should add version and buildNumber', async () => { fs.copySync(entryConfigFiles.NO_VERSION_AND_NO_BUILD, tempConfigFile); - await cordovaSetVersion('2.4.9', 86); + await cordovaSetVersion({ version: '2.4.9', buildNumber: 86 }); expect(readFile(tempConfigFile)).toBe( readFile(expectedXmlFiles.VERSION_AND_BUILD_TO_NO_VERSION_AND_NO_BUILD), ); }); - it('should return an error about configPath type', async () => { + it('should return an error about version type', async () => { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); try { - await cordovaSetVersion({}, 86); + await cordovaSetVersion({ version: {}, buildNumber: 86 }); } catch (error) { expect(error).not.toBeNil(); - expect(error.message).toContain('configPath'); + expect(error.message).toContain('version'); expect(error.message).toContain('must be a'); } }); @@ -56,7 +56,7 @@ function versionBuildNumberTest() { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); try { - await cordovaSetVersion('2.4.9', {}); + await cordovaSetVersion({ version: '2.4.9', buildNumber: {} }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('buildNumber'); @@ -66,7 +66,7 @@ function versionBuildNumberTest() { it('should return an error about missing config file', async () => { try { - await cordovaSetVersion('2.4.9', 86); + await cordovaSetVersion({ version: '2.4.9', buildNumber: 86 }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('no such file or directory'); @@ -78,7 +78,7 @@ function versionBuildNumberTest() { fs.copySync(entryConfigFiles.MALFORMED, tempConfigFile); try { - await cordovaSetVersion('2.4.9', 86); + await cordovaSetVersion({ version: '2.4.9', buildNumber: 86 }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).not.toContain('no such file or directory'); diff --git a/test/index-tests/version.js b/test/index-tests/version.js index 3cecb6af..23a95af3 100644 --- a/test/index-tests/version.js +++ b/test/index-tests/version.js @@ -5,11 +5,11 @@ import cordovaSetVersion from '../../src'; import { tempConfigFile, entryConfigFiles, expectedXmlFiles } from '../configs'; function versionTest() { - describe('(version)', () => { + describe('({ version })', () => { it('should override existing version and preserve existing buildNumber', async () => { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); - await cordovaSetVersion('2.4.9'); + await cordovaSetVersion({ version: '2.4.9' }); expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_TO_VERSION_AND_BUILD)); }); @@ -17,7 +17,7 @@ function versionTest() { it('should override existing version and not add buildNumber', async () => { fs.copySync(entryConfigFiles.VERSION_AND_NO_BUILD, tempConfigFile); - await cordovaSetVersion('2.4.9'); + await cordovaSetVersion({ version: '2.4.9' }); expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_TO_VERSION_AND_NO_BUILD)); }); @@ -25,7 +25,7 @@ function versionTest() { it('should add version and preserve existing buildNumber', async () => { fs.copySync(entryConfigFiles.NO_VERSION_AND_BUILD, tempConfigFile); - await cordovaSetVersion('2.4.9'); + await cordovaSetVersion({ version: '2.4.9' }); expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_TO_NO_VERSION_AND_BUILD)); }); @@ -33,26 +33,26 @@ function versionTest() { it('should add version and not add buildNumber', async () => { fs.copySync(entryConfigFiles.NO_VERSION_AND_NO_BUILD, tempConfigFile); - await cordovaSetVersion('2.4.9'); + await cordovaSetVersion({ version: '2.4.9' }); expect(readFile(tempConfigFile)).toBe(readFile(expectedXmlFiles.VERSION_TO_NO_VERSION_AND_NO_BUILD)); }); - it('should return an error about configPath type', async () => { + it('should return an error about version type', async () => { fs.copySync(entryConfigFiles.VERSION_AND_BUILD, tempConfigFile); try { - await cordovaSetVersion({}); + await cordovaSetVersion({ version: {} }); } catch (error) { expect(error).not.toBeNil(); - expect(error.message).toContain('configPath'); + expect(error.message).toContain('version'); expect(error.message).toContain('must be a'); } }); it('should return an error about missing config file', async () => { try { - await cordovaSetVersion('2.4.9'); + await cordovaSetVersion({ version: '2.4.9' }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).toContain('no such file or directory'); @@ -64,7 +64,7 @@ function versionTest() { fs.copySync(entryConfigFiles.MALFORMED, tempConfigFile); try { - await cordovaSetVersion('2.4.9'); + await cordovaSetVersion({ version: '2.4.9' }); } catch (error) { expect(error).not.toBeNil(); expect(error.message).not.toContain('no such file or directory');