Skip to content

Commit e6a8fdd

Browse files
Tests: Introduce assertion for comparing file paths independent of OS-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
1 parent baff405 commit e6a8fdd

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

tests/phpunit/includes/abstract-testcase.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,40 @@ public function assertNonEmptyMultidimensionalArray( $actual, $message = '' ) {
10701070
}
10711071
}
10721072

1073+
/**
1074+
* Assert that two text strings representing file paths are the same, while ignoring
1075+
* OS-specific differences in the directory separators.
1076+
*
1077+
* This allows for tests to be compatible for running on both *nix based as well as Windows OS.
1078+
*
1079+
* @since 6.7.0
1080+
*
1081+
* @param string $path_a File or directory path.
1082+
* @param string $path_b File or directory path.
1083+
*/
1084+
public function assertSamePathIgnoringDirectorySeparators( $path_a, $path_b ) {
1085+
$path_a = $this->normalizeDirectorySeparatorsInPath( $path_a );
1086+
$path_b = $this->normalizeDirectorySeparatorsInPath( $path_b );
1087+
1088+
$this->assertSame( $path_a, $path_b );
1089+
}
1090+
1091+
/**
1092+
* Normalize directory separators in a file path to be a forward slash.
1093+
*
1094+
* @since 6.7.0
1095+
*
1096+
* @param string $path File or directory path.
1097+
* @return string The normalized file or directory path.
1098+
*/
1099+
public function normalizeDirectorySeparatorsInPath( $path ) {
1100+
if ( ! is_string( $path ) || PHP_OS_FAMILY !== 'Windows' ) {
1101+
return $path;
1102+
}
1103+
1104+
return strtr( $path, '\\', '/' );
1105+
}
1106+
10731107
/**
10741108
* Checks each of the WP_Query is_* functions/properties against expected boolean value.
10751109
*

0 commit comments

Comments
 (0)