Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure base configuration is correct even if file already exists #1415

Merged
merged 1 commit into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Ensure base configuration is correct even if file already exists
  • Loading branch information
mattstauffer committed May 16, 2023
commit 9ce0ca97b25eab48406fb37e2cbd60f06c5e19cd
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();
}
}