def test_randomised_tree_descent(execution_number, depth, width): sl = [l for l in ascii_uppercase * RANGE * RANGE * RANGE * RANGE] random.shuffle(sl) def random_tree(depth, width): if depth == 0: return [File(sl.pop()) for _ in range(width)] folder_indexes = random.sample(range(width), random.randint(0, width - 1) + 1) results = [File(sl.pop()) for _ in range(width)] for index in folder_indexes: results[index] = Folder(sl.pop(), contents=random_tree(depth - 1, width)) return results randomised = Folder("", contents=random_tree(1 + depth, 1 + width)) print(randomised) all_lines = [line for line in tree(randomised)] parsed = TreeParser(all_lines)() print(randomised) print("Randomised:") print("\n".join(all_lines)) print(parsed) print("Parsed:") print("\n".join([l for l in tree(parsed)])) assert parsed == randomised
def text_tree( path: Path, ignore_globs: Optional[List[str]] = None, include_globs: Optional[List[str]] = None, ): """Generate the textual tree representation only""" structure = build_tree(path, ignore_globs, include_globs) return "\n".join(list(tree(structure)))
def test_randomised_idempotent(execution_number, depth, width): sl = [l for l in ascii_uppercase * 2 * RANGE * RANGE * RANGE * RANGE] random.shuffle(sl) def random_tree(depth, width): """More complex random tree. We need to make sure there are no duplicate files or folders, and that they have content""" def purge_repeated_files(results): def rename_if_needed(fil, nrf): if fil.name in nrf: doubled = fil.name + fil.name fil._rename(doubled) rename_if_needed(fil, nrf) non_repeated_filenames = [] cleaned_results = [] for fil in results: rename_if_needed(fil, non_repeated_filenames) non_repeated_filenames += [fil.name] cleaned_results += [fil] return cleaned_results if depth == 0: results = [ File(sl.pop()).set_contents("contents=" + sl.pop()) for _ in range(width) ] return purge_repeated_files(results) folder_indexes = random.sample(range(width), random.randint(0, width - 1) + 1) results = [ File(sl.pop()).set_contents("contents=" + sl.pop()) for _ in range(width) ] cleaned_results = purge_repeated_files(results) for index in folder_indexes: cleaned_results[index] = Folder(sl.pop(), contents=random_tree( depth - 1, width)) cleaned_folders = purge_repeated_files(cleaned_results) return cleaned_folders randomised = Folder("", contents=random_tree(1 + depth, 1 + width)) all_lines = [line for line in tree(randomised)] markdown = "\n".join( build_markdown(build_tree(randomised, ignore_globs=None, include_globs=None), max_length=15)) structure = _process_markdown(markdown, replacements=None) new_markdown = "\n".join( build_markdown(build_tree(structure, ignore_globs=None, include_globs=None), max_length=15)) assert structure == randomised
def build_markdown(structure: Folder, max_length): """Generate markdown from a path, given ignore files""" built_tree = tree(structure) markdown = [""] + ["# Tree structure"] markdown += [""] + ["```"] + list(built_tree) + ["```"] + [""] markdown += list(tree_links(structure)) markdown += [""] + build_file_markdown( structure, base="", max_length=max_length) return markdown
def test_tree_descent(): mbp = Folder( "", [ File("foo"), Folder("bar", [File("baz"), File("foobar")]), File("zzz"), Folder("meh", [File("buzz")]), ], ) all_lines = list(tree(mbp)) print("Original") print("\n".join(all_lines)) parsed = TreeParser(all_lines)() print(parsed) all_lines = list(tree(parsed)) print("parsed back") print("\n".join(all_lines)) assert parsed == mbp