From e55746b5aa0826e471adff76f917821c7912597c Mon Sep 17 00:00:00 2001 From: ronnnnn Date: Wed, 16 Oct 2024 09:09:24 +0900 Subject: [PATCH] fix: dart pubspec updater with prerelease --- __snapshots__/pubspec-yaml.js | 46 +++++++++++-- src/updaters/dart/pubspec-yaml.ts | 7 +- .../fixtures/pubspec_with_prerelease.yaml | 8 +++ .../pubspec_with_prerelease_and_build_no.yaml | 8 +++ ...spec_with_prerelease_and_build_no_bad.yaml | 8 +++ test/updaters/pubspec-yaml.ts | 64 +++++++++---------- 6 files changed, 101 insertions(+), 40 deletions(-) create mode 100644 test/updaters/fixtures/pubspec_with_prerelease.yaml create mode 100644 test/updaters/fixtures/pubspec_with_prerelease_and_build_no.yaml create mode 100644 test/updaters/fixtures/pubspec_with_prerelease_and_build_no_bad.yaml diff --git a/__snapshots__/pubspec-yaml.js b/__snapshots__/pubspec-yaml.js index be96c2ca1..e5c924f89 100644 --- a/__snapshots__/pubspec-yaml.js +++ b/__snapshots__/pubspec-yaml.js @@ -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 @@ -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' diff --git a/src/updaters/dart/pubspec-yaml.ts b/src/updaters/dart/pubspec-yaml.ts index 0e23f4160..665bb4431 100644 --- a/src/updaters/dart/pubspec-yaml.ts +++ b/src/updaters/dart/pubspec-yaml.ts @@ -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}`; diff --git a/test/updaters/fixtures/pubspec_with_prerelease.yaml b/test/updaters/fixtures/pubspec_with_prerelease.yaml new file mode 100644 index 000000000..74f1a6c48 --- /dev/null +++ b/test/updaters/fixtures/pubspec_with_prerelease.yaml @@ -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' diff --git a/test/updaters/fixtures/pubspec_with_prerelease_and_build_no.yaml b/test/updaters/fixtures/pubspec_with_prerelease_and_build_no.yaml new file mode 100644 index 000000000..1461e8d68 --- /dev/null +++ b/test/updaters/fixtures/pubspec_with_prerelease_and_build_no.yaml @@ -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' diff --git a/test/updaters/fixtures/pubspec_with_prerelease_and_build_no_bad.yaml b/test/updaters/fixtures/pubspec_with_prerelease_and_build_no_bad.yaml new file mode 100644 index 000000000..c55574441 --- /dev/null +++ b/test/updaters/fixtures/pubspec_with_prerelease_and_build_no_bad.yaml @@ -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' diff --git a/test/updaters/pubspec-yaml.ts b/test/updaters/pubspec-yaml.ts index bf5081c3e..4216068b6 100644 --- a/test/updaters/pubspec-yaml.ts +++ b/test/updaters/pubspec-yaml.ts @@ -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); }); }); });