Skip to content

Commit

Permalink
Tests: Introduce assertion for comparing file paths independent of OS…
Browse files Browse the repository at this point in the history
…-specifics.

Introduces `WP_UnitTestCase_Base::assertSamePathIgnoringDirectorySeparators()` and an associated helper method `WP_UnitTestCase_Base::normalizeDirectorySeparatorsInPath()` to allow for comparing two file path strings independently of OS-specific differences.

The normalization is done in a separate method to also allow this method to be used for path normalization within test methods themselves, like for normalizing a group of paths in an array.

The pretty specific method name for the helper (`normalizeDirectorySeparatorsInPath()`) is an attempt to prevent naming conflicts with methods which may exist in plugin test suites build on top of the WP Core test suite.

Props jrf, hellofromTonya.
See #61530.

git-svn-id: https://develop.svn.wordpress.org/trunk@59057 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Sep 18, 2024
1 parent baff405 commit e6a8fdd
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/phpunit/includes/abstract-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,40 @@ public function assertNonEmptyMultidimensionalArray( $actual, $message = '' ) {
}
}

/**
* Assert that two text strings representing file paths are the same, while ignoring
* OS-specific differences in the directory separators.
*
* This allows for tests to be compatible for running on both *nix based as well as Windows OS.
*
* @since 6.7.0
*
* @param string $path_a File or directory path.
* @param string $path_b File or directory path.
*/
public function assertSamePathIgnoringDirectorySeparators( $path_a, $path_b ) {
$path_a = $this->normalizeDirectorySeparatorsInPath( $path_a );
$path_b = $this->normalizeDirectorySeparatorsInPath( $path_b );

$this->assertSame( $path_a, $path_b );
}

/**
* Normalize directory separators in a file path to be a forward slash.
*
* @since 6.7.0
*
* @param string $path File or directory path.
* @return string The normalized file or directory path.
*/
public function normalizeDirectorySeparatorsInPath( $path ) {
if ( ! is_string( $path ) || PHP_OS_FAMILY !== 'Windows' ) {
return $path;
}

return strtr( $path, '\\', '/' );
}

/**
* Checks each of the WP_Query is_* functions/properties against expected boolean value.
*
Expand Down

0 comments on commit e6a8fdd

Please sign in to comment.