Skip to content

Commit

Permalink
Validate composer.json when composer binary is available
Browse files Browse the repository at this point in the history
fixes clue#43
  • Loading branch information
staabm committed Jun 26, 2015
1 parent 96706e8 commit 5d157ed
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ you spot any mistakes.
* Feature: Can now be installed as a `require-dev` composer dependency and
supports running as `./vendor/bin/phar-composer`.
([#36](https://github.com/clue/phar-composer/pull/36), thanks @radford)
* Feature: When composer or composer.phar is available in CLI `.json` files
passed to `PharComposer` are validated. In case of validation errors a
RuntimeException will be thrown.
([#36](https://github.com/clue/phar-composer/pull/48), thanks @staabm)

## 0.5.0 (2014-07-10)

Expand Down
21 changes: 18 additions & 3 deletions src/Clue/PharComposer/PharComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Clue\PharComposer;

use Symfony\Component\Finder\Finder;

use Herrera\Box\StubGenerator;
use UnexpectedValueException;
use InvalidArgumentException;
use RuntimeException;

use Herrera\Box\StubGenerator;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Process\ExecutableFinder;

class PharComposer
{
Expand Down Expand Up @@ -247,6 +249,19 @@ public function setStep($step)

private function loadJson($path)
{
$executableFinder = new ExecutableFinder();
foreach (['composer', 'composer.phar'] as $candidateName) {
if ($composerPath = $executableFinder->find($candidateName, null, array(getcwd()))) {
$process = new Process($candidateName . ' validate '. escapeshellarg($path));
$process->run();

if (!$process->isSuccessful()) {
throw new \RuntimeException($process->getErrorOutput());
}
break;
}
}

$ret = json_decode(file_get_contents($path), true);
if ($ret === null) {
var_dump(json_last_error(), JSON_ERROR_SYNTAX);
Expand Down
8 changes: 8 additions & 0 deletions tests/PharComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public function testSetters(PharComposer $pharcomposer)
return $pharcomposer;
}

/**
* @expectedException RuntimeException
*/
public function testComposerJsonValidation()
{
new PharComposer(__DIR__ . '/fixtures/invalid_composer.json');
}

private function getPathProjectAbsolute($path)
{
return realpath(__DIR__ . '/../' . $path);
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/invalid_composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "clue/phar-composer",
"description": "This file contains invalid json",
BAM
}

0 comments on commit 5d157ed

Please sign in to comment.