diff --git a/src/LogFile.php b/src/LogFile.php index 85245f0c..6faf98ae 100644 --- a/src/LogFile.php +++ b/src/LogFile.php @@ -58,6 +58,8 @@ public function logs(): LogReader public function size(): int { + clearstatcache(); + return filesize($this->path); } diff --git a/src/LogReader.php b/src/LogReader.php index eaef6f99..62d71651 100644 --- a/src/LogReader.php +++ b/src/LogReader.php @@ -364,6 +364,7 @@ public function scan(int $maxBytesToScan = null, bool $force = false): self if ($currentLog !== '' && preg_match($logMatchPattern, $currentLog) === 1) { if ((is_null($this->query) || preg_match($this->query, $currentLog))) { $logIndex->addToIndex($currentLogPosition, $currentTimestamp, $currentLogLevel, $currentIndex); + $currentIndex++; } } diff --git a/tests/Pest.php b/tests/Pest.php index 11e2963d..440a8410 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,5 +1,6 @@ $files - * @return void */ -function generateLogFiles(array $files): void +function generateLogFiles(array $files, string $content = null, bool $randomContent = false): array { - foreach ($files as $file) { - $file = storage_path('logs/'.$file); + return array_map( + fn ($file) => generateLogFile($file, $content, $randomContent), + $files + ); +} - if (File::exists($file)) { - File::delete($file); - } +function generateLogFile(string $fileName = null, string $content = null, bool $randomContent = false): LogFile +{ + if (is_null($fileName)) { + $fileName = \Illuminate\Support\Str::random().'.log'; + } - File::put($file, str()->random()); + $path = storage_path('logs/'.$fileName); - test()->assertFileExists($file); + if (File::exists($path)) { + File::delete($path); } + + File::put($path, $content ?? ($randomContent ? dummyLogData() : '')); + + // we perform a regular PHP assertion, so it doesn't count towards the unit test assertion count. + assert(file_exists($path)); + + return new LogFile($fileName, $path); +} + +function dummyLogData(int $lines = null): string +{ + if (is_null($lines)) { + $lines = rand(1, 10); + } + + return implode("\n", array_map( + fn ($_) => makeLogEntry(), + range(1, $lines) + )); } function clearGeneratedLogFiles(): void @@ -42,6 +65,14 @@ function clearGeneratedLogFiles(): void File::cleanDirectory(storage_path('logs')); } +function makeLogEntry(Carbon $date = null, string $level = 'debug', string $message = 'Testing log entry'): string +{ + $dateFormatted = $date instanceof Carbon ? $date->toDateTimeString() : now()->toDateTimeString(); + $level = strtoupper($level); + + return "[$dateFormatted] local.$level: $message"; +} + function createLogIndex($file = null, $query = null, array $predefinedLogs = []): LogIndex { if (is_null($file)) { diff --git a/tests/Unit/LogReaderTest.php b/tests/Unit/LogReaderTest.php new file mode 100644 index 00000000..e6d7306d --- /dev/null +++ b/tests/Unit/LogReaderTest.php @@ -0,0 +1,41 @@ +file = generateLogFile(); + File::append($this->file->path, makeLogEntry()); +}); + +afterEach(fn () => clearGeneratedLogFiles()); + +it('can scan a log file', function () { + $logReader = $this->file->logs(); + expect($logReader->requiresScan())->toBeTrue(); + + $logReader->scan(); + + expect($logReader->requiresScan())->toBeFalse() + ->and($logReader->index()->total())->toBe(1); +}); + +it('can re-scan the file after a new entry has been added', function () { + $logReader = $this->file->logs(); + $logReader->scan(); + + \Spatie\TestTime\TestTime::addMinute(); + + File::append($this->file->path, PHP_EOL.makeLogEntry()); + + // re-instantiate the log reader to make sure we don't have anything cached + $this->file = new LogFile($this->file->name, $this->file->path); + $logReader = $this->file->logs(); + expect($logReader->requiresScan())->toBeTrue(); + + $logReader->scan(); + + expect($logReader->requiresScan())->toBeFalse() + ->and($logReader->index()->total())->toBe(2) + ->and($logReader->index()->getFlatIndex())->toHaveCount(2); +}); diff --git a/tests/Unit/ProcessingLaravelLogEntriesTest.php b/tests/Unit/LogTest.php similarity index 100% rename from tests/Unit/ProcessingLaravelLogEntriesTest.php rename to tests/Unit/LogTest.php