Skip to content

Commit

Permalink
Merge pull request #1178 from cristiancalara/master
Browse files Browse the repository at this point in the history
Stop root homebrew services on php version switch
  • Loading branch information
mattstauffer authored Jan 26, 2022
2 parents 8659178 + 70335ba commit 6ddca24
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 14 deletions.
54 changes: 46 additions & 8 deletions cli/Valet/Brew.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,21 +370,59 @@ function ($exitCode, $errorOutput) use ($formula) {
);
}

/**
* Get all the currently running brew services.
*
* @return \Illuminate\Support\Collection
*/
public function getAllRunningServices()
{
return $this->getRunningServicesAsRoot()
->concat($this->getRunningServicesAsUser())
->unique();
}

/**
* Get the currently running brew services as root.
* i.e. /Library/LaunchDaemons (started at boot).
*
* @return \Illuminate\Support\Collection
*/
public function getRunningServicesAsRoot()
{
return $this->getRunningServices();
}

/**
* Get the currently running brew services.
* i.e. ~/Library/LaunchAgents (started at login).
*
* @return \Illuminate\Support\Collection
*/
public function getRunningServices()
public function getRunningServicesAsUser()
{
return collect(array_filter(explode(PHP_EOL, $this->cli->runAsUser(
'brew services list | grep started | awk \'{ print $1; }\'',
function ($exitCode, $errorOutput) {
output($errorOutput);
return $this->getRunningServices(true);
}

throw new DomainException('Brew was unable to check which services are running.');
}
))));
/**
* Get the currently running brew services.
*
* @param bool $asUser
* @return \Illuminate\Support\Collection
*/
public function getRunningServices($asUser = false)
{
$command = 'brew services list | grep started | awk \'{ print $1; }\'';
$onError = function ($exitCode, $errorOutput) {
output($errorOutput);

throw new DomainException('Brew was unable to check which services are running.');
};

return collect(array_filter(explode(PHP_EOL, $asUser
? $this->cli->runAsUser($command, $onError)
: $this->cli->run('sudo '.$command, $onError)
)));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion cli/Valet/PhpFpm.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function fpmConfigPath()
public function stopRunning()
{
$this->brew->stopService(
$this->brew->getRunningServices()
$this->brew->getAllRunningServices()
->filter(function ($service) {
return substr($service, 0, 3) === 'php';
})
Expand Down
40 changes: 38 additions & 2 deletions tests/BrewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public function test_getRunningServices_will_throw_exception_on_failure()
$onError(1, 'test error output');
});
swap(CommandLine::class, $cli);
resolve(Brew::class)->getRunningServices();
resolve(Brew::class)->getRunningServices(true);
}

public function test_getRunningServices_will_pass_to_brew_services_list_and_return_array()
Expand All @@ -319,7 +319,7 @@ public function test_getRunningServices_will_pass_to_brew_services_list_and_retu
])->andReturn('service1'.PHP_EOL.'service2'.PHP_EOL.PHP_EOL.'service3'.PHP_EOL);

swap(CommandLine::class, $cli);
$result = resolve(Brew::class)->getRunningServices('term');
$result = resolve(Brew::class)->getRunningServices(true);
$this->assertInstanceOf(Collection::class, $result);
$this->assertSame([
'service1',
Expand All @@ -328,6 +328,42 @@ public function test_getRunningServices_will_pass_to_brew_services_list_and_retu
], array_values($result->all()));
}

public function test_getAllRunningServices_will_return_both_root_and_user_services()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('run')->once()->withArgs([
'sudo brew services list | grep started | awk \'{ print $1; }\'',
Mockery::type('callable'),
])->andReturn('sudo_ran_service');
$cli->shouldReceive('runAsUser')->once()->withArgs([
'brew services list | grep started | awk \'{ print $1; }\'',
Mockery::type('callable'),
])->andReturn('user_ran_service');

swap(CommandLine::class, $cli);
$result = resolve(Brew::class)->getAllRunningServices();
$this->assertSame([
'sudo_ran_service',
'user_ran_service',
], array_values($result->all()));
}

public function test_getAllRunningServices_will_return_unique_services()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('run')->once()->andReturn('service1'.PHP_EOL.'service2'.PHP_EOL.'service1'.PHP_EOL);
$cli->shouldReceive('runAsUser')->once()->andReturn('service3'.PHP_EOL.'service4'.PHP_EOL.'service2'.PHP_EOL);

swap(CommandLine::class, $cli);
$result = resolve(Brew::class)->getAllRunningServices();
$this->assertSame([
'service1',
'service2',
'service3',
'service4',
], array_values($result->all()));
}

/**
* @dataProvider supportedPhpLinkPathProvider
*
Expand Down
6 changes: 3 additions & 3 deletions tests/PhpFpmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function test_fpm_is_configured_with_the_correct_user_group_and_port()
public function test_stopRunning_will_pass_filtered_result_of_getRunningServices_to_stopService()
{
$brewMock = Mockery::mock(Brew::class);
$brewMock->shouldReceive('getRunningServices')->once()
$brewMock->shouldReceive('getAllRunningServices')->once()
->andReturn(collect([
'php7.2',
'[email protected]',
Expand Down Expand Up @@ -83,7 +83,7 @@ public function test_use_version_will_convert_passed_php_version()
$brewMock->shouldReceive('link')->withArgs(['[email protected]', true]);
$brewMock->shouldReceive('linkedPhp');
$brewMock->shouldReceive('installed');
$brewMock->shouldReceive('getRunningServices')->andReturn(collect());
$brewMock->shouldReceive('getAllRunningServices')->andReturn(collect());
$brewMock->shouldReceive('stopService');

// Test both non prefixed and prefixed
Expand Down Expand Up @@ -128,7 +128,7 @@ public function test_use_version_if_already_linked_php_will_unlink_before_instal
$brewMock->shouldReceive('link')->withArgs(['[email protected]', true]);
$brewMock->shouldReceive('linkedPhp');
$brewMock->shouldReceive('installed');
$brewMock->shouldReceive('getRunningServices')->andReturn(collect());
$brewMock->shouldReceive('getAllRunningServices')->andReturn(collect());
$brewMock->shouldReceive('stopService');

// Test both non prefixed and prefixed
Expand Down

0 comments on commit 6ddca24

Please sign in to comment.