From 2a8578458348f27b3d3c441d940d5b4f907353b9 Mon Sep 17 00:00:00 2001 From: "Grant, Jeffrey (CORP)" Date: Wed, 18 Sep 2024 14:56:59 -0400 Subject: [PATCH 1/2] feat(python): add poetry support --- README.md | 9 ++++++++ lib/updaters/index.js | 4 ++++ lib/updaters/types/python.js | 30 ++++++++++++++++++++++++++ test/core.spec.js | 37 +++++++++++++++++++++++++++++++++ test/mocks/pyproject-1.0.0.toml | 12 +++++++++++ test/mocks/pyproject-1.1.0.toml | 12 +++++++++++ 6 files changed, 104 insertions(+) create mode 100644 lib/updaters/types/python.js create mode 100644 test/mocks/pyproject-1.0.0.toml create mode 100644 test/mocks/pyproject-1.1.0.toml diff --git a/README.md b/README.md index eaa655de5..6275c852a 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ _Having problems? Want to contribute? Join us on the [node-tooling community Sla - [.NET Support](#net-support) - [YAML Support](#yaml-support) - [OpenAPI Support](#openapi-support) + - [Python Support](#python-support) - [Installing `commit-and-tag-version`](#installing-commit-and-tag-version) - [As a local `npm run` script](#as-a-local-npm-run-script) - [As global `bin`](#as-global-bin) @@ -127,6 +128,14 @@ If you are using OpenAPI, then just point to your `openapi.yaml` file. commit-and-tag-version --packageFiles openapi.yaml --bumpFiles openapi.yaml ``` +### Python Support + +If you are using Python ***with Poetry***, then point to your `pyproject.toml` file. + +```sh +commit-and-tag-version --packageFiles pyproject.toml --bumpFiles pyproject.toml +``` + ## Installing `commit-and-tag-version` ### As a local `npm run` script diff --git a/lib/updaters/index.js b/lib/updaters/index.js index 47296e3d1..1d3c66ea2 100644 --- a/lib/updaters/index.js +++ b/lib/updaters/index.js @@ -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']; @@ -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\`.`, ); diff --git a/lib/updaters/types/python.js b/lib/updaters/types/python.js new file mode 100644 index 000000000..4d87456a8 --- /dev/null +++ b/lib/updaters/types/python.js @@ -0,0 +1,30 @@ +const versionExtractRegex = /version[" ]*=[ ]*["'](.*)["']/i + +const getVersionIndex = function (lines) { + let version + const lineNumber = lines.findIndex(line => { + const versionMatcher = line.match(versionExtractRegex) + // if version not found in lines provided, return false + if (versionMatcher == null) { + return false + } + version = versionMatcher[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') +} \ No newline at end of file diff --git a/test/core.spec.js b/test/core.spec.js index 880220216..6efc268f3 100644 --- a/test/core.spec.js +++ b/test/core.spec.js @@ -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 () { diff --git a/test/mocks/pyproject-1.0.0.toml b/test/mocks/pyproject-1.0.0.toml new file mode 100644 index 000000000..ed8392488 --- /dev/null +++ b/test/mocks/pyproject-1.0.0.toml @@ -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" diff --git a/test/mocks/pyproject-1.1.0.toml b/test/mocks/pyproject-1.1.0.toml new file mode 100644 index 000000000..03745363d --- /dev/null +++ b/test/mocks/pyproject-1.1.0.toml @@ -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" From 4f7b4a5d626c35d67749786cd525f2dfb49de62e Mon Sep 17 00:00:00 2001 From: Timothy Jones Date: Fri, 11 Oct 2024 00:38:57 +1100 Subject: [PATCH 2/2] chore: run autoformatter --- lib/updaters/index.js | 2 +- lib/updaters/types/python.js | 42 ++++++++++++++++++------------------ test/core.spec.js | 11 +++++----- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/lib/updaters/index.js b/lib/updaters/index.js index 1d3c66ea2..9eb6dd93f 100644 --- a/lib/updaters/index.js +++ b/lib/updaters/index.js @@ -43,7 +43,7 @@ function getUpdaterByFilename(filename) { return getUpdaterByType('yaml'); } if (/pyproject.toml/.test(filename)) { - return getUpdaterByType('python') + return getUpdaterByType('python'); } throw Error( `Unsupported file (${filename}) provided for bumping.\n Please specify the updater \`type\` or use a custom \`updater\`.`, diff --git a/lib/updaters/types/python.js b/lib/updaters/types/python.js index 4d87456a8..afaca68a5 100644 --- a/lib/updaters/types/python.js +++ b/lib/updaters/types/python.js @@ -1,30 +1,30 @@ -const versionExtractRegex = /version[" ]*=[ ]*["'](.*)["']/i +const versionExtractRegex = /version[" ]*=[ ]*["'](.*)["']/i; const getVersionIndex = function (lines) { - let version - const lineNumber = lines.findIndex(line => { - const versionMatcher = line.match(versionExtractRegex) + let version; + const lineNumber = lines.findIndex((line) => { + const versionMatcher = line.match(versionExtractRegex); // if version not found in lines provided, return false if (versionMatcher == null) { - return false + return false; } - version = versionMatcher[1] - return true - }) - return { version, lineNumber } -} + version = versionMatcher[1]; + return true; + }); + return { version, lineNumber }; +}; module.exports.readVersion = function (contents) { - const lines = contents.split('\n') - const versionIndex = getVersionIndex(lines) - return versionIndex.version -} + 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') -} \ No newline at end of file + 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'); +}; diff --git a/test/core.spec.js b/test/core.spec.js index 6efc268f3..3a579d78a 100644 --- a/test/core.spec.js +++ b/test/core.spec.js @@ -1136,7 +1136,7 @@ describe('cli', function () { './test/mocks/pyproject-1.1.0.toml', 'utf-8', ); - + const filename = 'python.toml'; mock({ bump: 'minor', @@ -1147,26 +1147,25 @@ describe('cli', function () { }, ], }); - + 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 () {