diff --git a/CHANGELOG.md b/CHANGELOG.md index 48886f39..3ebe46d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Added +- Add array property support for yaml and json resource types + ## 2.0.0 - 2020-07-08 ### Changed - Change module type to ESM diff --git a/README.md b/README.md index 121a3df4..7cddc3cf 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,20 @@ Example `json` resource configuration: ] } +Example `json` resource configuration with an array sub-property: + + { + "resources": [ + { + "path": "package.json", + "type": "json", + "params": { + "property": "versions.1.minor" + } + } + ] + } + Example `yaml` resource configuration: { @@ -96,6 +110,20 @@ Example `yaml` resource configuration: ] } +Example `yaml` resource configuration with an array sub-property: + + { + "resources": [ + { + "path": "playbook.yaml", + "type": "yaml", + "params": { + "property": "versions.1.minor" + } + } + ] + } + Example `keep-a-changelog` resource configuration: { diff --git a/test/resourcetypes/json.js b/test/resourcetypes/json.js index f0dd589f..12aebe77 100644 --- a/test/resourcetypes/json.js +++ b/test/resourcetypes/json.js @@ -50,6 +50,22 @@ describe('json', function() { } resourceType.setReleaseVersion('1.2.3', resource, { dryRun: false }, cb); }); + it('should set property under array sub-property', function(done) { + let resource = { + path: 'somepackage.json', + type: 'json', + params: { + property: 'versions.1.minor' + } + }; + this.mockFs.expects('readFileSync').once().withExactArgs('somepackage.json', 'UTF-8').returns('{ "versions": [{ "major": 1, "minor": 2, "patch": 3 }, { "major": 8, "minor": 9, "patch": 0 }] }'); + this.mockFs.expects('writeFile').once().withExactArgs('somepackage.json', '{\n "versions": [\n {\n "major": 1,\n "minor": 2,\n "patch": 3\n },\n {\n "major": 8,\n "minor": 9,\n "patch": 0\n }\n ]\n}', sinon.match.func).callsArgWith(2, null); + function cb(err, result) { + assert.equal(err, null); + done(); + } + resourceType.setReleaseVersion(9, resource, { dryRun: false }, cb); + }); }); describe('getVersion', function() { @@ -69,6 +85,22 @@ describe('json', function() { } resourceType.getVersion(resource, cb); }); + it('should get property under an array array sub-property', function(done) { + let resource = { + path: 'somepackage.json', + type: 'json', + params: { + property: 'versions.1.minor' + } + }; + this.mockFs.expects('readFile').once().withExactArgs('somepackage.json', 'UTF-8', sinon.match.func).callsArgWith(2, null, '{ "versions": [{ "major": 1, "minor": 2, "patch": 3 }, { "major": 8, "minor": 9, "patch": 0 }] }'); + function cb(err, result) { + assert.equal(err, null); + assert.equal(result, 9); + done(); + } + resourceType.getVersion(resource, cb); + }); }); }); diff --git a/test/resourcetypes/yaml.js b/test/resourcetypes/yaml.js index a0af5681..e90342c4 100644 --- a/test/resourcetypes/yaml.js +++ b/test/resourcetypes/yaml.js @@ -50,6 +50,22 @@ describe('yaml', function() { } resourceType.setReleaseVersion('1.2.3', resource, { dryRun: false }, cb); }); + it('should set property under array sub-property', function(done) { + let resource = { + path: 'someplaybook.yaml', + type: 'yaml', + params: { + property: 'versions.1.minor' + } + }; + this.mockFs.expects('readFileSync').once().withExactArgs('someplaybook.yaml', 'UTF-8').returns('{ "versions": [{ "major": 1, "minor": 2, "patch": 3 }, { "major": 8, "minor": 9, "patch": 0 }] }'); + this.mockFs.expects('writeFile').once().withExactArgs('someplaybook.yaml', 'versions:\n - major: 1\n minor: 2\n patch: 3\n - major: 8\n minor: 9\n patch: 0\n', sinon.match.func).callsArgWith(2, null); + function cb(err, result) { + assert.equal(err, null); + done(); + } + resourceType.setReleaseVersion(9, resource, { dryRun: false }, cb); + }); }); describe('getVersion', function() { @@ -69,6 +85,22 @@ describe('yaml', function() { } resourceType.getVersion(resource, cb); }); + it('should get property under an array array sub-property', function(done) { + let resource = { + path: 'someplaybook.yaml', + type: 'yaml', + params: { + property: 'versions.1.minor' + } + }; + this.mockFs.expects('readFile').once().withExactArgs('someplaybook.yaml', 'UTF-8', sinon.match.func).callsArgWith(2, null, '{ "versions": [{ "major": 1, "minor": 2, "patch": 3 }, { "major": 8, "minor": 9, "patch": 0 }] }'); + function cb(err, result) { + assert.equal(err, null); + assert.equal(result, 9); + done(); + } + resourceType.getVersion(resource, cb); + }); }); });