1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
# This file is part of the django-environ.
#
# Copyright (c) 2021-2024, Serghei Iakovlev <oss@serghei.pl>
# Copyright (c) 2013-2021, Daniele Faraglia <daniele.faraglia@gmail.com>
#
# For the full copyright and license information, please view
# the LICENSE.txt file that was distributed with this source code.
import os
import sys
import pytest
from environ import Path
from environ.compat import ImproperlyConfigured
def test_str(volume):
root = Path('/home')
if sys.platform == 'win32':
assert str(root) == '{}home'.format(volume)
assert str(root()) == '{}home'.format(volume)
assert str(root('dev')) == '{}home\\dev'.format(volume)
else:
assert str(root) == '/home'
assert str(root()) == '/home'
assert str(root('dev')) == '/home/dev'
def test_path_class():
root = Path(__file__, '..', is_file=True)
root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../'))
assert root() == root_path
assert root.__root__ == root_path
web = root.path('public')
assert web() == os.path.join(root_path, 'public')
assert web('css') == os.path.join(root_path, 'public', 'css')
def test_repr(volume):
root = Path('/home')
if sys.platform == 'win32':
assert root.__repr__() == '<Path:{}home>'.format(volume)
else:
assert root.__repr__() == '<Path:/home>'
def test_comparison(volume):
root = Path('/home')
assert root.__eq__(Path('/home'))
assert root in Path('/')
assert root not in Path('/other/path')
assert Path('/home') == Path('/home')
assert Path('/home') != Path('/home/dev')
assert Path('/home/foo/').rfind('/') == str(Path('/home/foo')).rfind('/')
assert Path('/home/foo/').find('/home') == str(Path('/home/foo/')).find('/home')
assert Path('/home/foo/')[1] == str(Path('/home/foo/'))[1]
assert Path('/home/foo/').__fspath__() == str(Path('/home/foo/'))
assert ~Path('/home') == Path('/')
if sys.platform == 'win32':
assert Path('/home') == '{}home'.format(volume)
assert '{}home'.format(volume) == Path('/home')
else:
assert Path('/home') == '/home'
assert '/home' == Path('/home')
assert Path('/home') != '/usr'
def test_sum():
"""Make sure Path correct handle __add__."""
assert Path('/') + 'home' == Path('/home')
assert Path('/') + '/home/public' == Path('/home/public')
def test_subtraction():
"""Make sure Path correct handle __sub__."""
assert Path('/home/dev/public') - 2 == Path('/home')
assert Path('/home/dev/public') - 'public' == Path('/home/dev')
def test_subtraction_not_int():
"""Subtraction with an invalid type should raise TypeError."""
with pytest.raises(TypeError) as excinfo:
Path('/home/dev/') - 'not int'
assert str(excinfo.value) == (
"unsupported operand type(s) for -: '<class 'environ.environ.Path'>' "
"and '<class 'str'>' unless value of <class 'environ.environ.Path'> "
"ends with value of <class 'str'>"
)
def test_required_path():
root = Path('/home')
with pytest.raises(ImproperlyConfigured) as excinfo:
root('dev', 'not_existing_dir', required=True)
assert "Create required path:" in str(excinfo.value)
with pytest.raises(ImproperlyConfigured) as excinfo:
Path('/not/existing/path/', required=True)
assert "Create required path:" in str(excinfo.value)
def test_complex_manipulation(volume):
root = Path('/home')
public = root.path('public')
assets, scripts = public.path('assets'), public.path('assets', 'scripts')
if sys.platform == 'win32':
assert public.__repr__() == '<Path:{}home\\public>'.format(volume)
assert str(public.root) == '{}home\\public'.format(volume)
assert str(public('styles')) == '{}home\\public\\styles'.format(volume)
assert str(assets.root) == '{}home\\public\\assets'.format(volume)
assert str(scripts.root) == '{}home\\public\\assets\\scripts'.format(
volume
)
assert (~assets).__repr__() == '<Path:{}home\\public>'.format(
volume
)
assert str(assets + 'styles') == (
'{}home\\public\\assets\\styles'.format(volume)
)
assert (assets + 'styles').__repr__() == (
'<Path:{}home\\public\\assets\\styles>'.format(volume)
)
else:
assert public.__repr__() == '<Path:/home/public>'
assert str(public.root) == '/home/public'
assert str(public('styles')) == '/home/public/styles'
assert str(assets.root) == '/home/public/assets'
assert str(scripts.root) == '/home/public/assets/scripts'
assert str(assets + 'styles') == '/home/public/assets/styles'
assert (~assets).__repr__() == '<Path:/home/public>'
assert (assets + 'styles').__repr__() == (
'<Path:/home/public/assets/styles>'
)
|