From 5894a9681023a1c35374d435d8f64eab7600f65c Mon Sep 17 00:00:00 2001 From: tkotosz Date: Sun, 5 Apr 2020 12:07:25 +0200 Subject: [PATCH 1/9] Make the working directory configurable --- .../Behat/Context/TestRunnerContextSpec.php | 15 ++---- src/TestRunnerContext.php | 53 ++++++++++++------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/spec/Bex/Behat/Context/TestRunnerContextSpec.php b/spec/Bex/Behat/Context/TestRunnerContextSpec.php index d64b04c..a048af6 100644 --- a/spec/Bex/Behat/Context/TestRunnerContextSpec.php +++ b/spec/Bex/Behat/Context/TestRunnerContextSpec.php @@ -23,7 +23,7 @@ class TestRunnerContextSpec extends ObjectBehavior { function let(Filesystem $filesystem, ProcessFactory $processFactory, Process $behatProcess) { - $this->beConstructedWith('bin/phantomjs', $filesystem, $processFactory); + $this->beConstructedWith('bin/phantomjs', null, $filesystem, $processFactory); $this->initFilesystemDouble($filesystem); $this->initProcessFactoryDouble($processFactory, $behatProcess); @@ -49,16 +49,6 @@ function it_creates_temporary_working_directory(Filesystem $filesystem) $this->createWorkingDirectory(); } - function it_removes_temporary_working_directory(Filesystem $filesystem) - { - $tempDir = sys_get_temp_dir() .'/behat-test-runner'; - - $filesystem->remove(Argument::containingString($tempDir))->shouldBeCalled(); - - $this->createWorkingDirectory(); - $this->clearWorkingDirectory(); - } - function it_saves_behat_configuration(PyStringNode $config, Filesystem $filesystem) { $config->getRaw()->willReturn('default:'); @@ -152,7 +142,7 @@ function it_does_not_run_browser_when_browser_binary_is_not_set( Argument::any() )->willReturn($browserProcess); - $this->beConstructedWith(null, $filesystem, $processFactory); + $this->beConstructedWith(null, null, $filesystem, $processFactory); $webServerProcess->start()->shouldBeCalled(); $browserProcess->start()->shouldNotBeCalled(); @@ -187,6 +177,7 @@ private function initFilesystemDouble($filesystem) { $filesystem->remove(Argument::type('string'))->willReturn(null); $filesystem->mkdir(Argument::type('string'), Argument::type('int'))->willReturn(null); + $filesystem->exists(Argument::type('string'))->willReturn(false); } private function initProcessFactoryDouble(ProcessFactory $processFactory, Process $behatProcess) diff --git a/src/TestRunnerContext.php b/src/TestRunnerContext.php index 1fcb376..16a7876 100644 --- a/src/TestRunnerContext.php +++ b/src/TestRunnerContext.php @@ -80,19 +80,25 @@ class TestRunnerContext implements SnippetAcceptingContext /** @var ProcessFactory $processFactory */ private $processFactory; + /** @var string[] List of created files */ + private $files = []; + /** * TestRunnerContext constructor. * * @param string|null $browserCommand Shell command which executes the tester browser + * @param string|null $workingDirectory * @param Filesystem|null $fileSystem * @param ProcessFactory|null $processFactory */ public function __construct( $browserCommand = null, + $workingDirectory = null, Filesystem $fileSystem = null, ProcessFactory $processFactory = null ) { $this->browserCommand = $browserCommand; + $this->workingDirectory = $workingDirectory; $this->filesystem = $fileSystem ?: new Filesystem(); $this->processFactory = $processFactory ?: new ProcessFactory(); } @@ -120,12 +126,21 @@ public function afterRunTests(AfterScenarioScope $scope) */ public function createWorkingDirectory() { - $this->workingDirectory = tempnam(sys_get_temp_dir(), 'behat-test-runner'); - $this->filesystem->remove($this->workingDirectory); - $this->filesystem->mkdir($this->workingDirectory . '/features/bootstrap', 0770); + if (empty($this->workingDirectory)) { + $this->workingDirectory = tempnam(sys_get_temp_dir(), 'behat-test-runner'); + } + + $featuresDirectory = $this->workingDirectory . '/features/bootstrap'; + + if (!$this->filesystem->exists($featuresDirectory)) { + $this->filesystem->mkdir($this->workingDirectory . '/features/bootstrap', 0770); + } $this->documentRoot = $this->workingDirectory .'/document_root'; - $this->filesystem->mkdir($this->documentRoot, 0770); + + if (!$this->filesystem->exists($featuresDirectory)) { + $this->filesystem->mkdir($this->documentRoot, 0770); + } } /** @@ -141,7 +156,8 @@ public function getWorkingDirectory() */ public function clearWorkingDirectory() { - $this->filesystem->remove($this->workingDirectory); + $this->filesystem->remove($this->files); + $this->files = []; } /** @@ -179,10 +195,9 @@ public function printTesterOutputOnFailure($scope) */ public function iHaveTheConfiguration(PyStringNode $config) { - $this->filesystem->dumpFile( - $this->workingDirectory.'/behat.yml', - $config->getRaw() - ); + $file = $this->workingDirectory.'/behat.yml'; + $this->filesystem->dumpFile($file, $config->getRaw()); + $this->files[] = $file; } /** @@ -190,21 +205,19 @@ public function iHaveTheConfiguration(PyStringNode $config) */ public function iHaveTheFeature(PyStringNode $content) { - $this->filesystem->dumpFile( - $this->workingDirectory.'/features/feature.feature', - $content->getRaw() - ); + $file = $this->workingDirectory . '/features/feature.feature'; + $this->filesystem->dumpFile($file, $content->getRaw()); + $this->files[] = $file; } - + /** * @Given I have the context: */ public function iHaveTheContext(PyStringNode $definition) { - $this->filesystem->dumpFile( - $this->workingDirectory.'/features/bootstrap/FeatureContext.php', - $definition->getRaw() - ); + $file = $this->workingDirectory.'/features/bootstrap/FeatureContext.php'; + $this->filesystem->dumpFile($file, $definition->getRaw()); + $this->files[] = $file; } /** @@ -223,7 +236,9 @@ public function iRunBehat($parameters = '', $phpParameters = '') */ public function iHaveTheFileInDocumentRoot($filename, PyStringNode $content) { - $this->filesystem->dumpFile($this->documentRoot .'/'. $filename, $content); + $file = $this->documentRoot .'/'. $filename; + $this->filesystem->dumpFile($file, $content); + $this->files[] = $file; } /** From 3df16989887ce240a8da0dd62f2d5922bc382666 Mon Sep 17 00:00:00 2001 From: tkotosz Date: Sun, 5 Apr 2020 12:39:12 +0200 Subject: [PATCH 2/9] Use /tmp/behat-test-runner as working dir by default --- README.md | 10 ++++++++++ src/TestRunnerContext.php | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f2da14e..be9d1fd 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,16 @@ default: browserCommand: %paths.base%/bin/phantomjs --webdriver=4444 ``` +You can configure the working directory like this: +```yml +default: + suites: + default: + contexts: + - Bex\Behat\Context\TestRunnerContext: + workingDirectory: path/to/your/working/dir # default: /tmp/behat-test-runner +``` + Usage ----- diff --git a/src/TestRunnerContext.php b/src/TestRunnerContext.php index 16a7876..ab6ed01 100644 --- a/src/TestRunnerContext.php +++ b/src/TestRunnerContext.php @@ -127,7 +127,7 @@ public function afterRunTests(AfterScenarioScope $scope) public function createWorkingDirectory() { if (empty($this->workingDirectory)) { - $this->workingDirectory = tempnam(sys_get_temp_dir(), 'behat-test-runner'); + $this->workingDirectory = sys_get_temp_dir() . '/behat-test-runner'; } $featuresDirectory = $this->workingDirectory . '/features/bootstrap'; From b05ae2bdfbb986f43581aace4dd351b958e5f941 Mon Sep 17 00:00:00 2001 From: tkotosz Date: Sun, 5 Apr 2020 14:52:14 +0200 Subject: [PATCH 3/9] Be open for extension --- src/TestRunnerContext.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/TestRunnerContext.php b/src/TestRunnerContext.php index ab6ed01..8fce949 100644 --- a/src/TestRunnerContext.php +++ b/src/TestRunnerContext.php @@ -60,28 +60,28 @@ class TestRunnerContext implements SnippetAcceptingContext { /** @var Filesystem $filesystem */ - private $filesystem; + protected $filesystem; /** @var string $workingDirectory Place for generated tests */ - private $workingDirectory; + protected $workingDirectory; /** @var string $documentRoot Root directory of the web server */ - private $documentRoot; + protected $documentRoot; /** @var Process[] $processes Active processes */ - private $processes = []; + protected $processes = []; /** @var Process $behatProcess Active behat process */ - private $behatProcess; + protected $behatProcess; /** @var string $browserCommand */ - private $browserCommand; + protected $browserCommand; /** @var ProcessFactory $processFactory */ - private $processFactory; + protected $processFactory; /** @var string[] List of created files */ - private $files = []; + protected $files = []; /** * TestRunnerContext constructor. From 2c46e24126320f23f1e3962dee6669789f10a805 Mon Sep 17 00:00:00 2001 From: tkotosz Date: Tue, 7 Apr 2020 07:30:00 +0200 Subject: [PATCH 4/9] fix issue when it tries to print behat process output even if it wasn't even started --- src/TestRunnerContext.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TestRunnerContext.php b/src/TestRunnerContext.php index 8fce949..2125d39 100644 --- a/src/TestRunnerContext.php +++ b/src/TestRunnerContext.php @@ -180,7 +180,7 @@ public function destroyProcesses() */ public function printTesterOutputOnFailure($scope) { - if (!$scope->getTestResult()->isPassed()) { + if ($this->behatProcess !== null && !$scope->getTestResult()->isPassed()) { $outputFile = sys_get_temp_dir() . '/behat-test-runner.out'; $this->filesystem->dumpFile( $outputFile, From b5fe2446ff645c09af042ebb5b9a2f5cde6c441b Mon Sep 17 00:00:00 2001 From: tkotosz Date: Tue, 7 Apr 2020 07:31:55 +0200 Subject: [PATCH 5/9] trigger clear working directory on destruct as well --- src/TestRunnerContext.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/TestRunnerContext.php b/src/TestRunnerContext.php index 2125d39..660ee49 100644 --- a/src/TestRunnerContext.php +++ b/src/TestRunnerContext.php @@ -103,6 +103,11 @@ public function __construct( $this->processFactory = $processFactory ?: new ProcessFactory(); } + public function __destruct() + { + $this->clearWorkingDirectory(); + } + /** * @BeforeScenario */ From 5f1c098318b0276da84daa7728f67ac885fb0741 Mon Sep 17 00:00:00 2001 From: tkotosz Date: Tue, 7 Apr 2020 07:36:23 +0200 Subject: [PATCH 6/9] fix bug in creating the document root dir --- src/TestRunnerContext.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TestRunnerContext.php b/src/TestRunnerContext.php index 660ee49..d54d33f 100644 --- a/src/TestRunnerContext.php +++ b/src/TestRunnerContext.php @@ -143,7 +143,7 @@ public function createWorkingDirectory() $this->documentRoot = $this->workingDirectory .'/document_root'; - if (!$this->filesystem->exists($featuresDirectory)) { + if (!$this->filesystem->exists($this->documentRoot)) { $this->filesystem->mkdir($this->documentRoot, 0770); } } From ebf033d59a0a5dc912cf88c89ef50f7b956d7e12 Mon Sep 17 00:00:00 2001 From: tkotosz Date: Tue, 7 Apr 2020 07:48:28 +0200 Subject: [PATCH 7/9] remove temp dir and add back related spec --- spec/Bex/Behat/Context/TestRunnerContextSpec.php | 10 ++++++++++ src/TestRunnerContext.php | 16 ++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/spec/Bex/Behat/Context/TestRunnerContextSpec.php b/spec/Bex/Behat/Context/TestRunnerContextSpec.php index a048af6..3564752 100644 --- a/spec/Bex/Behat/Context/TestRunnerContextSpec.php +++ b/spec/Bex/Behat/Context/TestRunnerContextSpec.php @@ -49,6 +49,16 @@ function it_creates_temporary_working_directory(Filesystem $filesystem) $this->createWorkingDirectory(); } + function it_removes_temporary_working_directory(Filesystem $filesystem) + { + $tempDir = sys_get_temp_dir() .'/behat-test-runner'; + + $filesystem->remove(Argument::containingString($tempDir))->shouldBeCalled(); + + $this->createWorkingDirectory(); + $this->clearWorkingDirectory(); + } + function it_saves_behat_configuration(PyStringNode $config, Filesystem $filesystem) { $config->getRaw()->willReturn('default:'); diff --git a/src/TestRunnerContext.php b/src/TestRunnerContext.php index d54d33f..50db0ee 100644 --- a/src/TestRunnerContext.php +++ b/src/TestRunnerContext.php @@ -83,6 +83,9 @@ class TestRunnerContext implements SnippetAcceptingContext /** @var string[] List of created files */ protected $files = []; + /** @var bool */ + protected $isAutogeneratedWorkingDirectory; + /** * TestRunnerContext constructor. * @@ -101,11 +104,7 @@ public function __construct( $this->workingDirectory = $workingDirectory; $this->filesystem = $fileSystem ?: new Filesystem(); $this->processFactory = $processFactory ?: new ProcessFactory(); - } - - public function __destruct() - { - $this->clearWorkingDirectory(); + $this->isAutogeneratedWorkingDirectory = ($workingDirectory === null); } /** @@ -161,7 +160,12 @@ public function getWorkingDirectory() */ public function clearWorkingDirectory() { - $this->filesystem->remove($this->files); + if ($this->isAutogeneratedWorkingDirectory) { + $this->filesystem->remove($this->workingDirectory); + } else { + $this->filesystem->remove($this->files); + } + $this->files = []; } From 42afba793cfb732bd097c61466a8914b247f3e9c Mon Sep 17 00:00:00 2001 From: tkotosz Date: Tue, 7 Apr 2020 07:54:19 +0200 Subject: [PATCH 8/9] add back tempnam usage --- README.md | 6 +++--- src/TestRunnerContext.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index be9d1fd..a4eef7a 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ default: - Bex\Behat\Context\TestRunnerContext ``` -You can configure the test web browser to be used for opening the pages, like this: +You can configure the test web browser to be used for opening the pages, like this (optional): ```yml default: suites: @@ -40,14 +40,14 @@ default: browserCommand: %paths.base%/bin/phantomjs --webdriver=4444 ``` -You can configure the working directory like this: +You can configure the working directory like this (optional): ```yml default: suites: default: contexts: - Bex\Behat\Context\TestRunnerContext: - workingDirectory: path/to/your/working/dir # default: /tmp/behat-test-runner + workingDirectory: path/to/your/working/dir # if not provided then a temporary working dir is autogenerated ``` Usage diff --git a/src/TestRunnerContext.php b/src/TestRunnerContext.php index 50db0ee..161a762 100644 --- a/src/TestRunnerContext.php +++ b/src/TestRunnerContext.php @@ -131,7 +131,7 @@ public function afterRunTests(AfterScenarioScope $scope) public function createWorkingDirectory() { if (empty($this->workingDirectory)) { - $this->workingDirectory = sys_get_temp_dir() . '/behat-test-runner'; + $this->workingDirectory = tempnam(sys_get_temp_dir(), 'behat-test-runner'); } $featuresDirectory = $this->workingDirectory . '/features/bootstrap'; From e814e3023f14bf7ffb581fee4db7aab8d52f8a76 Mon Sep 17 00:00:00 2001 From: tkotosz Date: Tue, 7 Apr 2020 11:32:27 +0200 Subject: [PATCH 9/9] move the working directory param to be the last parameter --- spec/Bex/Behat/Context/TestRunnerContextSpec.php | 4 ++-- src/TestRunnerContext.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/Bex/Behat/Context/TestRunnerContextSpec.php b/spec/Bex/Behat/Context/TestRunnerContextSpec.php index 3564752..7c16a45 100644 --- a/spec/Bex/Behat/Context/TestRunnerContextSpec.php +++ b/spec/Bex/Behat/Context/TestRunnerContextSpec.php @@ -23,7 +23,7 @@ class TestRunnerContextSpec extends ObjectBehavior { function let(Filesystem $filesystem, ProcessFactory $processFactory, Process $behatProcess) { - $this->beConstructedWith('bin/phantomjs', null, $filesystem, $processFactory); + $this->beConstructedWith('bin/phantomjs', $filesystem, $processFactory, null); $this->initFilesystemDouble($filesystem); $this->initProcessFactoryDouble($processFactory, $behatProcess); @@ -152,7 +152,7 @@ function it_does_not_run_browser_when_browser_binary_is_not_set( Argument::any() )->willReturn($browserProcess); - $this->beConstructedWith(null, null, $filesystem, $processFactory); + $this->beConstructedWith(null, $filesystem, $processFactory, null); $webServerProcess->start()->shouldBeCalled(); $browserProcess->start()->shouldNotBeCalled(); diff --git a/src/TestRunnerContext.php b/src/TestRunnerContext.php index 161a762..50fa866 100644 --- a/src/TestRunnerContext.php +++ b/src/TestRunnerContext.php @@ -90,20 +90,20 @@ class TestRunnerContext implements SnippetAcceptingContext * TestRunnerContext constructor. * * @param string|null $browserCommand Shell command which executes the tester browser - * @param string|null $workingDirectory * @param Filesystem|null $fileSystem * @param ProcessFactory|null $processFactory + * @param string|null $workingDirectory */ public function __construct( $browserCommand = null, - $workingDirectory = null, Filesystem $fileSystem = null, - ProcessFactory $processFactory = null + ProcessFactory $processFactory = null, + $workingDirectory = null ) { $this->browserCommand = $browserCommand; - $this->workingDirectory = $workingDirectory; $this->filesystem = $fileSystem ?: new Filesystem(); $this->processFactory = $processFactory ?: new ProcessFactory(); + $this->workingDirectory = $workingDirectory; $this->isAutogeneratedWorkingDirectory = ($workingDirectory === null); }