Skip to content

Commit

Permalink
Standardise filepath separators to / (Fixes #145 #146)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols authored and sarjona committed Apr 3, 2024
1 parent 61d3311 commit 2845389
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
5 changes: 3 additions & 2 deletions moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,10 @@ public function process(File $file, $pointer) {
if ($bspos !== false) { // Only if there are level2 and down namespace.
$relns = str_replace('\\', '/', substr(trim($namespace, ' \\'), $bspos + 1));

$filename = MoodleUtil::getStandardisedFilename($file);
// Calculate the relative path under tests directory.
$dirpos = strripos(trim(dirname($file->getFilename()), ' /') . '/', '/tests/');
$reldir = str_replace('\\', '/', substr(trim(dirname($file->getFilename()), ' /'), $dirpos + 7));
$dirpos = strripos(trim(dirname($filename), ' /') . '/', '/tests/');
$reldir = str_replace('\\', '/', substr(trim(dirname($filename), ' /'), $dirpos + 7));

// Warning if the relative namespace does not match the relative directory.
if ($reldir !== $relns) {
Expand Down
6 changes: 5 additions & 1 deletion moodle/Tests/MoodleCSBaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,13 @@ protected function verifyCsResults() {
// Let's process the fixture.
try {
if ($this->fixtureFileName !== null) {
$fixtureFilename = $this->fixtureFileName;
if (DIRECTORY_SEPARATOR !== '/') {
$fixtureFilename = str_replace('/', DIRECTORY_SEPARATOR, $fixtureFilename);
}
$fixtureSource = file_get_contents($this->fixture);
$fixtureContent = <<<EOF
phpcs_input_file: {$this->fixtureFileName}
phpcs_input_file: {$fixtureFilename}
{$fixtureSource}
EOF;
$phpcsfile = new \PHP_CodeSniffer\Files\DummyFile($fixtureContent, $ruleset, $config);
Expand Down
2 changes: 1 addition & 1 deletion moodle/Util/Docblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public static function isValidTag(
$tag = ltrim($tokens[$tagPtr]['content'], '@');
if (array_key_exists($tag, self::$validTags)) {
if (array_key_exists($tag, self::$pathRestrictedTags)) {
$file = $phpcsFile->getFilename();
$file = MoodleUtil::getStandardisedFilename($phpcsFile);
foreach (self::$pathRestrictedTags[$tag] as $path) {
if (preg_match($path, $file)) {
return true;
Expand Down
29 changes: 22 additions & 7 deletions moodle/Util/MoodleUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,17 @@ public static function getMoodleComponent(File $file, $selfPath = true): ?string
}
}

$filepath = MoodleUtil::getStandardisedFilename($file);
// Let's find the first component that matches the file path.
foreach ($components as $component => $componentPath) {
// Only components with path.
if (empty($componentPath)) {
continue;
}

// Look for component paths matching the file path.
if (strpos($file->path, $componentPath . '/') === 0) {
$componentPath = str_replace('\\', '/', $componentPath . DIRECTORY_SEPARATOR);
if (strpos($filepath, $componentPath) === 0) {
// First match found should be the better one always. We are done.
return $component;
}
Expand Down Expand Up @@ -450,14 +453,15 @@ public static function getMoodleRoot(?File $file = null, bool $selfPath = true):
*/
public static function isLangFile(File $phpcsFile): bool
{
$filename = MoodleUtil::getStandardisedFilename($phpcsFile);
// If the file is not under a /lang/[a-zA-Z0-9_-]+/ directory, nothing to check.
// (note that we are using that regex because it's what PARAM_LANG does).
if (preg_match('~/lang/[a-zA-Z0-9_-]+/~', $phpcsFile->getFilename()) === 0) {
if (preg_match('~/lang/[a-zA-Z0-9_-]+/~', $filename) === 0) {
return false;
}

// If the file is not a PHP file, nothing to check.
if (substr($phpcsFile->getFilename(), -4) !== '.php') {
if (substr($filename, -4) !== '.php') {
return false;
}

Expand All @@ -476,28 +480,29 @@ public static function isLangFile(File $phpcsFile): bool
*/
public static function isUnitTest(File $phpcsFile): bool
{
$filename = MoodleUtil::getStandardisedFilename($phpcsFile);
// If the file isn't called, _test.php, nothing to check.
if (stripos(basename($phpcsFile->getFilename()), '_test.php') === false) {
return false;
}

// If the file isn't under tests directory, nothing to check.
if (stripos($phpcsFile->getFilename(), '/tests/') === false) {
if (stripos($filename, '/tests/') === false) {
return false;
}

// If the file is in a fixture directory, ignore it.
if (stripos($phpcsFile->getFilename(), '/tests/fixtures/') !== false) {
if (stripos($filename, '/tests/fixtures/') !== false) {
return false;
}

// If the file is in a generator directory, ignore it.
if (stripos($phpcsFile->getFilename(), '/tests/generator/') !== false) {
if (stripos($filename, '/tests/generator/') !== false) {
return false;
}

// If the file is in a behat directory, ignore it.
if (stripos($phpcsFile->getFilename(), '/tests/behat/') !== false) {
if (stripos($filename, '/tests/behat/') !== false) {
return false;
}

Expand Down Expand Up @@ -609,4 +614,14 @@ public static function getTokensOnLine(
ARRAY_FILTER_USE_BOTH
);
}

/**
* Get the standardised filename for the file.
*
* @param File @phpcsFile
* @return string
*/
public static function getStandardisedFilename(File $phpcsFile): string {
return str_replace('\\', '/', $phpcsFile->getFilename());
}
}

0 comments on commit 2845389

Please sign in to comment.