Make WordPress Core

Changeset 59057


Ignore:
Timestamp:
09/18/2024 06:20:43 PM (14 months ago)
Author:
hellofromTonya
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r57987 r59057  
    10691069            $this->assertNotEmpty( $sub_array, $message . ' Subitem of the array is empty.' );
    10701070        }
     1071    }
     1072
     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, '\\', '/' );
    10711105    }
    10721106
Note: See TracChangeset for help on using the changeset viewer.