Skip to content

Commit

Permalink
feat(python): support src/packagename/__init__.py (#1062)
Browse files Browse the repository at this point in the history
* feat(python): support src/packagename/__init__.py

For the popular src directory layout,
https://blog.ionelmc.ro/2014/05/25/python-packaging/#the-structure

* Update test snapshots

* docs: add TODO note about configurability refactor for Python files

Co-authored-by: Benjamin E. Coe <bencoe@google.com>
Co-authored-by: Jeff Ching <chingor@google.com>
  • Loading branch information
3 people committed Sep 24, 2021
1 parent 0549a30 commit 598667d
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions __snapshots__/python-file-with-version.js
Expand Up @@ -3,6 +3,11 @@ __version__ = '0.6.0'
`

exports['src/project/__init__.py updateContent updates version in src/project/__init__.py 1'] = `
__version__ = '0.6.0'
`

exports['version.py updateContent updates version in version.py 1'] = `
# Copyright 2020 Google LLC
#
Expand Down
6 changes: 6 additions & 0 deletions __snapshots__/python.js
Expand Up @@ -168,6 +168,9 @@ version = "0.123.5"
filename: project/__init__.py
__version__ = '0.123.5'
filename: src/project/__init__.py
__version__ = '0.123.5'
filename: src/version.py
# Copyright 2020 Google LLC
#
Expand Down Expand Up @@ -743,6 +746,9 @@ version = "0.123.5"
filename: project/__init__.py
__version__ = '0.123.5'
filename: src/project/__init__.py
__version__ = '0.123.5'
filename: src/version.py
# Copyright 2020 Google LLC
#
Expand Down
9 changes: 9 additions & 0 deletions src/releasers/python.ts
Expand Up @@ -107,6 +107,7 @@ export class Python extends ReleasePR {
: `file ${chalk.green('pyproject.toml')} did not exist`
);
}
// TODO: figure out refactor that makes logic for updating __init__.py, version.py, __version__.py etc., configurable
updates.push(
new PythonFileWithVersion({
path: this.addPath(`${projectName}/__init__.py`),
Expand All @@ -115,6 +116,14 @@ export class Python extends ReleasePR {
packageName: packageName.name,
})
);
updates.push(
new PythonFileWithVersion({
path: this.addPath(`src/${projectName}/__init__.py`),
changelogEntry,
version: candidate.version,
packageName: packageName.name,
})
);
// There should be only one version.py, but foreach in case that is incorrect
const versionPyFilesSearch = this.gh.findFilesByFilename(
'version.py',
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/manifest/repo/python/src/foolib/__init__.py
@@ -0,0 +1 @@
__version__ = '1.2.3'
1 change: 1 addition & 0 deletions test/manifest.ts
Expand Up @@ -152,6 +152,7 @@ describe('Manifest', () => {
'python/HISTORY.md',
'package-lock.json',
'python/foolib/__init__.py',
'python/src/foolib/__init__.py',
'npm-shrinkwrap.json',
'samples/package.json',
'CHANGELOG.md',
Expand Down
14 changes: 14 additions & 0 deletions test/releasers/python.ts
Expand Up @@ -134,6 +134,12 @@ describe('Python', () => {
version: expectedVersion,
packageName: pkgName,
}),
new PythonFileWithVersion({
path: `src/${pkgName}/__init__.py`,
changelogEntry: perUpdateChangelog,
version: expectedVersion,
packageName: pkgName,
}),
new PythonFileWithVersion({
path: 'src/version.py',
changelogEntry: perUpdateChangelog,
Expand Down Expand Up @@ -195,6 +201,12 @@ describe('Python', () => {
version: expectedVersion,
packageName: pkgName,
}),
new PythonFileWithVersion({
path: `src/${pkgName}/__init__.py`,
changelogEntry: perUpdateChangelog,
version: expectedVersion,
packageName: pkgName,
}),
new PythonFileWithVersion({
path: 'src/version.py',
changelogEntry: perUpdateChangelog,
Expand Down Expand Up @@ -237,6 +249,7 @@ describe('Python', () => {
stubFilesToUpdate(releasePR.gh, [
'pyproject.toml',
'project/__init__.py',
'src/project/__init__.py',
'setup.py',
'src/version.py',
'setup.cfg',
Expand Down Expand Up @@ -304,6 +317,7 @@ describe('Python', () => {
stubFilesToUpdate(releasePR.gh, [
'pyproject.toml',
'project/__init__.py',
'src/project/__init__.py',
'setup.py',
'src/version.py',
'setup.cfg',
Expand Down
1 change: 1 addition & 0 deletions test/updaters/fixtures/src/project/__init__.py
@@ -0,0 +1 @@
__version__ = '3.2.1'
19 changes: 19 additions & 0 deletions test/updaters/python-file-with-version.ts
Expand Up @@ -57,3 +57,22 @@ describe('project/__init__.py', () => {
});
});
});

describe('src/project/__init__.py', () => {
describe('updateContent', () => {
it('updates version in src/project/__init__.py', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './src/project/__init__.py'),
'utf8'
).replace(/\r\n/g, '\n');
const version = new PythonFileWithVersion({
path: 'src/project/__init__.py',
changelogEntry: '',
version: '0.6.0',
packageName: '',
});
const newContent = version.updateContent(oldContent);
snapshot(newContent);
});
});
});

0 comments on commit 598667d

Please sign in to comment.