diff options
| author | 2023-03-22 23:19:44 +0100 | |
|---|---|---|
| committer | 2023-03-25 22:47:13 +0100 | |
| commit | feb54974056578836de841971a953f2cd206ce80 (patch) | |
| tree | b3d3cf0fb7e72e91154cab01f783759cb8b4ddf7 | |
| parent | Merge pull request #916 from python-discord/dependabot/pip/markdown-3.4.3 (diff) | |
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.
| -rw-r--r-- | poetry.lock | 50 | ||||
| -rw-r--r-- | pydis_site/apps/content/tests/helpers.py | 63 | ||||
| -rw-r--r-- | pyproject.toml | 1 | 
3 files changed, 70 insertions, 44 deletions
diff --git a/poetry.lock b/poetry.lock index c5dad711..7fad2146 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry and should not be changed by hand. +# This file is automatically @generated by Poetry 1.4.1 and should not be changed by hand.  [[package]]  name = "anyio" @@ -826,6 +826,26 @@ files = [  ]  [[package]] +name = "importlib-metadata" +version = "6.1.0" +description = "Read metadata from Python packages" +category = "main" +optional = false +python-versions = ">=3.7" +files = [ +    {file = "importlib_metadata-6.1.0-py3-none-any.whl", hash = "sha256:ff80f3b5394912eb1b108fcfd444dc78b7f1f3e16b16188054bd01cb9cb86f09"}, +    {file = "importlib_metadata-6.1.0.tar.gz", hash = "sha256:43ce9281e097583d758c2c708c4376371261a02c34682491a8e98352365aad20"}, +] + +[package.dependencies] +zipp = ">=0.5" + +[package.extras] +docs = ["furo", "jaraco.packaging (>=9)", "jaraco.tidelift (>=1.4)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-lint"] +perf = ["ipython"] +testing = ["flake8 (<5)", "flufl.flake8", "importlib-resources (>=1.3)", "packaging", "pyfakefs", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=1.3)", "pytest-flake8", "pytest-mypy (>=0.9.1)", "pytest-perf (>=0.9.2)"] + +[[package]]  name = "libsass"  version = "0.22.0"  description = "Sass for Python: A straightforward binding of libsass for Python." @@ -1122,18 +1142,6 @@ snowballstemmer = ">=2.2.0"  toml = ["tomli (>=1.2.3)"]  [[package]] -name = "pyfakefs" -version = "5.1.0" -description = "pyfakefs implements a fake file system that mocks the Python file system modules." -category = "dev" -optional = false -python-versions = ">=3.7" -files = [ -    {file = "pyfakefs-5.1.0-py3-none-any.whl", hash = "sha256:e6f34a8224b41f1b1ab25aa8d430121dac42e3c6e981e01eae76b3343fba47d0"}, -    {file = "pyfakefs-5.1.0.tar.gz", hash = "sha256:316c6026640d14a6b4fbde71fd9674576d1b5710deda8fabde8aad51d785dbc3"}, -] - -[[package]]  name = "pyflakes"  version = "3.0.1"  description = "passive checker of Python programs" @@ -1534,6 +1542,22 @@ files = [  [package.extras]  brotli = ["Brotli"] +[[package]] +name = "zipp" +version = "3.15.0" +description = "Backport of pathlib-compatible object wrapper for zip files" +category = "main" +optional = false +python-versions = ">=3.7" +files = [ +    {file = "zipp-3.15.0-py3-none-any.whl", hash = "sha256:48904fc76a60e542af151aded95726c1a5c34ed43ab4134b597665c86d7ad556"}, +    {file = "zipp-3.15.0.tar.gz", hash = "sha256:112929ad649da941c23de50f356a2b5570c954b65150642bccdd66bf194d224b"}, +] + +[package.extras] +docs = ["furo", "jaraco.packaging (>=9)", "jaraco.tidelift (>=1.4)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-lint"] +testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more-itertools", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=1.3)", "pytest-flake8", "pytest-mypy (>=0.9.1)"] +  [metadata]  lock-version = "2.0"  python-versions = "3.10.*" 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) diff --git a/pyproject.toml b/pyproject.toml index 21bc26d7..21857f84 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,7 +45,6 @@ pre-commit = "3.2.0"  [tool.poetry.group.test.dependencies]  coverage = "7.2.2" -pyfakefs = "5.1.0"  [build-system]  requires = ["poetry-core>=1.2.0"]  |