2012-09-09 19:07:04 +00:00
|
|
|
import os
|
2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
2019-08-10 09:28:00 +00:00
|
|
|
from pathlib import Path
|
2012-09-09 19:07:04 +00:00
|
|
|
|
2014-11-11 17:59:49 +00:00
|
|
|
from django.core.exceptions import SuspiciousFileOperation
|
2019-08-10 09:28:00 +00:00
|
|
|
from django.utils._os import safe_join, to_path
|
2012-09-07 20:49:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SafeJoinTests(unittest.TestCase):
|
|
|
|
def test_base_path_ends_with_sep(self):
|
2012-09-09 19:07:04 +00:00
|
|
|
drive, path = os.path.splitdrive(safe_join("/abc/", "abc"))
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(path, "{0}abc{0}abc".format(os.path.sep))
|
2012-09-07 20:49:22 +00:00
|
|
|
|
|
|
|
def test_root_path(self):
|
2012-09-09 19:07:04 +00:00
|
|
|
drive, path = os.path.splitdrive(safe_join("/", "path"))
|
2012-09-07 20:49:22 +00:00
|
|
|
self.assertEqual(
|
2012-09-09 19:07:04 +00:00
|
|
|
path,
|
2014-11-27 00:41:27 +00:00
|
|
|
"{}path".format(os.path.sep),
|
2012-09-07 20:49:22 +00:00
|
|
|
)
|
|
|
|
|
2012-09-09 19:07:04 +00:00
|
|
|
drive, path = os.path.splitdrive(safe_join("/", ""))
|
2012-09-07 20:49:22 +00:00
|
|
|
self.assertEqual(
|
2012-09-09 19:07:04 +00:00
|
|
|
path,
|
|
|
|
os.path.sep,
|
2012-09-07 20:49:22 +00:00
|
|
|
)
|
2014-11-11 17:59:49 +00:00
|
|
|
|
|
|
|
def test_parent_path(self):
|
|
|
|
with self.assertRaises(SuspiciousFileOperation):
|
|
|
|
safe_join("/abc/", "../def")
|
2019-08-10 09:28:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ToPathTests(unittest.TestCase):
|
|
|
|
def test_to_path(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
for path in ("/tmp/some_file.txt", Path("/tmp/some_file.txt")):
|
2019-08-10 09:28:00 +00:00
|
|
|
with self.subTest(path):
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(to_path(path), Path("/tmp/some_file.txt"))
|
2019-08-10 09:28:00 +00:00
|
|
|
|
|
|
|
def test_to_path_invalid_value(self):
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
to_path(42)
|