Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2k
test: make random component execution safer#10169
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
811476f50dd52387e7924d78dc8926aada4a1f027bFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -13,7 +13,7 @@ AutoReview | ||
| Autoloader | ||
| # Cache | ||
| CLI | ||
| # Commands | ||
| Commands | ||
| Config | ||
| Cookie | ||
| # DataCaster | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -21,11 +21,28 @@ public function save(string $key, mixed $val): void | ||
| { | ||
| $val = var_export($val, true); | ||
| // Two processes may try to create the directory at the same time. | ||
| // is_dir() confirms it exists, so suppressing the warning is safe. | ||
| if (! is_dir($this->path) && ! @mkdir($this->path, 0777, true) && ! is_dir($this->path)) { | ||
| log_message('error', 'FactoriesCache: cannot create cache directory: ' . $this->path); | ||
| return; | ||
memleakd marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| // Write to temp file first to ensure atomicity | ||
| $tmp = $this->path . "/{$key}." . uniqid('', true) . '.tmp'; | ||
| file_put_contents($tmp, '<?php return ' . $val . ';', LOCK_EX); | ||
| if (file_put_contents($tmp, '<?php return ' . $val . ';', LOCK_EX) === false) { | ||
| log_message('warning', 'FactoriesCache: failed to write temp file for key: ' . $key); | ||
| return; | ||
memleakd marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| // Another process may have wiped the directory. Clean up on failure. | ||
| if (! @rename($tmp, $this->path . "/{$key}")) { | ||
memleakd marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| log_message('warning', 'FactoriesCache: failed to commit cache file for key: ' . $key); | ||
| rename($tmp, $this->path . "/{$key}"); | ||
| @unlink($tmp); | ||
memleakd marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| } | ||
| public function delete(string $key): void | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -19,7 +19,7 @@ | ||
| /** | ||
| * Log error messages to file system | ||
| * | ||
| * @see \CodeIgniter\Log\Handlers\FileHandlerTest | ||
| * @see FileHandlerTest | ||
| */ | ||
| class FileHandler extends BaseHandler | ||
| { | ||
| @@ -121,7 +121,8 @@ public function handle($level, $message): bool | ||
| fclose($fp); | ||
| if ($newfile) { | ||
| chmod($filepath, $this->filePermissions); | ||
| // The log entry is already persisted - permission changes are best-effort. | ||
| @chmod($filepath, $this->filePermissions); | ||
memleakd marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| return is_int($result); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -29,33 +29,19 @@ final class MigrateStatusTest extends CIUnitTestCase | ||
| use StreamFilterTrait; | ||
| use DatabaseTestTrait; | ||
| private string $migrationFileFrom = SUPPORTPATH . 'MigrationTestMigrations/Database/Migrations/2018-01-24-102301_Some_migration.php'; | ||
| private string $migrationFileTo = APPPATH . 'Database/Migrations/2018-01-24-102301_Some_migration.php'; | ||
| private string $migrationNamespace = 'Tests\\Support\\MigrationTestMigrations'; | ||
| private string $migrationNamespacePath = SUPPORTPATH . 'MigrationTestMigrations/'; | ||
| protected function setUp(): void | ||
| { | ||
| $this->resetServices(); | ||
| parent::setUp(); | ||
| Database::connect()->table('migrations')->emptyTable(); | ||
| Database::forge()->dropTable('foo', true); | ||
| if (! is_file($this->migrationFileFrom)) { | ||
| $this->fail(clean_path($this->migrationFileFrom) . ' is not found.'); | ||
| } | ||
| if (is_file($this->migrationFileTo)) { | ||
| @unlink($this->migrationFileTo); | ||
| } | ||
| copy($this->migrationFileFrom, $this->migrationFileTo); | ||
| $contents = file_get_contents($this->migrationFileTo); | ||
| $contents = str_replace( | ||
| 'namespace Tests\Support\MigrationTestMigrations\Database\Migrations;', | ||
| 'namespace App\Database\Migrations;', | ||
| $contents, | ||
| ); | ||
| file_put_contents($this->migrationFileTo, $contents); | ||
| service('autoloader')->addNamespace($this->migrationNamespace, $this->migrationNamespacePath); | ||
| putenv('NO_COLOR=1'); | ||
| CLI::init(); | ||
| @@ -66,13 +52,12 @@ protected function tearDown(): void | ||
| parent::tearDown(); | ||
| Database::connect()->table('migrations')->emptyTable(); | ||
| if (is_file($this->migrationFileTo)) { | ||
| @unlink($this->migrationFileTo); | ||
| } | ||
| Database::forge()->dropTable('foo', true); | ||
| putenv('NO_COLOR'); | ||
| CLI::init(); | ||
| $this->resetServices(); | ||
| } | ||
| public function testMigrateAllWithWithTwoNamespaces(): void | ||
| @@ -82,41 +67,31 @@ public function testMigrateAllWithWithTwoNamespaces(): void | ||
| command('migrate:status'); | ||
| $result = str_replace(PHP_EOL, "\n", $this->getStreamFilterBuffer()); | ||
| $result = preg_replace('/\d{4}-\d\d-\d\d \d\d:\d\d:\d\d/', 'YYYY-MM-DD HH:MM:SS', $result); | ||
| $expected = <<<'EOL' | ||
| +---------------+-------------------+--------------------+-------+---------------------+-------+ | ||
| | Namespace | Version | Filename | Group | Migrated On | Batch | | ||
| +---------------+-------------------+--------------------+-------+---------------------+-------+ | ||
| | App | 2018-01-24-102301 | Some_migration | tests | YYYY-MM-DD HH:MM:SS | 1 | | ||
| | Tests\Support | 20160428212500 | Create_test_tables | tests | YYYY-MM-DD HH:MM:SS | 1 | | ||
| +---------------+-------------------+--------------------+-------+---------------------+-------+ | ||
| EOL; | ||
| $this->assertSame($expected, $result); | ||
| $this->assertMigrationStatusHasBothNamespaceMigrations(); | ||
memleakd marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| public function testMigrateWithWithTwoNamespaces(): void | ||
| { | ||
| command('migrate -n App'); | ||
| command('migrate -n Tests\\\\Support\\\\MigrationTestMigrations'); | ||
| command('migrate -n Tests\\\\Support'); | ||
| $this->resetStreamFilterBuffer(); | ||
| command('migrate:status'); | ||
| $result = str_replace(PHP_EOL, "\n", $this->getStreamFilterBuffer()); | ||
| $result = preg_replace('/\d{4}-\d\d-\d\d \d\d:\d\d:\d\d/', 'YYYY-MM-DD HH:MM:SS', $result); | ||
| $expected = <<<'EOL' | ||
| +---------------+-------------------+--------------------+-------+---------------------+-------+ | ||
| | Namespace | Version | Filename | Group | Migrated On | Batch | | ||
| +---------------+-------------------+--------------------+-------+---------------------+-------+ | ||
| | App | 2018-01-24-102301 | Some_migration | tests | YYYY-MM-DD HH:MM:SS | 1 | | ||
| | Tests\Support | 20160428212500 | Create_test_tables | tests | YYYY-MM-DD HH:MM:SS | 2 | | ||
| +---------------+-------------------+--------------------+-------+---------------------+-------+ | ||
| $this->assertMigrationStatusHasBothNamespaceMigrations(); | ||
| } | ||
| EOL; | ||
| $this->assertSame($expected, $result); | ||
| private function assertMigrationStatusHasBothNamespaceMigrations(): void | ||
| { | ||
| $result = str_replace(PHP_EOL, "\n", $this->getStreamFilterBuffer()); | ||
| $theadPattern = '/^\|[[:space:]]+Namespace[[:space:]]+\|[[:space:]]+Version[[:space:]]+\|[[:space:]]+Filename[[:space:]]+\|[[:space:]]+Group[[:space:]]+\|[[:space:]]+Migrated On[[:space:]]+\|[[:space:]]+Batch[[:space:]]+\|$/m'; | ||
| $this->assertMatchesRegularExpression($theadPattern, $result); | ||
| $this->assertStringContainsString($this->migrationNamespace, $result); | ||
| $this->assertStringContainsString('2018-01-24-102301', $result); | ||
| $this->assertStringContainsString('Some_migration', $result); | ||
| $this->assertStringContainsString('Tests\Support', $result); | ||
| $this->assertStringContainsString('20160428212500', $result); | ||
| $this->assertStringContainsString('Create_test_tables', $result); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.