Skip to content

Commit

Permalink
Merge pull request #13 from sroze/file-to-propagate-and-only-if
Browse files Browse the repository at this point in the history
Vote for the variable's requirement, instead of having a simple boolean
  • Loading branch information
sroze authored Feb 19, 2018
2 parents 5222d47 + d3fc621 commit 5d1986d
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 30 deletions.
20 changes: 18 additions & 2 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ public function theFileContains($path, PyStringNode $string)

/**
* @When I run the companion with the following answers:
* @When I run the companion
*/
public function iRunTheCompanionWithTheFollowingAnswers(TableNode $table)
public function iRunTheCompanionWithTheFollowingAnswers(TableNode $table = null)
{
$this->companion = new Companion(
$this->fileSystem,
$this->interaction = new InMemoryInteraction($table->getRowsHash()),
$this->interaction = new InMemoryInteraction($table !== null ? $table->getRowsHash() : []),
new Chained(Application::defaultExtensions())
);

Expand Down Expand Up @@ -76,4 +77,19 @@ function_exists('xdiff_string_diff') ? xdiff_string_diff($expected, $found) : $f
));
}
}

/**
* @Then the companion's output should be empty
*/
public function theCompanionsOutputShouldBeEmpty()
{
$found = strip_tags(trim($this->interaction->getBuffer()));

if (!empty($found)) {
throw new \RuntimeException(sprintf(
'Found the following instead: %s',
$found
));
}
}
}
18 changes: 18 additions & 0 deletions features/extensions/file-to-propagate.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ Feature:
"""
SOMETHING
"""

Scenario: It does not consider the variable as missing if we it not included in the "only-if"
Given the file ".env.dist" contains:
"""
## Y'a know...
# This is a configuration.
#+file-to-propagate(GOOGLE_CLOUD_AUDIT_LOG_SERVICE_ACCOUNT_PATH)
#+only-if(GOOGLE_CLOUD_AUDIT_LOG_SERVICE_ACCOUNT_PATH):(GOOGLE_CLOUD_AUDIT_ENABLED=true)
GOOGLE_CLOUD_AUDIT_ENABLED=false
GOOGLE_CLOUD_AUDIT_LOG_SERVICE_ACCOUNT_PATH=/runtime/keys/google-cloud-audit-log.json
"""
And the file ".env" contains:
"""
GOOGLE_CLOUD_AUDIT_ENABLED=false
GOOGLE_CLOUD_AUDIT_LOG_SERVICE_ACCOUNT_PATH=/runtime/keys/google-cloud-audit-log.json
"""
When I run the companion
Then the companion's output should be empty
2 changes: 1 addition & 1 deletion src/Companienv/Companion.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private function getVariablesRequiringValues()
foreach ($block->getVariables() as $variable) {
$currentValue = isset($variablesInFile[$variable->getName()]) ? $variablesInFile[$variable->getName()] : null;

if ($this->extension->isVariableRequiringValue($this, $block, $variable, $currentValue)) {
if ($this->extension->isVariableRequiringValue($this, $block, $variable, $currentValue) === Extension::VARIABLE_REQUIRED) {
$missingVariables[$variable->getName()] = new MissingVariable($variable, $currentValue);
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/Companienv/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

interface Extension
{
const VARIABLE_REQUIRED = 1;
const VARIABLE_SKIP = -1;
const ABSTAIN = 0;

/**
* Get the variable value, from a given source.
*
Expand All @@ -23,14 +27,14 @@ public function getVariableValue(Companion $companion, Block $block, Variable $v
/**
* Is this variable requiring a new value?
*
* Return true to make sure this variable will be asked for a refreshed value.
* The return value is one of the ABSTAINT, VARIABLE_REQUIRED or VARIABLE_SKIP constants.
*
* @param Companion $companion
* @param Block $block
* @param Variable $variable
* @param string|null $currentValue
*
* @return bool
* @return int
*/
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null);
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null) : int;
}
4 changes: 2 additions & 2 deletions src/Companienv/Extension/AbstractExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function getVariableValue(Companion $companion, Block $block, Variable $v
/**
* {@inheritdoc}
*/
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null)
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null) : int
{
return false;
return Extension::ABSTAIN;
}
}
8 changes: 4 additions & 4 deletions src/Companienv/Extension/Chained.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public function getVariableValue(Companion $companion, Block $block, Variable $v
/**
* {@inheritdoc}
*/
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null)
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null) : int
{
foreach ($this->extensions as $extension) {
if ($extension->isVariableRequiringValue($companion, $block, $variable, $currentValue)) {
return true;
if (($vote = $extension->isVariableRequiringValue($companion, $block, $variable, $currentValue)) != Extension::ABSTAIN) {
return $vote;
}
}

return false;
return Extension::ABSTAIN;
}
}
8 changes: 5 additions & 3 deletions src/Companienv/Extension/FileToPropagate.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ public function getVariableValue(Companion $companion, Block $block, Variable $v
/**
* {@inheritdoc}
*/
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null)
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null) : int
{
if (null === ($attribute = $block->getAttribute('file-to-propagate', $variable))) {
return false;
return Extension::ABSTAIN;
}

return !$companion->getFileSystem()->exists($variable->getValue());
return $companion->getFileSystem()->exists($variable->getValue())
? Extension::VARIABLE_REQUIRED
: Extension::ABSTAIN;
}
}
10 changes: 6 additions & 4 deletions src/Companienv/Extension/OnlyIf.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@ public function getVariableValue(Companion $companion, Block $block, Variable $v
/**
* {@inheritdoc}
*/
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null)
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null) : int
{
if (null === ($attribute = $block->getAttribute('only-if', $variable))) {
return false;
return Extension::ABSTAIN;
}

return $this->matchesCondition($companion, $attribute);
return $this->matchesCondition($companion, $attribute)
? Extension::ABSTAIN
: Extension::VARIABLE_SKIP;
}

