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

fix: dart pubspec updater with prerelease #2406

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
46 changes: 41 additions & 5 deletions __snapshots__/pubspec-yaml.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
exports['PubspecYaml updateContent leaves malformatted build numbers alone in pubspec.yaml file 1'] = `
exports['PubspecYaml updateContent updates version in ./pubspec.yaml 1'] = `
name: hello_world
description: Hello World
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 0.6.0

environment:
sdk: '>=2.12.0 <3.0.0'

`

exports['PubspecYaml updateContent updates version in ./pubspec_with_build_no.yaml 1'] = `
name: hello_world
description: Hello World
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 0.6.0+13

environment:
sdk: '>=2.12.0 <3.0.0'

`

exports['PubspecYaml updateContent updates version in ./pubspec_with_build_no_bad.yaml 1'] = `
name: hello_world
description: Hello World
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
Expand All @@ -10,24 +34,36 @@ environment:

`

exports['PubspecYaml updateContent updates version in pubspec.yaml file 1'] = `
exports['PubspecYaml updateContent updates version in ./pubspec_with_prerelease.yaml 1'] = `
name: hello_world
description: Hello World
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 0.6.0
version: 0.5.0-dev02

environment:
sdk: '>=2.12.0 <3.0.0'

`

exports['PubspecYaml updateContent updates version with build number in pubspec.yaml file 1'] = `
exports['PubspecYaml updateContent updates version in ./pubspec_with_prerelease_and_build_no.yaml 1'] = `
name: hello_world
description: Hello World
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 0.6.0+13
version: 0.5.0-dev02+13

environment:
sdk: '>=2.12.0 <3.0.0'

`

exports['PubspecYaml updateContent updates version in ./pubspec_with_prerelease_and_build_no_bad.yaml 1'] = `
name: hello_world
description: Hello World
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 0.5.0-dev02+abc

environment:
sdk: '>=2.12.0 <3.0.0'
Expand Down
7 changes: 5 additions & 2 deletions src/updaters/dart/pubspec-yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ export class PubspecYaml extends DefaultUpdater {
*/

updateContent(content: string, logger: Logger = defaultLogger): string {
const oldVersion = content.match(/^version: ([0-9.]+)\+?(.*$)/m);
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
const oldVersion = content.match(
/^version: ((?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?)(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/m
);
let buildNumber = '';

if (oldVersion) {
buildNumber = oldVersion[2];
buildNumber = oldVersion[2] || '';
const parsedBuild = parseInt(buildNumber);
if (!isNaN(parsedBuild)) {
buildNumber = `+${parsedBuild + 1}`;
Expand Down
8 changes: 8 additions & 0 deletions test/updaters/fixtures/pubspec_with_prerelease.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: hello_world
description: Hello World
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 0.5.0-dev01

environment:
sdk: '>=2.12.0 <3.0.0'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: hello_world
description: Hello World
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 0.5.0-dev01+12

environment:
sdk: '>=2.12.0 <3.0.0'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: hello_world
description: Hello World
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 0.5.0-dev01+abc

environment:
sdk: '>=2.12.0 <3.0.0'
64 changes: 31 additions & 33 deletions test/updaters/pubspec-yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,40 @@ import {Version} from '../../src/version';

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

describe('PubspecYaml', () => {
describe('updateContent', () => {
it('updates version in pubspec.yaml file', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './pubspec.yaml'),
'utf8'
).replace(/\r\n/g, '\n'); // required for windows
const version = new PubspecYaml({
version: Version.parse('0.6.0'),
});
const newContent = version.updateContent(oldContent);
snapshot(newContent);
});
interface TestCase {
filePath: string;
versionString: string;
}

it('updates version with build number in pubspec.yaml file', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './pubspec_with_build_no.yaml'),
'utf8'
).replace(/\r\n/g, '\n'); // required for windows
const version = new PubspecYaml({
version: Version.parse('0.6.0'),
});
const newContent = version.updateContent(oldContent);
snapshot(newContent);
});
const testCases: TestCase[] = [
{filePath: './pubspec.yaml', versionString: '0.6.0'},
{filePath: './pubspec_with_build_no.yaml', versionString: '0.6.0'},
{filePath: './pubspec_with_build_no_bad.yaml', versionString: '0.6.0'},
{filePath: './pubspec_with_prerelease.yaml', versionString: '0.5.0-dev02'},
{
filePath: './pubspec_with_prerelease_and_build_no.yaml',
versionString: '0.5.0-dev02',
},
{
filePath: './pubspec_with_prerelease_and_build_no_bad.yaml',
versionString: '0.5.0-dev02',
},
];

it('leaves malformatted build numbers alone in pubspec.yaml file', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './pubspec_with_build_no_bad.yaml'),
'utf8'
).replace(/\r\n/g, '\n'); // required for windows
const version = new PubspecYaml({
version: Version.parse('0.6.0'),
describe('PubspecYaml', () => {
describe('updateContent', () => {
testCases.forEach(({filePath, versionString}) => {
it(`updates version in ${filePath}`, async () => {
const oldContent = readFileSync(
resolve(fixturesPath, filePath),
'utf8'
).replace(/\r\n/g, '\n'); // required for windows
const version = new PubspecYaml({
version: Version.parse(versionString),
});
const newContent = version.updateContent(oldContent);
snapshot(newContent);
});
const newContent = version.updateContent(oldContent);
snapshot(newContent);
});
});
});
Loading