| 1 |
#!/usr/bin/env php |
| 2 |
<?php |
| 3 |
|
| 4 |
if (file_exists(__DIR__ . '/../vendor/autoload.php')) { |
| 5 |
require __DIR__ . '/../vendor/autoload.php'; |
| 6 |
} elseif (file_exists(__DIR__ . '/../../../autoload.php')) { |
| 7 |
require __DIR__ . '/../../../autoload.php'; |
| 8 |
} elseif (file_exists(__DIR__ . '/../autoload.php')) { |
| 9 |
require __DIR__ . '/../autoload.php'; |
| 10 |
} else { |
| 11 |
throw new RuntimeException('Unable to locate autoload.php file.'); |
| 12 |
} |
| 13 |
|
| 14 |
use JmesPath\Env; |
| 15 |
use JmesPath\DebugRuntime; |
| 16 |
|
| 17 |
$description = <<<EOT |
| 18 |
Runs a JMESPath expression on the provided input or a test case. |
| 19 |
|
| 20 |
Provide the JSON input and expression: |
| 21 |
echo '{}' | jp.php expression |
| 22 |
|
| 23 |
Or provide the path to a compliance script, a suite, and test case number: |
| 24 |
jp.php --file path_to_compliance_json --suite test_suite_number --case test_case_number [expression] |
| 25 |
|
| 26 |
EOT; |
| 27 |
|
| 28 |
$args = []; |
| 29 |
$currentKey = null; |
| 30 |
for ($i = 1, $total = count($argv); $i < $total; $i++) { |
| 31 |
if ($i % 2) { |
| 32 |
if (substr($argv[$i], 0, 2) == '--') { |
| 33 |
$currentKey = str_replace('--', '', $argv[$i]); |
| 34 |
} else { |
| 35 |
$currentKey = trim($argv[$i]); |
| 36 |
} |
| 37 |
} else { |
| 38 |
$args[$currentKey] = $argv[$i]; |
| 39 |
$currentKey = null; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
$expression = $currentKey; |
| 44 |
|
| 45 |
if (isset($args['file']) || isset($args['suite']) || isset($args['case'])) { |
| 46 |
if (!isset($args['file']) || !isset($args['suite']) || !isset($args['case'])) { |
| 47 |
die($description); |
| 48 |
} |
| 49 |
|
| 50 |
$path = realpath($args['file']); |
| 51 |
file_exists($path) or die('File not found at ' . $path); |
| 52 |
$json = json_decode(file_get_contents($path), true); |
| 53 |
$set = $json[$args['suite']]; |
| 54 |
$data = $set['given']; |
| 55 |
if (!isset($expression)) { |
| 56 |
$expression = $set['cases'][$args['case']]['expression']; |
| 57 |
echo "Expects\n=======\n"; |
| 58 |
if (isset($set['cases'][$args['case']]['result'])) { |
| 59 |
echo json_encode($set['cases'][$args['case']]['result'], JSON_PRETTY_PRINT) . "\n\n"; |
| 60 |
} elseif (isset($set['cases'][$args['case']]['error'])) { |
| 61 |
echo "{$set['cases'][$args['case']]['error']} error\n\n"; |
| 62 |
} else { |
| 63 |
echo "NULL\n\n"; |
| 64 |
} |
| 65 |
} |
| 66 |
} elseif (isset($expression)) { |
| 67 |
|
| 68 |
$data = json_decode(stream_get_contents(STDIN), true); |
| 69 |
} else { |
| 70 |
die($description); |
| 71 |
} |
| 72 |
|
| 73 |
$runtime = new DebugRuntime(Env::createRuntime()); |
| 74 |
$runtime($expression, $data); |