diff options
author | 2021-06-06 05:32:56 +0200 | |
---|---|---|
committer | 2021-06-06 03:32:56 +0000 | |
commit | 512bd175cffbc3463679f3e165bea6d0a486f6fa (patch) | |
tree | bb58bf64cf81564cb9973465e3b6f1ee28bcb297 /pydis_site/apps/content/tests/helpers.py | |
parent | Merge pull request #522 from python-discord/md-link-fix (diff) |
Fix `content` app tests not running on macOS (#519)
macOS uses `/var/...` as its temp directory, causing issues with the
hardcoded usage of `/tmp` as the temporary directory. Therefore,
relying on tmp is not portable.
Populating the true temporary directory is redundant and may cause more
problems because of nested directories. Move the fake content under a
subdirectory to avoid this issue.
Co-authored-by: MarkKoz <[email protected]>
Diffstat (limited to 'pydis_site/apps/content/tests/helpers.py')
-rw-r--r-- | pydis_site/apps/content/tests/helpers.py | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/pydis_site/apps/content/tests/helpers.py b/pydis_site/apps/content/tests/helpers.py index 29140375..d897c024 100644 --- a/pydis_site/apps/content/tests/helpers.py +++ b/pydis_site/apps/content/tests/helpers.py @@ -1,4 +1,13 @@ -from pyfakefs.fake_filesystem_unittest import TestCase +from pathlib import Path + +from pyfakefs import fake_filesystem_unittest + + +# 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") + # Valid markdown content with YAML metadata MARKDOWN_WITH_METADATA = """ @@ -41,11 +50,11 @@ PARSED_METADATA = { PARSED_CATEGORY_INFO = {"title": "Category Name", "description": "Description"} -class MockPagesTestCase(TestCase): +class MockPagesTestCase(fake_filesystem_unittest.TestCase): """ TestCase with a fake filesystem for testing. - Structure: + Structure (relative to BASE_PATH): ├── _info.yml ├── root.md ├── root_without_metadata.md @@ -68,24 +77,27 @@ class MockPagesTestCase(TestCase): """Create the fake filesystem.""" self.setUpPyfakefs() - self.fs.create_file("_info.yml", contents=CATEGORY_INFO) - self.fs.create_file("root.md", contents=MARKDOWN_WITH_METADATA) - self.fs.create_file("root_without_metadata.md", contents=MARKDOWN_WITHOUT_METADATA) - self.fs.create_file("not_a_page.md/_info.yml", contents=CATEGORY_INFO) - self.fs.create_file("category/_info.yml", contents=CATEGORY_INFO) - self.fs.create_file("category/with_metadata.md", contents=MARKDOWN_WITH_METADATA) - self.fs.create_file("category/subcategory/_info.yml", contents=CATEGORY_INFO) + 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( - "category/subcategory/with_metadata.md", contents=MARKDOWN_WITH_METADATA + f"{BASE_PATH}/category/subcategory/with_metadata.md", contents=MARKDOWN_WITH_METADATA ) self.fs.create_file( - "category/subcategory/without_metadata.md", contents=MARKDOWN_WITHOUT_METADATA + f"{BASE_PATH}/category/subcategory/without_metadata.md", + contents=MARKDOWN_WITHOUT_METADATA ) - # There is always a `tmp` directory in the filesystem, so make it a category - # for testing purposes. - # See: https://jmcgeheeiv.github.io/pyfakefs/release/usage.html#os-temporary-directories - self.fs.create_file("tmp/_info.yml", contents=CATEGORY_INFO) - self.fs.create_file("tmp.md", contents=MARKDOWN_WITH_METADATA) - self.fs.create_file("tmp/category/_info.yml", contents=CATEGORY_INFO) - self.fs.create_dir("tmp/category/subcategory_without_info") + 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") |