(testcase: DataDrivenTestCase, step: int)
| 50 | |
| 51 | |
| 52 | def test_python_cmdline(testcase: DataDrivenTestCase, step: int) -> None: |
| 53 | assert testcase.old_cwd is not None, "test was not properly set up" |
| 54 | # Write the program to a file. |
| 55 | program = "_program.py" |
| 56 | program_path = os.path.join(test_temp_dir, program) |
| 57 | with open(program_path, "w", encoding="utf8") as file: |
| 58 | for s in testcase.input: |
| 59 | file.write(f"{s}\n") |
| 60 | args = parse_args(normalize_devnull(testcase.input[0])) |
| 61 | custom_cwd = parse_cwd(testcase.input[1]) if len(testcase.input) > 1 else None |
| 62 | args.append("--show-traceback") |
| 63 | if "--error-summary" not in args: |
| 64 | args.append("--no-error-summary") |
| 65 | if "--show-error-codes" not in args: |
| 66 | args.append("--hide-error-codes") |
| 67 | if "--disallow-empty-bodies" not in args: |
| 68 | args.append("--allow-empty-bodies") |
| 69 | # Type check the program. |
| 70 | fixed = [python3_path, "-m", "mypy"] |
| 71 | env = os.environ.copy() |
| 72 | env.pop("COLUMNS", None) |
| 73 | extra_path = os.path.join(os.path.abspath(test_temp_dir), "pypath") |
| 74 | env["PYTHONPATH"] = PREFIX |
| 75 | if os.path.isdir(extra_path): |
| 76 | env["PYTHONPATH"] += os.pathsep + extra_path |
| 77 | cwd = os.path.join(test_temp_dir, custom_cwd or "") |
| 78 | args = [arg.replace("$CWD", os.path.abspath(cwd)) for arg in args] |
| 79 | process = subprocess.Popen( |
| 80 | fixed + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, env=env |
| 81 | ) |
| 82 | outb, errb = process.communicate() |
| 83 | result = process.returncode |
| 84 | # Split output into lines. |
| 85 | out = [s.rstrip("\n\r") for s in str(outb, "utf8").splitlines()] |
| 86 | err = [s.rstrip("\n\r") for s in str(errb, "utf8").splitlines()] |
| 87 | |
| 88 | if "PYCHARM_HOSTED" in os.environ: |
| 89 | for pos, line in enumerate(err): |
| 90 | if line.startswith("pydev debugger: "): |
| 91 | # Delete the attaching debugger message itself, plus the extra newline added. |
| 92 | del err[pos : pos + 2] |
| 93 | break |
| 94 | |
| 95 | # Remove temp file. |
| 96 | os.remove(program_path) |
| 97 | # Compare actual output to expected. |
| 98 | if testcase.output_files: |
| 99 | # Ignore stdout, but we insist on empty stderr and zero status. |
| 100 | if err or result: |
| 101 | raise AssertionError( |
| 102 | "Expected zero status and empty stderr%s, got %d and\n%s" |
| 103 | % (" on step %d" % step if testcase.output2 else "", result, "\n".join(err + out)) |
| 104 | ) |
| 105 | check_test_output_files(testcase, step) |
| 106 | else: |
| 107 | if testcase.normalize_output: |
| 108 | out = normalize_error_messages(err + out) |
| 109 | obvious_result = 1 if out else 0 |
no test coverage detected
searching dependent graphs…