From ea43572dfbac4770a27ef2169f72ff73ee4a4ae9 Mon Sep 17 00:00:00 2001 From: mxmlnkn Date: Sat, 23 Sep 2023 13:42:52 +0200 Subject: [PATCH] [test] Make ratarmountcore tests look for test files depending on __file__ --- core/tests/test_AutoMountLayer.py | 14 +++++++++++--- core/tests/test_SQLiteIndexedTar.py | 18 +++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/core/tests/test_AutoMountLayer.py b/core/tests/test_AutoMountLayer.py index ddc96f35..dd5ffcb2 100644 --- a/core/tests/test_AutoMountLayer.py +++ b/core/tests/test_AutoMountLayer.py @@ -14,6 +14,14 @@ from ratarmountcore import AutoMountLayer, openMountSource # noqa: E402 +def findTestFile(relativePathOrName): + for i in range(3): + path = os.path.sep.join([".."] * i + ["tests", relativePathOrName]) + if os.path.exists(path): + return path + return relativePathOrName + + @pytest.mark.parametrize("parallelization", [1, 2, 4]) class TestAutoMountLayer: @staticmethod @@ -25,7 +33,7 @@ def test_regex_mount_point_tar(parallelization): 'transformRecursiveMountPoint': ('.*/([^/]*).tar', r'\1'), } - with openMountSource("tests/packed-100-times.tar.gz", **options) as mountSource: + with openMountSource(findTestFile("packed-100-times.tar.gz"), **options) as mountSource: recursivelyMounted = AutoMountLayer(mountSource, **options) assert recursivelyMounted.listDir('/') @@ -47,7 +55,7 @@ def test_regex_mount_point_tar_gz(parallelization): # other files and those other files will actually take 10x or more longer than without this test running # before! It might be that the memory usage makes Python's garbage collector a bottleneck because of too # many small objects?! - with openMountSource("tests/compressed-100-times.tar.gz", **options) as mountSource: + with openMountSource(findTestFile("compressed-100-times.tar.gz"), **options) as mountSource: recursivelyMounted = AutoMountLayer(mountSource, **options) assert recursivelyMounted.listDir('/') @@ -68,7 +76,7 @@ def test_regex_mount_point_gz(parallelization): # > Recursively mounted: /ufo_805.gz # > File "core/ratarmountcore/SQLiteIndexedTar.py", line 2085, in _detectTar # > indexed_gzip.indexed_gzip.ZranError: zran_read returned error: ZRAN_READ_FAIL (file: n/a) - with openMountSource("tests/compressed-100-times.gz", **options) as mountSource: + with openMountSource(findTestFile("compressed-100-times.gz"), **options) as mountSource: recursivelyMounted = AutoMountLayer(mountSource, **options) assert recursivelyMounted.listDir('/') diff --git a/core/tests/test_SQLiteIndexedTar.py b/core/tests/test_SQLiteIndexedTar.py index 586cf7f5..196c019e 100644 --- a/core/tests/test_SQLiteIndexedTar.py +++ b/core/tests/test_SQLiteIndexedTar.py @@ -24,6 +24,14 @@ from ratarmountcore import RatarmountError, SQLiteIndexedTar # noqa: E402 +def findTestFile(relativePathOrName): + for i in range(3): + path = os.path.sep.join([".."] * i + ["tests", relativePathOrName]) + if os.path.exists(path): + return path + return relativePathOrName + + @pytest.mark.parametrize("parallelization", [1, 2, 4]) class TestSQLiteIndexedTarParallelized: @staticmethod @@ -40,13 +48,13 @@ def _makeFolder(tarArchive, name): @staticmethod def test_context_manager(parallelization): - with SQLiteIndexedTar('tests/single-file.tar', writeIndex=False, parallelization=parallelization) as indexedTar: + with SQLiteIndexedTar(findTestFile('single-file.tar'), writeIndex=False, parallelization=parallelization) as indexedTar: assert indexedTar.listDir('/') @staticmethod def test_tar_bz2_with_parallelization(parallelization): with SQLiteIndexedTar( - "tests/2k-recursive-tars.tar.bz2", + findTestFile("2k-recursive-tars.tar.bz2"), clearIndexCache=True, recursive=False, parallelization=parallelization, @@ -65,7 +73,7 @@ def test_tar_bz2_with_parallelization(parallelization): @staticmethod def test_recursive_tar_bz2_with_parallelization(parallelization): with SQLiteIndexedTar( - "tests/2k-recursive-tars.tar.bz2", + findTestFile("2k-recursive-tars.tar.bz2"), clearIndexCache=True, recursive=True, parallelization=parallelization, @@ -84,7 +92,7 @@ def test_recursive_tar_bz2_with_parallelization(parallelization): @staticmethod def test_deep_recursive(parallelization): with SQLiteIndexedTar( - "tests/packed-5-times.tar.gz", + findTestFile("packed-5-times.tar.gz"), clearIndexCache=True, recursive=True, parallelization=parallelization, @@ -430,7 +438,7 @@ def test_appending_to_large_archive(parallelization, tmpdir): # Create a TAR large in size as well as file count tarPath = os.path.join(tmpdir, "foo.tar") - with indexed_bzip2.open("tests/tar-with-300-folders-with-1000-files-0B-files.tar.bz2") as file, open( + with indexed_bzip2.open(findTestFile("tar-with-300-folders-with-1000-files-0B-files.tar.bz2")) as file, open( tarPath, 'wb' ) as extracted: while True: