Skip to content

Commit

Permalink
test: Reformat and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
j3soon committed Aug 21, 2024
1 parent 830f54e commit 26ef505
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 40 deletions.
14 changes: 7 additions & 7 deletions tests/lint_compose.py
Original file line number Diff line number Diff line change
@@ -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}'")
52 changes: 27 additions & 25 deletions tests/lint_devcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
2 changes: 1 addition & 1 deletion tests/lint_filenames.py
Original file line number Diff line number Diff line change
@@ -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__))
Expand Down
14 changes: 7 additions & 7 deletions tests/lint_workflows.py
Original file line number Diff line number Diff line change
@@ -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}'")

0 comments on commit 26ef505

Please sign in to comment.