Skip to content
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
5 changes: 2 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"editor.defaultFormatter": "vscode.json-language-features"
},
"[php]": {
"editor.defaultFormatter": "valeryanm.vscode-phpsab"
"editor.defaultFormatter": "wongjn.php-sniffer"
},
"files.exclude": {
"**/.git": true,
Expand All @@ -92,6 +92,5 @@
"**/CVS/**"
],
"intelephense.compatibility.preferPsalmPhpstanPrefixedAnnotations": true,
"phpsab.snifferMode": "onSave",
"phpsab.snifferShowSources": true,
"phpSniffer.run": "onSave",
}
31 changes: 28 additions & 3 deletions app/Commands/IncrementCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
use Throwable;
use Version\Version;

use function is_numeric;
use function is_string;
use function sprintf;
use function trim;

final class IncrementCommand extends Command
{
Expand All @@ -28,7 +30,7 @@ protected function configure(): void
$this->setDescription('Increment a version');
$this->setAliases(['incr']);
$this->addArgument('version', InputArgument::REQUIRED, 'Version to increment');
$this->addOption('part', 'p', InputArgument::OPTIONAL, 'Part to increment (major, minor, patch)', 'patch');
$this->addOption('part', 'p', InputArgument::OPTIONAL, 'Part to increment (major, minor, patch, and prerelease)', 'patch');
$this->addOption('build', 'b', InputArgument::OPTIONAL, 'Build metadata to append to the version');
$this->addOption('pre', null, InputArgument::OPTIONAL, 'Pre-release identifier to append to the version');
$this->setHelp(<<<'HELP'
Expand Down Expand Up @@ -104,11 +106,34 @@ private function increment(Version $version, string $part, $pre = null, $build =
$version = $version->incrementPatch();
break;

case 'prerelease':
$currentPrerelease = $version->getPreRelease();

if ($currentPrerelease === null) {
throw new InvalidArgumentException('Unable to increment prerelease on a version without a prerelease tag.');
}

if ($pre !== null && is_string($pre) && $pre !== '') {
throw new InvalidArgumentException('Specifying a prerelease tag when incrementing the prerelease part is not allowed.');
}

$identifier = $currentPrerelease->getIdentifiers();
$preTag = isset($identifier[0]) && is_string($identifier[0]) ? trim($identifier[0]) : null;

if ($preTag === null || $preTag === '') {
throw new InvalidArgumentException('Unable to determine the prerelease tag for incrementing.');
}

$preVersion = isset($identifier[1]) && is_numeric($identifier[1]) ? (int) $identifier[1] : 0;
$version = $version->withPreRelease(sprintf('%s.%d', $preTag, $preVersion + 1));

break;

default:
throw new InvalidArgumentException(sprintf("Invalid part '%s' provided. Expected 'major', 'minor', or 'patch'.", $part));
throw new InvalidArgumentException(sprintf("Invalid part '%s' provided. Expected 'major', 'minor', 'patch', or 'prerelease'.", $part));
}

if (is_string($pre) && $pre !== '') {
if ($part !== 'prerelease' && is_string($pre) && $pre !== '') {
$version = $version->withPreRelease($pre);
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"autoload-dev": {
"psr-4": {
"Syntatis\\Tests\\": "tests/"
"Syntatis\\Tests\\": ["tests/", "tests/phpunit/"]
}
},
"require": {
Expand Down
12 changes: 11 additions & 1 deletion tests/bashunit/test_increment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function test_increment_major() {
}

# @data_provider data_increment_with_pre_release
function test_increment_with_pre_release() {
function test_increment_with_prerelease() {
ver=$(bin/version increment $1 --pre alpha);
assert_equals $ver $2;
}
Expand All @@ -34,6 +34,16 @@ function test_increment_with_pre_release_and_build() {
assert_equals $ver $2;
}

# @data_provider data_increment_prerelease
function test_increment_with_prerelease_and_build() {
ver=$(bin/version increment $1 --part prerelease);
assert_equals $ver $2;
}

function data_increment_prerelease() {
echo "1.0.0-beta" "1.0.0-beta.1";
}

function data_increment_patch() {
echo "0.0.0" "0.0.1";
echo "1.0.0" "1.0.1";
Expand Down
44 changes: 44 additions & 0 deletions tests/phpunit/Commands/CommandTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Syntatis\Tests\Commands;

use PHPUnit\Framework\TestCase;

use function preg_replace;
use function str_replace;
use function trim;

abstract class CommandTestCase extends TestCase
{
protected static function normalizeOutput(string $value): string
{
$output = self::canonicalize($value);
$output = str_replace("\n", ' ', $output);
$output = preg_replace('/\s+/', ' ', $output);

if ($output === null) {
return $value;
}

return trim($output);
}

protected static function canonicalize(string $str): string
{
// normalize EOL style
$str = str_replace("\r\n", "\n", $str);

// trim newlines at end
$str = rtrim($str, "\n");

// remove trailing whitespace on all lines
$lines = explode("\n", $str);
$lines = array_map(static function ($line): string {
return rtrim($line, " \t");
}, $lines);

return implode("\n", $lines);
}
}
5 changes: 3 additions & 2 deletions tests/phpunit/Commands/EqualCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace Syntatis\Tests\Commands;

use Syntatis\Tests\Commands\CommandTestCase;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Syntatis\Version\CLI\Commander;

use function sprintf;

class EqualCommandTest extends TestCase
class EqualCommandTest extends CommandTestCase
{
private Commander $commander;
private CommandTester $tester;
Expand All @@ -30,7 +31,7 @@ public function testComparison(string $versionA, string $versionB, string $expec

self::assertStringContainsString(
$expect,
$this->tester->getDisplay(),
self::normalizeOutput($this->tester->getDisplay()),
);
}

Expand Down
5 changes: 3 additions & 2 deletions tests/phpunit/Commands/GreaterThanCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace Syntatis\Tests\Commands;

use Syntatis\Tests\Commands\CommandTestCase;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Syntatis\Version\CLI\Commander;

use function sprintf;

class GreaterThanCommandTest extends TestCase
class GreaterThanCommandTest extends CommandTestCase
{
private Commander $commander;
private CommandTester $tester;
Expand All @@ -30,7 +31,7 @@ public function testComparison(string $versionA, string $versionB, string $expec

self::assertStringContainsString(
$expect,
$this->tester->getDisplay(),
self::normalizeOutput($this->tester->getDisplay()),
);
}

Expand Down
54 changes: 47 additions & 7 deletions tests/phpunit/Commands/IncrementCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace Syntatis\Tests\Commands;

use Syntatis\Tests\Commands\CommandTestCase;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Syntatis\Version\CLI\Commander;

use function sprintf;

class IncrementCommandTest extends TestCase
class IncrementCommandTest extends CommandTestCase
{
private Commander $commander;
private CommandTester $tester;
Expand All @@ -30,7 +31,7 @@ public function testInvalidVersionArgument(string $version): void

self::assertStringContainsString(
sprintf("[ERROR] Version string '%s' is not valid and cannot be parsed", $version),
$this->tester->getDisplay(),
self::normalizeOutput($this->tester->getDisplay()),
);
}

Expand All @@ -40,7 +41,7 @@ public function testIncrementPatch(): void

self::assertStringContainsString(
'1.0.1',
$this->tester->getDisplay(),
self::normalizeOutput($this->tester->getDisplay()),
);
}

Expand All @@ -50,7 +51,7 @@ public function testIncrementMinor(): void

self::assertStringContainsString(
'1.1.0',
$this->tester->getDisplay(),
self::normalizeOutput($this->tester->getDisplay()),
);
}

Expand All @@ -60,7 +61,7 @@ public function testIncrementMajor(): void

self::assertStringContainsString(
'2.0.0',
$this->tester->getDisplay(),
self::normalizeOutput($this->tester->getDisplay()),
);
}

Expand All @@ -70,7 +71,7 @@ public function testIncrementWithBuildMetadata(): void

self::assertStringContainsString(
'1.0.1+123',
$this->tester->getDisplay(),
self::normalizeOutput($this->tester->getDisplay()),
);
}

Expand All @@ -80,7 +81,46 @@ public function testIncrementWithPreRelease(): void

self::assertStringContainsString(
'1.0.1-beta',
$this->tester->getDisplay(),
self::normalizeOutput($this->tester->getDisplay()),
);
}

/**
* @dataProvider dataIncrementPrerelease
*/
public function testIncrementPrerelease(string $version, string $expect): void
{
$this->tester->execute(['version' => $version, '--part' => 'prerelease']);

self::assertStringContainsString(
$expect,
self::normalizeOutput($this->tester->getDisplay()),
);
}

public static function dataIncrementPrerelease(): iterable
{
yield ['1.0.0-beta', '1.0.0-beta.1'];
yield ['1.0.0-alpha.1', '1.0.0-alpha.2'];
}

public function testIncrementPrereleaseWithPreTag(): void
{
$this->tester->execute(['version' => '1.0.0-beta', '--part' => 'prerelease', '--pre' => 'rc']);

self::assertStringContainsString(
"[ERROR] Specifying a prerelease tag when incrementing the prerelease part is not allowed.",
self::normalizeOutput($this->tester->getDisplay()),
);
}

public function testIncrementPrereleaseWithoutReleaseTag(): void
{
$this->tester->execute(['version' => '1.0.0', '--part' => 'prerelease']);

self::assertStringContainsString(
"[ERROR] Unable to increment prerelease on a version without a prerelease tag.",
self::normalizeOutput($this->tester->getDisplay()),
);
}

Expand Down
5 changes: 3 additions & 2 deletions tests/phpunit/Commands/LessThanCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace Syntatis\Tests\Commands;

use Syntatis\Tests\Commands\CommandTestCase;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Syntatis\Version\CLI\Commander;

use function sprintf;

class LessThanCommandTest extends TestCase
class LessThanCommandTest extends CommandTestCase
{
private Commander $commander;
private CommandTester $tester;
Expand All @@ -30,7 +31,7 @@ public function testComparison(string $versionA, string $versionB, string $expec

self::assertStringContainsString(
$expect,
$this->tester->getDisplay(),
self::normalizeOutput($this->tester->getDisplay()),
);
}

Expand Down
7 changes: 4 additions & 3 deletions tests/phpunit/Commands/ValidateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace Syntatis\Tests\Commands;

use Syntatis\Tests\Commands\CommandTestCase;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Syntatis\Version\CLI\Commander;

use function sprintf;

class ValidateCommandTest extends TestCase
class ValidateCommandTest extends CommandTestCase
{
private Commander $commander;
private CommandTester $tester;
Expand All @@ -30,7 +31,7 @@ public function testInvalidVersionArgument(string $version): void

self::assertStringContainsString(
sprintf("[ERROR] Version string '%s' is not valid and cannot be parsed", $version),
$this->tester->getDisplay(),
self::normalizeOutput($this->tester->getDisplay()),
);
}

Expand All @@ -41,7 +42,7 @@ public function testValidVersionArgument(string $version): void

self::assertStringContainsString(
sprintf("[OK] Version string '%s' is valid and can be parsed", $version),
$this->tester->getDisplay(),
self::normalizeOutput($this->tester->getDisplay()),
);
}

Expand Down