Skip to content

Commit

Permalink
Merge pull request #1415 from laravel/mes/ensure-base-configuration
Browse files Browse the repository at this point in the history
Ensure base configuration is correct even if file already exists
  • Loading branch information
mattstauffer authored May 16, 2023
2 parents c95334a + 9ce0ca9 commit f8d4ef5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
22 changes: 20 additions & 2 deletions cli/Valet/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Valet;

use Exception;

class Configuration
{
public function __construct(public Filesystem $files)
Expand All @@ -18,7 +20,7 @@ public function install(): void
$this->createSitesDirectory();
$this->createLogDirectory();
$this->createCertificatesDirectory();
$this->writeBaseConfiguration();
$this->ensureBaseConfiguration();

$this->files->chown($this->path(), user());
}
Expand Down Expand Up @@ -84,7 +86,23 @@ public function createCertificatesDirectory(): void
}

/**
* Write the base, initial configuration for Valet.
* Ensure the base initial configuration has been installed.
*/
public function ensureBaseConfiguration(): void
{
$this->writeBaseConfiguration();

if (empty($this->read()['tld'])) {
$this->updateKey('tld', 'test');
}

if (empty($this->read()['loopback'])) {
$this->updateKey('loopback', '127.0.0.1');
}
}

/**
* Write the base initial configuration for Valet.
*/
public function writeBaseConfiguration(): void
{
Expand Down
4 changes: 1 addition & 3 deletions cli/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ function (ConsoleCommandEvent $event) {
/**
* Upgrade helper: ensure the tld config exists and the loopback config exists.
*/
if (empty(Configuration::read()['tld']) || empty(Configuration::read()['loopback'])) {
Configuration::writeBaseConfiguration();
}
Configuration::ensureBaseConfiguration();

/**
* Get or set the TLD currently being used by Valet.
Expand Down
10 changes: 10 additions & 0 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,14 @@ public function test_trust_adds_the_sudoer_files()
resolve(Brew::class)->createSudoersEntry();
resolve(Valet::class)->createSudoersEntry();
}

public function test_ensure_configuration_exists_writes_tld_and_loopback_if_empty()
{
$config = Mockery::mock(Configuration::class.'[writeBaseConfiguration,read,updateKey]', [new Filesystem]);
$config->shouldReceive('writeBaseConfiguration')->once();
$config->shouldReceive('read')->times(2)->andReturn([]);
$config->shouldReceive('updateKey')->with('tld', 'test');
$config->shouldReceive('updateKey')->with('loopback', '127.0.0.1');
$config->ensureBaseConfiguration();
}
}

0 comments on commit f8d4ef5

Please sign in to comment.