Skip to content

Commit

Permalink
feat(python): add poetry support
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrant216 committed Sep 19, 2024
1 parent 959200c commit 39defd6
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/updaters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const updatersByType = {
csproj: require('./types/csproj'),
yaml: require('./types/yaml'),
openapi: require('./types/openapi'),
python: require('./types/python'),
};
const PLAIN_TEXT_BUMP_FILES = ['VERSION.txt', 'version.txt'];

Expand Down Expand Up @@ -41,6 +42,9 @@ function getUpdaterByFilename(filename) {
if (/\.ya?ml$/.test(filename)) {
return getUpdaterByType('yaml');
}
if (/pyproject.toml/.test(filename)) {
return getUpdaterByType('python')
}
throw Error(
`Unsupported file (${filename}) provided for bumping.\n Please specify the updater \`type\` or use a custom \`updater\`.`,
);
Expand Down
29 changes: 29 additions & 0 deletions lib/updaters/types/python.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const semverRegex = /version[" ]*=[ ]*["'](.*)["']/i

const getVersionIndex = function (lines) {
let version
const lineNumber = lines.findIndex(line => {
const found = line.match(semverRegex)
if (found == null) {
return false
}
version = found[1]
return true
})
return { version, lineNumber }
}

module.exports.readVersion = function (contents) {
const lines = contents.split('\n')
const versionIndex = getVersionIndex(lines)
return versionIndex.version
}

module.exports.writeVersion = function (contents, version) {
const lines = contents.split('\n')
const versionIndex = getVersionIndex(lines)
const versionLine = lines[versionIndex.lineNumber]
const newVersionLine = versionLine.replace(versionIndex.version, version)
lines[versionIndex.lineNumber] = newVersionLine
return lines.join('\n')
}
37 changes: 37 additions & 0 deletions test/core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,43 @@ describe('cli', function () {
console.warn = origWarn;
}
});

it('bumps version in Python `pyproject.toml` file', async function () {
const expected = fs.readFileSync(
'./test/mocks/pyproject-1.1.0.toml',
'utf-8',
);

const filename = 'python.toml';
mock({
bump: 'minor',
realTestFiles: [
{
filename,
path: './test/mocks/pyproject-1.0.0.toml',
},
],
});

await exec({
packageFiles: [{ filename, type: 'python' }],
bumpFiles: [{ filename, type: 'python' }],
});

// filePath is the first arg passed to writeFileSync
const packageJsonWriteFileSynchCall = findWriteFileCallForPath({
writeFileSyncSpy,
filename,
});

if (!packageJsonWriteFileSynchCall) {
throw new Error(`writeFileSynch not invoked with path ${filename}`);
}

const calledWithContentStr = packageJsonWriteFileSynchCall[1];
expect(calledWithContentStr).toEqual(expected);
});

});

it('`packageFiles` are bumped along with `bumpFiles` defaults [commit-and-tag-version#533]', async function () {
Expand Down
12 changes: 12 additions & 0 deletions test/mocks/pyproject-1.0.0.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.poetry]
name = "test"
version = "1.0.0"
description = ""
authors = []

[tool.poetry.dependencies]
python = "^3.8"

[build-system]
requires = ["poetry>=1"]
build-backend = "poetry.masonry.api"
12 changes: 12 additions & 0 deletions test/mocks/pyproject-1.1.0.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.poetry]
name = "test"
version = "1.1.0"
description = ""
authors = []

[tool.poetry.dependencies]
python = "^3.8"

[build-system]
requires = ["poetry>=1"]
build-backend = "poetry.masonry.api"

0 comments on commit 39defd6

Please sign in to comment.