diff options
author | nkozlovskiy <nmk@ydb.tech> | 2023-09-29 12:24:06 +0300 |
---|---|---|
committer | nkozlovskiy <nmk@ydb.tech> | 2023-09-29 12:41:34 +0300 |
commit | e0e3e1717e3d33762ce61950504f9637a6e669ed (patch) | |
tree | bca3ff6939b10ed60c3d5c12439963a1146b9711 /library/python/runtime_py3/test | |
parent | 38f2c5852db84c7b4d83adfcb009eb61541d1ccd (diff) | |
download | ydb-e0e3e1717e3d33762ce61950504f9637a6e669ed.tar.gz |
add ydb deps
Diffstat (limited to 'library/python/runtime_py3/test')
30 files changed, 863 insertions, 0 deletions
diff --git a/library/python/runtime_py3/test/.dist-info/METADATA b/library/python/runtime_py3/test/.dist-info/METADATA new file mode 100644 index 0000000000..bb36162199 --- /dev/null +++ b/library/python/runtime_py3/test/.dist-info/METADATA @@ -0,0 +1,14 @@ +Metadata-Version: 2.1 +Name: foo-bar +Version: 1.2.3 +Summary: +Home-page: https://foo.org/ +Author: Foo +Author-email: foo@ya.com +License: UNKNOWN +Platform: any +Classifier: Development Status :: 4 - Beta +Classifier: Programming Language :: Python :: 3 +Requires-Python: >=3.8 +Requires-Dist: Werkzeug (>=0.15) +Requires-Dist: Jinja2 (>=2.10.1) diff --git a/library/python/runtime_py3/test/.dist-info/RECORD b/library/python/runtime_py3/test/.dist-info/RECORD new file mode 100644 index 0000000000..dabbbff80d --- /dev/null +++ b/library/python/runtime_py3/test/.dist-info/RECORD @@ -0,0 +1 @@ +foo_bar.py,sha256=0000000000000000000000000000000000000000000,20 diff --git a/library/python/runtime_py3/test/.dist-info/entry_points.txt b/library/python/runtime_py3/test/.dist-info/entry_points.txt new file mode 100644 index 0000000000..f5e2fd2657 --- /dev/null +++ b/library/python/runtime_py3/test/.dist-info/entry_points.txt @@ -0,0 +1,2 @@ +[console_scripts] +foo_cli = foo_bar:cli diff --git a/library/python/runtime_py3/test/.dist-info/top_level.txt b/library/python/runtime_py3/test/.dist-info/top_level.txt new file mode 100644 index 0000000000..d2c068bc6b --- /dev/null +++ b/library/python/runtime_py3/test/.dist-info/top_level.txt @@ -0,0 +1 @@ +foo_bar diff --git a/library/python/runtime_py3/test/canondata/result.json b/library/python/runtime_py3/test/canondata/result.json new file mode 100644 index 0000000000..8e684deb43 --- /dev/null +++ b/library/python/runtime_py3/test/canondata/result.json @@ -0,0 +1,50 @@ +{ + "test_traceback.test_traceback[custom-default]": { + "stderr": { + "uri": "file://test_traceback.test_traceback_custom-default_/stderr.txt" + }, + "stdout": { + "uri": "file://test_traceback.test_traceback_custom-default_/stdout.txt" + } + }, + "test_traceback.test_traceback[custom-ultratb_color]": { + "stderr": { + "uri": "file://test_traceback.test_traceback_custom-ultratb_color_/stderr.txt" + }, + "stdout": { + "uri": "file://test_traceback.test_traceback_custom-ultratb_color_/stdout.txt" + } + }, + "test_traceback.test_traceback[custom-ultratb_verbose]": { + "stderr": { + "uri": "file://test_traceback.test_traceback_custom-ultratb_verbose_/stderr.txt" + }, + "stdout": { + "uri": "file://test_traceback.test_traceback_custom-ultratb_verbose_/stdout.txt" + } + }, + "test_traceback.test_traceback[main-default]": { + "stderr": { + "uri": "file://test_traceback.test_traceback_main-default_/stderr.txt" + }, + "stdout": { + "uri": "file://test_traceback.test_traceback_main-default_/stdout.txt" + } + }, + "test_traceback.test_traceback[main-ultratb_color]": { + "stderr": { + "uri": "file://test_traceback.test_traceback_main-ultratb_color_/stderr.txt" + }, + "stdout": { + "uri": "file://test_traceback.test_traceback_main-ultratb_color_/stdout.txt" + } + }, + "test_traceback.test_traceback[main-ultratb_verbose]": { + "stderr": { + "uri": "file://test_traceback.test_traceback_main-ultratb_verbose_/stderr.txt" + }, + "stdout": { + "uri": "file://test_traceback.test_traceback_main-ultratb_verbose_/stdout.txt" + } + } +} diff --git a/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-default_/stderr.txt b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-default_/stderr.txt new file mode 100644 index 0000000000..a18678b50e --- /dev/null +++ b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-default_/stderr.txt @@ -0,0 +1,14 @@ +Traceback (most recent call last): + File "library/python/runtime_py3/test/traceback/crash.py", line 42, in main + one() + File "library/python/runtime_py3/test/traceback/crash.py", line 11, in one + modfunc(two) # aaa + ^^^^^^^^^^^^ + File "library/python/runtime_py3/test/traceback/mod/__init__.py", line 3, in modfunc + f() # call back to caller + ^^^ + File "library/python/runtime_py3/test/traceback/crash.py", line 15, in two + three(42) + File "library/python/runtime_py3/test/traceback/crash.py", line 19, in three + raise RuntimeError(f"Kaboom! I'm dead: {x}") +RuntimeError: Kaboom! I'm dead: 42 diff --git a/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-default_/stdout.txt b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-default_/stdout.txt new file mode 100644 index 0000000000..2c9793eb14 --- /dev/null +++ b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-default_/stdout.txt @@ -0,0 +1,2 @@ +__name__ = library.python.runtime_py3.test.traceback.crash +__file__ = library/python/runtime_py3/test/traceback/crash.py diff --git a/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-ultratb_color_/stderr.txt b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-ultratb_color_/stderr.txt new file mode 100644 index 0000000000..a28234f11c --- /dev/null +++ b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-ultratb_color_/stderr.txt @@ -0,0 +1,13 @@ +Traceback (most recent call last): + File library/python/runtime_py3/test/traceback/crash.py:42 in main + one() + File library/python/runtime_py3/test/traceback/crash.py:11 in one + modfunc(two) # aaa + File library/python/runtime_py3/test/traceback/mod/__init__.py:3 in modfunc + f() # call back to caller + File library/python/runtime_py3/test/traceback/crash.py:15 in two + three(42) + File library/python/runtime_py3/test/traceback/crash.py:19 in three + raise RuntimeError(f"Kaboom! I'm dead: {x}") +RuntimeError: Kaboom! I'm dead: 42 + diff --git a/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-ultratb_color_/stdout.txt b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-ultratb_color_/stdout.txt new file mode 100644 index 0000000000..2c9793eb14 --- /dev/null +++ b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-ultratb_color_/stdout.txt @@ -0,0 +1,2 @@ +__name__ = library.python.runtime_py3.test.traceback.crash +__file__ = library/python/runtime_py3/test/traceback/crash.py diff --git a/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-ultratb_verbose_/stderr.txt b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-ultratb_verbose_/stderr.txt new file mode 100644 index 0000000000..f5545c6976 --- /dev/null +++ b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-ultratb_verbose_/stderr.txt @@ -0,0 +1,27 @@ +--------------------------------------------------------------------------- +RuntimeError Traceback (most recent call last) +File library/python/runtime_py3/test/traceback/crash.py:42, in main() + 39 time.time = lambda: 1531996624.0 # Freeze time + 40 sys.executable = "<traceback test>" +---> 42 one() + +File library/python/runtime_py3/test/traceback/crash.py:11, in one() + 10 def one(): +---> 11 modfunc(two) + +File library/python/runtime_py3/test/traceback/mod/__init__.py:3, in modfunc(f=<function two>) + 1 def modfunc(f): + 2 # lalala +----> 3 f() + f = <function two> + +File library/python/runtime_py3/test/traceback/crash.py:15, in two() + 14 def two(): +---> 15 three(42) + +File library/python/runtime_py3/test/traceback/crash.py:19, in three(x=42) + 18 def three(x): +---> 19 raise RuntimeError(f"Kaboom! I'm dead: {x}") + x = 42 + +RuntimeError: Kaboom! I'm dead: 42 diff --git a/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-ultratb_verbose_/stdout.txt b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-ultratb_verbose_/stdout.txt new file mode 100644 index 0000000000..2c9793eb14 --- /dev/null +++ b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_custom-ultratb_verbose_/stdout.txt @@ -0,0 +1,2 @@ +__name__ = library.python.runtime_py3.test.traceback.crash +__file__ = library/python/runtime_py3/test/traceback/crash.py diff --git a/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-default_/stderr.txt b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-default_/stderr.txt new file mode 100644 index 0000000000..a18678b50e --- /dev/null +++ b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-default_/stderr.txt @@ -0,0 +1,14 @@ +Traceback (most recent call last): + File "library/python/runtime_py3/test/traceback/crash.py", line 42, in main + one() + File "library/python/runtime_py3/test/traceback/crash.py", line 11, in one + modfunc(two) # aaa + ^^^^^^^^^^^^ + File "library/python/runtime_py3/test/traceback/mod/__init__.py", line 3, in modfunc + f() # call back to caller + ^^^ + File "library/python/runtime_py3/test/traceback/crash.py", line 15, in two + three(42) + File "library/python/runtime_py3/test/traceback/crash.py", line 19, in three + raise RuntimeError(f"Kaboom! I'm dead: {x}") +RuntimeError: Kaboom! I'm dead: 42 diff --git a/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-default_/stdout.txt b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-default_/stdout.txt new file mode 100644 index 0000000000..2c9793eb14 --- /dev/null +++ b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-default_/stdout.txt @@ -0,0 +1,2 @@ +__name__ = library.python.runtime_py3.test.traceback.crash +__file__ = library/python/runtime_py3/test/traceback/crash.py diff --git a/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-ultratb_color_/stderr.txt b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-ultratb_color_/stderr.txt new file mode 100644 index 0000000000..a28234f11c --- /dev/null +++ b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-ultratb_color_/stderr.txt @@ -0,0 +1,13 @@ +Traceback (most recent call last): + File library/python/runtime_py3/test/traceback/crash.py:42 in main + one() + File library/python/runtime_py3/test/traceback/crash.py:11 in one + modfunc(two) # aaa + File library/python/runtime_py3/test/traceback/mod/__init__.py:3 in modfunc + f() # call back to caller + File library/python/runtime_py3/test/traceback/crash.py:15 in two + three(42) + File library/python/runtime_py3/test/traceback/crash.py:19 in three + raise RuntimeError(f"Kaboom! I'm dead: {x}") +RuntimeError: Kaboom! I'm dead: 42 + diff --git a/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-ultratb_color_/stdout.txt b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-ultratb_color_/stdout.txt new file mode 100644 index 0000000000..2c9793eb14 --- /dev/null +++ b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-ultratb_color_/stdout.txt @@ -0,0 +1,2 @@ +__name__ = library.python.runtime_py3.test.traceback.crash +__file__ = library/python/runtime_py3/test/traceback/crash.py diff --git a/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-ultratb_verbose_/stderr.txt b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-ultratb_verbose_/stderr.txt new file mode 100644 index 0000000000..f5545c6976 --- /dev/null +++ b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-ultratb_verbose_/stderr.txt @@ -0,0 +1,27 @@ +--------------------------------------------------------------------------- +RuntimeError Traceback (most recent call last) +File library/python/runtime_py3/test/traceback/crash.py:42, in main() + 39 time.time = lambda: 1531996624.0 # Freeze time + 40 sys.executable = "<traceback test>" +---> 42 one() + +File library/python/runtime_py3/test/traceback/crash.py:11, in one() + 10 def one(): +---> 11 modfunc(two) + +File library/python/runtime_py3/test/traceback/mod/__init__.py:3, in modfunc(f=<function two>) + 1 def modfunc(f): + 2 # lalala +----> 3 f() + f = <function two> + +File library/python/runtime_py3/test/traceback/crash.py:15, in two() + 14 def two(): +---> 15 three(42) + +File library/python/runtime_py3/test/traceback/crash.py:19, in three(x=42) + 18 def three(x): +---> 19 raise RuntimeError(f"Kaboom! I'm dead: {x}") + x = 42 + +RuntimeError: Kaboom! I'm dead: 42 diff --git a/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-ultratb_verbose_/stdout.txt b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-ultratb_verbose_/stdout.txt new file mode 100644 index 0000000000..2c9793eb14 --- /dev/null +++ b/library/python/runtime_py3/test/canondata/test_traceback.test_traceback_main-ultratb_verbose_/stdout.txt @@ -0,0 +1,2 @@ +__name__ = library.python.runtime_py3.test.traceback.crash +__file__ = library/python/runtime_py3/test/traceback/crash.py diff --git a/library/python/runtime_py3/test/resources/__init__.py b/library/python/runtime_py3/test/resources/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/library/python/runtime_py3/test/resources/__init__.py diff --git a/library/python/runtime_py3/test/resources/foo.txt b/library/python/runtime_py3/test/resources/foo.txt new file mode 100644 index 0000000000..ba0e162e1c --- /dev/null +++ b/library/python/runtime_py3/test/resources/foo.txt @@ -0,0 +1 @@ +bar
\ No newline at end of file diff --git a/library/python/runtime_py3/test/resources/submodule/__init__.py b/library/python/runtime_py3/test/resources/submodule/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/library/python/runtime_py3/test/resources/submodule/__init__.py diff --git a/library/python/runtime_py3/test/resources/submodule/bar.txt b/library/python/runtime_py3/test/resources/submodule/bar.txt new file mode 100644 index 0000000000..1910281566 --- /dev/null +++ b/library/python/runtime_py3/test/resources/submodule/bar.txt @@ -0,0 +1 @@ +foo
\ No newline at end of file diff --git a/library/python/runtime_py3/test/test_arcadia_source_finder.py b/library/python/runtime_py3/test/test_arcadia_source_finder.py new file mode 100644 index 0000000000..5110abc412 --- /dev/null +++ b/library/python/runtime_py3/test/test_arcadia_source_finder.py @@ -0,0 +1,389 @@ +import stat +import unittest +import yaml +from collections import namedtuple +from unittest.mock import patch +from parameterized import parameterized + +import __res as res + + +NAMESPACE_PREFIX = b"py/namespace/" +TEST_SOURCE_ROOT = "/home/arcadia" + + +class ImporterMocks: + def __init__(self, mock_fs, mock_resources): + self._mock_fs = mock_fs + self._mock_resources = mock_resources + self._patchers = [ + patch("__res.iter_keys", wraps=self._iter_keys), + patch("__res.__resource.find", wraps=self._resource_find), + patch("__res._path_isfile", wraps=self._path_isfile), + patch("__res._os.listdir", wraps=self._os_listdir), + patch("__res._os.lstat", wraps=self._os_lstat), + ] + for patcher in self._patchers: + patcher.start() + + def stop(self): + for patcher in self._patchers: + patcher.stop() + + def _iter_keys(self, prefix): + assert prefix == NAMESPACE_PREFIX + for k in self._mock_resources.keys(): + yield k, k.removeprefix(prefix) + + def _resource_find(self, key): + return self._mock_resources.get(key) + + def _lookup_mock_fs(self, filename): + path = filename.lstrip("/").split("/") + curdir = self._mock_fs + for item in path: + if item in curdir: + curdir = curdir[item] + else: + return None + return curdir + + def _path_isfile(self, filename): + f = self._lookup_mock_fs(filename) + return isinstance(f, str) + + def _os_lstat(self, filename): + f = self._lookup_mock_fs(filename) + mode = stat.S_IFDIR if isinstance(f, dict) else stat.S_IFREG + return namedtuple("fake_stat_type", "st_mode")(st_mode=mode) + + def _os_listdir(self, dirname): + f = self._lookup_mock_fs(dirname) + if isinstance(f, dict): + return f.keys() + else: + return [] + + +class ArcadiaSourceFinderTestCase(unittest.TestCase): + def setUp(self): + self.import_mock = ImporterMocks(yaml.safe_load(self._get_mock_fs()), self._get_mock_resources()) + self.arcadia_source_finder = res.ArcadiaSourceFinder(TEST_SOURCE_ROOT) + + def tearDown(self): + self.import_mock.stop() + + def _get_mock_fs(self): + raise NotImplementedError() + + def _get_mock_resources(self): + raise NotImplementedError() + + +class TestLibraryWithoutNamespace(ArcadiaSourceFinderTestCase): + def _get_mock_fs(self): + return """ + home: + arcadia: + project: + lib: + mod1.py: "" + package1: + mod2.py: "" + """ + + def _get_mock_resources(self): + return { + b"py/namespace/unique_prefix1/project/lib": b"project.lib.", + } + + @parameterized.expand( + [ + ("project.lib.mod1", b"project/lib/mod1.py"), + ("project.lib.package1.mod2", b"project/lib/package1/mod2.py"), + ("project.lib.unknown_module", None), + ("project.lib", None), # package + ] + ) + def test_get_module_path(self, module, path): + assert path == self.arcadia_source_finder.get_module_path(module) + + @parameterized.expand( + [ + ("project.lib.mod1", False), + ("project.lib.package1.mod2", False), + ("project", True), + ("project.lib", True), + ("project.lib.package1", True), + ] + ) + def test_is_packages(self, module, is_package): + assert is_package == self.arcadia_source_finder.is_package(module) + + def test_is_package_for_unknown_module(self): + self.assertRaises( + ImportError, + lambda: self.arcadia_source_finder.is_package("project.lib.package2"), + ) + + @parameterized.expand( + [ + ( + "", + { + ("PFX.project", True), + }, + ), + ( + "project.", + { + ("PFX.lib", True), + }, + ), + ( + "project.lib.", + { + ("PFX.mod1", False), + ("PFX.package1", True), + }, + ), + ( + "project.lib.package1.", + { + ("PFX.mod2", False), + }, + ), + ] + ) + def test_iter_modules(self, package_prefix, expected): + got = self.arcadia_source_finder.iter_modules(package_prefix, "PFX.") + assert expected == set(got) + + # Check iter_modules() don't crash and return correct result after not existing module was requested + def test_iter_modules_after_unknown_module_import(self): + self.arcadia_source_finder.get_module_path("project.unknown_module") + assert {("lib", True)} == set(self.arcadia_source_finder.iter_modules("project.", "")) + + +class TestLibraryExtendedFromAnotherLibrary(ArcadiaSourceFinderTestCase): + def _get_mock_fs(self): + return """ + home: + arcadia: + project: + lib: + mod1.py: '' + lib_extension: + mod2.py: '' + """ + + def _get_mock_resources(self): + return { + b"py/namespace/unique_prefix1/project/lib": b"project.lib.", + b"py/namespace/unique_prefix2/project/lib_extension": b"project.lib.", + } + + @parameterized.expand( + [ + ("project.lib.mod1", b"project/lib/mod1.py"), + ("project.lib.mod2", b"project/lib_extension/mod2.py"), + ] + ) + def test_get_module_path(self, module, path): + assert path == self.arcadia_source_finder.get_module_path(module) + + @parameterized.expand( + [ + ( + "project.lib.", + { + ("PFX.mod1", False), + ("PFX.mod2", False), + }, + ), + ] + ) + def test_iter_modules(self, package_prefix, expected): + got = self.arcadia_source_finder.iter_modules(package_prefix, "PFX.") + assert expected == set(got) + + +class TestNamespaceAndTopLevelLibraries(ArcadiaSourceFinderTestCase): + def _get_mock_fs(self): + return """ + home: + arcadia: + project: + ns_lib: + mod1.py: '' + top_level_lib: + mod2.py: '' + """ + + def _get_mock_resources(self): + return { + b"py/namespace/unique_prefix1/project/ns_lib": b"ns.", + b"py/namespace/unique_prefix2/project/top_level_lib": b".", + } + + @parameterized.expand( + [ + ("ns.mod1", b"project/ns_lib/mod1.py"), + ("mod2", b"project/top_level_lib/mod2.py"), + ] + ) + def test_get_module_path(self, module, path): + assert path == self.arcadia_source_finder.get_module_path(module) + + @parameterized.expand( + [ + ("ns", True), + ("ns.mod1", False), + ("mod2", False), + ] + ) + def test_is_packages(self, module, is_package): + assert is_package == self.arcadia_source_finder.is_package(module) + + @parameterized.expand( + [ + "project", + "project.ns_lib", + "project.top_level_lib", + ] + ) + def test_is_package_for_unknown_modules(self, module): + self.assertRaises(ImportError, lambda: self.arcadia_source_finder.is_package(module)) + + @parameterized.expand( + [ + ( + "", + { + ("PFX.ns", True), + ("PFX.mod2", False), + }, + ), + ( + "ns.", + { + ("PFX.mod1", False), + }, + ), + ] + ) + def test_iter_modules(self, package_prefix, expected): + got = self.arcadia_source_finder.iter_modules(package_prefix, "PFX.") + assert expected == set(got) + + +class TestIgnoreDirectoriesWithYaMakeFile(ArcadiaSourceFinderTestCase): + """Packages and modules from tests should not be part of pylib namespace""" + + def _get_mock_fs(self): + return """ + home: + arcadia: + contrib: + python: + pylib: + mod1.py: "" + tests: + conftest.py: "" + ya.make: "" + """ + + def _get_mock_resources(self): + return { + b"py/namespace/unique_prefix1/contrib/python/pylib": b"pylib.", + } + + def test_get_module_path_for_lib(self): + assert b"contrib/python/pylib/mod1.py" == self.arcadia_source_finder.get_module_path("pylib.mod1") + + def test_get_module_for_tests(self): + assert self.arcadia_source_finder.get_module_path("pylib.tests.conftest") is None + + def test_is_package_for_tests(self): + self.assertRaises(ImportError, lambda: self.arcadia_source_finder.is_package("pylib.tests")) + + +class TestMergingNamespaceAndDirectoryPackages(ArcadiaSourceFinderTestCase): + """Merge parent package (top level in this test) dirs with namespace dirs (DEVTOOLS-8979)""" + + def _get_mock_fs(self): + return """ + home: + arcadia: + contrib: + python: + pylint: + ya.make: "" + pylint: + __init__.py: "" + patcher: + patch.py: "" + ya.make: "" + """ + + def _get_mock_resources(self): + return { + b"py/namespace/unique_prefix1/contrib/python/pylint": b".", + b"py/namespace/unique_prefix1/contrib/python/pylint/patcher": b"pylint.", + } + + @parameterized.expand( + [ + ("pylint.__init__", b"contrib/python/pylint/pylint/__init__.py"), + ("pylint.patch", b"contrib/python/pylint/patcher/patch.py"), + ] + ) + def test_get_module_path(self, module, path): + assert path == self.arcadia_source_finder.get_module_path(module) + + +class TestEmptyResources(ArcadiaSourceFinderTestCase): + def _get_mock_fs(self): + return """ + home: + arcadia: + project: + lib: + mod1.py: '' + """ + + def _get_mock_resources(self): + return {} + + def test_get_module_path(self): + assert self.arcadia_source_finder.get_module_path("project.lib.mod1") is None + + def test_is_package(self): + self.assertRaises(ImportError, lambda: self.arcadia_source_finder.is_package("project")) + + def test_iter_modules(self): + assert [] == list(self.arcadia_source_finder.iter_modules("", "PFX.")) + + +class TestDictionaryChangedSizeDuringIteration(ArcadiaSourceFinderTestCase): + def _get_mock_fs(self): + return """ + home: + arcadia: + project: + lib1: + mod1.py: '' + lib2: + mod2.py: '' + """ + + def _get_mock_resources(self): + return { + b"py/namespace/unique_prefix1/project/lib1": b"project.lib1.", + b"py/namespace/unique_prefix1/project/lib2": b"project.lib2.", + } + + def test_no_crash_on_recusive_iter_modules(self): + for package in self.arcadia_source_finder.iter_modules("project.", ""): + for _ in self.arcadia_source_finder.iter_modules(package[0], ""): + pass diff --git a/library/python/runtime_py3/test/test_metadata.py b/library/python/runtime_py3/test/test_metadata.py new file mode 100644 index 0000000000..686c176468 --- /dev/null +++ b/library/python/runtime_py3/test/test_metadata.py @@ -0,0 +1,44 @@ +import importlib.metadata as im + +import pytest + + +@pytest.mark.parametrize("name", ("foo-bar", "foo_bar", "Foo-Bar")) +def test_distribution(name): + assert im.distribution(name) is not None + + +def test_unknown_package(): + with pytest.raises(im.PackageNotFoundError): + im.distribution("bar") + + +def test_version(): + assert im.version("foo-bar") == "1.2.3" + + +def test_metadata(): + assert im.metadata("foo-bar") is not None + + +def test_files(): + files = im.files("foo-bar") + assert len(files) == 1 + assert files[0].name == "foo_bar.py" + assert files[0].size == 20 + + +def test_requires(): + assert im.requires("foo-bar") == ["Werkzeug (>=0.15)", "Jinja2 (>=2.10.1)"] + + +def test_entry_points(): + entry_points = im.entry_points() + assert "console_scripts" in entry_points + + flg_found = False + for entry_point in entry_points["console_scripts"]: + if entry_point.name == "foo_cli" and entry_point.value == "foo_bar:cli": + flg_found = True + + assert flg_found diff --git a/library/python/runtime_py3/test/test_resources.py b/library/python/runtime_py3/test/test_resources.py new file mode 100644 index 0000000000..059cc039e6 --- /dev/null +++ b/library/python/runtime_py3/test/test_resources.py @@ -0,0 +1,73 @@ +import importlib.resources as ir + +import pytest + + +@pytest.mark.parametrize( + "package, resource", + ( + ("resources", "foo.txt"), + ("resources.submodule", "bar.txt"), + ), +) +def test_is_resource_good_path(package, resource): + assert ir.is_resource(package, resource) + + +@pytest.mark.parametrize( + "package, resource", + ( + ("resources", "111.txt"), + ("resources.submodule", "222.txt"), + ), +) +def test_is_resource_missing(package, resource): + assert not ir.is_resource(package, resource) + + +def test_is_resource_subresource_directory(): + # Directories are not resources. + assert not ir.is_resource("resources", "submodule") + + +@pytest.mark.parametrize( + "package, resource, expected", + ( + ("resources", "foo.txt", b"bar"), + ("resources.submodule", "bar.txt", b"foo"), + ), +) +def test_read_binary_good_path(package, resource, expected): + assert ir.read_binary(package, resource) == expected + + +def test_read_binary_missing(): + with pytest.raises(FileNotFoundError): + ir.read_binary("resources", "111.txt") + + +@pytest.mark.parametrize( + "package, resource, expected", + ( + ("resources", "foo.txt", "bar"), + ("resources.submodule", "bar.txt", "foo"), + ), +) +def test_read_text_good_path(package, resource, expected): + assert ir.read_text(package, resource) == expected + + +def test_read_text_missing(): + with pytest.raises(FileNotFoundError): + ir.read_text("resources", "111.txt") + + +@pytest.mark.parametrize( + "package, expected", + ( + ("resources", ["submodule", "foo.txt"]), + ("resources.submodule", ["bar.txt"]), + ), +) +def test_contents_good_path(package, expected): + assert sorted(ir.contents(package)) == sorted(expected) diff --git a/library/python/runtime_py3/test/test_traceback.py b/library/python/runtime_py3/test/test_traceback.py new file mode 100644 index 0000000000..08aed1a510 --- /dev/null +++ b/library/python/runtime_py3/test/test_traceback.py @@ -0,0 +1,65 @@ +import os +import re + +import pytest + +import yatest.common as yc + + +def clean_traceback(traceback): + traceback = re.sub(rb"\033\[(\d|;)+?m", b"", traceback) # strip ANSI codes + traceback = re.sub(rb" at 0x[0-9a-fA-F]+", b"", traceback) # remove object ids + return traceback + + +@pytest.mark.parametrize( + "mode", + [ + "default", + "ultratb_color", + "ultratb_verbose", + ], +) +@pytest.mark.parametrize( + "entry_point", + [ + "main", + "custom", + ], +) +def test_traceback(mode, entry_point): + tb_tool = yc.build_path("library/python/runtime_py3/test/traceback/traceback") + stdout_path = yc.test_output_path("stdout_raw.txt") + stderr_path = yc.test_output_path("stderr_raw.txt") + filtered_stdout_path = yc.test_output_path("stdout.txt") + filtered_stderr_path = yc.test_output_path("stderr.txt") + + env = os.environ.copy() + env.pop("PYTHONPATH", None) # Do not let program peek into its sources on filesystem + if entry_point == "custom": + env["Y_PYTHON_ENTRY_POINT"] = "library.python.runtime_py3.test.traceback.crash:main" + + proc = yc.execute( + command=[tb_tool, mode], + env=env, + stdout=stdout_path, + stderr=stderr_path, + check_exit_code=False, + ) + + with open(filtered_stdout_path, "wb") as f: + f.write(clean_traceback(proc.std_out)) + + with open(filtered_stderr_path, "wb") as f: + f.write(clean_traceback(proc.std_err)) + + return { + "stdout": yc.canonical_file( + filtered_stdout_path, + local=True, + ), + "stderr": yc.canonical_file( + filtered_stderr_path, + local=True, + ), + } diff --git a/library/python/runtime_py3/test/traceback/__main__.py b/library/python/runtime_py3/test/traceback/__main__.py new file mode 100644 index 0000000000..364db169f0 --- /dev/null +++ b/library/python/runtime_py3/test/traceback/__main__.py @@ -0,0 +1,4 @@ +from library.python.runtime_py3.test.traceback.crash import main + +if __name__ == "__main__": + main() diff --git a/library/python/runtime_py3/test/traceback/crash.py b/library/python/runtime_py3/test/traceback/crash.py new file mode 100644 index 0000000000..dfd316d4c6 --- /dev/null +++ b/library/python/runtime_py3/test/traceback/crash.py @@ -0,0 +1,42 @@ +import argparse +import sys +import time + +from IPython.core import ultratb + +from .mod import modfunc + + +def one(): + modfunc(two) # aaa + + +def two(): + three(42) + + +def three(x): + raise RuntimeError(f"Kaboom! I'm dead: {x}") + + +def main(): + hooks = { + "default": lambda: sys.excepthook, + "ultratb_color": lambda: ultratb.ColorTB(ostream=sys.stderr), + "ultratb_verbose": lambda: ultratb.VerboseTB(ostream=sys.stderr), + } + + parser = argparse.ArgumentParser() + parser.add_argument("hook", choices=sorted(hooks), default="default") + + args = parser.parse_args() + + sys.excepthook = hooks[args.hook]() + + print("__name__ =", __name__) + print("__file__ =", __file__) + + time.time = lambda: 1531996624.0 # Freeze time + sys.executable = "<traceback test>" + + one() diff --git a/library/python/runtime_py3/test/traceback/mod/__init__.py b/library/python/runtime_py3/test/traceback/mod/__init__.py new file mode 100644 index 0000000000..f00843d786 --- /dev/null +++ b/library/python/runtime_py3/test/traceback/mod/__init__.py @@ -0,0 +1,3 @@ +def modfunc(f): + # lalala + f() # call back to caller diff --git a/library/python/runtime_py3/test/traceback/ya.make b/library/python/runtime_py3/test/traceback/ya.make new file mode 100644 index 0000000000..0703e75401 --- /dev/null +++ b/library/python/runtime_py3/test/traceback/ya.make @@ -0,0 +1,16 @@ +PY3_PROGRAM() + +STYLE_PYTHON() + +PEERDIR( + contrib/python/ipython +) + +PY_SRCS( + MAIN + __main__.py=main + crash.py + mod/__init__.py +) + +END() diff --git a/library/python/runtime_py3/test/ya.make b/library/python/runtime_py3/test/ya.make new file mode 100644 index 0000000000..e0c4061ad2 --- /dev/null +++ b/library/python/runtime_py3/test/ya.make @@ -0,0 +1,37 @@ +PY3TEST() + +STYLE_PYTHON() + +DEPENDS(library/python/runtime_py3/test/traceback) + +PEERDIR( + contrib/python/parameterized + contrib/python/PyYAML +) + +PY_SRCS( + TOP_LEVEL + resources/__init__.py + resources/submodule/__init__.py +) + +TEST_SRCS( + test_metadata.py + test_resources.py + test_traceback.py + test_arcadia_source_finder.py +) + +RESOURCE_FILES( + PREFIX library/python/runtime_py3/test/ + .dist-info/METADATA + .dist-info/RECORD + .dist-info/entry_points.txt + .dist-info/top_level.txt + resources/foo.txt + resources/submodule/bar.txt +) + +END() + +RECURSE_FOR_TESTS(traceback) |