Skip to content

Commit

Permalink
👔 up: add more helper method for FS and FTB, add ci on php8.3
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 26, 2024
1 parent f5d0a8f commit eea510d
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [8.1, 8.2] #
php: [8.1, 8.2, 8.3] #
os: [ubuntu-latest, macOS-latest] # windows-latest,
# include:
# - os: 'ubuntu-latest'
Expand Down
34 changes: 12 additions & 22 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="test/bootstrap.php"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"

>
<testsuites>
<testsuite name="Php Library Test Suite">
<directory>test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="test/bootstrap.php" colors="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage/>
<testsuites>
<testsuite name="Php Library Test Suite">
<directory>test</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
49 changes: 48 additions & 1 deletion src/Extra/FileTreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function copy(string $srcFile, string $dstFile, ?callable $afterFn = null
* @param array $options = [
* 'include' => [], // limit copy files or dirs
* 'exclude' => [], // exclude files or dirs
* 'renderOn' => ['*.java', ], // patterns to render
* 'renderOn' => ['*.java', ], // patterns to render file
* 'afterFn' => function(string $newFile) {},
* ]
*
Expand Down Expand Up @@ -450,6 +450,18 @@ public function renderFile(string $tplFile, array $tplVars = []): static
return $this->tplFile($tplFile, '', $tplVars);
}

/**
* Render template vars in the give files, will update file contents to rendered.
*
* @param string ...$tplFiles
*
* @return $this
*/
public function renderFiles(string ...$tplFiles): static
{
return $this->tplFiles($tplFiles);
}

/**
* Create files from template files
*
Expand Down Expand Up @@ -640,6 +652,7 @@ protected function renderPathVars(string $path): string
'workdir' => $this->workdir,
]);

$vars['projectDir'] = $this->baseDir;
return Str::renderVars($path, $vars, '{%s}', true);
}

Expand Down Expand Up @@ -756,6 +769,40 @@ public function setTplVars(array $tplVars): self
return $this;
}

/**
* @param array $tplVars
*
* @return $this
*/
public function addTplVars(array $tplVars): self
{
$this->tplVars = array_merge($this->tplVars, $tplVars);
return $this;
}

/**
* @param string $key
* @param mixed $value
*
* @return $this
*/
public function setTplVar(string $key, mixed $value): self
{
$this->tplVars[$key] = $value;
return $this;
}

/**
* @param string $key
* @param mixed|null $default
*
* @return mixed
*/
public function getTplVar(string $key, mixed $default = null): mixed
{
return $this->tplVars[$key] ?? $default;
}

/**
* @param callable(string $newFile): void $afterCopy
*
Expand Down
24 changes: 12 additions & 12 deletions src/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,7 @@ public static function isExclude(string $path, array $patterns): bool
if (!$patterns) {
return false;
}

foreach ($patterns as $pattern) {
if ($pattern === '*' || $pattern === '**/*') {
return true;
}

if (fnmatch($pattern, $path)) {
return true;
}
}
return false;
return self::isMatch($path, $patterns);
}

/**
Expand All @@ -130,13 +120,23 @@ public static function isInclude(string $path, array $patterns): bool
if (!$patterns) {
return true;
}
return self::isMatch($path, $patterns);
}

/**
* @param string $path
* @param array $patterns
*
* @return bool
*/
public static function isMatch(string $path, array $patterns): bool
{
foreach ($patterns as $pattern) {
if ($pattern === '*' || $pattern === '**/*') {
return true;
}

if (fnmatch($pattern, $path)) {
if ($pattern === $path || fnmatch($pattern, $path)) {
return true;
}
}
Expand Down
37 changes: 24 additions & 13 deletions test/FsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use Toolkit\FsUtil\FS;
use Toolkit\Stdlib\OS;
use function strlen;
use function vdump;

Expand Down Expand Up @@ -37,32 +38,42 @@ public function testBasicFsMethods(): void
{
// join
$this->assertEquals('/ab', FS::join('/ab'));
$this->assertEquals('/ab/d', FS::join('/ab', '', 'd'));
$this->assertEquals('/ab/d/e', FS::join('/ab', 'd', 'e'));
$this->assertEquals('/ab', FS::join('/ab', '.'));
$this->assertEquals('/ab', FS::join('/ab', './'));
$this->assertEquals('/ab/cd', FS::join('/ab', './cd'));
if (OS::isWindows()) {
$this->assertEquals('/ab\\d', FS::join('/ab', '', 'd'));
$this->assertEquals('/ab\\d\\e', FS::join('/ab', 'd', 'e'));
$this->assertEquals('/ab\\cd', FS::join('/ab', './cd'));
} else {
$this->assertEquals('/ab/d', FS::join('/ab', 'd'));
$this->assertEquals('/ab/d/e', FS::join('/ab', 'd', 'e'));
$this->assertEquals('/ab/cd', FS::join('/ab', './cd'));
}

}

public function testIsExclude_isInclude(): void
{
$this->assertTrue(FS::isInclude('./abc.php', []));
$this->assertTrue(FS::isInclude('./abc.php', ['*']));
$this->assertTrue(FS::isInclude('./abc.php', ['*.php']));
$this->assertTrue(FS::isInclude('path/to/abc.php', ['*.php']));
$this->assertFalse(FS::isInclude('./abc.php', ['*.xml']));
$tests = [
['./abc.php', '*', true],
['./abc.php', '*.php', true],
['./abc.php', '*.yml', false],
['path/to/abc.php', '*.php', true],
];
foreach ($tests as $item) {
$this->assertEquals($item[2], FS::isInclude($item[0], [$item[1]]));
$this->assertEquals($item[2], FS::isExclude($item[0], [$item[1]]));
$this->assertEquals($item[2], FS::isMatch($item[0], [$item[1]]));
}

$this->assertTrue(FS::isInclude('./abc.php', []));
$this->assertFalse(FS::isExclude('./abc.php', []));
$this->assertTrue(FS::isExclude('./abc.php', ['*']));
$this->assertTrue(FS::isExclude('./abc.php', ['*.php']));
$this->assertTrue(FS::isExclude('path/to/abc.php', ['*.php']));
$this->assertFalse(FS::isExclude('./abc.php', ['*.yml']));
}

public function testRealpath(): void
{
$rPaths = [];
$tests = [
$tests = [
'~',
'~/.kite',
];
Expand Down

0 comments on commit eea510d

Please sign in to comment.