Skip to content

Conversation

@guanguans
Copy link

When using die, exit related debugging statements [dd(...)] to debug code in testing, the tearDown method will not be called, resulting in the inability to delete temporary input file.

  • Register shutdown function to delete the temporary input file via FileSystem::delete
  • Avoid leftover test artifacts by removing fixture-derived files on process exit

Copilot AI review requested due to automatic review settings January 3, 2026 07:27
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a testing cleanup issue where temporary input files are not deleted when debugging statements like dd(), die(), or exit() prevent the normal execution of PHPUnit's tearDown() method. The solution registers a shutdown function to ensure file deletion even when the test process terminates abnormally.

Key Changes

  • Registers a shutdown handler in createInputFilePath to delete temporary test input files
  • Ensures cleanup occurs even when die or exit statements bypass tearDown()

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

$fixtureBasename = pathinfo($trimmedFixtureFilePath, PATHINFO_BASENAME);
return $inputFileDirectory . '/' . $fixtureBasename;
$inputFilePath = $inputFileDirectory . '/' . $fixtureBasename;
register_shutdown_function([FileSystem::class, 'delete'], $inputFilePath);
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registering a shutdown function for every test execution will accumulate multiple shutdown handlers throughout the test run. Each test that calls doTestFile will register a new shutdown function, and all of them will attempt to execute at process termination.

Additionally, when tearDown executes normally, it already deletes the temporary file. The shutdown function will then attempt to delete an already-deleted file, which may cause warnings or errors depending on how Nette\Utils\FileSystem::delete handles non-existent files.

A better approach would be to:

  1. Check if the file exists before attempting deletion in the shutdown function
  2. Or, use register_shutdown_function only in setUp/constructor and track files to delete, or
  3. Consider using a static property to track whether a shutdown handler has already been registered globally, registering it only once to clean up any remaining files

Copilot uses AI. Check for mistakes.
$fixtureBasename = pathinfo($trimmedFixtureFilePath, PATHINFO_BASENAME);
return $inputFileDirectory . '/' . $fixtureBasename;
$inputFilePath = $inputFileDirectory . '/' . $fixtureBasename;
register_shutdown_function([FileSystem::class, 'delete'], $inputFilePath);
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The shutdown function approach registers a handler for each test execution, which could lead to performance degradation in large test suites. Consider alternative approaches that are more efficient:

  1. Use a static collection to track all temporary files and register a single shutdown function once
  2. Override the PHPUnit tearDownAfterClass method to ensure cleanup happens at the class level
  3. Use a temporary directory that gets cleaned up once after all tests

The current implementation may result in hundreds or thousands of registered shutdown functions for large test suites, each attempting to delete a file that was likely already cleaned up by tearDown.

Copilot uses AI. Check for mistakes.
@guanguans guanguans force-pushed the main branch 2 times, most recently from 5035316 to f200aeb Compare January 3, 2026 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant