-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wp-now: accept arguments for
wp-now php
command (#451)
Accept multiple arguments for `wp-now php` command. Solves WordPress/wordpress-playground#437 To test, run `wp-now php --php=8.2 -- example.php --foo=bar --php=7.4`. Observe the output `This is "example.php" file and the php version is: 8.2.0-dev`
- Loading branch information
Showing
11 changed files
with
193 additions
and
106 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import startWPNow from './wp-now'; | ||
import { WPNowOptions } from './config'; | ||
import { disableOutput } from './output'; | ||
|
||
/** | ||
* Execute a PHP cli given its parameters. | ||
* | ||
* @param phpArgs - Arguments to pass to the PHP cli. The first argument should be the string 'php'. | ||
* @param options - Optional configuration object for WPNow. Defaults to an empty object. | ||
* @returns - Returns a Promise that resolves to an object containing | ||
* the exit name and status (0 for success). | ||
* @throws - Throws an error if the first element in phpArgs is not the string 'php'. | ||
*/ | ||
export async function executePHP( | ||
phpArgs: string[], | ||
options: WPNowOptions = {} | ||
) { | ||
if (phpArgs[0] !== 'php') { | ||
throw new Error( | ||
'The first argument to executePHP must be the string "php".' | ||
); | ||
} | ||
disableOutput(); | ||
const { phpInstances } = await startWPNow({ | ||
...options, | ||
numberOfPhpInstances: 2, | ||
}); | ||
const [, php] = phpInstances; | ||
|
||
try { | ||
php.useHostFilesystem(); | ||
await php.cli(phpArgs); | ||
} catch (resultOrError) { | ||
const success = | ||
resultOrError.name === 'ExitStatus' && resultOrError.status === 0; | ||
if (!success) { | ||
throw resultOrError; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
import { executePHP } from '../execute-php'; | ||
import getWpNowConfig from '../config'; | ||
import { runCli } from '../run-cli'; | ||
|
||
const exampleDir = path.join(__dirname, 'execute-php'); | ||
|
||
test('php file execution in index mode', async () => { | ||
const resultFilePath = path.join(exampleDir, 'hello-world-result.txt'); | ||
// reset result file | ||
fs.writeFileSync(resultFilePath, ''); | ||
const options = await getWpNowConfig({ | ||
path: exampleDir, | ||
}); | ||
await executePHP( | ||
['php', path.join(exampleDir, 'hello-world.php')], | ||
options | ||
); | ||
const output = fs.readFileSync(resultFilePath, 'utf8'); | ||
expect(output).toBe('Hello World!'); | ||
}); | ||
|
||
test('php file execution for each PHP Version', async () => { | ||
const resultFilePath = path.join(exampleDir, 'php-version-result.txt'); | ||
const options = await getWpNowConfig({ | ||
path: exampleDir, | ||
}); | ||
await executePHP(['php', path.join(exampleDir, 'php-version.php')], { | ||
...options, | ||
phpVersion: '7.4', | ||
}); | ||
let output = fs.readFileSync(resultFilePath, 'utf8'); | ||
expect(output.substring(0, 16)).toBe('PHP Version: 7.4'); | ||
|
||
await executePHP(['php', path.join(exampleDir, 'php-version.php')], { | ||
...options, | ||
phpVersion: '8.0', | ||
}); | ||
output = fs.readFileSync(resultFilePath, 'utf8'); | ||
expect(output.substring(0, 16)).toBe('PHP Version: 8.0'); | ||
|
||
await executePHP(['php', path.join(exampleDir, 'php-version.php')], { | ||
...options, | ||
phpVersion: '8.2', | ||
}); | ||
output = fs.readFileSync(resultFilePath, 'utf8'); | ||
expect(output.substring(0, 16)).toBe('PHP Version: 8.2'); | ||
|
||
fs.writeFileSync(resultFilePath, 'PHP Version: X.Y'); | ||
}); | ||
|
||
test('php throws an error if the first element is not the string "php"', async () => { | ||
const options = await getWpNowConfig({ | ||
path: exampleDir, | ||
}); | ||
try { | ||
await executePHP( | ||
['word-different-to-php', path.join(exampleDir, 'php-version.php')], | ||
{ | ||
...options, | ||
phpVersion: '7.4', | ||
} | ||
); | ||
} catch (error) { | ||
expect(error.message).toBe( | ||
'The first argument to executePHP must be the string "php".' | ||
); | ||
} | ||
}); | ||
|
||
describe('validate php arguments passed through yargs', () => { | ||
let output = ''; | ||
let consoleLogMock; | ||
let processExitMock; | ||
const argv = process.argv; | ||
beforeEach(() => { | ||
consoleLogMock = vi | ||
.spyOn(console, 'log') | ||
.mockImplementation((newLine: string) => { | ||
output += `${newLine}\n`; | ||
}); | ||
processExitMock = vi | ||
.spyOn(process, 'exit') | ||
.mockImplementation(() => null); | ||
}); | ||
|
||
afterEach(() => { | ||
output = ''; | ||
process.argv = argv; | ||
consoleLogMock.mockRestore(); | ||
processExitMock.mockRestore(); | ||
}); | ||
|
||
test('php should receive the correct yargs arguments', async () => { | ||
process.argv = ['node', 'wp-now', 'php', '--', '--version']; | ||
await runCli(); | ||
expect(output).toMatch(/PHP 8\.0(.*)\(cli\)/i); | ||
expect(processExitMock).toHaveBeenCalledWith(0); | ||
}); | ||
|
||
test('wp-now should change the php version', async () => { | ||
process.argv = [ | ||
'node', | ||
'wp-now', | ||
'php', | ||
'--php=7.4', | ||
'--', | ||
'--version', | ||
]; | ||
await runCli(); | ||
expect(output).toMatch(/PHP 7\.4(.*)\(cli\)/i); | ||
expect(processExitMock).toHaveBeenCalledWith(0); | ||
}); | ||
|
||
test('php should execute a file', async () => { | ||
const filePath = path.join(exampleDir, 'print-php-version.php'); | ||
process.argv = ['node', 'wp-now', 'php', filePath]; | ||
await runCli(); | ||
expect(output).toMatch(/8\.0/i); | ||
expect(processExitMock).toHaveBeenCalledWith(0); | ||
}); | ||
|
||
test('php should execute a file and change php version', async () => { | ||
const filePath = path.join(exampleDir, 'print-php-version.php'); | ||
process.argv = ['node', 'wp-now', 'php', '--php=7.4', '--', filePath]; | ||
await runCli(); | ||
expect(output).toMatch(/7\.4/i); | ||
expect(processExitMock).toHaveBeenCalledWith(0); | ||
}); | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php echo phpversion(); |