Skip to content

Commit

Permalink
Merge pull request #5 from mirpedrol/use-nfcore-dev
Browse files Browse the repository at this point in the history
Use nfcore dev
  • Loading branch information
mirpedrol authored Aug 28, 2024
2 parents f6e8f16 + 6c3dea5 commit cf824bc
Show file tree
Hide file tree
Showing 13 changed files with 415 additions and 30 deletions.
4 changes: 4 additions & 0 deletions .github/.coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[run]
omit = nf_class/*-template/*
source = nf_class
relative_files = True
33 changes: 33 additions & 0 deletions .github/workflows/lint-code.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Lint code formatting
on:
push:
branches:
- dev
pull_request:
release:
types: [published]

# Cancel if a newer run is started
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
Pre-commit:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4

- name: Set up Python 3.12
uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5
with:
python-version: "3.12"
cache: "pip"

- name: Install pre-commit
run: pip install pre-commit

- name: Run pre-commit
run: pre-commit run --all-files
174 changes: 174 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
name: Python tests
# This workflow is triggered on pushes and PRs to the repository.
# Only run if we changed a Python file
on:
push:
branches:
- dev
paths-ignore:
- "CHANGELOG.md"
pull_request:
paths-ignore:
- "CHANGELOG.md"
# ignore github workflows except for the current one
- ".github/**"
- "!.github/workflows/pytest.yml"
release:
types: [published]
workflow_dispatch:

# Cancel if a newer run with the same workflow name is queued
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
setup:
runs-on: "ubuntu-latest"
strategy:
matrix:
python-version: ["3.8", "3.12"]
runner: ["ubuntu-latest"]
include:
- python-version: "3.8"
runner: "ubuntu-20.04"

steps:
- name: Check conditions
id: conditions
run: echo "run-tests=${{ github.ref == 'refs/heads/master' || (matrix.runner == 'ubuntu-20.04' && matrix.python-version == '3.8') }}" >> "$GITHUB_OUTPUT"

outputs:
python-version: ${{ matrix.python-version }}
runner: ${{ matrix.runner }}
run-tests: ${{ steps.conditions.outputs.run-tests }}

# create a test matrix based on all python files in /tests
list_tests:
name: Get test file matrix
runs-on: "ubuntu-latest"
steps:
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
name: Check out source-code repository

- name: List tests
id: list_tests
run: |
echo "tests=$(find tests -type f -name "test_*.py" | tac | sed 's/tests\///g' | jq -R -s -c '{test: (split("\n")[:-1])}')" >> $GITHUB_OUTPUT
outputs:
tests: ${{ steps.list_tests.outputs.tests }}

test:
name: Run ${{matrix.test}} with Python ${{ needs.setup.outputs.python-version }} on ${{ needs.setup.outputs.runner }}
needs: [setup, list_tests]
if: ${{ needs.setup.outputs.run-tests }}
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJson(needs.list_tests.outputs.tests) }}
fail-fast: false # run all tests even if one fails
steps:
- name: go to subdirectory and change nextflow workdir
run: |
mkdir -p pytest
cd pytest
export NXF_WORK=$(pwd)
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
name: Check out source-code repository

- name: Set up Python ${{ needs.setup.outputs.python-version }}
uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5
with:
python-version: ${{ needs.setup.outputs.python-version }}
cache: "pip"
token: ${{ secrets.GITHUB_TOKEN }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip -r requirements-dev.txt
pip install -e .
- name: Install dev version of nf-core
# reinstall the dev version of nf-core until v3.0.0 is released
run: |
pip install --upgrade --force-reinstall git+https://github.com/nf-core/tools.git@dev
- name: Downgrade git to the Ubuntu official repository's version
if: ${{ needs.setup.outputs.runner == 'ubuntu-20.04' && needs.setup.outputs.python-version == '3.8' }}
run: |
sudo apt update
sudo apt remove -y git git-man
sudo add-apt-repository --remove ppa:git-core/ppa
sudo apt install -y git
- name: Set up Singularity
if: ${{ matrix.test == 'test_download.py'}}
uses: eWaterCycle/setup-singularity@931d4e31109e875b13309ae1d07c70ca8fbc8537 # v7
with:
singularity-version: 3.8.3

- name: Get current date
id: date
run: echo "date=$(date +'%Y-%m')" >> $GITHUB_ENV

- name: Install Nextflow
uses: nf-core/setup-nextflow@v2

- name: Install nf-test
uses: nf-core/setup-nf-test@v1

- name: move coveragerc file up
run: |
mv .github/.coveragerc .
- name: Test with pytest
run: |
python3 -m pytest tests/${{matrix.test}} --color=yes --cov --durations=0 && exit_code=0|| exit_code=$?
- name: remove slashes from test name
run: |
test=$(echo ${{ matrix.test }} | sed 's/\//__/g')
echo "test=${test}" >> $GITHUB_ENV
- name: Upload coverage
uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4
with:
name: coverage_${{ env.test }}
path: .coverage

coverage:
needs: test
# use the runner given by the input if it is dispatched manually, run on github if it is a rerun or on self-hosted by default
runs-on: ubuntu-latest
steps:
- name: go to subdirectory
run: |
mkdir -p pytest
cd pytest
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
- name: Set up Python 3.12
uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5
with:
python-version: "3.12"
cache: "pip"

- name: Install dependencies
run: |
python -m pip install --upgrade pip -r requirements-dev.txt
pip install -e .
- name: move coveragerc file up
run: |
mv .github/.coveragerc .
- name: Download all artifacts
uses: actions/download-artifact@65a9edc5881444af0b9093a5e628f2fe47ea3b2e # v4
- name: Run coverage
run: |
coverage combine --keep coverage*/.coverage*
coverage report
coverage xml
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
## v1.0.0dev

