Skip to content

Commit

Permalink
Merge pull request #4 from CCBR/tests
Browse files Browse the repository at this point in the history
Setup unit tests with pytest & nf-test
  • Loading branch information
kelly-sovacool authored Oct 3, 2023
2 parents eca72db + 8889eeb commit 410475a
Show file tree
Hide file tree
Showing 24 changed files with 1,694 additions and 16 deletions.
82 changes: 82 additions & 0 deletions .github/check_duplicate_md5s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python

from rich import print
from rich.table import Table
import click
import glob
import os
import yaml


@click.command()
@click.option(
"--min_dups",
default=5,
show_default=True,
help="Minimum number of duplicates to report",
)
@click.option(
"--search_dir",
default=f"{os.path.dirname(__file__)}/../tests/**/test.yml",
show_default=True,
help="Glob directory pattern used to find test YAML files",
)
def find_duplicate_md5s(min_dups, search_dir):
"""
Find duplicate file MD5 sums in test YAML files.
"""
md5_filenames = {}
md5_output_fn_counts = {}
module_counts = {}

# Loop through all files in tests/ called test.yml
for test_yml in glob.glob(search_dir, recursive=True):
# Open file and parse YAML
with open(test_yml, "r") as fh:
test_config = yaml.safe_load(fh)
# Loop through tests and check for duplicate md5s
for test in test_config:
for test_file in test.get("files", []):
if "md5sum" in test_file:
md5 = test_file["md5sum"]
md5_filenames[md5] = md5_filenames.get(md5, []) + [
os.path.basename(test_file.get("path"))
]
md5_output_fn_counts[md5] = md5_output_fn_counts.get(md5, 0) + 1
# Log the module that this md5 was in
modname = os.path.basename(os.path.dirname(test_yml))
# If tool/subtool show the whole thing
# Ugly code but trying to stat os-agnostic
if os.path.basename(
os.path.dirname(os.path.dirname(test_yml))
) not in ["modules", "config", "subworkflows"]:
modname = "{}/{}".format(
os.path.basename(
os.path.dirname(os.path.dirname(test_yml))
),
os.path.basename(os.path.dirname(test_yml)),
)
module_counts[md5] = module_counts.get(md5, []) + [modname]

# Set up rich table
table = Table(title="Duplicate MD5s", row_styles=["dim", ""])
table.add_column("MD5", style="cyan", no_wrap=True)
table.add_column("Count", style="magenta", justify="right")
table.add_column("Num modules", style="blue", justify="right")
table.add_column("Filenames", style="green")

# Add rows - sort md5_output_fn_counts by value
for md5 in sorted(md5_output_fn_counts, key=md5_output_fn_counts.get):
if md5_output_fn_counts[md5] >= min_dups:
table.add_row(
md5,
str(md5_output_fn_counts[md5]),
str(len(set(module_counts[md5]))),
", ".join(set(md5_filenames[md5])),
)

print(table)


if __name__ == "__main__":
find_duplicate_md5s()
72 changes: 72 additions & 0 deletions .github/workflows/fix-linting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: fix-linting
run-name: fix linting (automated)
on:
issue_comment:
types: [created]

jobs:
fix-linting:
# Only run if comment is on a PR with the main repo, and if it contains the magic keywords
if: >
contains(github.event.comment.html_url, '/pull/') &&
contains(github.event.comment.body, '/fix linting') &&
(github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') &&
github.repository == 'CCBR/nf-modules'
runs-on: ubuntu-latest
steps:
# Use the @nf-core-bot token to check out so we can push later
- uses: actions/checkout@v3
with:
token: ${{ secrets.GH_PAT }}

# indication that the linting is being fixed
- name: React on comment
uses: peter-evans/create-or-update-comment@v2
with:
comment-id: ${{ github.event.comment.id }}
reactions: eyes

# Action runs on the issue comment, so we don't get the PR by default
# Use the gh cli to check out the PR
- name: Checkout Pull Request
run: gh pr checkout ${{ github.event.issue.number }}
env:
GITHUB_TOKEN: ${{ secrets.GH_PAT }}

- uses: actions/setup-node@v2

- name: Install Prettier
run: npm install -g prettier @prettier/plugin-php

# Check that we actually need to fix something
- name: Run 'prettier --check'
id: prettier_status
run: |
if prettier --check ${GITHUB_WORKSPACE}; then
echo "::set-output name=result::pass"
else
echo "::set-output name=result::fail"
fi
- name: Run 'prettier --write'
if: steps.prettier_status.outputs.result == 'fail'
run: prettier --write ${GITHUB_WORKSPACE}

- name: Post nothing-to-do comment
if: steps.prettier_status.outputs.result == 'pass'
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ github.event.issue.number }}
body: |
Nothing for me to do here! :shrug:
This is probably because the linting errors come from `nf-core lint` and have to be fixed manually (or with `nf-core lint --fix`).
- name: Commit & push changes
if: steps.prettier_status.outputs.result == 'fail'
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git config push.default upstream
git add .
git status
git commit -m "🤖 Fix linting with Prettier"
git push
Loading

0 comments on commit 410475a

Please sign in to comment.