fix: set paths for component media as posix paths even on windows (#799)

* fix: set paths for component media as posix paths even on windows

* refactor: try running django server in windows

* refactor: handle Windows paths in component media

* refactor: temp test only windows

* refactor: try to fix tests

* refactor: more test fixes

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* refactor: remove extraneous import

* refactor: more fixes

* refactor: fix path conversion for windows

* refactor: revert windows env in CI

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Juro Oravec 2024-12-02 19:03:41 +01:00 committed by GitHub
parent 230ceee537
commit 025562ac0c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 110 additions and 82 deletions

View file

@ -1,5 +1,4 @@
import os
import re
from pathlib import Path
from unittest.mock import MagicMock, patch
@ -35,8 +34,10 @@ class ComponentDirsTest(BaseTestCase):
# Apps with a `components` dir
self.assertEqual(len(apps_dirs), 2)
self.assertRegex(str(apps_dirs[0]), re.compile(r"\/django_components\/components$"))
self.assertRegex(str(apps_dirs[1]), re.compile(r"\/tests\/test_app\/components$"))
# NOTE: Compare parts so that the test works on Windows too
self.assertTupleEqual(apps_dirs[0].parts[-2:], ("django_components", "components"))
self.assertTupleEqual(apps_dirs[1].parts[-3:], ("tests", "test_app", "components"))
@override_settings(
BASE_DIR=Path(__file__).parent.resolve() / "test_structures" / "test_structure_1", # noqa
@ -49,8 +50,10 @@ class ComponentDirsTest(BaseTestCase):
# Apps with a `components` dir
self.assertEqual(len(apps_dirs), 2)
self.assertRegex(str(apps_dirs[0]), re.compile(r"\/django_components\/components$"))
self.assertRegex(str(apps_dirs[1]), re.compile(r"\/tests\/test_app\/components$"))
# NOTE: Compare parts so that the test works on Windows too
self.assertTupleEqual(apps_dirs[0].parts[-2:], ("django_components", "components"))
self.assertTupleEqual(apps_dirs[1].parts[-3:], ("tests", "test_app", "components"))
expected = [
Path(__file__).parent.resolve() / "test_structures" / "test_structure_1" / "components",
@ -76,8 +79,10 @@ class ComponentDirsTest(BaseTestCase):
# Apps with a `components` dir
self.assertEqual(len(apps_dirs), 2)
self.assertRegex(str(apps_dirs[0]), re.compile(r"\/django_components\/components$"))
self.assertRegex(str(apps_dirs[1]), re.compile(r"\/tests\/test_app\/components$"))
# NOTE: Compare parts so that the test works on Windows too
self.assertTupleEqual(apps_dirs[0].parts[-2:], ("django_components", "components"))
self.assertTupleEqual(apps_dirs[1].parts[-3:], ("tests", "test_app", "components"))
self.assertEqual(
own_dirs,
@ -104,8 +109,10 @@ class ComponentDirsTest(BaseTestCase):
# Apps with a `components` dir
self.assertEqual(len(apps_dirs), 2)
self.assertRegex(str(apps_dirs[0]), re.compile(r"\/django_components\/components$"))
self.assertRegex(str(apps_dirs[1]), re.compile(r"\/tests\/test_app\/components$"))
# NOTE: Compare parts so that the test works on Windows too
self.assertTupleEqual(apps_dirs[0].parts[-2:], ("django_components", "components"))
self.assertTupleEqual(apps_dirs[1].parts[-3:], ("tests", "test_app", "components"))
@override_settings(
BASE_DIR=Path(__file__).parent.resolve(),
@ -141,7 +148,9 @@ class ComponentDirsTest(BaseTestCase):
# Apps with a `components` dir
self.assertEqual(len(apps_dirs), 1)
self.assertRegex(str(apps_dirs[0]), re.compile(r"\/tests\/test_app\/custom_comps_dir$"))
# NOTE: Compare parts so that the test works on Windows too
self.assertTupleEqual(apps_dirs[0].parts[-3:], ("tests", "test_app", "custom_comps_dir"))
self.assertEqual(
own_dirs,
@ -204,8 +213,10 @@ class ComponentDirsTest(BaseTestCase):
# Apps with a `components` dir
self.assertEqual(len(apps_dirs), 2)
self.assertRegex(str(apps_dirs[0]), re.compile(r"\/django_components\/components$"))
self.assertRegex(str(apps_dirs[1]), re.compile(r"\/tests\/test_app_nested\/app\/components$"))
# NOTE: Compare parts so that the test works on Windows too
self.assertTupleEqual(apps_dirs[0].parts[-2:], ("django_components", "components"))
self.assertTupleEqual(apps_dirs[1].parts[-4:], ("tests", "test_app_nested", "app", "components"))
self.assertEqual(
own_dirs,
@ -225,7 +236,7 @@ class ComponentFilesTest(BaseTestCase):
files = sorted(get_component_files(".py"))
dot_paths = [f.dot_path for f in files]
file_paths = [str(f.filepath) for f in files]
file_paths = [f.filepath for f in files]
self.assertEqual(
dot_paths,
@ -243,20 +254,20 @@ class ComponentFilesTest(BaseTestCase):
],
)
self.assertEqual(
[
file_paths[0].endswith("tests/components/__init__.py"),
file_paths[1].endswith("tests/components/multi_file/multi_file.py"),
file_paths[2].endswith("tests/components/relative_file/relative_file.py"),
file_paths[3].endswith("tests/components/relative_file_pathobj/relative_file_pathobj.py"),
file_paths[4].endswith("tests/components/single_file.py"),
file_paths[5].endswith("tests/components/staticfiles/staticfiles.py"),
file_paths[6].endswith("tests/components/urls.py"),
file_paths[7].endswith("django_components/components/__init__.py"),
file_paths[8].endswith("django_components/components/dynamic.py"),
file_paths[9].endswith("tests/test_app/components/app_lvl_comp/app_lvl_comp.py"),
],
[True for _ in range(len(file_paths))],
# NOTE: Compare parts so that the test works on Windows too
self.assertTupleEqual(file_paths[0].parts[-3:], ("tests", "components", "__init__.py"))
self.assertTupleEqual(file_paths[1].parts[-4:], ("tests", "components", "multi_file", "multi_file.py"))
self.assertTupleEqual(file_paths[2].parts[-4:], ("tests", "components", "relative_file", "relative_file.py"))
self.assertTupleEqual(
file_paths[3].parts[-4:], ("tests", "components", "relative_file_pathobj", "relative_file_pathobj.py")
)
self.assertTupleEqual(file_paths[4].parts[-3:], ("tests", "components", "single_file.py"))
self.assertTupleEqual(file_paths[5].parts[-4:], ("tests", "components", "staticfiles", "staticfiles.py"))
self.assertTupleEqual(file_paths[6].parts[-3:], ("tests", "components", "urls.py"))
self.assertTupleEqual(file_paths[7].parts[-3:], ("django_components", "components", "__init__.py"))
self.assertTupleEqual(file_paths[8].parts[-3:], ("django_components", "components", "dynamic.py"))
self.assertTupleEqual(
file_paths[9].parts[-5:], ("tests", "test_app", "components", "app_lvl_comp", "app_lvl_comp.py")
)
@override_settings(
@ -266,9 +277,7 @@ class ComponentFilesTest(BaseTestCase):
files = sorted(get_component_files(".js"))
dot_paths = [f.dot_path for f in files]
file_paths = [str(f.filepath) for f in files]
print(file_paths)
file_paths = [f.filepath for f in files]
self.assertEqual(
dot_paths,
@ -280,14 +289,14 @@ class ComponentFilesTest(BaseTestCase):
],
)
self.assertEqual(
[
file_paths[0].endswith("tests/components/relative_file/relative_file.js"),
file_paths[1].endswith("tests/components/relative_file_pathobj/relative_file_pathobj.js"),
file_paths[2].endswith("tests/components/staticfiles/staticfiles.js"),
file_paths[3].endswith("tests/test_app/components/app_lvl_comp/app_lvl_comp.js"),
],
[True for _ in range(len(file_paths))],
# NOTE: Compare parts so that the test works on Windows too
self.assertTupleEqual(file_paths[0].parts[-4:], ("tests", "components", "relative_file", "relative_file.js"))
self.assertTupleEqual(
file_paths[1].parts[-4:], ("tests", "components", "relative_file_pathobj", "relative_file_pathobj.js")
)
self.assertTupleEqual(file_paths[2].parts[-4:], ("tests", "components", "staticfiles", "staticfiles.js"))
self.assertTupleEqual(
file_paths[3].parts[-5:], ("tests", "test_app", "components", "app_lvl_comp", "app_lvl_comp.js")
)
@ -307,31 +316,44 @@ class TestFilepathToPythonModule(BaseTestCase):
"tests.components.relative_file.relative_file",
)
def test_handles_nonlinux_paths(self):
base_path = str(settings.BASE_DIR).replace("/", "//")
def test_handles_separators_based_on_os_name(self):
base_path = str(settings.BASE_DIR)
with patch("os.path.sep", new="//"):
the_path = os.path.join(base_path, "tests.py")
with patch("os.name", new="posix"):
the_path = base_path + "/" + "tests.py"
self.assertEqual(
_filepath_to_python_module(the_path, base_path, None),
"tests",
)
the_path = os.path.join(base_path, "tests//components//relative_file//relative_file.py")
the_path = base_path + "/" + "tests/components/relative_file/relative_file.py"
self.assertEqual(
_filepath_to_python_module(the_path, base_path, None),
"tests.components.relative_file.relative_file",
)
base_path = str(settings.BASE_DIR).replace("//", "\\")
with patch("os.path.sep", new="\\"):
the_path = os.path.join(base_path, "tests.py")
base_path = str(settings.BASE_DIR).replace("/", "\\")
with patch("os.name", new="nt"):
the_path = base_path + "\\" + "tests.py"
self.assertEqual(
_filepath_to_python_module(the_path, base_path, None),
"tests",
)
the_path = os.path.join(base_path, "tests\\components\\relative_file\\relative_file.py")
the_path = base_path + "\\" + "tests\\components\\relative_file\\relative_file.py"
self.assertEqual(
_filepath_to_python_module(the_path, base_path, None),
"tests.components.relative_file.relative_file",
)
# NOTE: Windows should handle also POSIX separator
the_path = base_path + "/" + "tests.py"
self.assertEqual(
_filepath_to_python_module(the_path, base_path, None),
"tests",
)
the_path = base_path + "/" + "tests/components/relative_file/relative_file.py"
self.assertEqual(
_filepath_to_python_module(the_path, base_path, None),
"tests.components.relative_file.relative_file",