Skip to content

Commit

Permalink
Merge 9c0a8bf into 608df6a
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Jan 20, 2021
2 parents 608df6a + 9c0a8bf commit aa95c13
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/Listener/TerminateListener.php
Expand Up @@ -26,11 +26,11 @@
use function get_class;
use function getcwd;
use function gettype;
use function in_array;
use function is_array;
use function is_object;
use function is_string;
use function json_decode;
use function preg_match;
use function preg_replace;
use function realpath;
use function rtrim;
Expand All @@ -51,10 +51,7 @@ final class TerminateListener
'mezzio',
];

private const HOME_PATHS = [
'~',
'$HOME',
];
private const HOME_PATH_REGEX = '#^(~|\$HOME)#';

/** @var array */
private $config;
Expand Down Expand Up @@ -247,17 +244,17 @@ private function normalizePath(string $path): string
/**
* Resolve references to the HOME directory.
*
* Composer allows you to specify the strings "~" or "$HOME" for the
* config.vendor-dir setting. If so specified, it will replace it with the
* value of the $HOME path.
* Composer allows you to specify the strings "~" or "$HOME" within the
* config.vendor-dir setting. If so specified, it will replace those values
* with the value of the $HOME path.
*
* This routine detects the usage of one of those strings, and returns the
* value of $_SERVER['HOME'] if it exists. If not, it returns the $directory
* argument verbatim.
* This routine detects the usage of one of those strings, replacing it with
* the value of $_SERVER['HOME'] if it exists. If not, it returns the
* $directory argument verbatim.
*/
private function resolveHomePath(string $directory): string
{
if (! in_array($directory, self::HOME_PATHS, true)) {
if (! preg_match(self::HOME_PATH_REGEX, $directory)) {
return $directory;
}

Expand All @@ -267,6 +264,9 @@ private function resolveHomePath(string $directory): string

Assert::string($_SERVER['HOME']);

return $_SERVER['HOME'];
$updated = preg_replace(self::HOME_PATH_REGEX, $_SERVER['HOME'], $directory);
Assert::string($updated);

return $updated;
}
}
49 changes: 49 additions & 0 deletions test/Listener/TerminateListenerTest.php
Expand Up @@ -30,8 +30,11 @@
use Webmozart\Assert\Assert;

use function getcwd;
use function is_dir;
use function opendir;
use function preg_match;
use function preg_replace;
use function readdir;
use function realpath;
use function rtrim;

Expand Down Expand Up @@ -322,4 +325,50 @@ public function testVendorDirectorySpecifiedAsHomeInComposerSettingResolvesToHom
$r->invoke($listener, $composerJson)
);
}

private function getFirstHomeSubdirectory(string $home): ?string
{
Assert::directory($home);
$handle = opendir($home);
while (false !== ($entry = readdir($handle))) {
$path = $home . '/' . $entry;
if (! preg_match('/^\.{1,2}$/', $entry) && is_dir($path)) {
return $entry;
}
}

return null;
}

/**
* @dataProvider homeDirectorySpecifications
*/
public function testVendorDirectoryStartingWithHomeInComposerSettingResolvesViaHomeDirectory(string $spec): void
{
$home = $_SERVER['HOME'];
Assert::string($home);

$subdir = $this->getFirstHomeSubdirectory($home);
if (null === $subdir) {
$this->markTestSkipped('No $HOME subdirectory; cannot complete test');
}

$composerJson = <<<END
{
"config": {
"vendor-dir": "$spec/$subdir"
}
}
END;

$expected = rtrim(realpath(preg_replace('#\\\\#', '/', $home)), '/') . '/' . $subdir . '/';
$listener = new TerminateListener([]);
$r = new ReflectionMethod($listener, 'getVendorDirectory');
$r->setAccessible(true);

$this->assertSame(
$expected,
$r->invoke($listener, $composerJson)
);
}
}

0 comments on commit aa95c13

Please sign in to comment.