Skip to content

Commit

Permalink
Exit with an evaluable return code
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelstolt committed May 2, 2017
1 parent 95e476e commit 4914750
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to this publication will be documented in this file.

## 1.0.1 - 2017-??-??
The validate command returns an evaluable return code.

## 1.0.0 - 2017-25-01

First stable release.
16 changes: 15 additions & 1 deletion src/ComplianceValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ class ComplianceValidator
const STATE_INCORRECT_PRESENT = 4;

protected $files = null;
protected $compliant = true;

public function execute($root = null)
{
$lines = $this->getFiles($root);
$results = $this->validate($lines);
$this->outputResults($results);
return true;
if ($this->getCompliant()) {
exit(0);
}
exit(1);
}

public function getCompliant()
{
return $this->compliant;
}

public function validate($lines)
Expand All @@ -39,6 +48,11 @@ public function validate($lines)
$state = $complianceResult[0];
$expected = $complianceResult[1];
$actual = $complianceResult[2];

if ($expected !== $actual && ($state == self::STATE_INCORRECT_PRESENT || $state == self::STATE_RECOMMENDED_NOT_PRESENT)) {
$this->compliant = false;
}

$results[$expected] = [
'label' => $label,
'state' => $state,
Expand Down
37 changes: 36 additions & 1 deletion tests/ComplianceValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public static function run()
{
$tester = new ComplianceValidatorTest();
$tester->testValidate_WithIncorrectBin_ReturnsIncorrectBin();
$tester->testValidate_WithNonCompliance_SetsNonCompliantState();
$tester->testValidate_WithCompliance_SetsCompliantState();
echo __CLASS__ . " errors: {$tester->numErrors}" . PHP_EOL;
}

Expand Down Expand Up @@ -51,4 +53,37 @@ public function testValidate_WithIncorrectBin_ReturnsIncorrectBin()
}
}
}
}

public function testValidate_WithNonCompliance_SetsNonCompliantState()
{
$paths = [
'cli/',
'test/',
];

$validator = new ComplianceValidator();
$results = $validator->validate($paths);

if ($validator->getCompliant() == true) {
$this->numErrors++;
echo __FUNCTION__ . ": Expected a non compliant state" . PHP_EOL;
}
}

public function testValidate_WithCompliance_SetsCompliantState()
{
$paths = [
'bin/',
'tests/',
'LICENSE.md',
];

$validator = new ComplianceValidator();
$results = $validator->validate($paths);

if ($validator->getCompliant() == false) {
$this->numErrors++;
echo __FUNCTION__ . ": Expected a compliant state" . PHP_EOL;
}
}
}

0 comments on commit 4914750

Please sign in to comment.