test_runtests_pytest.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import pathlib
  2. from typing import List
  3. import pytest
  4. from sympy.testing.runtests_pytest import (
  5. make_absolute_path,
  6. sympy_dir,
  7. update_args_with_paths,
  8. )
  9. class TestMakeAbsolutePath:
  10. @staticmethod
  11. @pytest.mark.parametrize(
  12. 'partial_path', ['sympy', 'sympy/core', 'sympy/nonexistant_directory'],
  13. )
  14. def test_valid_partial_path(partial_path: str):
  15. """Paths that start with `sympy` are valid."""
  16. _ = make_absolute_path(partial_path)
  17. @staticmethod
  18. @pytest.mark.parametrize(
  19. 'partial_path', ['not_sympy', 'also/not/sympy'],
  20. )
  21. def test_invalid_partial_path_raises_value_error(partial_path: str):
  22. """A `ValueError` is raises on paths that don't start with `sympy`."""
  23. with pytest.raises(ValueError):
  24. _ = make_absolute_path(partial_path)
  25. class TestUpdateArgsWithPaths:
  26. @staticmethod
  27. def test_no_paths():
  28. """If no paths are passed, only `sympy` and `doc/src` are appended.
  29. `sympy` and `doc/src` are the `testpaths` stated in `pytest.ini`. They
  30. need to be manually added as if any path-related arguments are passed
  31. to `pytest.main` then the settings in `pytest.ini` may be ignored.
  32. """
  33. paths = []
  34. args = update_args_with_paths(paths=paths, keywords=None, args=[])
  35. expected = [
  36. str(pathlib.Path(sympy_dir(), 'sympy')),
  37. str(pathlib.Path(sympy_dir(), 'doc/src')),
  38. ]
  39. assert args == expected
  40. @staticmethod
  41. @pytest.mark.parametrize(
  42. 'path',
  43. ['sympy/core/tests/test_basic.py', '_basic']
  44. )
  45. def test_one_file(path: str):
  46. """Single files/paths, full or partial, are matched correctly."""
  47. args = update_args_with_paths(paths=[path], keywords=None, args=[])
  48. expected = [
  49. str(pathlib.Path(sympy_dir(), 'sympy/core/tests/test_basic.py')),
  50. ]
  51. assert args == expected
  52. @staticmethod
  53. def test_partial_path_from_root():
  54. """Partial paths from the root directly are matched correctly."""
  55. args = update_args_with_paths(paths=['sympy/functions'], keywords=None, args=[])
  56. expected = [str(pathlib.Path(sympy_dir(), 'sympy/functions'))]
  57. assert args == expected
  58. @staticmethod
  59. def test_multiple_paths_from_root():
  60. """Multiple paths, partial or full, are matched correctly."""
  61. paths = ['sympy/core/tests/test_basic.py', 'sympy/functions']
  62. args = update_args_with_paths(paths=paths, keywords=None, args=[])
  63. expected = [
  64. str(pathlib.Path(sympy_dir(), 'sympy/core/tests/test_basic.py')),
  65. str(pathlib.Path(sympy_dir(), 'sympy/functions')),
  66. ]
  67. assert args == expected
  68. @staticmethod
  69. @pytest.mark.parametrize(
  70. 'paths, expected_paths',
  71. [
  72. (
  73. ['/core', '/util'],
  74. [
  75. 'doc/src/modules/utilities',
  76. 'doc/src/reference/public/utilities',
  77. 'sympy/core',
  78. 'sympy/logic/utilities',
  79. 'sympy/utilities',
  80. ]
  81. ),
  82. ]
  83. )
  84. def test_multiple_paths_from_non_root(paths: List[str], expected_paths: List[str]):
  85. """Multiple partial paths are matched correctly."""
  86. args = update_args_with_paths(paths=paths, keywords=None, args=[])
  87. assert len(args) == len(expected_paths)
  88. for arg, expected in zip(sorted(args), expected_paths):
  89. assert expected in arg
  90. @staticmethod
  91. @pytest.mark.parametrize(
  92. 'paths',
  93. [
  94. [],
  95. ['sympy/physics'],
  96. ['sympy/physics/mechanics'],
  97. ['sympy/physics/mechanics/tests'],
  98. ['sympy/physics/mechanics/tests/test_kane3.py'],
  99. ]
  100. )
  101. def test_string_as_keyword(paths: List[str]):
  102. """String keywords are matched correctly."""
  103. keywords = ('bicycle', )
  104. args = update_args_with_paths(paths=paths, keywords=keywords, args=[])
  105. expected_args = ['sympy/physics/mechanics/tests/test_kane3.py::test_bicycle']
  106. assert len(args) == len(expected_args)
  107. for arg, expected in zip(sorted(args), expected_args):
  108. assert expected in arg
  109. @staticmethod
  110. @pytest.mark.parametrize(
  111. 'paths',
  112. [
  113. [],
  114. ['sympy/core'],
  115. ['sympy/core/tests'],
  116. ['sympy/core/tests/test_sympify.py'],
  117. ]
  118. )
  119. def test_integer_as_keyword(paths: List[str]):
  120. """Integer keywords are matched correctly."""
  121. keywords = ('3538', )
  122. args = update_args_with_paths(paths=paths, keywords=keywords, args=[])
  123. expected_args = ['sympy/core/tests/test_sympify.py::test_issue_3538']
  124. assert len(args) == len(expected_args)
  125. for arg, expected in zip(sorted(args), expected_args):
  126. assert expected in arg
  127. @staticmethod
  128. def test_multiple_keywords():
  129. """Multiple keywords are matched correctly."""
  130. keywords = ('bicycle', '3538')
  131. args = update_args_with_paths(paths=[], keywords=keywords, args=[])
  132. expected_args = [
  133. 'sympy/core/tests/test_sympify.py::test_issue_3538',
  134. 'sympy/physics/mechanics/tests/test_kane3.py::test_bicycle',
  135. ]
  136. assert len(args) == len(expected_args)
  137. for arg, expected in zip(sorted(args), expected_args):
  138. assert expected in arg
  139. @staticmethod
  140. def test_keyword_match_in_multiple_files():
  141. """Keywords are matched across multiple files."""
  142. keywords = ('1130', )
  143. args = update_args_with_paths(paths=[], keywords=keywords, args=[])
  144. expected_args = [
  145. 'sympy/integrals/tests/test_heurisch.py::test_heurisch_symbolic_coeffs_1130',
  146. 'sympy/utilities/tests/test_lambdify.py::test_python_div_zero_issue_11306',
  147. ]
  148. assert len(args) == len(expected_args)
  149. for arg, expected in zip(sorted(args), expected_args):
  150. assert expected in arg