create-test.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | https://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Bob Weinand <bwoebi@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #####
  19. ## This is just a helper for intercepting stdin/stdout and the file and create a half-finished test.
  20. ## The output still may need adapting to match file names etc.
  21. #####
  22. error_reporting(-1);
  23. $phpdbg = getenv('TEST_PHPDBG_EXECUTABLE') ?: null;
  24. $pass_options = ' -qbI -n -d "error_reporting=32767" -d "display_errors=1" -d "display_startup_errors=1" -d "log_errors=0"';
  25. $file = "";
  26. $cmdargv = "";
  27. if (isset($argc) && $argc > 1) {
  28. $post_ddash = false;
  29. for ($i = 1; $i < $argc; $i++) {
  30. if ($argv[$i][0] == "-" && !$post_ddash) {
  31. switch (substr($argv[$i], 1)) {
  32. case "p":
  33. $phpdbg = $argv[++$i];
  34. break;
  35. case "n":
  36. $pass_options .= " -n";
  37. break;
  38. case "d":
  39. $pass_options .= " -d ".escapeshellarg($argv[++$i]);
  40. $ini[] = $argv[$i];
  41. break;
  42. case "-":
  43. $post_ddash = true;
  44. break;
  45. }
  46. } else {
  47. $real_argv[] = $argv[$i];
  48. }
  49. }
  50. if (isset($real_argv[0])) {
  51. $file = realpath($real_argv[0]);
  52. $cmdargv = implode(" ", array_map("escapeshellarg", array_slice($real_argv, 1)));
  53. }
  54. }
  55. $proc = proc_open("$phpdbg $pass_options $file -- $cmdargv", [["pipe", "r"], ["pipe", "w"], ["pipe", "w"]], $pipes);
  56. if (!$proc) {
  57. die("Couldn't start phpdbg\n");
  58. }
  59. $input = $output = "";
  60. stream_set_blocking(STDIN, false);
  61. do {
  62. $r = [$pipes[1], STDIN];
  63. $w = $e = null;
  64. $n = @stream_select($r, $w, $e, null);
  65. if ($n > 0) {
  66. if ("" != $in = fread(STDIN, 1024)) {
  67. $input .= $in;
  68. fwrite($pipes[0], $in);
  69. continue;
  70. }
  71. if (feof(STDIN)) {
  72. die("stdin closed?!\n");
  73. }
  74. if (feof($pipes[1])) {
  75. $n = false;
  76. } else {
  77. $output .= $c = fgetc($pipes[1]);
  78. echo $c;
  79. }
  80. }
  81. } while ($n !== false);
  82. stream_set_blocking(STDIN, true);
  83. print "\n";
  84. if (!isset($name)) {
  85. print "Specify the test description: ";
  86. $desc = trim(fgets(STDIN));
  87. }
  88. while (!isset($testfile)) {
  89. print "Specify the test file name (leave empty to write to stderr): ";
  90. $testfile = trim(fgets(STDIN));
  91. if ($testfile != "" && file_exists($testfile)) {
  92. print "That file already exists. Type y or yes to overwrite: ";
  93. $y = trim(fgets(STDIN));
  94. if ($y !== "y" && $y !== "yes") {
  95. unset($testfile);
  96. }
  97. }
  98. }
  99. $output = str_replace("string(".strlen($file).") \"$file\"", 'string(%d) "%s"', $output);
  100. $output = str_replace($file, "%s", $output);
  101. $input = trim($input);
  102. $testdata = <<<TEST
  103. --TEST--
  104. $desc
  105. --PHPDBG--
  106. $input
  107. --EXPECTF--
  108. $output
  109. TEST;
  110. if (!empty($ini)) {
  111. $testdata .= "\n--INI--\n".implode("\n", $ini);
  112. }
  113. if ($cmdargv != "") {
  114. $testdata .= "\n--ARGS--\n$cmdargv";
  115. }
  116. if ($file != "") {
  117. $testdata .= "\n--FILE--\n".file_get_contents($file);
  118. }
  119. if ($testfile == "") {
  120. print "\n";
  121. } elseif (file_put_contents($testfile, $testdata)) {
  122. print "Test saved to $testfile\n";
  123. } else {
  124. print "The test could not be saved to $testfile; outputting on stderr now\n";
  125. $testfile = "";
  126. }
  127. if ($testfile == "") {
  128. fwrite(STDERR, $testdata);
  129. }