testcmdline.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. """Test cases for the command line.
  2. To begin we test that "mypy <directory>[/]" always recurses down the
  3. whole tree.
  4. """
  5. from __future__ import annotations
  6. import os
  7. import re
  8. import subprocess
  9. import sys
  10. from mypy.test.config import PREFIX, test_temp_dir
  11. from mypy.test.data import DataDrivenTestCase, DataSuite
  12. from mypy.test.helpers import (
  13. assert_string_arrays_equal,
  14. check_test_output_files,
  15. normalize_error_messages,
  16. )
  17. try:
  18. import lxml # type: ignore[import]
  19. except ImportError:
  20. lxml = None
  21. import pytest
  22. # Path to Python 3 interpreter
  23. python3_path = sys.executable
  24. # Files containing test case descriptions.
  25. cmdline_files = ["cmdline.test", "cmdline.pyproject.test", "reports.test", "envvars.test"]
  26. class PythonCmdlineSuite(DataSuite):
  27. files = cmdline_files
  28. native_sep = True
  29. def run_case(self, testcase: DataDrivenTestCase) -> None:
  30. if lxml is None and os.path.basename(testcase.file) == "reports.test":
  31. pytest.skip("Cannot import lxml. Is it installed?")
  32. for step in [1] + sorted(testcase.output2):
  33. test_python_cmdline(testcase, step)
  34. def test_python_cmdline(testcase: DataDrivenTestCase, step: int) -> None:
  35. assert testcase.old_cwd is not None, "test was not properly set up"
  36. # Write the program to a file.
  37. program = "_program.py"
  38. program_path = os.path.join(test_temp_dir, program)
  39. with open(program_path, "w", encoding="utf8") as file:
  40. for s in testcase.input:
  41. file.write(f"{s}\n")
  42. args = parse_args(testcase.input[0])
  43. custom_cwd = parse_cwd(testcase.input[1]) if len(testcase.input) > 1 else None
  44. args.append("--show-traceback")
  45. if "--error-summary" not in args:
  46. args.append("--no-error-summary")
  47. if "--show-error-codes" not in args:
  48. args.append("--hide-error-codes")
  49. if "--disallow-empty-bodies" not in args:
  50. args.append("--allow-empty-bodies")
  51. if "--no-force-uppercase-builtins" not in args:
  52. args.append("--force-uppercase-builtins")
  53. if "--no-force-union-syntax" not in args:
  54. args.append("--force-union-syntax")
  55. # Type check the program.
  56. fixed = [python3_path, "-m", "mypy"]
  57. env = os.environ.copy()
  58. env.pop("COLUMNS", None)
  59. extra_path = os.path.join(os.path.abspath(test_temp_dir), "pypath")
  60. env["PYTHONPATH"] = PREFIX
  61. if os.path.isdir(extra_path):
  62. env["PYTHONPATH"] += os.pathsep + extra_path
  63. cwd = os.path.join(test_temp_dir, custom_cwd or "")
  64. args = [arg.replace("$CWD", os.path.abspath(cwd)) for arg in args]
  65. process = subprocess.Popen(
  66. fixed + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, env=env
  67. )
  68. outb, errb = process.communicate()
  69. result = process.returncode
  70. # Split output into lines.
  71. out = [s.rstrip("\n\r") for s in str(outb, "utf8").splitlines()]
  72. err = [s.rstrip("\n\r") for s in str(errb, "utf8").splitlines()]
  73. if "PYCHARM_HOSTED" in os.environ:
  74. for pos, line in enumerate(err):
  75. if line.startswith("pydev debugger: "):
  76. # Delete the attaching debugger message itself, plus the extra newline added.
  77. del err[pos : pos + 2]
  78. break
  79. # Remove temp file.
  80. os.remove(program_path)
  81. # Compare actual output to expected.
  82. if testcase.output_files:
  83. # Ignore stdout, but we insist on empty stderr and zero status.
  84. if err or result:
  85. raise AssertionError(
  86. "Expected zero status and empty stderr%s, got %d and\n%s"
  87. % (" on step %d" % step if testcase.output2 else "", result, "\n".join(err + out))
  88. )
  89. check_test_output_files(testcase, step)
  90. else:
  91. if testcase.normalize_output:
  92. out = normalize_error_messages(err + out)
  93. obvious_result = 1 if out else 0
  94. if obvious_result != result:
  95. out.append(f"== Return code: {result}")
  96. expected_out = testcase.output if step == 1 else testcase.output2[step]
  97. # Strip "tmp/" out of the test so that # E: works...
  98. expected_out = [s.replace("tmp" + os.sep, "") for s in expected_out]
  99. assert_string_arrays_equal(
  100. expected_out,
  101. out,
  102. "Invalid output ({}, line {}){}".format(
  103. testcase.file, testcase.line, " on step %d" % step if testcase.output2 else ""
  104. ),
  105. )
  106. def parse_args(line: str) -> list[str]:
  107. """Parse the first line of the program for the command line.
  108. This should have the form
  109. # cmd: mypy <options>
  110. For example:
  111. # cmd: mypy pkg/
  112. """
  113. m = re.match("# cmd: mypy (.*)$", line)
  114. if not m:
  115. return [] # No args; mypy will spit out an error.
  116. return m.group(1).split()
  117. def parse_cwd(line: str) -> str | None:
  118. """Parse the second line of the program for the command line.
  119. This should have the form
  120. # cwd: <directory>
  121. For example:
  122. # cwd: main/subdir
  123. """
  124. m = re.match("# cwd: (.*)$", line)
  125. return m.group(1) if m else None