Skip to content

Commit

Permalink
wp-now: Fix php cli path error in dev mode (#75)
Browse files Browse the repository at this point in the history
<!-- Thanks for contributing to WordPress Playground Tools! -->
Fixes #16

## What?

<!-- In a few words, what is the PR actually doing? Include screenshots
or screencasts if applicable -->

I get an error when I use wp-now in development mode, symlinked using
the `npm link`, and try executing the PHP script in the current
directory.

## Why?

<!-- Why is this PR necessary? What problem is it solving? Reference any
existing previous issue(s) or PR(s), but please add a short summary
here, too -->

`wp-now php` should work in both cases consistently.

## How?

<!-- How is your PR addressing the issue at hand? What are the
implementation details? -->
PR adds a code that replaces the second argument with its absolute
version if the absolute version exists in the filesystem.

## Testing Instructions

1. Create a hello world PHP script
2. Run it with an absolute path
```
$ wp-now php /Volumes/Sites/vip-directory/test.php
```
3. Run it using the relative path
```
$ wp-now php test.php
```

Test both cases for wp-now installed using npm and for wp-now symlinked
using `npm link`.
  • Loading branch information
wojtekn authored Jun 23, 2023
1 parent 832a8ba commit fc891ad
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/wp-now/src/execute-php.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import startWPNow from './wp-now';
import { WPNowOptions } from './config';
import { disableOutput } from './output';
import * as path from 'path';
import fs from 'fs-extra';

/**
* Execute a PHP cli given its parameters.
Expand All @@ -21,14 +23,23 @@ export async function executePHP(
);
}
disableOutput();
const { phpInstances } = await startWPNow({
const { phpInstances, options: wpNowOptions } = await startWPNow({
...options,
numberOfPhpInstances: 2,
});
const [, php] = phpInstances;

try {
php.useHostFilesystem();
if (!path.isAbsolute(phpArgs[1])) {
const maybePhpFile = path.join(
wpNowOptions.projectPath,
phpArgs[1]
);
if (fs.existsSync(maybePhpFile)) {
phpArgs[1] = maybePhpFile;
}
}
await php.cli(phpArgs);
} catch (resultOrError) {
const success =
Expand Down
8 changes: 8 additions & 0 deletions packages/wp-now/src/tests/execute-php.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ describe('validate php execution', () => {
});
expect(output.substring(0, 16)).toBe('PHP Version: 8.2');
});
test('php file execution with non absolute path', async () => {
const options = await getWpNowConfig({
path: exampleDir,
});
await executePHP(['php', 'hello-world.php'], options);

expect(output).toMatch(/Hello World!/);
});

test('php throws an error if the first element is not the string "php"', async () => {
const options = await getWpNowConfig({
Expand Down

0 comments on commit fc891ad

Please sign in to comment.