def test_two_files_with_common_grandparent(monkeypatch): paths = [ pathlib.Path('/a/b/c/file1.txt'), pathlib.Path('/a/b/d/file2.txt'), ] monkeypatch.setattr(pathlib.Path, 'is_dir', lambda _: False) expected = pathlib.Path('/a/b') obtained = deepest_common_path(paths) assert expected == obtained
def test_a_dir_and_a_file_directly_contained(monkeypatch): def fake_is_dir(path): return str(path) == '/a/b/c' paths = [ pathlib.Path('/a/b/c'), pathlib.Path('/a/b/c/file1.txt'), ] monkeypatch.setattr(pathlib.Path, 'is_dir', fake_is_dir) expected = pathlib.Path('/a/b/c') obtained = deepest_common_path(paths) assert expected == obtained
def test_a_bunch_of_dirs_and_files(monkeypatch): def fake_is_dir(path): return not str(path).endswith(".txt") paths = [ pathlib.Path('/a/b/c'), pathlib.Path('/a/b/c/file1.txt'), pathlib.Path('/a/b/d/e'), pathlib.Path('/a/b/f/g/file2.txt'), ] monkeypatch.setattr(pathlib.Path, 'is_dir', fake_is_dir) expected = pathlib.Path('/a/b') obtained = deepest_common_path(paths) assert expected == obtained
def check_options(options): """ checks the existence of the paths it breaks execution if: - any of the options['paths'] doesn't exist - in case options['base_folder'] is provided, it is not an ancestor of all the paths In case base_folder is not provided, it is set to the deepest common path of all the paths """ if any(not path.exists() for path in options['paths']): print("ERROR: all the paths must exist") sys.exit(1) cdp = rstutils.deepest_common_path(options['paths']) if 'base_folder' not in options: options['base_folder'] = cdp return if options['base_folder'] not in (cdp / '_').parents: print("ERROR: base folder must contain all the paths") sys.exit(1)
def test_just_one_file(monkeypatch): paths = [pathlib.Path('/a/b/c/file.txt')] monkeypatch.setattr(pathlib.Path, 'is_dir', lambda _: False) expected = pathlib.Path('/a/b/c') obtained = deepest_common_path(paths) assert expected == obtained
def test_empty_list_of_paths(): paths = [] expected = pathlib.Path('/') obtained = deepest_common_path(paths) assert expected == obtained