From 05eeda1a94c82d8f226febe6194e5079a82cc334 Mon Sep 17 00:00:00 2001 From: Renato Mendes Figueiredo Date: Tue, 17 Mar 2015 08:54:13 -0300 Subject: [PATCH 1/6] Few improvements: - When using inside SF2 projects the bin path is at the same level as src path; - Few more things at travis install script --- .travis.yml | 4 +++- composer/bin/test-reporter | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e82c53e..3235dc8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,12 @@ language: php php: + - 5.4 - 5.5 + - 5.6 env: global: secure: Yc+Xohkr/iEUU7FCQuSLXAE9ywNW9g6CfrM1Ki0Hl+fS15F3AXT7dFY8EyCJ4dP1/oI0dBmwrGWrltXV0XWIjGV1Ms3tefCgQpBBAqwT+hImzVP3RbpZW8Iyo2d0VgiDemQF1LWYD/pKu6d8WljTnv5D77NIMdEJjQ0uzeTLWdw= -install: composer install --dev +install: composer self-update && composer install --optimize-autoloader --prefer-source before_script: - git config --global user.email "travis-ci@codeclimate.com" - git config --global user.name "Travis CI" diff --git a/composer/bin/test-reporter b/composer/bin/test-reporter index 877de15..e78d429 100755 --- a/composer/bin/test-reporter +++ b/composer/bin/test-reporter @@ -2,6 +2,7 @@ Date: Tue, 17 Mar 2015 14:29:43 -0300 Subject: [PATCH 2/6] Few improvements: - Updating PHPUnit version; - test-reporter bin timezone check; - test-reproter bin optional git option --- composer.json | 2 +- composer/bin/test-reporter | 27 +++++++++++++- .../Command/TestReporterCommand.php | 36 +++++++++++-------- .../Component/System/Git/GitCommand.php | 12 ++++++- 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/composer.json b/composer.json index fbded47..919f5fe 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "symfony/console": ">=2.0" }, "require-dev": { - "phpunit/phpunit": "3.7.*@stable", + "phpunit/phpunit": "4.5.*", "ext-xdebug": "*" }, "autoload": { diff --git a/composer/bin/test-reporter b/composer/bin/test-reporter index e78d429..325928b 100755 --- a/composer/bin/test-reporter +++ b/composer/bin/test-reporter @@ -1,6 +1,10 @@ #!/usr/bin/env php getParameterOption(array('--git', '-g'), exec('which git') ?: 'git'); + +define('PHP_TEST_REPORTER_GIT_BIN', $gitBinPath); + +$git = new GitCommand(); +try { + $git->getVersion(); +} catch (\RuntimeException $e) { + die( + 'Git command is not executable: ' . PHP_TEST_REPORTER_GIT_BIN . PHP_EOL . + 'You can set GIT Binary path with --git or -g' . PHP_EOL . + 'i.e.: $./bin/test-reporter -g /usr/bin/git' . PHP_EOL + ); +} $rootDir = realpath(dirname(PHP_TEST_REPORTER_COMPOSER_INSTALL) . '/..'); diff --git a/src/CodeClimate/Bundle/TestReporterBundle/Command/TestReporterCommand.php b/src/CodeClimate/Bundle/TestReporterBundle/Command/TestReporterCommand.php index 4fd439d..0a0e742 100644 --- a/src/CodeClimate/Bundle/TestReporterBundle/Command/TestReporterCommand.php +++ b/src/CodeClimate/Bundle/TestReporterBundle/Command/TestReporterCommand.php @@ -28,21 +28,27 @@ class TestReporterCommand extends Command protected function configure() { $this - ->setName('test-reporter') - ->setDescription('Code Climate PHP Test Reporter') - ->addOption( - 'stdout', - null, - InputOption::VALUE_NONE, - 'Do not upload, print JSON payload to stdout' - ) - ->addOption( - 'coverage-report', - null, - InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, - 'Location of clover style CodeCoverage report, as produced by PHPUnit\'s --coverage-clover option.', - array('build/logs/clover.xml') - ); + ->setName('test-reporter') + ->setDescription('Code Climate PHP Test Reporter') + ->addOption( + 'git', + 'g', + InputOption::VALUE_OPTIONAL, + 'Defines GIT Binary path' + ) + ->addOption( + 'stdout', + null, + InputOption::VALUE_NONE, + 'Do not upload, print JSON payload to stdout' + ) + ->addOption( + 'coverage-report', + null, + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + 'Location of clover style CodeCoverage report, as produced by PHPUnit\'s --coverage-clover option.', + array('build/logs/clover.xml') + ); } /** diff --git a/src/CodeClimate/Component/System/Git/GitCommand.php b/src/CodeClimate/Component/System/Git/GitCommand.php index 00a9e6d..19c9b26 100644 --- a/src/CodeClimate/Component/System/Git/GitCommand.php +++ b/src/CodeClimate/Component/System/Git/GitCommand.php @@ -5,7 +5,17 @@ class GitCommand extends SystemCommand { - protected $commandPath = 'git'; + public function __construct() + { + $this->commandPath = PHP_TEST_REPORTER_GIT_BIN; + } + + public function getVersion() + { + $command = $this->createCommand("--version"); + + return current($this->executeCommand($command)); + } public function getHead() { From 16209a398e840c38cd17a3481e5fd21b4a3907b5 Mon Sep 17 00:00:00 2001 From: Renato Mendes Figueiredo Date: Tue, 17 Mar 2015 14:46:55 -0300 Subject: [PATCH 3/6] Fixing build error --- src/CodeClimate/Bundle/TestReporterBundle/Entity/JsonFile.php | 3 ++- src/CodeClimate/Component/System/Git/GitCommand.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/CodeClimate/Bundle/TestReporterBundle/Entity/JsonFile.php b/src/CodeClimate/Bundle/TestReporterBundle/Entity/JsonFile.php index 658c7d8..e13b10a 100644 --- a/src/CodeClimate/Bundle/TestReporterBundle/Entity/JsonFile.php +++ b/src/CodeClimate/Bundle/TestReporterBundle/Entity/JsonFile.php @@ -5,6 +5,7 @@ use CodeClimate\Bundle\TestReporterBundle\Entity\CiInfo; use CodeClimate\Bundle\TestReporterBundle\Version; use Contrib\Bundle\CoverallsV1Bundle\Entity\JsonFile as SatooshiJsonFile; +use Contrib\Bundle\CoverallsV1Bundle\Entity\SourceFile; class JsonFile extends SatooshiJsonFile { @@ -72,7 +73,7 @@ protected function collectSourceFiles() return $sourceFiles; } - protected function calculateBlobId($sourceFile) + protected function calculateBlobId(SourceFile $sourceFile) { $content = file_get_contents($sourceFile->getPath()); $header = "blob ".strlen($content)."\0"; diff --git a/src/CodeClimate/Component/System/Git/GitCommand.php b/src/CodeClimate/Component/System/Git/GitCommand.php index 19c9b26..d3be30c 100644 --- a/src/CodeClimate/Component/System/Git/GitCommand.php +++ b/src/CodeClimate/Component/System/Git/GitCommand.php @@ -7,7 +7,7 @@ class GitCommand extends SystemCommand { public function __construct() { - $this->commandPath = PHP_TEST_REPORTER_GIT_BIN; + $this->commandPath = (defined('PHP_TEST_REPORTER_GIT_BIN') ? PHP_TEST_REPORTER_GIT_BIN : 'git'); } public function getVersion() From a481ef8f3a082998604f6a37a16de0d34cc22e72 Mon Sep 17 00:00:00 2001 From: Renato Mendes Figueiredo Date: Tue, 17 Mar 2015 16:29:00 -0300 Subject: [PATCH 4/6] Option to inform codeclimate token New tests Tests needs to run under different processes (env vars) Docs update --- README.md | 19 ++++++ phpunit.xml.dist | 2 +- .../Command/TestReporterCommand.php | 25 +++++++- .../TestReporterBundle/CoverageCollector.php | 25 +++++++- .../TestReporterBundle/Entity/JsonFile.php | 6 +- .../Console/ApplicationTest.php | 60 +++++++++++++++---- 6 files changed, 121 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 14a3297..ad05402 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ There are several requirements you'll need in order to use the PHP test reporter - [PHPUnit](http://phpunit.de) - [Xdebug](http://xdebug.org) - [Composer](http://getcomposer.org) +- [Git](http://git-scm.com/) The test reporter uses the [PHPUnit](http://phpunit.de) testing tool to generate [code coverage](http://en.wikipedia.org/wiki/Code_coverage) information. These results show how much of your application's code is being executed by your unit tests. PHPUnit can't generate this information on it's own though - it needs another tool, [Xdebug](http://xdebug.org). This is *not* included as a part of the PHPUnit (or PHP) install by default so you'll need to install it yourself. @@ -75,10 +76,28 @@ $ phpunit --coverage-clover build/logs/clover.xml $ CODECLIMATE_REPO_TOKEN="..." vendor/bin/test-reporter ``` +or + +``` +vendor/bin/test-reporter -t "..." +``` + +or even + +``` +vendor/bin/test-reporter --codeclimate-repo-token="..." +``` + The `CODECLIMATE_REPO_TOKEN` value is provided after you add your repo to your Code Climate account by clicking on "Setup Test Coverage" on the right hand side of your feed. +- You can choose the Git binary path: + +``` +$ CODECLIMATE_REPO_TOKEN="..." vendor/bin/test-reporter --git /usr/bin/git +``` + Please contact hello@codeclimate.com if you need any assistance setting this up. diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c6d4905..c8c0cfe 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -6,7 +6,7 @@ convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" - processIsolation="false" + processIsolation="true" stopOnFailure="false" syntaxCheck="false" bootstrap="tests/bootstrap.php"> diff --git a/src/CodeClimate/Bundle/TestReporterBundle/Command/TestReporterCommand.php b/src/CodeClimate/Bundle/TestReporterBundle/Command/TestReporterCommand.php index 0a0e742..99c9dee 100644 --- a/src/CodeClimate/Bundle/TestReporterBundle/Command/TestReporterCommand.php +++ b/src/CodeClimate/Bundle/TestReporterBundle/Command/TestReporterCommand.php @@ -44,10 +44,16 @@ protected function configure() ) ->addOption( 'coverage-report', - null, + 'c', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Location of clover style CodeCoverage report, as produced by PHPUnit\'s --coverage-clover option.', array('build/logs/clover.xml') + ) + ->addOption( + 'codeclimate-repo-token', + 't', + InputOption::VALUE_OPTIONAL, + 'The `CODECLIMATE_REPO_TOKEN` value is provided after you add your repo to your Code Climate account by clicking on "Setup Test Coverage" on the right hand side of your feed.' ); } @@ -59,14 +65,27 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { $ret = 0; + $collector = new CoverageCollector($input->getOption('coverage-report')); + + // Env vars have higher priority than args + if ($input->getOption('codeclimate-repo-token') && !array_key_exists('CODECLIMATE_REPO_TOKEN', $_SERVER)) { + $collector->setCodeclimateRepoToken($input->getOption('codeclimate-repo-token')); + } + $json = $collector->collectAsJson(); + if (!$json->getRepoToken()) { + $output->writeln("Please inform code-climate-token"); + return 3; + } + if ($input->getOption('stdout')) { $output->writeln((string)$json); } else { $client = new ApiClient(); $response = $client->send($json); + switch ($response->code) { case 200: $output->writeln("Test coverage data sent."); @@ -78,9 +97,9 @@ protected function execute(InputInterface $input, OutputInterface $output) break; default: - $output->writeln("Unexpected response: ".$response->code." ".$response->message); + $output->writeln("Unexpected response: " . $response->code . " " . $response->message); $output->writeln($response->body); - $ret = 1; + $ret = 2; break; } } diff --git a/src/CodeClimate/Bundle/TestReporterBundle/CoverageCollector.php b/src/CodeClimate/Bundle/TestReporterBundle/CoverageCollector.php index 24e0f88..0017efc 100644 --- a/src/CodeClimate/Bundle/TestReporterBundle/CoverageCollector.php +++ b/src/CodeClimate/Bundle/TestReporterBundle/CoverageCollector.php @@ -1,7 +1,6 @@ api = new Jobs($config); } + /** + * @return mixed + */ + public function getCodeclimateRepoToken() + { + return $this->codeclimateRepoToken; + } + + /** + * @param mixed $codeclimateRepoToken + */ + public function setCodeclimateRepoToken($codeclimateRepoToken) + { + $this->codeclimateRepoToken = $codeclimateRepoToken; + } + /** * Set a list of Clover XML paths * @param array $paths Array of relative paths to Clovers XML files @@ -50,12 +67,18 @@ public function getCloverPaths() { return $this->cloverPaths; } + + /** + * Get + * @return JsonFile + */ public function collectAsJson() { $cloverJsonFile = $this->api->collectCloverXml()->getJsonFile(); $jsonFile = new JsonFile(); $jsonFile->setRunAt($cloverJsonFile->getRunAt()); + $jsonFile->setRepoToken($this->getCodeclimateRepoToken()); foreach ($cloverJsonFile->getSourceFiles() as $sourceFile) { $jsonFile->addSourceFile($sourceFile); diff --git a/src/CodeClimate/Bundle/TestReporterBundle/Entity/JsonFile.php b/src/CodeClimate/Bundle/TestReporterBundle/Entity/JsonFile.php index e13b10a..a5d0a3f 100644 --- a/src/CodeClimate/Bundle/TestReporterBundle/Entity/JsonFile.php +++ b/src/CodeClimate/Bundle/TestReporterBundle/Entity/JsonFile.php @@ -29,7 +29,11 @@ public function getRunAt() public function getRepoToken() { - return $_SERVER["CODECLIMATE_REPO_TOKEN"]; + if (parent::getRepoToken() === null) { + $this->setRepoToken($_SERVER["CODECLIMATE_REPO_TOKEN"]); + } + + return parent::getRepoToken(); } protected function getEnvironment() diff --git a/tests/CodeClimate/Bundle/TestReporterBundle/Console/ApplicationTest.php b/tests/CodeClimate/Bundle/TestReporterBundle/Console/ApplicationTest.php index b093f2c..b9f3921 100644 --- a/tests/CodeClimate/Bundle/TestReporterBundle/Console/ApplicationTest.php +++ b/tests/CodeClimate/Bundle/TestReporterBundle/Console/ApplicationTest.php @@ -1,14 +1,26 @@ srcDir, 'PHP Test Reporter', Version::VERSION); + $app->setAutoExit(false); + $tester = new ApplicationTester($app); + + return $tester->run($inputParams, $options); + } + public function setUp() { $this->srcDir = realpath(__DIR__ . '/../../../../..'); @@ -16,27 +28,56 @@ public function setUp() $this->setupEnvironment(); } + public function testWithoutToken() + { + $status = $this->runTestReporterCommand(array()); + + // TODO Why is it returning 8? + $this->assertEquals(8, $status); + } + + public function testInvalidTokenParam() + { + $status = $this->runTestReporterCommand(array('--codeclimate-repo-token' => static::INVALID_TOKEN)); + + $this->assertEquals(1, $status); + } + /** * @test */ public function shouldExecuteSuccessfully() { - $app = new Application($this->srcDir, 'PHP Test Reporter', '1.0.0'); - $app->setAutoExit(false); - $tester = new ApplicationTester($app); + $_SERVER["CODECLIMATE_REPO_TOKEN"] = static::INVALID_TOKEN; + + $status = $this->runTestReporterCommand(array('--stdout' => true)); + + $this->assertEquals(0, $status); + } - $status = $tester->run(array('--stdout' => true)); + public function testInvalidTokenEnvVar() + { + $_SERVER["CODECLIMATE_REPO_TOKEN"] = static::INVALID_TOKEN; + + $status = $this->runTestReporterCommand(array()); + + $this->assertEquals(1, $status); + } + + public function testWithTokenParamStdout() + { + $status = $this->runTestReporterCommand(array('--stdout' => true, '--codeclimate-repo-token' => static::INVALID_TOKEN)); $this->assertEquals(0, $status); } private function setupProject() { - shell_exec("rm -rf ".static::PROJECT_DIR); - mkdir(static::PROJECT_DIR."/build/logs", 0755, true); - copy("tests/files/test.php", static::PROJECT_DIR."/test.php"); - copy("tests/files/test.php", static::PROJECT_DIR."/test2.php"); - copy("tests/files/clover.xml", static::PROJECT_DIR."/build/logs/clover.xml"); + shell_exec("rm -rf " . static::PROJECT_DIR); + mkdir(static::PROJECT_DIR . "/build/logs", 0755, true); + copy("tests/files/test.php", static::PROJECT_DIR . "/test.php"); + copy("tests/files/test.php", static::PROJECT_DIR . "/test2.php"); + copy("tests/files/clover.xml", static::PROJECT_DIR . "/build/logs/clover.xml"); chdir(static::PROJECT_DIR); @@ -48,7 +89,6 @@ private function setupProject() private function setupEnvironment() { - $_SERVER["CODECLIMATE_REPO_TOKEN"] = 'abc123'; $_SERVER["TRAVIS"] = "1"; $_SERVER["TRAVIS_BRANCH"] = "master"; $_SERVER["TRAVIS_JOB_ID"] = "1"; From ee3b992feb0c9467d42494977e036fb210395015 Mon Sep 17 00:00:00 2001 From: Renato Mendes Figueiredo Date: Tue, 17 Mar 2015 16:34:38 -0300 Subject: [PATCH 5/6] I was wrong, params should have high priority than ENV Vars! --- .../Bundle/TestReporterBundle/Command/TestReporterCommand.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/CodeClimate/Bundle/TestReporterBundle/Command/TestReporterCommand.php b/src/CodeClimate/Bundle/TestReporterBundle/Command/TestReporterCommand.php index 99c9dee..67755ac 100644 --- a/src/CodeClimate/Bundle/TestReporterBundle/Command/TestReporterCommand.php +++ b/src/CodeClimate/Bundle/TestReporterBundle/Command/TestReporterCommand.php @@ -68,10 +68,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $collector = new CoverageCollector($input->getOption('coverage-report')); - // Env vars have higher priority than args - if ($input->getOption('codeclimate-repo-token') && !array_key_exists('CODECLIMATE_REPO_TOKEN', $_SERVER)) { + if ($input->getOption('codeclimate-repo-token')) $collector->setCodeclimateRepoToken($input->getOption('codeclimate-repo-token')); - } $json = $collector->collectAsJson(); From f5c97c4cba796ae3487ca3c7a57b2b5c87a90203 Mon Sep 17 00:00:00 2001 From: Renato Mendes Figueiredo Date: Tue, 17 Mar 2015 16:44:41 -0300 Subject: [PATCH 6/6] Docs updates --- README.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ad05402..3c30c44 100644 --- a/README.md +++ b/README.md @@ -79,13 +79,13 @@ $ CODECLIMATE_REPO_TOKEN="..." vendor/bin/test-reporter or ``` -vendor/bin/test-reporter -t "..." +$ vendor/bin/test-reporter -t "..." ``` or even ``` -vendor/bin/test-reporter --codeclimate-repo-token="..." +$ vendor/bin/test-reporter --codeclimate-repo-token="..." ``` The `CODECLIMATE_REPO_TOKEN` value is provided after you add your repo @@ -95,7 +95,19 @@ right hand side of your feed. - You can choose the Git binary path: ``` -$ CODECLIMATE_REPO_TOKEN="..." vendor/bin/test-reporter --git /usr/bin/git +$ vendor/bin/test-reporter -t "..." --git /usr/bin/git +``` + +or + +``` +$ vendor/bin/test-reporter -t "..." -g /usr/bin/git +``` + +- Check test-reporter options: + +``` +$ vendor/bin/test-reporter --help ``` Please contact hello@codeclimate.com if you need any assistance setting