- Initial creation of the package
- Add command `nf-class modules create-from-class` command [#1](https://github.com/mirpedrol/nf-class/pull/1)
- Add command `nf-class modules create-from-class` [#1](https://github.com/mirpedrol/nf-class/pull/1)
- Add command `nf-class subworkflows expand-class` [#2](https://github.com/mirpedrol/nf-class/pull/2)
- Add pytests [#5](https://github.com/mirpedrol/nf-class/pull/5)
8 changes: 4 additions & 4 deletions nf_class/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,15 @@ def command_modules_create_from_class(

try:
create_obj = ModuleCreateFromClass(
ctx,
classname,
toolname,
dir,
author,
force,
conda_name,
conda_package_version,
ctx.obj["modules_repo_url"],
ctx.obj["modules_repo_branch"],
)
create_obj.create_from_class()
except UserWarning as e:
Expand Down Expand Up @@ -369,21 +370,20 @@ def command_subworkflows_expand_class(

try:
create_obj = SubworkflowExpandClass(
ctx,
classname,
dir,
author,
force,
expand_modules,
prefix,
suffix,
ctx.obj["modules_repo_url"],
ctx.obj["modules_repo_branch"],
)
create_obj.expand_class()
except UserWarning as e:
raise
log.error(e)
sys.exit(1)
except LookupError as e:
raise
log.error(e)
sys.exit(1)
19 changes: 7 additions & 12 deletions nf_class/components/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@

# import nf_core.components.components_utils
import nf_core.components.create
import nf_core.modules.modules_repo

# TODO: include this once new version of nf-core is released
# import nf_core.pipelines.lint_utils
import nf_core.lint_utils
import nf_core.modules.modules_repo
import nf_core.pipelines.lint_utils
from nf_class.utils import NF_CLASS_MODULES_REMOTE
from nf_core.utils import nfcore_question_style

log = logging.getLogger(__name__)
Expand All @@ -27,7 +28,6 @@ class ComponentCreateFromClass(nf_core.components.create.ComponentCreate):
Create a new module or subworkflow from a class.
Args:
ctx (dict): Click context object.
component_type (str): Type of component to create. [modules|subworkflows]
directory (str): Directory to create the component in. [default: <current directory>]
classname (str): Name of the class to create the component from.
Expand All @@ -54,7 +54,6 @@ class ComponentCreateFromClass(nf_core.components.create.ComponentCreate):

def __init__(
self,
ctx,
component_type: str,
directory: str = ".",
classname: str = "",
Expand All @@ -63,6 +62,8 @@ def __init__(
force: bool = False,
conda_name: Optional[str] = None,
conda_version: Optional[str] = None,
modules_repo_url: Optional[str] = NF_CLASS_MODULES_REMOTE,
modules_repo_branch: Optional[str] = "main",
):
super().__init__(
component_type=component_type,
Expand All @@ -77,9 +78,7 @@ def __init__(
empty_template=False,
migrate_pytest=False,
)
self.modules_repo = nf_core.modules.modules_repo.ModulesRepo(
ctx.obj["modules_repo_url"], ctx.obj["modules_repo_branch"]
)
self.modules_repo = nf_core.modules.modules_repo.ModulesRepo(modules_repo_url, modules_repo_branch)
self.classname = classname

def create_from_class(self) -> None:
Expand Down Expand Up @@ -109,8 +108,6 @@ def create_from_class(self) -> None:

# Check existence of directories early for fast-fail
self.file_paths = self._get_component_dirs()
# TODO: remove this lines once the new version of nf-core/tools is released
self.file_paths.pop("tests/tags.yml")

if self.component_type == "modules":
# Try to find a bioconda package for 'component'
Expand Down Expand Up @@ -254,9 +251,7 @@ def _render_template(self) -> None:
with open(dest_fn, "w") as fh:
log.debug(f"Writing output to: '{dest_fn}'")
fh.write(rendered_output)
# TODO: change line once new version of nf-core is released
# nf_core.pipelines.lint_utils.run_prettier_on_file(dest_fn)
nf_core.lint_utils.run_prettier_on_file(dest_fn)
nf_core.pipelines.lint_utils.run_prettier_on_file(dest_fn)

# Mirror file permissions
template_stat = (Path(nf_class.__file__).parent / f"{self.component_type}-template" / template_fn).stat()
Expand Down
9 changes: 6 additions & 3 deletions nf_class/modules/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@ class ModuleCreateFromClass(ComponentCreateFromClass):
Create a new module from a class.
Args:
ctx (dict): Click context object.
classname (str): Name of the class to create the module from.
component (str): Name of the module to create.
dir (str): Directory to create the module in. [default: <current directory>]
author (str): Author of the module.
force (bool): Overwrite existing files.
conda_name (str): Name of the conda environment.
conda_version (str): Version of the conda environment.
modules_repo_url (str): URL of the modules repository.
modules_repo_branch (str): Branch of the modules repository.
"""

def __init__(
self,
ctx,
classname: str = "",
component: str = "",
dir: str = ".",
author: Optional[str] = None,
force: bool = False,
conda_name: Optional[str] = None,
conda_version: Optional[str] = None,
modules_repo_url: Optional[str] = None,
modules_repo_branch: Optional[str] = None,
):
super().__init__(
ctx,
"modules",
dir,
classname,
Expand All @@ -39,4 +40,6 @@ def __init__(
force,
conda_name,
conda_version,
modules_repo_url,
modules_repo_branch,
)
Loading

0 comments on commit cf824bc

Please sign in to comment.