Skip to content

Commit

Permalink
Start Amp only if PHP-Scoper is used (box-project#171)
Browse files Browse the repository at this point in the history
Starting Amp has an overhead and increase the likelyhood of rising an issue due to how Amp works. As
a result, it is better to not have to start it unless necessary. So far the current compactors are
fast enough to not require parallel processing, the build from Box without PHP-Scoper goes from 5s
with parallel processing to 3s without.

Closes box-project#135
  • Loading branch information
theofidry authored Apr 28, 2018
1 parent 85dc37a commit e697134
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ private function processContents(array $files, string $cwd): array
return [$local, $processedContents];
};

return is_parallel_processing_enabled()
return is_parallel_processing_enabled() && false === ($this->scoper instanceof NullScoper)
? wait(parallelMap($files, $processFile))
: array_map($processFile, $files)
;
Expand Down
23 changes: 11 additions & 12 deletions tests/BoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

namespace KevinGH\Box;

use Amp\MultiReasonException;
use Exception;
use InvalidArgumentException;
use KevinGH\Box\Compactor\FakeCompactor;
Expand Down Expand Up @@ -768,7 +767,7 @@ public function test_the_temporary_directory_created_for_box_is_removed_upon_fai
$this->box->addFiles(['/nowhere/foo'], false);

$this->fail('Expected exception to be thrown.');
} catch (MultiReasonException $exception) {
} catch (InvalidArgumentException $exception) {
$tmpDirs = iterator_to_array(
Finder::create()
->directories()
Expand All @@ -777,20 +776,20 @@ public function test_the_temporary_directory_created_for_box_is_removed_upon_fai
);

$boxDir = current(
array_filter(
$tmpDirs,
function (SplFileInfo $fileInfo) use ($boxTmp): bool {
return false === in_array(
$fileInfo->getRealPath(),
[realpath($boxTmp), realpath($this->tmp)],
true
);
}
array_filter(
$tmpDirs,
function (SplFileInfo $fileInfo) use ($boxTmp): bool {
return false === in_array(
$fileInfo->getRealPath(),
[realpath($boxTmp), realpath($this->tmp)],
true
);
}
)
);

$this->assertFalse(
$boxDir,
$boxDir,
sprintf(
'Did not expect to find the directory "%s".',
$boxDir
Expand Down
25 changes: 12 additions & 13 deletions tests/Console/Command/CompileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@

namespace KevinGH\Box\Console\Command;

use Amp\MultiReasonException;
use Amp\Parallel\Worker\TaskException;
use DirectoryIterator;
use Generator;
use InvalidArgumentException;
use KevinGH\Box\Compactor\Php;
use KevinGH\Box\Console\DisplayNormalizer;
use KevinGH\Box\Test\CommandTestCase;
Expand All @@ -33,10 +32,13 @@
use function preg_replace;
use function sort;

///**
// * @covers \KevinGH\Box\Console\Command\Compile
// * @runTestsInSeparateProcesses This is necessary as instantiating a PHAR in memory may load/autoload some stuff which
// * can create undesirable side-effects.
// */
/**
* @covers \KevinGH\Box\Console\Command\Compile
* @runTestsInSeparateProcesses This is necessary as instantiating a PHAR in memory may load/autoload some stuff which
* can create undesirable side-effects.
* @coversNothing
*/
class CompileTest extends CommandTestCase
{
Expand Down Expand Up @@ -408,6 +410,8 @@ public function test_it_can_build_a_PHAR_without_any_configuration(): void

$this->assertEquals($expectedFiles, $actualFiles, '', .0, 10, true);

unset($phar);
Phar::unlinkArchive('index.phar');
// Executes the compilation again

$commandTester->execute(
Expand Down Expand Up @@ -1103,15 +1107,10 @@ public function test_it_cannot_build_a_PHAR_using_unreadable_files(): void
);

$this->fail('Expected exception to be thrown.');
} catch (MultiReasonException $exception) {
$this->assertCount(1, $exception->getReasons());

/** @var TaskException $reason */
$reason = current($exception->getReasons());

} catch (InvalidArgumentException $exception) {
$this->assertRegExp(
'/^Uncaught .+?ArgumentException in worker with message ".+?" was expected to be readable\.".*$/',
$reason->getMessage()
'/^Path ".+?" was expected to be readable\.$/',
$exception->getMessage()
);
}
}
Expand Down

0 comments on commit e697134

Please sign in to comment.