Skip to content
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

Tests: fix some test failures for Windows OS #7364

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

As this method is not an expect or assert, shouldn't it use the WordPress name convention?

Suggested change
public function normalizeDirectorySeparatorsInPath( $path ) {
public function normalize_directory_separators_in_path( $path ) {

Copy link
Member Author

Choose a reason for hiding this comment

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

The use of camelCase is deliberate for consistency with other expectation/assertion methods in the TestCase file.

Copy link
Contributor

Choose a reason for hiding this comment

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

But in this case, it's a public helper function and not an expectation or assertion. It is used by a new assertion, yes. But it could also be used within a test class / method as a helper for setting up conditions before doing an assertion or expectation, such as being done in this PR in test_get_block_templates_paths_dir_exists().

As it's a helper, I'm wondering if it should follow snake_case, similar to other helper methods.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, as I hinted at before, quite a lot of those helpers in the TestCase use camelCase, so I still think this is most consistent. Some examples:

  • skipOnAutomatedBranches()
  • expectDeprecated()
  • setExpectedIncorrectUsage()
  • knownWPBug()
  • prepareTemplate()

Note: AFAICS none of these are required to be camelCase by PHPUnit, still they are.

Copy link
Contributor

Choose a reason for hiding this comment

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

You're right. There are mixed naming conventions for the helpers, i.e. some are camelCase (as you note) and others are snake_case.

I don't have strong opinions. Given precedence, seems okay to use camelCase.

Note: Though out of scope for this PR, might be worth establishing a code standard for test case helper methods and updating the handbook.

Copy link
Member Author

Choose a reason for hiding this comment

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

Note: Though out of scope for this PR, might be worth establishing a code standard for test case helper methods and updating the handbook.

I vaguely remember we opened a ticket about this quite a while back (including about naming conventions for data providers and such). Would need to search for it, but I think it should still be open.

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
2 changes: 1 addition & 1 deletion tests/phpunit/tests/admin/includesFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function test_get_home_path() {
$home = get_option( 'home' );
$siteurl = get_option( 'siteurl' );
$sfn = $_SERVER['SCRIPT_FILENAME'];
$this->assertSame( str_replace( '\\', '/', ABSPATH ), get_home_path() );
$this->assertSamePathIgnoringDirectorySeparators( ABSPATH, get_home_path() );

update_option( 'home', 'http://localhost' );
update_option( 'siteurl', 'http://localhost/wp' );
Expand Down
4 changes: 3 additions & 1 deletion tests/phpunit/tests/block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ public function data_get_block_theme_folders() {
* @covers ::_get_block_templates_paths
*/
public function test_get_block_templates_paths_dir_exists() {
$theme_dir = get_template_directory();
$theme_dir = $this->normalizeDirectorySeparatorsInPath( get_template_directory() );
// Templates in the current theme.
$templates = array(
'parts/small-header.html',
Expand All @@ -415,6 +415,8 @@ static function ( $template ) use ( $theme_dir ) {
);

$template_paths = _get_block_templates_paths( $theme_dir );
$template_paths = array_map( array( $this, 'normalizeDirectorySeparatorsInPath' ), _get_block_templates_paths( $theme_dir ) );

$this->assertSameSets( $expected_template_paths, $template_paths );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function test_should_change_directory() {
'Changing working directory failed.'
);

$this->assertSame(
$this->assertSamePathIgnoringDirectorySeparators(
$path,
$cwd_result,
'The current working directory was incorrect.'
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ public function test_is_child_theme_false() {
*/
public function test_get_stylesheet_directory() {
switch_theme( 'block-theme-child' );
$this->assertSame( realpath( DIR_TESTDATA ) . '/themedir1/block-theme-child', get_stylesheet_directory() );
$this->assertSamePathIgnoringDirectorySeparators( realpath( DIR_TESTDATA ) . '/themedir1/block-theme-child', get_stylesheet_directory() );
}

/**
Expand All @@ -937,7 +937,7 @@ public function test_get_stylesheet_directory() {
*/
public function test_get_template_directory() {
switch_theme( 'block-theme-child' );
$this->assertSame( realpath( DIR_TESTDATA ) . '/themedir1/block-theme', get_template_directory() );
$this->assertSamePathIgnoringDirectorySeparators( realpath( DIR_TESTDATA ) . '/themedir1/block-theme', get_template_directory() );
}

/**
Expand Down
Loading