diff --git a/tests/lint_compose.py b/tests/lint_compose.py index 0261be10..3255731f 100644 --- a/tests/lint_compose.py +++ b/tests/lint_compose.py @@ -1,16 +1,16 @@ -import os import glob import logging +import os +from pathlib import Path logging.basicConfig(level=logging.INFO) current_dir = os.path.dirname(os.path.realpath(__file__)) repo_dir = os.path.realpath(f"{current_dir}/..") logging.info("Checking `compose.yaml` files...") -for filename in glob.glob(f"{repo_dir}/**/compose*.yaml", recursive=True): +for filename in glob.glob(f"{repo_dir}/*_ws/docker/compose*.yaml"): logging.debug(f"Checking: '{filename[len(repo_dir)+1:]}'...") - with open(filename, "r") as f: - content = f.read() - if "version:" in content: - # Ref: https://docs.docker.com/compose/compose-file/04-version-and-name/#version-top-level-element-optional - raise ValueError(f"`version` should not exist since it's obsolete: '{filename}'") + content = Path(filename).read_text() + if "version:" in content: + # Ref: https://docs.docker.com/compose/compose-file/04-version-and-name/#version-top-level-element-optional + raise ValueError(f"`version` should not exist since it's obsolete: '{filename}'") diff --git a/tests/lint_devcontainer.py b/tests/lint_devcontainer.py index d0375fa3..f03f38ca 100644 --- a/tests/lint_devcontainer.py +++ b/tests/lint_devcontainer.py @@ -9,29 +9,31 @@ current_dir = os.path.dirname(os.path.realpath(__file__)) repo_dir = os.path.realpath(f"{current_dir}/..") -template_path = f"{repo_dir}/tests/diff_base/.devcontainer/devcontainer.json" -template = Path(template_path).read_text().splitlines(keepends=True) +def compare_file_with_template(filepath, ignored_workspaces=[]): + logging.info(f"Checking if '{filepath}' matches the template...") + template_path = f"{repo_dir}/tests/diff_base/{filepath}" + template = Path(template_path).read_text().splitlines(keepends=True) + for filename in glob.glob(f"{repo_dir}/*_ws/{filepath}"): + # Skip certain cases intentionally + if any(ws in filename for ws in ignored_workspaces): + continue + logging.debug(f"Checking: '{filename[len(repo_dir)+1:]}'...") + content = Path(filename).read_text().splitlines(keepends=True) + diff = list(filter(lambda x: x.startswith('- ') or x.startswith('+ '), difflib.ndiff(template, content))) + def error(msg, i): + diff.insert(i+2, "! <<< Parsing failed before reaching here >>>\n") + logging.info('\n' + ''.join(diff)) + logging.error(msg) + raise ValueError(f"'{filepath}' does not match the template: '{filename}'") + i = 0 + while i < len(diff): + if i+1 >= len(diff): + error("Odd lines", i) + if not diff[i].startswith('- ') or not diff[i+1].startswith('+ '): + error("Expected no line deletion and addition", i) + regexp = "^" + re.escape(diff[i][1:]).replace('PLACEHOLDER', '.*') + "$" + if not re.match(regexp, diff[i+1][1:]): + error("Expected line deletion and addition to differ only in the placeholder", i) + i += 2 -logging.info("Checking if `devcontainer.json` matches the template...") -for filename in glob.glob(f"{repo_dir}/**/.devcontainer/devcontainer.json", recursive=True): - # Skip certain cases intentionally - if "ros1_bridge_ws" in filename: - continue - logging.debug(f"Checking: '{filename[len(repo_dir)+1:]}'...") - content = Path(filename).read_text().splitlines(keepends=True) - diff = list(filter(lambda x: x.startswith('- ') or x.startswith('+ '), difflib.ndiff(template, content))) - def error(msg, i): - diff.insert(i+2, "! <<< Parsing failed before reaching here >>>\n") - logging.info('\n' + ''.join(diff)) - logging.error(msg) - raise ValueError(f"devcontainer.json does not match the template: '{filename}'") - i = 0 - while i < len(diff): - if i+1 >= len(diff): - error("Odd lines", i) - if not diff[i].startswith('- ') or not diff[i+1].startswith('+ '): - error("Expected no line deletion and addition", i) - regexp = "^" + re.escape(diff[i][1:]).replace('PLACEHOLDER', '.*') + "$" - if not re.match(regexp, diff[i+1][1:]): - error("Expected line deletion and addition to differ only in the placeholder", i) - i += 2 +compare_file_with_template(".devcontainer/devcontainer.json", ["ros1_bridge_ws"]) diff --git a/tests/lint_filenames.py b/tests/lint_filenames.py index 219a2ea2..d307fb29 100644 --- a/tests/lint_filenames.py +++ b/tests/lint_filenames.py @@ -1,6 +1,6 @@ -import os import glob import logging +import os logging.basicConfig(level=logging.INFO) current_dir = os.path.dirname(os.path.realpath(__file__)) diff --git a/tests/lint_workflows.py b/tests/lint_workflows.py index 4e5544bc..36a29285 100644 --- a/tests/lint_workflows.py +++ b/tests/lint_workflows.py @@ -1,16 +1,16 @@ -import os import glob import logging +import os +from pathlib import Path logging.basicConfig(level=logging.INFO) current_dir = os.path.dirname(os.path.realpath(__file__)) repo_dir = os.path.realpath(f"{current_dir}/..") logging.info("Checking if `master` branch is accidentally used in github workflows...") -for filename in glob.glob(f"{repo_dir}/.github/workflows/*.yaml", recursive=True): +for filename in glob.glob(f"{repo_dir}/.github/workflows/*.yaml"): logging.debug(f"Checking: '{filename[len(repo_dir)+1:]}'...") - with open(filename, "r") as f: - content = f.read() - if "master" in content: - # Ref: https://github.com/j3soon/ros2-essentials/pull/44#pullrequestreview-2251404984 - raise ValueError(f"`master` should not exist since it's obsolete: '{filename}'") + content = Path(filename).read_text() + if "master" in content: + # Ref: https://github.com/j3soon/ros2-essentials/pull/44#pullrequestreview-2251404984 + raise ValueError(f"`master` should not exist since it's obsolete: '{filename}'")