jp.php 2.4 KB

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