1. import os
    
  2. import unittest
    
  3. from pathlib import Path
    
  4. 
    
  5. from django.core.exceptions import SuspiciousFileOperation
    
  6. from django.utils._os import safe_join, to_path
    
  7. 
    
  8. 
    
  9. class SafeJoinTests(unittest.TestCase):
    
  10.     def test_base_path_ends_with_sep(self):
    
  11.         drive, path = os.path.splitdrive(safe_join("/abc/", "abc"))
    
  12.         self.assertEqual(path, "{0}abc{0}abc".format(os.path.sep))
    
  13. 
    
  14.     def test_root_path(self):
    
  15.         drive, path = os.path.splitdrive(safe_join("/", "path"))
    
  16.         self.assertEqual(
    
  17.             path,
    
  18.             "{}path".format(os.path.sep),
    
  19.         )
    
  20. 
    
  21.         drive, path = os.path.splitdrive(safe_join("/", ""))
    
  22.         self.assertEqual(
    
  23.             path,
    
  24.             os.path.sep,
    
  25.         )
    
  26. 
    
  27.     def test_parent_path(self):
    
  28.         with self.assertRaises(SuspiciousFileOperation):
    
  29.             safe_join("/abc/", "../def")
    
  30. 
    
  31. 
    
  32. class ToPathTests(unittest.TestCase):
    
  33.     def test_to_path(self):
    
  34.         for path in ("/tmp/some_file.txt", Path("/tmp/some_file.txt")):
    
  35.             with self.subTest(path):
    
  36.                 self.assertEqual(to_path(path), Path("/tmp/some_file.txt"))
    
  37. 
    
  38.     def test_to_path_invalid_value(self):
    
  39.         with self.assertRaises(TypeError):
    
  40.             to_path(42)