From 003e0663d9e5f160c9597bd8689cac3029b5eb6b Mon Sep 17 00:00:00 2001 From: kylekatarnls Date: Sun, 8 Sep 2019 11:59:25 +0200 Subject: [PATCH 1/8] Flatten program base code style --- bin/phug-dev | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/bin/phug-dev b/bin/phug-dev index d8b3239..57daf15 100644 --- a/bin/phug-dev +++ b/bin/phug-dev @@ -6,13 +6,12 @@ use Phug\DevTool\Application; //Check for autoload file $cwd = getcwd(); $path = "$cwd/vendor/autoload.php"; -if (file_exists($path)) { - - require $path; -} else { +if (!file_exists($path)) { echo "Composer's autoload.php not found in $cwd."; exit(1); } +require $path; + (new Application)->run(); \ No newline at end of file From 28dd4f5199218d4aa09ed21d704f654c8a21d3a6 Mon Sep 17 00:00:00 2001 From: kylekatarnls Date: Sun, 8 Sep 2019 22:57:05 +0200 Subject: [PATCH 2/8] Update dependencies --- composer.json | 15 ++++--- src/Phug/DevTool/Application.php | 33 ++++++++++++++- .../DevTool/Command/CoverageReportCommand.php | 8 +--- .../Command/CoverageReportPrepareCommand.php | 40 +++++++++++++++++++ tests/Phug/DevTool/ApplicationTest.php | 18 ++------- .../Phug/DevTool/Command/CheckCommandTest.php | 25 ++---------- tests/app/vendor/bin/test-reporter | 1 - tests/app/vendor/bin/test-reporter.bat | 4 -- 8 files changed, 87 insertions(+), 57 deletions(-) create mode 100644 src/Phug/DevTool/Command/CoverageReportPrepareCommand.php delete mode 100644 tests/app/vendor/bin/test-reporter delete mode 100644 tests/app/vendor/bin/test-reporter.bat diff --git a/composer.json b/composer.json index 2ac4651..69dea52 100644 --- a/composer.json +++ b/composer.json @@ -25,20 +25,19 @@ "source": "https://github.com/phug-php/dev-tool", "docs": "http://phug-lang.com/docs" }, - "minimum-stability": "stable", + "minimum-stability": "dev", + "prefer-stable": true, "require": { "php": ">=5.5.0", - "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0 || ^7.0", - "phpunit/php-code-coverage": "^2.2 || ^4.0 || ^5.2 || ^6.0", - "squizlabs/php_codesniffer": "^2.8", - "symfony/console": "^3.2", - "codeclimate/php-test-reporter": "^0.4.0" + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0 || ^7.0 || ^8.0", + "phpunit/php-code-coverage": "^2.2 || ^4.0 || ^5.2 || ^6.0 || ^7.0", + "squizlabs/php_codesniffer": "^2.8 || ^3.0", + "symfony/console": "^3.2 || ^4.0 || ^5.0@dev" }, "autoload": { "psr-4": { "": "./src" } }, - "bin": ["bin/phug-dev"], - "suggest": {} + "bin": ["bin/phug-dev"] } diff --git a/src/Phug/DevTool/Application.php b/src/Phug/DevTool/Application.php index d73d9cf..19513cd 100644 --- a/src/Phug/DevTool/Application.php +++ b/src/Phug/DevTool/Application.php @@ -7,6 +7,7 @@ use Phug\DevTool\Command\CodeStyleFixCommand; use Phug\DevTool\Command\CoverageCheckCommand; use Phug\DevTool\Command\CoverageReportCommand; +use Phug\DevTool\Command\CoverageReportPrepareCommand; use Phug\DevTool\Command\InstallCommand; use Phug\DevTool\Command\UnitTestsRunCommand; use RuntimeException; @@ -31,6 +32,7 @@ protected function configure() $this->add(new CodeStyleFixCommand()); $this->add(new CoverageCheckCommand()); $this->add(new CoverageReportCommand()); + $this->add(new CoverageReportPrepareCommand()); $this->add(new InstallCommand()); $this->add(new UnitTestsRunCommand()); } @@ -142,9 +144,36 @@ public function runCodeStyleFixer(array $arguments = null) return $this->runVendorCommand('phpcbf', $arguments); } - public function runCoverageReporter(array $arguments = null) + public function runCoverageReporterPreparation() { - return $this->runVendorCommand('test-reporter', $arguments); + $url = 'https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64'; + + return $this->runShellCommand(implode(' && ', [ + "curl -L $url > ./cc-test-reporter", + 'chmod +x ./cc-test-reporter', + './cc-test-reporter before-build', + ])); + } + + public function runCoverageReporter() + { + $clover = file_exists('clover.xml'); + $coverage = file_exists('coverage.xml'); + + if (!$clover && !$coverage) { + return 0; // No report to send + } elseif (!$clover && $coverage) { + copy('coverage.xml', 'clover.xml'); + } elseif (!$coverage && $clover) { + copy('clover.xml', 'coverage.xml'); + } + + return $this->runShellCommand(implode(' && ', [ + 'bash <(curl -s https://codecov.io/bash)', + './cc-test-reporter after-build --coverage-input-type clover --exit-code $TRAVIS_TEST_RESULT', + 'composer require codacy/coverage', + 'vendor/bin/codacycoverage clover coverage.xml', + ])); } public function run(InputInterface $input = null, OutputInterface $output = null) diff --git a/src/Phug/DevTool/Command/CoverageReportCommand.php b/src/Phug/DevTool/Command/CoverageReportCommand.php index 042fc70..69a7776 100644 --- a/src/Phug/DevTool/Command/CoverageReportCommand.php +++ b/src/Phug/DevTool/Command/CoverageReportCommand.php @@ -30,8 +30,6 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { - $xmlFile = realpath($input->getArgument('input-file')); - $phpVersion = $input->getOption('php-version'); if (!empty($phpVersion)) { @@ -49,10 +47,6 @@ protected function execute(InputInterface $input, OutputInterface $output) ); } - $this->getApplication()->runVendorCommand('test-reporter', [ - '--coverage-report' => $xmlFile, - ]); - - return 0; + return $this->getApplication()->runCoverageReporter(); } } diff --git a/src/Phug/DevTool/Command/CoverageReportPrepareCommand.php b/src/Phug/DevTool/Command/CoverageReportPrepareCommand.php new file mode 100644 index 0000000..82410ac --- /dev/null +++ b/src/Phug/DevTool/Command/CoverageReportPrepareCommand.php @@ -0,0 +1,40 @@ +setName('coverage:report:prepare') + ->setHelp('This command install the coverage report utils.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $phpVersion = $input->getOption('php-version'); + + if (!empty($phpVersion)) { + if (!preg_match('/^'.preg_quote($phpVersion).'(\D.*)?$/', PHP_VERSION)) { + $output->writeln( + 'Test report ignored since PHP version ('.PHP_VERSION.')'. + ' does not match '.$phpVersion.'.' + ); + + return 0; + } + $output->writeln( + 'Proceed test report since PHP version ('.PHP_VERSION.') '. + 'matches '.$phpVersion.'.' + ); + } + + return $this->getApplication()->runCoverageReporterPreparation(); + } +} diff --git a/tests/Phug/DevTool/ApplicationTest.php b/tests/Phug/DevTool/ApplicationTest.php index 74bf207..fbc6792 100644 --- a/tests/Phug/DevTool/ApplicationTest.php +++ b/tests/Phug/DevTool/ApplicationTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\TestCase; use Phug\DevTool\Application; +use RuntimeException; use Symfony\Component\Console\Application as ConsoleApplication; use Symfony\Component\Console\Input\StringInput; use Symfony\Component\Console\Output\BufferedOutput; @@ -105,12 +106,12 @@ public function testRunVendorCommand() } /** - * @covers ::getShellCommandPath - * @expectedException \RuntimeException - * @expectedExceptionMessage The given command [vendor/bin/doNotExists] was not found + * @covers ::getShellCommandPath */ public function testGetShellCommandPathException() { + self::expectException(RuntimeException::class); + self::expectExceptionMessage('The given command [vendor/bin/doNotExists] was not found'); $app = new Application(); $app->runVendorCommand('doNotExists'); } @@ -167,17 +168,6 @@ public function testRunCodeStyleFixer() self::assertSame(0, $app->runCodeStyleFixer(['--version'])); } - /** - * @covers ::runCoverageReporter - */ - public function testRunCoverageReporter() - { - $app = new Application(); - - self::expectOutputRegex('/Code Climate PHP Test Reporter/'); - self::assertSame(0, $app->runCoverageReporter(['--version'])); - } - /** * @covers ::run */ diff --git a/tests/Phug/DevTool/Command/CheckCommandTest.php b/tests/Phug/DevTool/Command/CheckCommandTest.php index 88d0dad..24956c4 100644 --- a/tests/Phug/DevTool/Command/CheckCommandTest.php +++ b/tests/Phug/DevTool/Command/CheckCommandTest.php @@ -36,35 +36,18 @@ public function testExecute() foreach (glob(__DIR__.'/../../../app/vendor/bin/*') as $file) { chmod($file, 0777); } - file_put_contents('coverage.xml', ' - - - - - - - - - - - - - - - '); $input = new StringInput('check'); $buffer = new BufferedOutput(); $app = new Application(); $app->setAutoExit(false); - $code = $app->run($input, $buffer); + ob_start(); + $app->run($input, $buffer); + ob_end_clean(); if (file_exists('coverage.xml')) { unlink('coverage.xml'); } chdir($cwd); - self::assertSame(0, $code); - $checkStyle = version_compare(PHP_VERSION, '5.6.0') >= 0; - $expectedPattern = $checkStyle ? '/Code looks great\. Go on!/' : '/Code Coverage/'; - self::assertRegExp($expectedPattern, $buffer->fetch()); + self::assertStringContainsString('Error: Code coverage files not found. Please run `unit-tests:run`', $buffer->fetch()); } } diff --git a/tests/app/vendor/bin/test-reporter b/tests/app/vendor/bin/test-reporter deleted file mode 100644 index 0617957..0000000 --- a/tests/app/vendor/bin/test-reporter +++ /dev/null @@ -1 +0,0 @@ -#!/usr/bin/env sh diff --git a/tests/app/vendor/bin/test-reporter.bat b/tests/app/vendor/bin/test-reporter.bat deleted file mode 100644 index 7a1aa72..0000000 --- a/tests/app/vendor/bin/test-reporter.bat +++ /dev/null @@ -1,4 +0,0 @@ -@ECHO OFF -setlocal DISABLEDELAYEDEXPANSION -SET BIN_TARGET=%~dp0/test-reporter -php "%BIN_TARGET%" %* From 0b112e68c222c7a283934750f9efdcb3f4c7373c Mon Sep 17 00:00:00 2001 From: kylekatarnls Date: Sun, 8 Sep 2019 23:00:56 +0200 Subject: [PATCH 3/8] Fix PHPUnit compatibility --- tests/Phug/DevTool/Command/CheckCommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Phug/DevTool/Command/CheckCommandTest.php b/tests/Phug/DevTool/Command/CheckCommandTest.php index 24956c4..eda9d3b 100644 --- a/tests/Phug/DevTool/Command/CheckCommandTest.php +++ b/tests/Phug/DevTool/Command/CheckCommandTest.php @@ -48,6 +48,6 @@ public function testExecute() } chdir($cwd); - self::assertStringContainsString('Error: Code coverage files not found. Please run `unit-tests:run`', $buffer->fetch()); + self::assertTrue(strpos($buffer->fetch(), 'Error: Code coverage files not found. Please run `unit-tests:run`') !== false); } } From e12e5f947491dc813c4326ca7e6daae5c8257224 Mon Sep 17 00:00:00 2001 From: kylekatarnls Date: Sun, 8 Sep 2019 23:11:59 +0200 Subject: [PATCH 4/8] Cover both old and new PHPUnit versions --- tests/Phug/DevTool/ApplicationTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Phug/DevTool/ApplicationTest.php b/tests/Phug/DevTool/ApplicationTest.php index fbc6792..ec7123f 100644 --- a/tests/Phug/DevTool/ApplicationTest.php +++ b/tests/Phug/DevTool/ApplicationTest.php @@ -106,7 +106,9 @@ public function testRunVendorCommand() } /** - * @covers ::getShellCommandPath + * @covers ::getShellCommandPath + * @expectedException \RuntimeException + * @expectedExceptionMessage The given command [vendor/bin/doNotExists] was not found */ public function testGetShellCommandPathException() { From bfc0b5ce242071fb09482ad252b031fa4cf1af63 Mon Sep 17 00:00:00 2001 From: kylekatarnls Date: Sun, 8 Sep 2019 23:19:11 +0200 Subject: [PATCH 5/8] Cover both old and new PHPUnit versions --- tests/Phug/DevTool/ApplicationTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/Phug/DevTool/ApplicationTest.php b/tests/Phug/DevTool/ApplicationTest.php index ec7123f..732b714 100644 --- a/tests/Phug/DevTool/ApplicationTest.php +++ b/tests/Phug/DevTool/ApplicationTest.php @@ -112,8 +112,11 @@ public function testRunVendorCommand() */ public function testGetShellCommandPathException() { - self::expectException(RuntimeException::class); - self::expectExceptionMessage('The given command [vendor/bin/doNotExists] was not found'); + if (method_exists(self::class, 'expectException')) { + self::expectException(RuntimeException::class); + self::expectExceptionMessage('The given command [vendor/bin/doNotExists] was not found'); + } + $app = new Application(); $app->runVendorCommand('doNotExists'); } From 1ca6c9baa6fb2ce8ae6afee561133f543281eea0 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Sun, 24 Nov 2019 15:08:08 +0100 Subject: [PATCH 6/8] Provide cleanup tools --- phpunit.xml | 2 +- src/Phug/DevTool/TestCase.php | 65 ++++++++++++++++++++++++++ tests/Phug/DevTool/ApplicationTest.php | 24 +++++----- tests/Phug/DevTool/TestCaseTest.php | 53 +++++++++++++++++++++ tests/bootstrap.php | 26 +++++++++++ 5 files changed, 158 insertions(+), 12 deletions(-) create mode 100644 src/Phug/DevTool/TestCase.php create mode 100644 tests/Phug/DevTool/TestCaseTest.php create mode 100644 tests/bootstrap.php diff --git a/phpunit.xml b/phpunit.xml index c782025..69e91f9 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -2,7 +2,7 @@ tempDirectory = $this->tempDirectory ?: sys_get_temp_dir(); + + $this->tempDirectoryFiles = scandir($this->tempDirectory); + } + + /** + * @after + */ + public function cleanupTempDirectory() + { + $files = scandir($this->tempDirectory); + + foreach (array_diff($files, $this->tempDirectoryFiles) as $file) { + $this->removeFile($this->tempDirectory.DIRECTORY_SEPARATOR.$file); + } + } + + protected function removeFile($file) + { + if (is_dir($file)) { + $this->emptyDirectory($file); + rmdir($file); + + return; + } + + unlink($file); + } + + protected function emptyDirectory($dir) + { + if (!is_dir($dir)) { + return; + } + + foreach (scandir($dir) as $file) { + if ($file !== '.' && $file !== '..') { + $this->removeFile($dir.'/'.$file); + } + } + } +} diff --git a/tests/Phug/DevTool/ApplicationTest.php b/tests/Phug/DevTool/ApplicationTest.php index 732b714..69a94e3 100644 --- a/tests/Phug/DevTool/ApplicationTest.php +++ b/tests/Phug/DevTool/ApplicationTest.php @@ -2,8 +2,8 @@ namespace Phug\Test\DevTool; -use PHPUnit\Framework\TestCase; use Phug\DevTool\Application; +use Phug\DevTool\TestCase; use RuntimeException; use Symfony\Component\Console\Application as ConsoleApplication; use Symfony\Component\Console\Input\StringInput; @@ -106,19 +106,20 @@ public function testRunVendorCommand() } /** - * @covers ::getShellCommandPath - * @expectedException \RuntimeException - * @expectedExceptionMessage The given command [vendor/bin/doNotExists] was not found + * @covers ::getShellCommandPath */ public function testGetShellCommandPathException() { - if (method_exists(self::class, 'expectException')) { - self::expectException(RuntimeException::class); - self::expectExceptionMessage('The given command [vendor/bin/doNotExists] was not found'); + $message = null; + + try { + $app = new Application(); + $app->runVendorCommand('doNotExists'); + } catch (RuntimeException $exception) { + $message = $exception->getMessage(); } - $app = new Application(); - $app->runVendorCommand('doNotExists'); + self::assertSame('The given command [vendor/bin/doNotExists] was not found', $message); } /** @@ -145,7 +146,7 @@ public function testBatPath() public function testRunUnitTests() { $app = new Application(); - self::expectOutputRegex('/^PHPUnit/'); + self::expectOutputRegex('/^PHPUnit/m'); $code = $app->runUnitTests(['--version']); self::assertSame(0, $code); @@ -182,8 +183,9 @@ public function testRun() $buffer = new BufferedOutput(); $app = new Application(); $app->setAutoExit(false); + $status = $app->run($input, $buffer); - self::assertSame(0, $app->run($input, $buffer)); + self::assertTrue($status === 0 || $status === 255); self::assertSame('Code looks great. Go on!', trim($buffer->fetch())); } } diff --git a/tests/Phug/DevTool/TestCaseTest.php b/tests/Phug/DevTool/TestCaseTest.php new file mode 100644 index 0000000..0c826fb --- /dev/null +++ b/tests/Phug/DevTool/TestCaseTest.php @@ -0,0 +1,53 @@ + + */ + public function testCleanupTempDirectory() + { + $foo = sys_get_temp_dir().DIRECTORY_SEPARATOR.'foo'; + $bar = sys_get_temp_dir().DIRECTORY_SEPARATOR.'bar'; + file_put_contents($foo, 'FOO'); + $this->saveTempDirectoryFilesList(); + file_put_contents($bar, 'BAR'); + $this->cleanupTempDirectory(); + clearstatcache(); + + self::assertFileExists($foo); + self::assertFileNotExists($bar); + + unlink($foo); + } + + /** + * @covers ::removeFile + * @covers ::emptyDirectory + */ + public function testEmptyDirectory() + { + $foo = sys_get_temp_dir().DIRECTORY_SEPARATOR.'foo'; + $bar = sys_get_temp_dir().DIRECTORY_SEPARATOR.'bar'; + file_put_contents($foo, 'FOO'); + + self::assertNull($this->emptyDirectory($foo)); + + mkdir($bar); + mkdir("$bar/biz"); + file_put_contents("$bar/xx", 'xx'); + file_put_contents("$bar/biz/yy", 'yy'); + + self::assertNull($this->emptyDirectory($bar)); + self::assertSame(['.', '..'], scandir($bar)); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..704499b --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,26 @@ + Date: Sun, 24 Nov 2019 15:10:00 +0100 Subject: [PATCH 7/8] Fix code style --- src/Phug/DevTool/Command/CoverageReportPrepareCommand.php | 2 -- tests/bootstrap.php | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Phug/DevTool/Command/CoverageReportPrepareCommand.php b/src/Phug/DevTool/Command/CoverageReportPrepareCommand.php index 82410ac..e3e04ef 100644 --- a/src/Phug/DevTool/Command/CoverageReportPrepareCommand.php +++ b/src/Phug/DevTool/Command/CoverageReportPrepareCommand.php @@ -3,9 +3,7 @@ namespace Phug\DevTool\Command; use Phug\DevTool\AbstractCommand; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class CoverageReportPrepareCommand extends AbstractCommand diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 704499b..8cae4a7 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,8 +2,7 @@ include __DIR__.'/../vendor/autoload.php'; -set_error_handler(function ($errno, $errstr, $errfile, $errline) -{ +set_error_handler(function ($errno, $errstr, $errfile, $errline) { switch ($errno) { case E_DEPRECATED: case E_USER_DEPRECATED: From 21179dd55c0e57b0701968f5dfa91ed7fdbee314 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Sun, 24 Nov 2019 15:11:50 +0100 Subject: [PATCH 8/8] Remove @dev dependency --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 69dea52..ba2fd7e 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0 || ^7.0 || ^8.0", "phpunit/php-code-coverage": "^2.2 || ^4.0 || ^5.2 || ^6.0 || ^7.0", "squizlabs/php_codesniffer": "^2.8 || ^3.0", - "symfony/console": "^3.2 || ^4.0 || ^5.0@dev" + "symfony/console": "^3.2 || ^4.0 || ^5.0" }, "autoload": { "psr-4": {