Files
galaxy/test/unit/util/test_paths.py
T
Nicola Soranzo 2f0f4c7e08 Replace 2 uses of deprecated `imp` module
Also:
- Move doctests for `in_directory()` function to unit tests, add test
  for `local_path_module` parameter to exercise the posixpath/ntpath
  import code.
2022-04-21 19:53:35 +01:00

29 lines
842 B
Python

import ntpath
import os
import posixpath
import tempfile
from galaxy.util import in_directory
def test_in_directory():
base_dir = tempfile.mkdtemp()
safe_dir = os.path.join(base_dir, "user")
os.mkdir(safe_dir)
good_file = os.path.join(safe_dir, "1")
with open(good_file, "w") as f:
f.write("hello")
assert in_directory(good_file, safe_dir)
assert not in_directory("/other/file/is/here.txt", safe_dir)
unsafe_link = os.path.join(safe_dir, "2")
os.symlink("/other/file/bad.fasta", unsafe_link)
assert not in_directory(unsafe_link, safe_dir)
# Test local_path_module parameter
if os.path == posixpath:
assert in_directory(good_file, safe_dir, local_path_module=ntpath)
elif os.path == ntpath:
assert in_directory(good_file, safe_dir, local_path_module=posixpath)