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

Don't display estimated search index generation time if it's less than 1 second #177

13 changes: 10 additions & 3 deletions packages/framework/src/Commands/HydeBuildSearchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public function handle(): int
$actionTime = microtime(true);

$this->comment('Generating documentation site search index...');
$this->line('<fg=gray> > This will take an estimated '.round($this->guesstimateGenerationTime() / 1000).' seconds. Terminal may seem non-responsive.</>');
$expected = $this->guesstimateGenerationTime();

if ($expected > 0) {
$this->line("<fg=gray> > This will take an estimated $expected seconds. Terminal may seem non-responsive.</>");
}
GeneratesDocumentationSearchIndexFile::run();

$this->line(' > Created <info>'.GeneratesDocumentationSearchIndexFile::$filePath.'</> in '.
Expand All @@ -67,9 +71,12 @@ protected function createSearchPage(): void
$this->getExecutionTimeInMs($actionTime)."ms\n");
}

protected function guesstimateGenerationTime(): float
/** @internal Estimated processing time per file in ms */
public static float $guesstimationFactor = 52.5;

protected function guesstimateGenerationTime(): int
{
return count(CollectionService::getDocumentationPageFiles()) * 52.5;
return round(count(CollectionService::getDocumentationPageFiles()) * static::$guesstimationFactor) / 1000;
}

protected function getExecutionTimeInMs(float $timeStart): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hyde\Framework\Testing\Feature\Commands;

use Hyde\Framework\Commands\HydeBuildSearchCommand;
use Hyde\Framework\Hyde;
use Hyde\Testing\TestCase;

Expand All @@ -10,14 +11,67 @@
*/
class HydeBuildSearchCommandTest extends TestCase
{
public function test_it_creates_the_search_json_file()
protected function setUp(): void
{
parent::setUp();
unlinkIfExists(Hyde::path('_site/docs/search.json'));
unlinkIfExists(Hyde::path('_site/docs/search.html'));
}

protected function tearDown(): void
{
unlinkIfExists(Hyde::path('_site/docs/search.html'));
unlinkIfExists(Hyde::path('_site/docs/search.json'));
HydeBuildSearchCommand::$guesstimationFactor = 52.5;
parent::tearDown();
}

public function test_it_creates_the_search_json_file()
{
$this->artisan('build:search')
->expectsOutput('Generating documentation site search index...')
->assertExitCode(0);

$this->assertFileExists(Hyde::path('_site/docs/search.json'));
unlink(Hyde::path('_site/docs/search.json'));
}

public function test_it_creates_the_search_page()
{
$this->artisan('build:search')
->expectsOutput('Generating documentation site search index...')
->assertExitCode(0);

$this->assertFileExists(Hyde::path('_site/docs/search.html'));
}

public function test_it_does_not_create_the_search_page_if_disabled()
{
config(['docs.create_search_page' => false]);
$this->artisan('build:search')
->expectsOutput('Generating documentation site search index...')
->assertExitCode(0);

$this->assertFileDoesNotExist(Hyde::path('_site/docs/search.html'));
}

public function test_it_does_not_display_the_estimation_message_when_it_is_less_than_1_second()
{
HydeBuildSearchCommand::$guesstimationFactor = 0;

$this->artisan('build:search')
->expectsOutput('Generating documentation site search index...')
->doesntExpectOutputToContain('> This will take an estimated')
->assertExitCode(0);
}

public function test_it_displays_the_estimation_message_when_it_is_greater_than_1_second()
{
HydeBuildSearchCommand::$guesstimationFactor = 1000;
touch(Hyde::path('_docs/foo.md'));
$this->artisan('build:search')
->expectsOutput('Generating documentation site search index...')
->expectsOutputToContain('> This will take an estimated')
->assertExitCode(0);
unlink(Hyde::path('_docs/foo.md'));
}
}