From feb54974056578836de841971a953f2cd206ce80 Mon Sep 17 00:00:00 2001 From: Johannes Christ Date: Wed, 22 Mar 2023 23:19:44 +0100 Subject: Drop dependency to pyfakefs Create a temporary directory to manage our resource tests instead of reyling on pyfakefs to mock it away for us. This also makes the code more portable: all we need now is a way to create a temporary directory. `pathlib` mostly abstracts away the other parts for us. Since we're well-behaved, we clean up the temporary directory at the end of the Python interpreter's life using `atexit` and `shutil.rmtree`. This PR was written and tested with Python 3.9 which required some hacks in `pyproject.toml` to make it work, it may require re-locking if CI throws up. Closes #679. --- pydis_site/apps/content/tests/helpers.py | 63 +++++++++++++++++--------------- 1 file changed, 33 insertions(+), 30 deletions(-) (limited to 'pydis_site/apps/content/tests') diff --git a/pydis_site/apps/content/tests/helpers.py b/pydis_site/apps/content/tests/helpers.py index d897c024..1eab62be 100644 --- a/pydis_site/apps/content/tests/helpers.py +++ b/pydis_site/apps/content/tests/helpers.py @@ -1,12 +1,12 @@ +import atexit +import shutil +import tempfile from pathlib import Path -from pyfakefs import fake_filesystem_unittest +from django.test import TestCase -# Set the module constant within Patcher to use the fake filesystem -# https://jmcgeheeiv.github.io/pyfakefs/master/usage.html#modules-to-reload -with fake_filesystem_unittest.Patcher() as _: - BASE_PATH = Path("res") +BASE_PATH = Path(tempfile.mkdtemp(prefix='pydis-site-content-app-tests-')) # Valid markdown content with YAML metadata @@ -50,7 +50,7 @@ PARSED_METADATA = { PARSED_CATEGORY_INFO = {"title": "Category Name", "description": "Description"} -class MockPagesTestCase(fake_filesystem_unittest.TestCase): +class MockPagesTestCase(TestCase): """ TestCase with a fake filesystem for testing. @@ -75,29 +75,32 @@ class MockPagesTestCase(fake_filesystem_unittest.TestCase): def setUp(self): """Create the fake filesystem.""" - self.setUpPyfakefs() - - self.fs.create_file(f"{BASE_PATH}/_info.yml", contents=CATEGORY_INFO) - self.fs.create_file(f"{BASE_PATH}/root.md", contents=MARKDOWN_WITH_METADATA) - self.fs.create_file( - f"{BASE_PATH}/root_without_metadata.md", contents=MARKDOWN_WITHOUT_METADATA - ) - self.fs.create_file(f"{BASE_PATH}/not_a_page.md/_info.yml", contents=CATEGORY_INFO) - self.fs.create_file(f"{BASE_PATH}/category/_info.yml", contents=CATEGORY_INFO) - self.fs.create_file( - f"{BASE_PATH}/category/with_metadata.md", contents=MARKDOWN_WITH_METADATA - ) - self.fs.create_file(f"{BASE_PATH}/category/subcategory/_info.yml", contents=CATEGORY_INFO) - self.fs.create_file( - f"{BASE_PATH}/category/subcategory/with_metadata.md", contents=MARKDOWN_WITH_METADATA - ) - self.fs.create_file( - f"{BASE_PATH}/category/subcategory/without_metadata.md", - contents=MARKDOWN_WITHOUT_METADATA - ) + Path(f"{BASE_PATH}/_info.yml").write_text(CATEGORY_INFO) + Path(f"{BASE_PATH}/root.md").write_text(MARKDOWN_WITH_METADATA) + Path(f"{BASE_PATH}/root_without_metadata.md").write_text(MARKDOWN_WITHOUT_METADATA) + Path(f"{BASE_PATH}/not_a_page.md").mkdir(exist_ok=True) + Path(f"{BASE_PATH}/not_a_page.md/_info.yml").write_text(CATEGORY_INFO) + Path(f"{BASE_PATH}/category").mkdir(exist_ok=True) + Path(f"{BASE_PATH}/category/_info.yml").write_text(CATEGORY_INFO) + Path(f"{BASE_PATH}/category/with_metadata.md").write_text(MARKDOWN_WITH_METADATA) + Path(f"{BASE_PATH}/category/subcategory").mkdir(exist_ok=True) + Path(f"{BASE_PATH}/category/subcategory/_info.yml").write_text(CATEGORY_INFO) + Path( + f"{BASE_PATH}/category/subcategory/with_metadata.md" + ).write_text(MARKDOWN_WITH_METADATA) + Path( + f"{BASE_PATH}/category/subcategory/without_metadata.md" + ).write_text(MARKDOWN_WITHOUT_METADATA) temp = f"{BASE_PATH}/tmp" # noqa: S108 - self.fs.create_file(f"{temp}/_info.yml", contents=CATEGORY_INFO) - self.fs.create_file(f"{temp}.md", contents=MARKDOWN_WITH_METADATA) - self.fs.create_file(f"{temp}/category/_info.yml", contents=CATEGORY_INFO) - self.fs.create_dir(f"{temp}/category/subcategory_without_info") + Path(f"{temp}").mkdir(exist_ok=True) + Path(f"{temp}/_info.yml").write_text(CATEGORY_INFO) + Path(f"{temp}.md").write_text(MARKDOWN_WITH_METADATA) + Path(f"{temp}/category").mkdir(exist_ok=True) + Path(f"{temp}/category/_info.yml").write_text(CATEGORY_INFO) + Path(f"{temp}/category/subcategory_without_info").mkdir(exist_ok=True) + + @classmethod + def setUpTestData(cls): + # May get called multiple times - ignore erorrs in that case. + atexit.register(shutil.rmtree, BASE_PATH, ignore_errors=True) -- cgit v1.2.3 From 22c653a7179f97e4f7c4de391f9204d71f4fb6c7 Mon Sep 17 00:00:00 2001 From: Johannes Christ Date: Fri, 24 Mar 2023 22:55:32 +0100 Subject: Register cleanup job after module load --- pydis_site/apps/content/tests/helpers.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'pydis_site/apps/content/tests') diff --git a/pydis_site/apps/content/tests/helpers.py b/pydis_site/apps/content/tests/helpers.py index 1eab62be..fad91050 100644 --- a/pydis_site/apps/content/tests/helpers.py +++ b/pydis_site/apps/content/tests/helpers.py @@ -7,6 +7,7 @@ from django.test import TestCase BASE_PATH = Path(tempfile.mkdtemp(prefix='pydis-site-content-app-tests-')) +atexit.register(shutil.rmtree, BASE_PATH, ignore_errors=True) # Valid markdown content with YAML metadata @@ -99,8 +100,3 @@ class MockPagesTestCase(TestCase): Path(f"{temp}/category").mkdir(exist_ok=True) Path(f"{temp}/category/_info.yml").write_text(CATEGORY_INFO) Path(f"{temp}/category/subcategory_without_info").mkdir(exist_ok=True) - - @classmethod - def setUpTestData(cls): - # May get called multiple times - ignore erorrs in that case. - atexit.register(shutil.rmtree, BASE_PATH, ignore_errors=True) -- cgit v1.2.3