Skip to content

Commit

Permalink
Add toml resource type #13
Browse files Browse the repository at this point in the history
  • Loading branch information
cliffano committed Oct 12, 2023
1 parent 2bf0bca commit 367b6f6
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added
- Add toml resource type #13

## 3.0.0 - 2023-07-23
### Added
- Add label to log messages when dry-run is enabled
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ Example `keep-a-changelog` resource configuration:
]
}

Example `toml` resource configuration:

{
"resources": [
{
"path": "pyproject.toml",
"type": "toml",
"params": {
"property": "version"
}
}
]
}

Example `makefile` resource configuration:

{
Expand Down
54 changes: 54 additions & 0 deletions lib/resource-types/toml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use strict"
import {getProperty, setProperty} from 'dot-prop';
import fs from 'fs';
import toml from '@iarna/toml';

/**
* Set version value in the TOML resource's property (defined in dot-notation).
*
* @param {String} version: version value to set
* @param {Object} resource: resource configuration which contains type, path, and params
* @param {Object} opts: optional settings
* - dryRun: when true, TOML file won't be modified
* @param {Function} cb: standard cb(err, result) callback
*/
function setVersion(version, resource, opts, cb) {
const property = resource.params.property;
let data = toml.parse(fs.readFileSync(resource.path, 'UTF-8'));
setProperty(data, property, version);
if (!opts.dryRun) {
fs.writeFile(resource.path, toml.stringify(data, null, 2), cb);
} else {
cb();
}
}

/**
* Get version value from the TOML resource's property (defined in dot-notation).
*
* @param {Object} resource: resource configuration which contains type, path, and params
* @param {Function} cb: standard cb(err, result) callback
*/
function getVersion(resource, cb) {
const property = resource.params.property;

function readCb(err, result) {
let version;
if (!err) {
const data = toml.parse(result);
version = getProperty(data, property);
}
cb(err, version);
}
fs.readFile(resource.path, 'UTF-8', readCb);
}

const exports = {
setReleaseVersion: setVersion,
setPostReleaseVersion: setVersion,
getVersion: getVersion
};

export {
exports as default
};
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"lodash": "^4.17.21",
"@kba/makefile-parser": "^0.0.6",
"simple-git": "^3.20.0",
"string-format": "^2.0.0"
"string-format": "^2.0.0",
"@iarna/toml": "^2.2.5"
},
"devDependencies": {
"sinon": "^16.1.0",
Expand Down
106 changes: 106 additions & 0 deletions test/resource-types/toml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"use strict"
/* eslint no-unused-vars: 0 */
import assert from 'assert';
import fs from 'fs';
import resourceType from '../../lib/resource-types/toml.js';
import sinon from 'sinon';

describe('toml', function() {

beforeEach(function (done) {
this.mockFs = sinon.mock(fs);
done();
});
afterEach(function (done) {
this.mockFs.verify();
sinon.restore();
done();
});

describe('setVersion', function() {
it('should only set version but not modify toml file when dry run is enabled', function(done) {
let resource = {
path: 'somepackage.toml',
type: 'toml',
params: {
property: 'version'
}
};
this.mockFs.expects('readFileSync').once().withExactArgs('somepackage.toml', 'UTF-8').returns('version = "0.0.0"');
this.mockFs.expects('writeFile').never();
function cb(err, result) {
assert.equal(err, null);
done();
}
resourceType.setReleaseVersion('1.2.3', resource, { dryRun: true }, cb);
});
it('should set version and modify toml file when dry run is disabled', function(done) {
let resource = {
path: 'somepackage.toml',
type: 'toml',
params: {
property: 'version'
}
};
this.mockFs.expects('readFileSync').once().withExactArgs('somepackage.toml', 'UTF-8').returns('version = "0.0.0"');
this.mockFs.expects('writeFile').once().withExactArgs('somepackage.toml', 'version = "1.2.3"\n', sinon.match.func).callsArgWith(2, null);
function cb(err, result) {
assert.equal(err, null);
done();
}
resourceType.setReleaseVersion('1.2.3', resource, { dryRun: false }, cb);
});
it('should set array property under a section', function(done) {
let resource = {
path: 'somepackage.toml',
type: 'toml',
params: {
property: 'package.versions[1]'
}
};
this.mockFs.expects('readFileSync').once().withExactArgs('somepackage.toml', 'UTF-8').returns('[package]\nversions = ["9.9.9", "0.0.0", "8.8.8"]');
this.mockFs.expects('writeFile').once().withExactArgs('somepackage.toml', '[package]\nversions = [ "9.9.9", "1.2.3", "8.8.8" ]\n', sinon.match.func).callsArgWith(2, null);
function cb(err, result) {
assert.equal(err, null);
done();
}
resourceType.setReleaseVersion('1.2.3', resource, { dryRun: false }, cb);
});
});

describe('getVersion', function() {
it('should get version from resource property', function(done) {
let resource = {
path: 'somepackage.toml',
type: 'toml',
params: {
property: 'version'
}
};
this.mockFs.expects('readFile').once().withExactArgs('somepackage.toml', 'UTF-8', sinon.match.func).callsArgWith(2, null, 'version = "0.0.0"');
function cb(err, result) {
assert.equal(err, null);
assert.equal(result, '0.0.0');
done();
}
resourceType.getVersion(resource, cb);
});
it('should get array property under a section', function(done) {
let resource = {
path: 'somepackage.toml',
type: 'toml',
params: {
property: 'package.versions[1]'
}
};
this.mockFs.expects('readFile').once().withExactArgs('somepackage.toml', 'UTF-8', sinon.match.func).callsArgWith(2, null, '[package]\nversions = ["9.9.9", "0.0.0", "8.8.8"]');
function cb(err, result) {
assert.equal(err, null);
assert.equal(result, '0.0.0');
done();
}
resourceType.getVersion(resource, cb);
});
});

});

0 comments on commit 367b6f6

Please sign in to comment.