Skip to content

Commit c592736

Browse files
committed
Improved cross os support
1 parent eaf56d0 commit c592736

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Unitary will, by default, find all files prefixed with "unitary-" recursively fr
3838

3939
Start by creating a test file with a name that starts with "unitary-", e.g., "unitary-lib-name.php". You can place the file inside `tests/unitary-lib-name.php` for example.
4040

41-
**Note: All of your library classes should be automatically be autoloaded when used in test file!**
41+
**Note: All of your library classes should be automatically be autoloaded if you are using composers autoloader inside your test file!**
4242

4343
```php
4444
<?php
@@ -74,14 +74,14 @@ $unit->add("Checking data type", function($inst) {
7474

7575
$unit->execute();
7676
```
77-
The example above uses both built-in validation and custom validation (see below for all built-in validation options).
77+
The example above uses both built-in validation and custom validation (see below for all built-in validation options).
7878

7979
### 2. Run the Tests
8080

8181
Now you are ready to execute the tests. Open your command line of choice, navigate (cd) to your project's root directory (where your `composer.json` file exists), and execute the following command:
8282

8383
```bash
84-
./vendor/bin/unitary
84+
php vendor/bin/unitary
8585
```
8686

8787
And that is it. Your tests have been successfully executed.
@@ -97,15 +97,15 @@ You can change the default root testing path and exclude files or whole director
9797
The path argument takes both absolute and relative paths. The command below will find all tests recursively from the "tests" directory.
9898

9999
```bash
100-
./vendor/bin/unitary --path="./tests/"
100+
php vendor/bin/unitary --path="./tests/"
101101
```
102102

103103
#### 2. Exclude Specific Files or Directories
104104

105105
The exclude argument will always be a relative path from the `--path` argument's path.
106106

107107
```bash
108-
./vendor/bin/unitary --exclude="./tests/unitary-query-php, tests/otherTests/*, */extras/*"
108+
php vendor/bin/unitary --exclude="./tests/unitary-query-php, tests/otherTests/*, */extras/*"
109109
```
110110

111111
## Example Breakdown

Unit.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,17 @@ private function findFiles($dir): array
148148
{
149149
$files = [];
150150
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
151+
152+
$key = 0;
151153
foreach ($iterator as $file) {
152154
if (fnmatch(static::PATTERN, $file->getFilename()) &&
153-
(isset($this->args['path']) || !str_contains($file->getPathname(), "vendor/"))) {
155+
(isset($this->args['path']) || !str_contains($file->getPathname(), DIRECTORY_SEPARATOR . "vendor" . DIRECTORY_SEPARATOR ))) {
156+
154157
if(!$this->findExcluded($this->exclude(), $dir, $file->getPathname())) {
155158
$files[] = $file->getPathname();
156159
}
157160
}
161+
158162
}
159163
return $files;
160164
}
@@ -169,9 +173,10 @@ function exclude(): array
169173
if(isset($this->args['exclude'])) {
170174
$exclude = explode(',', $this->args['exclude']);
171175
foreach ($exclude as $file) {
176+
$file = str_replace(['"', "'"], "", $file);
172177
$new = trim($file);
173178
$lastChar = substr($new, -1);
174-
if($lastChar === "/") {
179+
if($lastChar === DIRECTORY_SEPARATOR) {
175180
$new .= "*";
176181
}
177182
$excl[] = trim($new);
@@ -180,13 +185,32 @@ function exclude(): array
180185
return $excl;
181186
}
182187

188+
/**
189+
* Validate a exclude path
190+
* @param array $exclArr
191+
* @param string $relativeDir
192+
* @param string $file
193+
* @return bool
194+
*/
183195
function findExcluded(array $exclArr, string $relativeDir, string $file): bool
184196
{
197+
$file = $this->getNaturalPath($file);
185198
foreach ($exclArr as $excl) {
186-
if(fnmatch($relativeDir . "/". $excl, $file)) {
187-
return true;
199+
$relativeExclPath = $this->getNaturalPath($relativeDir . DIRECTORY_SEPARATOR . $excl);
200+
if(fnmatch($relativeExclPath, $file)) {
201+
return true;
188202
}
189203
}
190204
return false;
191205
}
192-
}
206+
207+
/**
208+
* Get path as natural path
209+
* @param string $path
210+
* @return string
211+
*/
212+
function getNaturalPath(string $path): string
213+
{
214+
return str_replace("\\", "/", $path);
215+
}
216+
}

0 commit comments

Comments
 (0)