Skip to content

Few improvements on GIT binary and travis #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
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
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"
- git config --global user.name "Travis CI"
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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.

Expand Down Expand Up @@ -87,10 +88,40 @@ $ 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:

```
$ 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 [email protected] if you need any assistance setting
this up.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"symfony/console": ">=2.0"
},
"require-dev": {
"phpunit/phpunit": "3.7.*@stable",
"phpunit/phpunit": "4.5.*",
"ext-xdebug": "*"
},
"autoload": {
Expand Down
28 changes: 27 additions & 1 deletion composer/bin/test-reporter
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
#!/usr/bin/env php
<?php

if (!ini_get('date.timezone')) {
ini_set('date.timezone', 'UTC');
}

$files = array(
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../../../../autoload.php'
);

foreach ($files as $file) {
if (file_exists($file)) {
include_once $file;
require_once $file;

define('PHP_TEST_REPORTER_COMPOSER_INSTALL', $file);

break;
}
}

unset($files);

if (!defined('PHP_TEST_REPORTER_COMPOSER_INSTALL')) {
die(
'You need to set up the project dependencies using the following commands:' . PHP_EOL .
Expand All @@ -24,8 +31,27 @@ if (!defined('PHP_TEST_REPORTER_COMPOSER_INSTALL')) {
);
}


use CodeClimate\Bundle\TestReporterBundle\Version;
use CodeClimate\Bundle\TestReporterBundle\Console\Application;
use CodeClimate\Component\System\Git\GitCommand;
use Symfony\Component\Console\Input\ArgvInput;

$input = new ArgvInput();
$gitBinPath = $input->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) . '/..');

Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
processIsolation="true"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,33 @@ 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',
'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.'
);
}

/**
Expand All @@ -53,14 +65,25 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$ret = 0;

$collector = new CoverageCollector($input->getOption('coverage-report'));

if ($input->getOption('codeclimate-repo-token'))
$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.");
Expand All @@ -72,9 +95,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;
}
}
Expand Down
25 changes: 24 additions & 1 deletion src/CodeClimate/Bundle/TestReporterBundle/CoverageCollector.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace CodeClimate\Bundle\TestReporterBundle;

use CodeClimate\Component\System\Git\GitCommand;
use CodeClimate\Bundle\TestReporterBundle\Entity\JsonFile;
use Contrib\Bundle\CoverallsV1Bundle\Api\Jobs;
use Contrib\Bundle\CoverallsV1Bundle\Config\Configuration;
Expand All @@ -10,6 +9,8 @@ class CoverageCollector
{
protected $api;

protected $codeclimateRepoToken;

/**
* Array that holds list of relative paths to Clover XML files
* @var array
Expand All @@ -33,6 +34,22 @@ public function __construct($paths)
$this->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
Expand All @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions src/CodeClimate/Bundle/TestReporterBundle/Entity/JsonFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -28,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()
Expand Down Expand Up @@ -72,7 +77,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";
Expand Down
12 changes: 11 additions & 1 deletion src/CodeClimate/Component/System/Git/GitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@

class GitCommand extends SystemCommand
{
protected $commandPath = 'git';
public function __construct()
{
$this->commandPath = (defined('PHP_TEST_REPORTER_GIT_BIN') ? PHP_TEST_REPORTER_GIT_BIN : 'git');
}

public function getVersion()
{
$command = $this->createCommand("--version");

return current($this->executeCommand($command));
}

public function getHead()
{
Expand Down
Loading