Skip to content

Commit 512bd17

Browse files
jchristgitMarkKoz
andauthored
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]>
1 parent c11c06e commit 512bd17

File tree

3 files changed

+51
-41
lines changed

3 files changed

+51
-41
lines changed

pydis_site/apps/content/tests/helpers.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
from pyfakefs.fake_filesystem_unittest import TestCase
1+
from pathlib import Path
2+
3+
from pyfakefs import fake_filesystem_unittest
4+
5+
6+
# Set the module constant within Patcher to use the fake filesystem
7+
# https://jmcgeheeiv.github.io/pyfakefs/master/usage.html#modules-to-reload
8+
with fake_filesystem_unittest.Patcher() as _:
9+
BASE_PATH = Path("res")
10+
211

312
# Valid markdown content with YAML metadata
413
MARKDOWN_WITH_METADATA = """
@@ -41,11 +50,11 @@
4150
PARSED_CATEGORY_INFO = {"title": "Category Name", "description": "Description"}
4251

4352

44-
class MockPagesTestCase(TestCase):
53+
class MockPagesTestCase(fake_filesystem_unittest.TestCase):
4554
"""
4655
TestCase with a fake filesystem for testing.
4756
48-
Structure:
57+
Structure (relative to BASE_PATH):
4958
├── _info.yml
5059
├── root.md
5160
├── root_without_metadata.md
@@ -68,24 +77,27 @@ def setUp(self):
6877
"""Create the fake filesystem."""
6978
self.setUpPyfakefs()
7079

71-
self.fs.create_file("_info.yml", contents=CATEGORY_INFO)
72-
self.fs.create_file("root.md", contents=MARKDOWN_WITH_METADATA)
73-
self.fs.create_file("root_without_metadata.md", contents=MARKDOWN_WITHOUT_METADATA)
74-
self.fs.create_file("not_a_page.md/_info.yml", contents=CATEGORY_INFO)
75-
self.fs.create_file("category/_info.yml", contents=CATEGORY_INFO)
76-
self.fs.create_file("category/with_metadata.md", contents=MARKDOWN_WITH_METADATA)
77-
self.fs.create_file("category/subcategory/_info.yml", contents=CATEGORY_INFO)
80+
self.fs.create_file(f"{BASE_PATH}/_info.yml", contents=CATEGORY_INFO)
81+
self.fs.create_file(f"{BASE_PATH}/root.md", contents=MARKDOWN_WITH_METADATA)
82+
self.fs.create_file(
83+
f"{BASE_PATH}/root_without_metadata.md", contents=MARKDOWN_WITHOUT_METADATA
84+
)
85+
self.fs.create_file(f"{BASE_PATH}/not_a_page.md/_info.yml", contents=CATEGORY_INFO)
86+
self.fs.create_file(f"{BASE_PATH}/category/_info.yml", contents=CATEGORY_INFO)
87+
self.fs.create_file(
88+
f"{BASE_PATH}/category/with_metadata.md", contents=MARKDOWN_WITH_METADATA
89+
)
90+
self.fs.create_file(f"{BASE_PATH}/category/subcategory/_info.yml", contents=CATEGORY_INFO)
7891
self.fs.create_file(
79-
"category/subcategory/with_metadata.md", contents=MARKDOWN_WITH_METADATA
92+
f"{BASE_PATH}/category/subcategory/with_metadata.md", contents=MARKDOWN_WITH_METADATA
8093
)
8194
self.fs.create_file(
82-
"category/subcategory/without_metadata.md", contents=MARKDOWN_WITHOUT_METADATA
95+
f"{BASE_PATH}/category/subcategory/without_metadata.md",
96+
contents=MARKDOWN_WITHOUT_METADATA
8397
)
8498

85-
# There is always a `tmp` directory in the filesystem, so make it a category
86-
# for testing purposes.
87-
# See: https://jmcgeheeiv.github.io/pyfakefs/release/usage.html#os-temporary-directories
88-
self.fs.create_file("tmp/_info.yml", contents=CATEGORY_INFO)
89-
self.fs.create_file("tmp.md", contents=MARKDOWN_WITH_METADATA)
90-
self.fs.create_file("tmp/category/_info.yml", contents=CATEGORY_INFO)
91-
self.fs.create_dir("tmp/category/subcategory_without_info")
99+
temp = f"{BASE_PATH}/tmp" # noqa: S108
100+
self.fs.create_file(f"{temp}/_info.yml", contents=CATEGORY_INFO)
101+
self.fs.create_file(f"{temp}.md", contents=MARKDOWN_WITH_METADATA)
102+
self.fs.create_file(f"{temp}/category/_info.yml", contents=CATEGORY_INFO)
103+
self.fs.create_dir(f"{temp}/category/subcategory_without_info")

pydis_site/apps/content/tests/test_utils.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,54 @@
44

55
from pydis_site.apps.content import utils
66
from pydis_site.apps.content.tests.helpers import (
7-
MockPagesTestCase, PARSED_CATEGORY_INFO, PARSED_HTML, PARSED_METADATA
7+
BASE_PATH, MockPagesTestCase, PARSED_CATEGORY_INFO, PARSED_HTML, PARSED_METADATA
88
)
99

1010

