create-test.php 4.0 KB

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