-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
feat: make search path for BinaryFinder customizable. #43968
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Test; | ||
|
||
use OC\BinaryFinder; | ||
use OC\Memcache\ArrayCache; | ||
use OCP\ICache; | ||
use OCP\ICacheFactory; | ||
use OCP\IConfig; | ||
|
||
class BinaryFinderTest extends TestCase { | ||
private ICache $cache; | ||
private ICacheFactory $cacheFactory; | ||
private $oldEnv; | ||
|
||
protected function setUp(): void { | ||
$this->oldEnv = getenv('PATH'); | ||
// BinaryFinder always includes the "PATH" environment variable into the search path, | ||
// which we want to avoid in this test because they are not usually found in webserver | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Why should the path environment variable not be available in webserver deployments? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apache does not set or pass it through by default. Phpfpm does not set it as well. Not sure about others but I would suspect that most servers don't provide it by default. |
||
// deployments. | ||
putenv('PATH=""'); | ||
$this->cacheFactory = $this->createMock(ICacheFactory::class); | ||
$this->cache = new ArrayCache(); | ||
$this->cacheFactory->method('createLocal')->with('findBinaryPath')->willReturn($this->cache); | ||
} | ||
|
||
protected function tearDown(): void { | ||
putenv('PATH=' . $this->oldEnv); | ||
} | ||
|
||
public function testDefaultFindsCat() { | ||
$config = $this->createMock(IConfig::class); | ||
$config | ||
->method('getSystemValue') | ||
->with('binary_search_paths', $this->anything()) | ||
->will($this->returnCallback(function ($key, $default) { | ||
return $default; | ||
})); | ||
$finder = new BinaryFinder($this->cacheFactory, $config); | ||
$this->assertEquals($finder->findBinaryPath('cat'), '/usr/bin/cat'); | ||
$this->assertEquals($this->cache->get('cat'), '/usr/bin/cat'); | ||
} | ||
|
||
public function testDefaultDoesNotFindCata() { | ||
$config = $this->createMock(IConfig::class); | ||
$config | ||
->method('getSystemValue') | ||
->with('binary_search_paths', $this->anything()) | ||
->will($this->returnCallback(function ($key, $default) { | ||
return $default; | ||
})); | ||
$finder = new BinaryFinder($this->cacheFactory, $config); | ||
$this->assertFalse($finder->findBinaryPath('cata')); | ||
$this->assertFalse($this->cache->get('cata')); | ||
} | ||
|
||
public function testCustomPathFindsCat() { | ||
$config = $this->createMock(IConfig::class); | ||
$config | ||
->method('getSystemValue') | ||
->with('binary_search_paths', $this->anything()) | ||
->willReturn(['/usr/bin']); | ||
$finder = new BinaryFinder($this->cacheFactory, $config); | ||
$this->assertEquals($finder->findBinaryPath('cat'), '/usr/bin/cat'); | ||
$this->assertEquals($this->cache->get('cat'), '/usr/bin/cat'); | ||
} | ||
|
||
public function testWrongCustomPathDoesNotFindCat() { | ||
$config = $this->createMock(IConfig::class); | ||
$config | ||
->method('getSystemValue') | ||
->with('binary_search_paths') | ||
->willReturn(['/wrong']); | ||
$finder = new BinaryFinder($this->cacheFactory, $config); | ||
$this->assertFalse($finder->findBinaryPath('cat')); | ||
$this->assertFalse($this->cache->get('cat')); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we assume people are clever enough to purge the local cache when changing the config?
Or should we add e.g.
md5(json_encode($this->config->getSystemValue('binary_search_paths')))
to the cache prefix?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cache is only for 5 minutes + we currently cache not found/fallback results and find that acceptable. So I don't think it's necessary.