Skip to content

Normalize temp dir in Given a directory step#329

Merged
swissspidy merged 2 commits intomainfrom
fix/realpath
Apr 14, 2026
Merged

Normalize temp dir in Given a directory step#329
swissspidy merged 2 commits intomainfrom
fix/realpath

Conversation

@swissspidy
Copy link
Copy Markdown
Member

@swissspidy swissspidy commented Apr 13, 2026

This fixes some failing Windows tests in language-command and potentially others using Given a(n) ... directory.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 13, 2026

Codecov Report

❌ Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Context/GivenStepDefinitions.php 0.00% 3 Missing ⚠️
src/Context/FeatureContext.php 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the temporary directory handling in GivenStepDefinitions.php to resolve macOS path normalization issues by incorporating realpath() and stripping the /private/var/ prefix. The review feedback recommends extending the path normalization to include /private/tmp/ to ensure consistency and refactoring the directory assignment logic for better readability.

Comment thread src/Context/GivenStepDefinitions.php
Comment thread src/Context/GivenStepDefinitions.php
@swissspidy swissspidy marked this pull request as ready for review April 14, 2026 10:38
@swissspidy swissspidy requested a review from a team as a code owner April 14, 2026 10:38
Copilot AI review requested due to automatic review settings April 14, 2026 10:38
Copy link
Copy Markdown
Contributor

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 updates the Behat test contexts to better normalize temp directory paths (notably on macOS) so the Given ... directory step can safely validate and delete directories under the system temp directory even when paths differ due to /private prefixing.

Changes:

  • Canonicalize sys_get_temp_dir() via realpath() before normalizing and comparing paths in given_a_specific_directory().
  • Add additional macOS-specific normalization for the computed temp dir.
  • Canonicalize the generated RUN_DIR path via realpath() after mkdir().

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/Context/GivenStepDefinitions.php Updates temp dir computation/normalization used for the safety check in the “Given a directory” step.
src/Context/FeatureContext.php Canonicalizes the newly created RUN_DIR path using realpath().

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

Comment on lines +56 to +62
$temp_dir = realpath( sys_get_temp_dir() );
$temp_dir = $temp_dir ? Path::normalize( $temp_dir ) : Path::normalize( sys_get_temp_dir() );
$dir = Path::normalize( $dir );

// Also normalize temp dir for Mac OS X.
$temp_dir = preg_replace( '|^/private/var/|', '/var/', $temp_dir );

Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

realpath( sys_get_temp_dir() ) can resolve macOS temp paths to /private/tmp (e.g. when sys_get_temp_dir() is /tmp), but the current normalization only rewrites /private/var. This can make the safety check reject directories under /tmp because $dir may start with /tmp while $temp_dir starts with /private/tmp. Consider normalizing a leading /private/ prefix (not just /private/var) for both $dir and $temp_dir, or allowing both the realpath and non-realpath variants when checking the prefix.

Copilot uses AI. Check for mistakes.
Comment on lines 63 to 64
// Also check for temp dir prefixed with `/private` for Mac OS X.
if ( 0 !== strpos( $dir, $temp_dir ) && 0 !== strpos( $dir, "/private{$temp_dir}" ) ) {
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

The containment check uses strpos( $dir, $temp_dir ) === 0, which can incorrectly allow paths that merely share a prefix (e.g. temp dir /tmp and dir /tmp2/...). To avoid accidental deletion outside the intended temp dir, tighten this to require a path boundary (exact match or prefix followed by a directory separator after normalization).

Copilot uses AI. Check for mistakes.
Comment thread src/Context/FeatureContext.php
Comment on lines +56 to +62
$temp_dir = realpath( sys_get_temp_dir() );
$temp_dir = $temp_dir ? Path::normalize( $temp_dir ) : Path::normalize( sys_get_temp_dir() );
$dir = Path::normalize( $dir );

// Also normalize temp dir for Mac OS X.
$temp_dir = preg_replace( '|^/private/var/|', '/var/', $temp_dir );

Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

The updated temp-dir canonicalization is OS/filesystem-dependent (e.g. /tmp vs /private/tmp, /var vs /private/var). There are Behat scenarios covering the “Given a specific directory” step, but none appear to exercise these canonicalization differences; adding a scenario that derives both sys_get_temp_dir() and realpath(sys_get_temp_dir()) and verifies both path forms are accepted would help prevent regressions on macOS runners.

Copilot uses AI. Check for mistakes.
@swissspidy swissspidy added this to the 5.1.8 milestone Apr 14, 2026
@swissspidy swissspidy merged commit 70f8de0 into main Apr 14, 2026
225 of 227 checks passed
@swissspidy swissspidy deleted the fix/realpath branch April 14, 2026 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants