Skip to content

Commit

Permalink
Merge pull request #13 from creative-commoners/pulls/main/file-exists
Browse files Browse the repository at this point in the history
FIX Use check_file_exists()
  • Loading branch information
GuySartorelli authored Aug 16, 2023
2 parents 629ec4f + c7c4b5f commit 0354a80
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 1 addition & 2 deletions scripts/cms-any/license.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@
foreach (['LICENSE.md', 'license.md', 'license'] as $filename) {
rename_file_if_exists($filename, $licenseFilename);
}

// only update licence contents if module is on silverstripe account
if (module_account() === 'silverstripe') {
if (file_exists('LICENSE')) {
if (check_file_exists($licenseFilename)) {
$oldContents = read_file($licenseFilename);
$newContents = str_replace('SilverStripe', 'Silverstripe', $oldContents);
if ($newContents !== $oldContents) {
Expand Down
24 changes: 24 additions & 0 deletions tests/ScriptsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use PHPUnit\Framework\TestCase;

class ScriptsTest extends TestCase
{
/**
* This is a weird unit-test as it's essentially linting, though it's useful to ensure that the scripts are valid
*
* Ensure that file_exists() isn't used any scripts as it it's a native PHP function that does not use $MODULE_DIR
* Instead you need to use check_file_exists() which does use $MODULE_DIR
*/
public function testNoFileExists()
{
$scripts = glob(__DIR__ . '/../scripts/**/*.php');
foreach ($scripts as $script) {
$contents = file_get_contents($script);
$found = (bool) preg_match('#(?<!check_)file_exists\(#', $contents);
$this->assertFalse($found, "Script $script has file_exists() in it, use check_file_exists() instead");
}
// need at least one assertion or phpunit says this is a risky test
$this->assertTrue(true);
}
}

0 comments on commit 0354a80

Please sign in to comment.