1111
class GetCategoryTests(MockPagesTestCase):
1212
"""Tests for the get_category function."""
1313

1414
def test_get_valid_category(self):
15-
result = utils.get_category(Path("category"))
15+
result = utils.get_category(Path(BASE_PATH, "category"))
1616

1717
self.assertEqual(result, {"title": "Category Name", "description": "Description"})
1818

1919
def test_get_nonexistent_category(self):
2020
with self.assertRaises(Http404):
21-
utils.get_category(Path("invalid"))
21+
utils.get_category(Path(BASE_PATH, "invalid"))
2222

2323
def test_get_category_with_path_to_file(self):
2424
# Valid categories are directories, not files
2525
with self.assertRaises(Http404):
26-
utils.get_category(Path("root.md"))
26+
utils.get_category(Path(BASE_PATH, "root.md"))
2727

2828
def test_get_category_without_info_yml(self):
2929
# Categories should provide an _info.yml file
3030
with self.assertRaises(FileNotFoundError):
31-
utils.get_category(Path("tmp/category/subcategory_without_info"))
31+
utils.get_category(Path(BASE_PATH, "tmp/category/subcategory_without_info"))
3232

3333

3434
class GetCategoriesTests(MockPagesTestCase):
3535
"""Tests for the get_categories function."""
3636

3737
def test_get_root_categories(self):
38-
result = utils.get_categories(Path("."))
38+
result = utils.get_categories(BASE_PATH)
3939

4040
info = PARSED_CATEGORY_INFO
41-
self.assertEqual(result, {"category": info, "tmp": info, "not_a_page.md": info})
41+
categories = {
42+
"category": info,
43+
"tmp": info,
44+
"not_a_page.md": info,
45+
}
46+
self.assertEqual(result, categories)
4247

4348
def test_get_categories_with_subcategories(self):
44-
result = utils.get_categories(Path("category"))
49+
result = utils.get_categories(Path(BASE_PATH, "category"))
4550

4651
self.assertEqual(result, {"subcategory": PARSED_CATEGORY_INFO})
4752

4853
def test_get_categories_without_subcategories(self):
49-
result = utils.get_categories(Path("category/subcategory"))
54+
result = utils.get_categories(Path(BASE_PATH, "category/subcategory"))
5055

5156
self.assertEqual(result, {})
5257

@@ -56,14 +61,14 @@ class GetCategoryPagesTests(MockPagesTestCase):
5661

5762
def test_get_pages_in_root_category_successfully(self):
5863
"""The method should successfully retrieve page metadata."""
59-
root_category_pages = utils.get_category_pages(Path("."))
64+
root_category_pages = utils.get_category_pages(BASE_PATH)
6065
self.assertEqual(
6166
root_category_pages, {"root": PARSED_METADATA, "root_without_metadata": {}}
6267
)
6368

6469
def test_get_pages_in_subcategories_successfully(self):
6570
"""The method should successfully retrieve page metadata."""
66-
category_pages = utils.get_category_pages(Path("category"))
71+
category_pages = utils.get_category_pages(Path(BASE_PATH, "category"))
6772

6873
# Page metadata is properly retrieved
6974
self.assertEqual(category_pages, {"with_metadata": PARSED_METADATA})
@@ -84,10 +89,10 @@ def test_get_page(self):
8489

8590
for msg, page_path, expected_html, expected_metadata in cases:
8691
with self.subTest(msg=msg):
87-
html, metadata = utils.get_page(Path(page_path))
92+
html, metadata = utils.get_page(Path(BASE_PATH, page_path))
8893
self.assertEqual(html, expected_html)
8994
self.assertEqual(metadata, expected_metadata)
9095

9196
def test_get_nonexistent_page_returns_404(self):
9297
with self.assertRaises(Http404):
93-
utils.get_page(Path("invalid"))
98+
utils.get_page(Path(BASE_PATH, "invalid"))

pydis_site/apps/content/tests/test_views.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,20 @@
33

44
from django.http import Http404
55
from django.test import RequestFactory, SimpleTestCase, override_settings
6-
from pyfakefs import fake_filesystem_unittest
76

87
from pydis_site.apps.content.tests.helpers import (
9-
MockPagesTestCase, PARSED_CATEGORY_INFO, PARSED_HTML, PARSED_METADATA
8+
BASE_PATH, MockPagesTestCase, PARSED_CATEGORY_INFO, PARSED_HTML, PARSED_METADATA
109
)
1110
from pydis_site.apps.content.views import PageOrCategoryView
1211

1312

14-
# Set the module constant within Patcher to use the fake filesystem
15-
# https://jmcgeheeiv.github.io/pyfakefs/master/usage.html#modules-to-reload
16-
with fake_filesystem_unittest.Patcher() as _:
17-
BASE_PATH = Path(".")
18-
19-
2013
def patch_dispatch_attributes(view: PageOrCategoryView, location: str) -> None:
2114
"""
2215
Set the attributes set in the `dispatch` method manually.
2316
2417
This is necessary because it is never automatically called during tests.
2518
"""
26-
view.location = Path(location)
19+
view.location = Path(BASE_PATH, location)
2720

2821
# URL location on the filesystem
2922
view.full_location = view.location

0 commit comments

Comments
 (0)