diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 29a8bd0..f3b59ea 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -68,6 +68,8 @@ jobs: max-parallel: 3 matrix: php: ["7.4", "8.0", "8.1", "8.2", "8.3", "8.4"] + stability: [prefer-lowest, prefer-stable] + machine: [windows-latest, ubuntu-latest] steps: - name: Checkout code diff --git a/app/Commander.php b/app/Commander.php index a27b5f1..9ef0cbf 100644 --- a/app/Commander.php +++ b/app/Commander.php @@ -6,6 +6,7 @@ use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; +use Syntatis\Version\CLI\Commands\GreaterThanCommand; use Syntatis\Version\CLI\Commands\IncrementCommand; use Syntatis\Version\CLI\Commands\ValidateCommand; @@ -15,7 +16,7 @@ final class Commander extends Application public function __construct() { - parent::__construct('Ver', self::VERSION); + parent::__construct('Version', self::VERSION); $this->addCommands($this->getCommands()); } @@ -29,6 +30,7 @@ private function getCommands(): array return [ new IncrementCommand(), new ValidateCommand(), + new GreaterThanCommand(), ]; } } diff --git a/app/Commands/GreaterThanCommand.php b/app/Commands/GreaterThanCommand.php new file mode 100644 index 0000000..70a6280 --- /dev/null +++ b/app/Commands/GreaterThanCommand.php @@ -0,0 +1,75 @@ +setName('gt'); + $this->setDescription('Compare if a version is greater than another'); + $this->addArgument('version-a', InputArgument::REQUIRED, 'First version to compare'); + $this->addArgument('version-b', InputArgument::REQUIRED, 'Second version to compare against the first'); + $this->setHelp(<<<'HELP' + This command compares two versions and checks if the first version is greater than the second. + + Usage: + version gt 1.0.0 0.9.0 + version gt 2.1.0 2.1.0 + HELP); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $style = new SymfonyStyle($input, $output); + $versionA = $input->getArgument('version-a'); + $versionB = $input->getArgument('version-b'); + + try { + if (! is_string($versionA)) { + throw new InvalidArgumentType($versionA); + } + + if (! is_string($versionB)) { + throw new InvalidArgumentType($versionB); + } + + /** @var Version $a */ + $a = Version::fromString($versionA); + + /** @var Version $b */ + $b = Version::fromString($versionB); + + if ($a->isGreaterThan($b)) { + $style->success(sprintf("Version '%s' is greater than '%s'.", $a, $b)); + + return Command::SUCCESS; + } + + $style->error(sprintf("Version '%s' is not greater than '%s'.", $a, $b)); + + return Command::FAILURE; + } catch (Throwable $th) { + $style->error($th->getMessage()); + + return Command::FAILURE; + } + } +} diff --git a/app/Commands/IncrementCommand.php b/app/Commands/IncrementCommand.php index e79e7cf..bdd39db 100644 --- a/app/Commands/IncrementCommand.php +++ b/app/Commands/IncrementCommand.php @@ -25,9 +25,8 @@ final class IncrementCommand extends Command protected function configure(): void { $this->setName('increment'); - $this->setDescription('Increment a version.'); - $this->setHelp('This command increments the provided version by the specified part (major, minor, patch).'); - $this->setAliases(['incr', 'bump']); + $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('build', 'b', InputArgument::OPTIONAL, 'Build metadata to append to the version'); diff --git a/app/Commands/ValidateCommand.php b/app/Commands/ValidateCommand.php index f76b65a..7d62b47 100644 --- a/app/Commands/ValidateCommand.php +++ b/app/Commands/ValidateCommand.php @@ -24,7 +24,7 @@ final class ValidateCommand extends Command protected function configure(): void { $this->setName('validate'); - $this->setDescription('Validate a version.'); + $this->setDescription('Validate a version'); $this->setHelp('This command checks if the provided value is a valid Semantic Version (SemVer) version format.'); $this->setAliases(['val']); $this->addArgument('version', InputArgument::REQUIRED, 'Version to validate'); diff --git a/composer.json b/composer.json index ee16d62..ab86dda 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,11 @@ { "name": "syntatis/version-cli", "description": "Validate, increment, and compare SemVer-compliant version number with CLI", - "keywords": ["version", "cli", "semver"], + "keywords": [ + "version", + "cli", + "semver" + ], "authors": [ { "name": "Thoriq Firdaus" @@ -57,5 +61,8 @@ }, "preferred-install": "dist", "sort-packages": true - } + }, + "non-feature-branches": [ + "dependabot/*" + ] } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 6fcd983..fa14e6c 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,7 +2,7 @@ parameters: level: 10 paths: - app - - tests/app + - tests/phpunit ignoreErrors: - identifier: missingType.iterableValue diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 01f6e31..ad87e4c 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -11,8 +11,8 @@ printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printer" > - - tests/app/ + + tests/phpunit/ diff --git a/tests/phpunit/Commands/GreaterThanCommandTest.php b/tests/phpunit/Commands/GreaterThanCommandTest.php new file mode 100644 index 0000000..d18bc63 --- /dev/null +++ b/tests/phpunit/Commands/GreaterThanCommandTest.php @@ -0,0 +1,44 @@ +commander = new Commander(); + } + + /** @dataProvider dataComparison */ + public function testComparison(string $versionA, string $versionB, string $expect): void + { + $tester = new CommandTester($this->commander->get('gt')); + $tester->execute(['version-a' => $versionA, 'version-b' => $versionB]); + + self::assertStringContainsString( + $expect, + $tester->getDisplay(), + ); + } + + public static function dataComparison(): iterable + { + yield ['1.0.0', '2.0.0', sprintf("[ERROR] Version '%s' is not greater than '%s'.", '1.0.0', '2.0.0')]; + yield ['2.0.0', '1.0.0', sprintf("[OK] Version '%s' is greater than '%s'.", '2.0.0', '1.0.0')]; + yield ['1.0.0', '1.0.0', sprintf("[ERROR] Version '%s' is not greater than '%s'.", '1.0.0', '1.0.0')]; + yield ['1.0.0-alpha.1', '1.0.0-alpha.2', sprintf("[ERROR] Version '%s' is not greater than '%s'.", '1.0.0-alpha.1', '1.0.0-alpha.2')]; + yield ['1.0.0-alpha.2', '1.0.0-alpha.1', sprintf("[OK] Version '%s' is greater than '%s'.", '1.0.0-alpha.2', '1.0.0-alpha.1')]; + } +} diff --git a/tests/app/Commands/IncrementCommandTest.php b/tests/phpunit/Commands/IncrementCommandTest.php similarity index 100% rename from tests/app/Commands/IncrementCommandTest.php rename to tests/phpunit/Commands/IncrementCommandTest.php diff --git a/tests/app/Commands/ValidateCommandTest.php b/tests/phpunit/Commands/ValidateCommandTest.php similarity index 100% rename from tests/app/Commands/ValidateCommandTest.php rename to tests/phpunit/Commands/ValidateCommandTest.php