Skip to content
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

[6.x] Add purge console command #887

Merged
merged 4 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions src/Console/PurgeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace Laravel\Dusk\Console;

use Illuminate\Console\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;

class PurgeCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dusk:purge';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Purge dusk test debugging files';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();

$this->ignoreValidationErrors();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->purgeScreenshots();
$this->purgeConsoleLogs();
$this->purgeSourceLogs();
}

/**
* Purge the failure screenshots.
*
* @return void
*/
protected function purgeScreenshots()
{
$this->purgeDebuggingFiles(
'tests/Browser/screenshots', 'failure-*'
);
}

/**
* Purge the console logs.
*
* @return void
*/
protected function purgeConsoleLogs()
{
$this->purgeDebuggingFiles(
'tests/Browser/console', '*.log'
);
}

/**
* Purge the source logs.
*
* @return void
*/
protected function purgeSourceLogs()
{
$this->purgeDebuggingFiles(
'tests/Browser/source', '*.txt'
);
}

/**
* Purge debugging files based on path and patterns.
*
* @param string $relativePath
* @param string $patterns
* @return void
*/
protected function purgeDebuggingFiles($relativePath, $patterns)
{
$path = base_path($relativePath);

if (! is_dir($path)) {
$this->warn(
"Unable to purge missing directory [{$relativePath}].", OutputInterface::VERBOSITY_DEBUG
);

return;
}

$files = Finder::create()->files()
->in($path)
->name($patterns);

foreach ($files as $file) {
@unlink($file->getRealPath());
}

$this->info("Purged \"{$patterns}\" from [{$relativePath}].");
}
}
1 change: 1 addition & 0 deletions src/DuskServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function boot()
Console\DuskFailsCommand::class,
Console\MakeCommand::class,
Console\PageCommand::class,
Console\PurgeCommand::class,
Console\ComponentCommand::class,
Console\ChromeDriverCommand::class,
]);
Expand Down