Skip to content

Commit

Permalink
Increment metaver for patch increment
Browse files Browse the repository at this point in the history
When incrementing with ‘patch’ and the current stability is unstable increment the metaver while keeping
all other segments intact.

1.0.0-alpha1 becomes 1.0.0-alpha2 for patch.

BC break: Previously this threw an exception.
  • Loading branch information
sstok committed Nov 24, 2017
1 parent bfbd7c3 commit df2b5eb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ $newVersion = $version->increase('rc'); // v1.4.0-RC1

// ...
// Increasing minor or patch is prohibited until the meta-ver (alpha,beta,rc) is 0
// For patch this resolves to "next".

$version = Version::fromString('v1.4.0-BETA1');
$newVersion = $version->increase('beta'); // v1.4.0-BETA2
$newVersion = $version->increase('rc'); // v1.4.0-RC1
$newVersion = $version->increase('major'); // v1.4.0
$newVersion = $version->increase('next'); // v1.4.0-BETA2
$newVersion = $version->increase('patch'); // v1.4.0-BETA2
$newVersion = $version->increase('stable'); // v1.4.0

// Version validation
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@
"psr-4": {
"Rollerworks\\Component\\Version\\Tests\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "0.2-dev"
}
}
}
4 changes: 2 additions & 2 deletions src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function equalTo(Version $second): bool
*
* Note. Using 'major' on a beta release will create a stable release
* for that major version. Using 'stable' on an existing stable will increase
* minor.
* minor. Using 'patch' on an unstable release will increase the metaver instead.
*
* @param string $stability Eg. alpha, beta, rc, stable, major, minor, patch
*
Expand All @@ -179,7 +179,7 @@ public function increase(string $stability): Version
switch ($stability) {
case 'patch':
if ($this->major > 0 && $this->metaver > 0) {
throw new \InvalidArgumentException('Cannot increase patch for an unstable version.');
return $this->increaseNext();
}

return new self($this->major, $this->minor, $this->patch + 1, 3);
Expand Down
12 changes: 9 additions & 3 deletions tests/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ public function provideExpectedIncreasedVersion(): array
return [
'patch with patch 0' => ['0.1.0', '0.1.1', 'patch'],
'patch with patch 1' => ['0.1.1', '0.1.2', 'patch'],
'patch with patch stable' => ['1.0.0', '1.0.1', 'patch'],

// Patch with unstable. Increase metaver instead
'patch with patch alpha' => ['1.0.0-alpha1', '1.0.0-alpha2', 'patch'],
'patch with patch beta' => ['1.0.0-beta1', '1.0.0-beta2', 'patch'],
'patch with patch rc' => ['1.0.0-rc1', '1.0.0-rc2', 'patch'],

// Minor, patch must be reset
'minor with patch 0' => ['0.1.0', '0.2.0', 'minor'],
Expand Down Expand Up @@ -196,11 +202,11 @@ public function it_increases_to_next_version(string $current, string $expected,
}

/** @test */
public function it_cannot_increases_patch_on_unstable()
public function it_cannot_increases_for_unsupported()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot increase patch for an unstable version.');
$this->expectExceptionMessage('Unknown stability "next-stable", accepts "alpha", "beta", "rc", "stable", "major", "next", "minor", "patch".');

Version::fromString('1.0.0-beta1')->increase('patch');
Version::fromString('1.0.0-beta1')->increase('next-stable');
}
}

0 comments on commit df2b5eb

Please sign in to comment.