From 2e138f993027c5eff92c012f9f73304e7479f2e0 Mon Sep 17 00:00:00 2001 From: Lukáš Růžička Date: Jun 24 2022 12:03:32 +0000 Subject: Save progress on discovering missing file tags. --- diff --git a/needle_mapper.py b/needle_mapper.py index e46f33f..944c72a 100755 --- a/needle_mapper.py +++ b/needle_mapper.py @@ -177,24 +177,26 @@ class Solver: tags mentioned in the texts.""" found_tags = {} needle_commands = ['assert_and_click', 'assert_screen', 'check_screen', - 'match_has_tag', 'assert_and_dclick'] + 'match_has_tag', 'assert_and_dclick', 'start_with_launcher', + 'send_key_until_needlematch'] script = self.reader.read_script(filename) for line in script: - match = re.search(r"(match_has_tag|assert_and_dclick|assert_and_click|check_screen|assert_screen).(\"|')(\w*)('|\").", line) + match = re.search(r"(match_has_tag|assert_and_dclick|assert_and_click|check_screen|assert_screen|start_with_launcher|send_key_until_needlematch).(\"|')(\w*)('|\").", line) if match: try: tag = match.group(3) except AttributeError: - tag = None + tag = None if tag and tag not in found_tags.keys(): paths = self.solve_tag(tag)[0] found_tags[tag] = paths else: pass + return found_tags - def solve_missing_needles(self): + def collect_paths(self, pathtype): needles = [] scripts = [] # To avoid all other files except for needles and scripts @@ -203,9 +205,24 @@ class Solver: needles.append(path) elif '/tests/' in path and '.pm' in path: scripts.append(path) + elif '/lib/' in path and '.pm' in path: + scripts.append(path) + elif 'main.pm' in path: + scripts.append(path) else: pass - self.reader.paths = needles + if pathtype == 'scripts': + return scripts + elif pathtype == 'needles': + return needles + else: + return [] + + + def solve_missing_needles(self): + scripts = self.collect_paths('scripts') + self.reader.paths = self.collect_paths('needles') + problems = [] for script in scripts: tags = self.solve_file_inspection(script) if tags: @@ -215,8 +232,35 @@ class Solver: if '$' in tag: pass else: - print("Problematic tag: ", tag, " found in ", script) - + problems.append((tag, script)) + return problems + + def solve_missing_tags(self): + needles = self.collect_paths('needles') + scripts = self.collect_paths('scripts') + self.reader.paths = needles + problems = [] + script_tags = [] + for script in scripts: + tags = self.solve_file_inspection(script) + if tags: + for tag in tags: + if tag not in script_tags: + script_tags.append(tag) + print(f"..... read {len(script_tags)} script tags.") + + for needle in needles: + content = self.reader.read_json(needle) + tags = content['tags'] + clearance = False + for tag in tags: + if tag in script_tags: + print(f"{tag} is used") + clearance = True + elif tag not in script_tags and clearance != True: + problems.append((tag, needle)) + print(tag) + return problems class Printer: @@ -280,6 +324,16 @@ class Printer: first, last = os.path.split(path) print(f"{first}/{colored(last, 'blue')}".rjust(80, '.')) + def print_incomplete(self, problems, datatype): + if datatype == 'needles': + print(f"Showing script tags that do not have a needle counterpart ".ljust(80, '*')) + else: + print(f"Showing needle tags that do not have a script counterpart ".ljust(80, '*')) + for problem in problems: + tag = problem[0] + path = problem[1] + print(f"{tag} ".ljust(80, '.')) + print(f" {path}".rjust(80, '.')) def main(): """ Main program """ @@ -316,102 +370,12 @@ def main(): target = args.target if target == 'needles': solver = Solver(args.root, 'all') - solver.solve_missing_needles() - - elif script_command == 'orphaned': - orphaned_needles = [] - found_tags = [] - called_tags = ["apps_menu_utilities"] - orphaned_tags = [] - problematic_tags = [] - ndir = args.needles - tdir = args.tests - all_needles = FileTree(ndir) - all_tests = FileTree(tdir, needle=False) - print(f"Found {all_needles.count()} needles in the given location.") - print(f"Found {all_tests.count()} tests in the given location.") - - test_reader = FileReader(all_needles.return_paths()) - tests = all_tests.return_paths() - total = len(tests) - count = 0 - for test in tests: - content = test_reader.read_script(test) - tags = test_reader.inspect_file(content) - for tag in tags: - if "$" in tag: - if tag not in problematic_tags: - problematic_tags.append(tag) - else: - if tag not in called_tags: - called_tags.append(tag) - count += 1 - if total == 0: - total = 1 - done = round(count/total*100) - steps = [25, 50, 75, 100] - if done in steps: - print(f"{done}% test scripts parsed.") - print("----------------------------------------------------------") - print(f"Found {len(called_tags)} unique tags called from scripts.") - - needle_reader = FileReader(all_needles.return_paths()) - for needle in all_needles.return_paths(): - used_in_needle = 0 - path, name = os.path.split(needle) - tags = needle_reader.show_file(name)[2] - for tag in tags: - if tag in called_tags: - used_in_needle += 1 - if tag not in called_tags and tag not in orphaned_tags: - orphaned_tags.append(tag) - if used_in_needle == 0: - orphaned_needles.append(needle) - print(f"Found {len(orphaned_tags)} tags in needles that are nowhere used in test scripts.") - print(f"Found {len(problematic_tags)} tags that use a variable in their name and could not be correctly resolved in the first run.") - print(f"Found {len(orphaned_needles)} possibly orphaned needles after matching whole tags.") - print("----------------------------------------------------------") - - recheck_needles = orphaned_needles[:] - recheck_tags = problematic_tags[:] - - needle_reader = FileReader(recheck_needles) - print(f"Rechecking {len(recheck_needles)} possibly orphaned needles.") - orphaned_needles = [] - problematic_tags = [] - for ptag in recheck_tags: - fir = re.search(r"^(\$\{?\w+\}?)\_(\w+)$", ptag) - mid = re.search(r"^(\w+)(\$\{?\w+\}?)\_(\w+)$", ptag) - end = re.search(r"^(\w+)(\$\{?\w+\}?)$", ptag) - circum = re.search(r"^(\$\{?\w+\}?)\_(\w+)_(\$\{?\w+\}?)$", ptag) - - if fir: - subtag = fir.groups()[1] - elif mid: - subtag = mid.groups()[0] - elif end: - subtag = end.groups()[0] - elif circum: - subtag = circum.groups()[1] - if subtag not in problematic_tags: - problematic_tags.append(subtag) - - for needle in recheck_needles: - path, name = os.path.split(needle) - tags = needle_reader.show_file(name)[2] - used_in_needle = 0 - for tag in tags: - for ptag in problematic_tags: - if ptag in tag: - used_in_needle += 1 - if used_in_needle == 0: - orphaned_needles.append(needle) - - - print(f"Found {len(orphaned_needles)} needles that only use orphaned tags:") - print("-----------------------------------------------------------------") - for path in orphaned_needles: - print(path) + problems = solver.solve_missing_needles() + #printer.print_incomplete(problems, 'needles') + elif target == 'tags': + solver = Solver(args.root, 'all') + problems = solver.solve_missing_tags() + #printer.print_incomplete(problems, 'tags') else: print(f"The operation {script_command} is not supported.")