[lang-ref] ( build_path ) ( python )
def test_build_path():
# os.path.join(a, b)
# A missing separator is inserted automatically.
assert os.path.join('/path/to' , 'file1.txt') == '/path/to/file1.txt'
assert os.path.join('/path/to/' , 'file1.txt') == '/path/to/file1.txt'
# The result is not normalized.
assert os.path.join('/path//to' , 'file1.txt') == '/path//to/file1.txt'
assert os.path.join('/path/to//' , 'file1.txt') == '/path/to//file1.txt'
assert os.path.join('/path/to/' , './file1.txt') == '/path/to/./file1.txt'
assert os.path.join('/path/to' , '../file1.txt') == '/path/to/../file1.txt'
# If an absolute path is given, previous parts are discarded.
assert os.path.join('/path/to/' , '/file1.txt') == '/file1.txt'
def test_build_path_alternative():
# pathlib.Path / joinpath
# A missing separator is inserted automatically.
assert (Path('/path/to') / 'file1.txt').as_posix() == '/path/to/file1.txt'
assert (Path('/path/to/') / 'file1.txt').as_posix() == '/path/to/file1.txt'
# Path is partially normalized (e.g., "//" and "./" are collapsed).
assert (Path('/path//to') / 'file1.txt').as_posix() == '/path/to/file1.txt'
assert (Path('/path/to//') / 'file1.txt').as_posix() == '/path/to/file1.txt'
assert (Path('/path/to/') / './file1.txt').as_posix() == '/path/to/file1.txt'
# ".." is kept as-is (not resolved) unless you call resolve().
assert (Path('/path/to') / '../file1.txt').as_posix() == '/path/to/../file1.txt'
# If an absolute path is given, previous parts are discarded.
assert (Path('/path/to') / '/file1.txt').as_posix() == '/file1.txt'