Skip to content

Commit b718254

Browse files
committed
Allow empty variables and add more tests around blocks
1 parent b0a5402 commit b718254

File tree

6 files changed

+166
-6
lines changed

6 files changed

+166
-6
lines changed

features/blocks.feature

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Feature:
2+
In order to separate the variables into sensible blocks with explainations
3+
As a user
4+
I want to define my blocks in the .env.dist file
5+
6+
Scenario: Displays the block title
7+
Given the file ".env.dist" contains:
8+
"""
9+
## Something
10+
MY_VARIABLE=default-value
11+
"""
12+
When I run the companion with the following answers:
13+
| Let's fix this? (y) | y |
14+
| MY_VARIABLE ? (default-value) | my-value |
15+
Then the companion's output will look like that:
16+
"""
17+
It looks like you are missing some configuration (1 variables). I will help you to sort this out.
18+
Let's fix this? (y)
19+
20+
Something
21+
22+
MY_VARIABLE ? (default-value)
23+
"""
24+
25+
Scenario: Displays the block description
26+
Given the file ".env.dist" contains:
27+
"""
28+
## Something
29+
# With more details, so it's clearer to the user...
30+
MY_VARIABLE=default-value
31+
"""
32+
When I run the companion with the following answers:
33+
| Let's fix this? (y) | y |
34+
| MY_VARIABLE ? (default-value) | my-value |
35+
Then the companion's output will look like that:
36+
"""
37+
It looks like you are missing some configuration (1 variables). I will help you to sort this out.
38+
Let's fix this? (y)
39+
40+
Something
41+
With more details, so it's clearer to the user...
42+
43+
MY_VARIABLE ? (default-value)
44+
"""

features/bootstrap/FeatureContext.php

+19-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
class FeatureContext implements Context
1414
{
15+
private $interaction;
1516
private $fileSystem;
1617
private $companion;
1718

@@ -35,13 +36,13 @@ public function iRunTheCompanionWithTheFollowingAnswers(TableNode $table)
3536
{
3637
$this->companion = new Companion(
3738
$this->fileSystem,
38-
$interaction = new InMemoryInteraction($table->getRowsHash()),
39+
$this->interaction = new InMemoryInteraction($table->getRowsHash()),
3940
new Chained(Application::defaultExtensions())
4041
);
4142

4243
$this->companion->fillGaps();
4344

44-
echo $interaction->getBuffer();
45+
echo $this->interaction->getBuffer();
4546
}
4647

4748
/**
@@ -59,4 +60,20 @@ public function theFileShouldContain($path, PyStringNode $string)
5960
));
6061
}
6162
}
63+
64+
/**
65+
* @Then the companion's output will look like that:
66+
*/
67+
public function theCompanionsOutputWillLookLikeThat(PyStringNode $string)
68+
{
69+
$found = strip_tags(trim($this->interaction->getBuffer()));
70+
$expected = trim($string->getRaw());
71+
72+
if ($found != $expected) {
73+
throw new \RuntimeException(sprintf(
74+
'Found the following instead: %s',
75+
function_exists('xdiff_string_diff') ? xdiff_string_diff($expected, $found) : $found
76+
));
77+
}
78+
}
6279
}

