Skip to content

Commit 01cdc35

Browse files
committed
Add tests.
1 parent d02ede5 commit 01cdc35

File tree

10 files changed

+149
-165
lines changed

10 files changed

+149
-165
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ jobs:
1212
- uses: actions/checkout@v3
1313
- name: Install Composer dependencies
1414
uses: php-actions/composer@v6
15+
with:
16+
php_version: "8.2"
1517
# Store version number without `v`
1618
- name: Write release version
1719
run: |

.github/workflows/tests.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
php: [8.2, 8.3, 8.4, 8.5]
16+
name: PHP ${{ matrix.php }}
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
- name: Setup PHP
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: ${{ matrix.php }}
24+
extensions: json, curl
25+
- name: Install dependencies
26+
run: composer update --prefer-dist --no-interaction --no-progress ${{ matrix.php == '8.5' && '--ignore-platform-req=php' || '' }}
27+
- name: Run tests
28+
run: composer test

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
.DS_Store
3-
vendor/
3+
vendor/
4+
composer.lock

composer.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,27 @@
77
],
88
"license": "MIT",
99
"require": {
10-
"php": "^8.0",
10+
"php": "^8.2",
1111
"joetannenbaum/alfred-workflow": "dev-master",
1212
"loilo/fuse": "^7.0"
1313
},
14+
"require-dev": {
15+
"pestphp/pest": "^3.0"
16+
},
1417
"minimum-stability": "stable",
1518
"prefer-stable": true,
1619
"repositories": [
1720
{
1821
"type": "vcs",
1922
"url": "https://github.com/mattstein/alfred-workflow"
2023
}
21-
]
24+
],
25+
"config": {
26+
"allow-plugins": {
27+
"pestphp/pest-plugin": true
28+
}
29+
},
30+
"scripts": {
31+
"test": "pest"
32+
}
2233
}

composer.lock

Lines changed: 0 additions & 132 deletions
This file was deleted.

helpers.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* Takes the comma-separated setting value of paths, cleans any leading
5+
* or trailing space, expands relative home references (`~/`) into
6+
* absolute paths, and joins them back together into a glob search string
7+
* using braces (`{/path/one,/path/two}/*`).
8+
*
9+
* @param string $settingValue
10+
* @param string $homePath
11+
* @return string
12+
*/
13+
function buildSearchPathString(string $settingValue, string $homePath): string
14+
{
15+
$searchPaths = array_map(static function($path) use ($homePath) {
16+
$path = trim($path);
17+
if (str_starts_with($path, '~/')) {
18+
// Expand `~/` to full path
19+
$fullHomePath = $homePath.'/';
20+
return substr_replace(
21+
$path,
22+
$fullHomePath,
23+
0,
24+
strlen('~/')
25+
);
26+
}
27+
return $path;
28+
}, explode(',', $settingValue));
29+
30+
return '{'.implode(',', $searchPaths).'}/*';
31+
}

phpunit.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4+
colors="true"
5+
>
6+
<testsuites>
7+
<testsuite name="Tests">
8+
<directory>tests</directory>
9+
</testsuite>
10+
</testsuites>
11+
</phpunit>

search.php

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
include 'vendor/autoload.php';
4+
include 'helpers.php';
45

56
use Alfred\Workflows\Workflow;
67
use Alfred\Workflows\ParamBuilder\Mod;
@@ -76,33 +77,3 @@
7677
}
7778

7879
$workflow->output();
79-
80-
/**
81-
* Takes the comma-separated setting value of paths, cleans any leading
82-
* or trailing space, expands relative home references (`~/`) into
83-
* absolute paths, and joins them back together into a glob search string
84-
* using braces (`{/path/one,/path/two}/*`).
85-
*
86-
* @param string $settingValue
87-
* @param string $homePath
88-
* @return string
89-
*/
90-
function buildSearchPathString(string $settingValue, string $homePath): string
91-
{
92-
$searchPaths = array_map(static function($path) use ($homePath) {
93-
$path = trim($path);
94-
if (str_starts_with($path, '~/')) {
95-
// Expand `~/` to full path
96-
$fullHomePath = $homePath.'/';
97-
return substr_replace(
98-
$path,
99-
$fullHomePath,
100-
0,
101-
strlen('~/')
102-
);
103-
}
104-
return $path;
105-
}, explode(',', $settingValue));
106-
107-
return '{'.implode(',', $searchPaths).'}/*';
108-
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
require_once __DIR__.'/../helpers.php';
4+
5+
describe('buildSearchPathString', function () {
6+
it('wraps a single path in braces with glob wildcard', function () {
7+
$result = buildSearchPathString('/Projects', '/Users/test');
8+
9+
expect($result)->toBe('{/Projects}/*');
10+
});
11+
12+
it('joins multiple comma-separated paths', function () {
13+
$result = buildSearchPathString('/Projects,/Work', '/Users/test');
14+
15+
expect($result)->toBe('{/Projects,/Work}/*');
16+
});
17+
18+
it('expands tilde to home directory', function () {
19+
$result = buildSearchPathString('~/Projects', '/Users/test');
20+
21+
expect($result)->toBe('{/Users/test/Projects}/*');
22+
});
23+
24+
it('expands tilde in multiple paths', function () {
25+
$result = buildSearchPathString('~/Projects,~/Work', '/Users/test');
26+
27+
expect($result)->toBe('{/Users/test/Projects,/Users/test/Work}/*');
28+
});
29+
30+
it('handles mixed absolute and tilde paths', function () {
31+
$result = buildSearchPathString('~/Projects,/var/www', '/Users/test');
32+
33+
expect($result)->toBe('{/Users/test/Projects,/var/www}/*');
34+
});
35+
36+
it('trims whitespace from paths', function () {
37+
$result = buildSearchPathString(' /Projects , /Work ', '/Users/test');
38+
39+
expect($result)->toBe('{/Projects,/Work}/*');
40+
});
41+
42+
it('trims whitespace and expands tilde together', function () {
43+
$result = buildSearchPathString(' ~/Projects ', '/Users/test');
44+
45+
expect($result)->toBe('{/Users/test/Projects}/*');
46+
});
47+
});

tests/Pest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Test Case
6+
|--------------------------------------------------------------------------
7+
|
8+
| The closure you provide to your test functions is always bound to a specific PHPUnit test
9+
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
10+
| need to change it using the "pest()" function to bind a different classes or traits.
11+
|
12+
*/
13+
14+
// pest()->extend(Tests\TestCase::class)->in('Feature');

0 commit comments

Comments
 (0)