Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add updater for python readme #157

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/strategies/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import {PythonFileWithVersion} from '../updaters/python/python-file-with-version';
import {FileNotFoundError} from '../errors';
import {filterCommits} from '../util/filter-commits';
import {PythonReadme} from '../updaters/python/python-readme';

const CHANGELOG_SECTIONS = [
{type: 'feat', section: 'Features'},
Expand Down Expand Up @@ -79,6 +80,14 @@ export class Python extends BaseStrategy {
}),
});

updates.push({
path: this.addPath('README.md'),
createIfMissing: false,
updater: new PythonReadme({
version,
}),
});

const parsedPyProject = await this.getPyProject(
this.addPath('pyproject.toml')
);
Expand Down
30 changes: 30 additions & 0 deletions src/updaters/python/python-readme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {DefaultUpdater} from '../default';

/**
* Updates a README.md file
*/
export class PythonReadme extends DefaultUpdater {
/**
* Given initial file contents, return updated contents.
* @param {string} content The initial content
* @returns {string} The updated content
*/
updateContent(content: string): string {
// differs for windows vs. linux/ios
const newlineType = content.includes('\r\n') ? '\r\n' : '\n';

if (this.version.preRelease) {
// it's a pre-release, so add the pre-release flag if it's missing
return content.replace(
/^# install from PyPI\r?\n^pip install (\S+)$/gm,
`# install from PyPI${newlineType}pip install --pre $1`
);
} else {
// it's not a pre-release, so remove the pre-release flag if it's present
return content.replace(
/^# install from PyPI\r?\n^pip install --pre (\S+)$/gm,
`# install from PyPI${newlineType}pip install $1`
);
}
}
}
35 changes: 35 additions & 0 deletions test/strategies/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {SetupPy} from '../../src/updaters/python/setup-py';
import {Changelog} from '../../src/updaters/changelog';
import {ChangelogJson} from '../../src/updaters/changelog-json';
import snapshot = require('snap-shot-it');
import {PullRequestBody} from '../../src/util/pull-request-body';
import {PythonReadme} from '../../src/updaters/python/python-readme';

const sandbox = sinon.createSandbox();
const fixturesPath = './test/fixtures/strategies/python';
Expand Down Expand Up @@ -164,6 +166,39 @@ describe('Python', () => {
assertHasUpdate(updates, 'pyproject.toml', PyProjectToml);
});

it('finds and updates a README.md', async () => {
const strategy = new Python({
targetBranch: 'main',
github,
component: 'google-cloud-automl',
});
sandbox
.stub(github, 'getFileContentsOnBranch')
.resolves(
buildGitHubFileContent(
'./test/updaters/fixtures',
'README-python-pre.md'
)
);
sandbox.stub(github, 'findFilesByFilenameAndRef').resolves([]);
const release = await strategy.buildReleasePullRequest({
commits: COMMITS,
latestRelease: await strategy.buildRelease({
title: 'chore(main): release v1.2.3',
headBranchName: 'release-please/branches/main',
baseBranchName: 'main',
number: 1234,
body: new PullRequestBody([]).toString(),
labels: [],
files: [],
sha: 'abc123',
}),
});
const updates = release!.updates;

assertHasUpdate(updates, 'README.md', PythonReadme);
});

it('finds and updates a version.py file', async () => {
const strategy = new Python({
targetBranch: 'main',
Expand Down
6 changes: 6 additions & 0 deletions test/updaters/fixtures/README-python-no-pre.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Installation

```sh
# install from PyPI
pip install test-repo
```
6 changes: 6 additions & 0 deletions test/updaters/fixtures/README-python-pre.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Installation

```sh
# install from PyPI
pip install --pre test-repo
```
50 changes: 50 additions & 0 deletions test/updaters/python-readme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {readFileSync} from 'fs';
import {resolve} from 'path';
import {describe, it} from 'mocha';
import {expect} from 'chai';
import {Version} from '../../src/version';
import {PythonReadme} from '../../src/updaters/python/python-readme';

const fixturesPath = './test/updaters/fixtures';

describe('PythonReadme', () => {
const withPreFlag = readFileSync(
resolve(fixturesPath, 'README-python-pre.md'),
'utf8'
);

const withoutPreFlag = readFileSync(
resolve(fixturesPath, 'README-python-no-pre.md'),
'utf8'
);

it('makes no changes if there is a --pre on the install instructions in a README for a prerelease version', async () => {
const readmeUpdater = new PythonReadme({
version: Version.parse('0.6.0-alpha.1'),
});
expect(readmeUpdater.updateContent(withPreFlag)).to.equal(withPreFlag);
});

it('makes no changes if there is no --pre on the install instructions in a README for a non-prerelease version', async () => {
const readmeUpdater = new PythonReadme({
version: Version.parse('0.6.0'),
});
expect(readmeUpdater.updateContent(withoutPreFlag)).to.equal(
withoutPreFlag
);
});

it('adds --pre if there is no --pre on the install instructions in a README for a prerelease version', async () => {
const readmeUpdater = new PythonReadme({
version: Version.parse('0.6.0-alpha.1'),
});
expect(readmeUpdater.updateContent(withoutPreFlag)).to.equal(withPreFlag);
});

it('removes --pre if there is a --pre on the install instructions in a README for a non-prerelease version', async () => {
const readmeUpdater = new PythonReadme({
version: Version.parse('0.6.0'),
});
expect(readmeUpdater.updateContent(withPreFlag)).to.equal(withoutPreFlag);
});
});
Loading