private function matchesCondition(Companion $companion, Attribute $attribute) : bool
{
$definedVariablesHash = $companion->getDefinedVariablesHash();
foreach ($attribute->getLabels() as $otherVariableName => $expectedValue) {
if (!isset($definedVariablesHash[$otherVariableName]) || $definedVariablesHash[$otherVariableName] != $expectedValue) {
if (isset($definedVariablesHash[$otherVariableName]) && $definedVariablesHash[$otherVariableName] != $expectedValue) {
return false;
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/Companienv/Extension/RsaKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@ public function getVariableValue(Companion $companion, Block $block, Variable $v
/**
* {@inheritdoc}
*/
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null)
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null) : int
{
if (null === ($attribute = $block->getAttribute('rsa-pair', $variable))) {
return false;
return Extension::ABSTAIN;
}

$fileSystem = $companion->getFileSystem();

return !$fileSystem->exists($block->getVariable($attribute->getVariableNames()[0])->getValue())
|| !$fileSystem->exists($block->getVariable($attribute->getVariableNames()[1])->getValue());
return (
!$fileSystem->exists($block->getVariable($attribute->getVariableNames()[0])->getValue())
|| !$fileSystem->exists($block->getVariable($attribute->getVariableNames()[1])->getValue())
) ? Extension::VARIABLE_REQUIRED
: Extension::ABSTAIN;
}
}
9 changes: 6 additions & 3 deletions src/Companienv/Extension/SslCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,18 @@ public function getVariableValue(Companion $companion, Block $block, Variable $v
/**
* {@inheritdoc}
*/
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null)
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null) : int
{
if (null === ($attribute = $block->getAttribute('ssl-certificate', $variable))) {
return false;
}

$fileSystem = $companion->getFileSystem();

return !$fileSystem->exists($block->getVariable($privateKeyVariableName = $attribute->getVariableNames()[0])->getValue())
|| !$fileSystem->exists($block->getVariable($attribute->getVariableNames()[1])->getValue());
return (
!$fileSystem->exists($block->getVariable($privateKeyVariableName = $attribute->getVariableNames()[0])->getValue())
|| !$fileSystem->exists($block->getVariable($attribute->getVariableNames()[1])->getValue())
) ? Extension::VARIABLE_REQUIRED
: Extension::ABSTAIN;
}
}
10 changes: 6 additions & 4 deletions src/Companienv/Interaction/AskVariableValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ public function getVariableValue(Companion $companion, Block $block, Variable $v
/**
* {@inheritdoc}
*/
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null)
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null) : int
{
return $currentValue === null || (
$currentValue === '' && $variable->getValue() !== ''
);
return (
$currentValue === null || (
$currentValue === '' && $variable->getValue() !== ''
)
) ? Extension::VARIABLE_REQUIRED : Extension::ABSTAIN;
}
}

0 comments on commit 5d1986d

Please sign in to comment.