features/fill-variables.feature

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
Feature:
2+
In order to fill my .env file
3+
As a developer
4+
I want the companion to ask me the values of each missing variable, from a .env.dist file
5+
6+
Scenario: It asks all the variables if the file is missing
7+
Given the file ".env.dist" contains:
8+
"""
9+
## Something
10+
MY_VARIABLE=default-value
11+
"""
12+
When I run the companion with the following answers:
13+
| Let's fix this? (y) | y |
14+
| MY_VARIABLE ? (default-value) | my-value |
15+
And the file ".env" should contain:
16+
"""
17+
MY_VARIABLE=my-value
18+
"""
19+
20+
Scenario: It asks only the missing variables
21+
Given the file ".env.dist" contains:
22+
"""
23+
## Something
24+
MY_VARIABLE=default-value
25+
A_NEW_VARIABLE=
26+
"""
27+
And the file ".env" contains:
28+
"""
29+
MY_VARIABLE=something-else
30+
31+
"""
32+
When I run the companion with the following answers:
33+
| Let's fix this? (y) | y |
34+
| A_NEW_VARIABLE ? | value |
35+
And the file ".env" should contain:
36+
"""
37+
MY_VARIABLE=something-else
38+
A_NEW_VARIABLE=value
39+
"""
40+
41+
Scenario: We ask an empty variable if it has a value
42+
Given the file ".env.dist" contains:
43+
"""
44+
## Something
45+
MY_VARIABLE=default-value
46+
"""
47+
And the file ".env" contains:
48+
"""
49+
MY_VARIABLE=
50+
"""
51+
When I run the companion with the following answers:
52+
| Let's fix this? (y) | y |
53+
| MY_VARIABLE ? | |
54+
And the file ".env" should contain:
55+
"""
56+
MY_VARIABLE=
57+
"""
58+
59+
Scenario: We do not ask an empty variable if the reference is empty
60+
Given the file ".env.dist" contains:
61+
"""
62+
## Something
63+
EMPTY_VARIABLE=
64+
"""
65+
And the file ".env" contains:
66+
"""
67+
EMPTY_VARIABLE=
68+
"""
69+
When I run the companion with the following answers:
70+
| Let's fix this? (y) | y |
71+
And the file ".env" should contain:
72+
"""
73+
EMPTY_VARIABLE=
74+
"""
75+
76+
Scenario: We do not ask for variable than have a falsy value
77+
Given the file ".env.dist" contains:
78+
"""
79+
## Something
80+
MY_VARIABLE=default-value
81+
"""
82+
And the file ".env" contains:
83+
"""
84+
MY_VARIABLE=0
85+
"""
86+
When I run the companion with the following answers:
87+
| Let's fix this? (y) | y |
88+
And the file ".env" should contain:
89+
"""
90+
MY_VARIABLE=0
91+
"""

src/Companienv/Companion.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ private function fillBlockGaps(Block $block, array $missingVariables)
6464
$this->interaction->writeln([
6565
'',
6666
'<info>'.$block->getTitle().'</info>',
67-
$block->getDescription(),
68-
''
6967
]);
7068

69+
if (!empty($description = $block->getDescription())) {
70+
$this->interaction->writeln($description);
71+
}
72+
73+
$this->interaction->writeln('');
74+
7175
foreach ($block->getVariables() as $variable) {
7276
if (isset($missingVariables[$variable->getName()])) {
7377
$this->writeVariable($variable->getName(), $this->extension->getVariableValue($this, $block, $variable));
@@ -84,7 +88,8 @@ private function writeVariable(string $name, string $value)
8488
$variablesInFileHash = $this->getDefinedVariablesHash();
8589

8690
$writer = new DotenvWriter(new DotenvFormatter());
87-
$writer->setBuffer($this->fileSystem->getContents($this->envFileName));
91+
$fileContents = $this->fileSystem->getContents($this->envFileName);
92+
$writer->setBuffer($fileContents);
8893

8994
if (isset($variablesInFileHash[$name])) {
9095
$writer->updateSetter($name, $value);

src/Companienv/Interaction/AskVariableValues.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public function getVariableValue(Companion $companion, Block $block, Variable $v
3030
*/
3131
public function isVariableRequiringValue(Companion $companion, Block $block, Variable $variable, string $currentValue = null)
3232
{
33-
return empty($currentValue);
33+
return $currentValue === null || (
34+
$currentValue === '' && $variable->getValue() !== ''
35+
);
3436
}
3537
}

tests/Companienv/IO/InMemoryInteraction.php

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function askConfirmation(string $question): bool
2626
public function ask(string $question, string $default = null): string
2727
{
2828
$normalizedKey = trim(strip_tags($question));
29+
$this->buffer .= trim(strip_tags($question))."\n";
2930

3031
if (isset($this->answers[$normalizedKey])) {
3132
return $this->answers[$normalizedKey];

0 commit comments

Comments
 (0)