Skip to content

Commit

Permalink
refactor: Continued prefix.dev
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundmiller committed Nov 21, 2024
1 parent 518f996 commit 95549a3
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 29 deletions.
6 changes: 3 additions & 3 deletions nf_core/components/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,16 @@ def create(self) -> bool:
log.info("Created following files:\n " + "\n ".join(new_files))
return True

def _get_bioconda_tool(self):
async def _get_bioconda_tool(self):
"""
Try to find a bioconda package for 'tool'
"""
while True:
try:
if self.tool_conda_name:
anaconda_response = nf_core.utils.anaconda_package(self.tool_conda_name, ["bioconda"])
anaconda_response = await nf_core.utils.anaconda_package(self.tool_conda_name, ["bioconda"])
else:
anaconda_response = nf_core.utils.anaconda_package(self.component, ["bioconda"])
anaconda_response = await nf_core.utils.anaconda_package(self.component, ["bioconda"])

if not self.tool_conda_version:
version = anaconda_response.get("latest_version")
Expand Down
14 changes: 6 additions & 8 deletions nf_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pathlib import Path
from typing import Any, Callable, Dict, Generator, List, Literal, Optional, Tuple, Union

import aiohttp
import git
import prompt_toolkit.styles
import questionary
Expand Down Expand Up @@ -639,7 +640,7 @@ def request_retry(self, url, post_data=None):
gh_api = GitHubAPISession()


def anaconda_package(dep, dep_channels=None):
async def anaconda_package(dep, dep_channels=None):
"""Query conda package information.
Sends a HTTP GET request to the Prefix.dev remote GraphQL API.
Expand Down Expand Up @@ -676,7 +677,7 @@ def anaconda_package(dep, dep_channels=None):
}
"""

variables = {"name": package_name}
variables = {"name": depname}

headers = {
"Content-Type": "application/json",
Expand All @@ -690,14 +691,14 @@ def anaconda_package(dep, dep_channels=None):
if response.status == 200:
data = await response.json()
if "errors" in data:
log.debug(f"GraphQL error for {package_name}: {data['errors']}")
log.debug(f"GraphQL error for {depname}: {data['errors']}")
return None
return data["data"]["package"]
else:
log.debug(f"HTTP {response.status} error for {package_name}")
log.debug(f"HTTP {response.status} error for {depname}")
return None
except Exception as e:
log.debug(f"Error checking package {package_name}: {str(e)}")
log.debug(f"Error checking package {depname}: {str(e)}")
return None


Expand Down Expand Up @@ -1373,6 +1374,3 @@ def set_wd(path: Path) -> Generator[None, None, None]:
yield
finally:
os.chdir(start_wd)


async def check_anaconda_package(package_name, version=None):
10 changes: 5 additions & 5 deletions tests/modules/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from tests.utils import (
GITLAB_SUBWORKFLOWS_ORG_PATH_BRANCH,
GITLAB_URL,
mock_anaconda_api_calls,
mock_biocontainers_api_calls,
mock_prefix_api_calls,
)

from ..test_modules import TestModules
Expand All @@ -24,7 +24,7 @@ class TestModulesCreate(TestModules):
def test_modules_create_succeed(self):
"""Succeed at creating the TrimGalore! module"""
with responses.RequestsMock() as rsps:
mock_anaconda_api_calls(rsps, "trim-galore", "0.6.7")
mock_prefix_api_calls(rsps, "trim-galore", "0.6.7")
mock_biocontainers_api_calls(rsps, "trim-galore", "0.6.7")
module_create = nf_core.modules.create.ModuleCreate(
self.pipeline_dir, "trimgalore", "@author", "process_single", True, True, conda_name="trim-galore"
Expand All @@ -36,7 +36,7 @@ def test_modules_create_succeed(self):
def test_modules_create_fail_exists(self):
"""Fail at creating the same module twice"""
with responses.RequestsMock() as rsps:
mock_anaconda_api_calls(rsps, "trim-galore", "0.6.7")
mock_prefix_api_calls(rsps, "trim-galore", "0.6.7")
mock_biocontainers_api_calls(rsps, "trim-galore", "0.6.7")
module_create = nf_core.modules.create.ModuleCreate(
self.pipeline_dir, "trimgalore", "@author", "process_single", False, False, conda_name="trim-galore"
Expand All @@ -51,7 +51,7 @@ def test_modules_create_fail_exists(self):
def test_modules_create_nfcore_modules(self):
"""Create a module in nf-core/modules clone"""
with responses.RequestsMock() as rsps:
mock_anaconda_api_calls(rsps, "fastqc", "0.11.9")
mock_prefix_api_calls(rsps, "fastqc", "0.11.9")
mock_biocontainers_api_calls(rsps, "fastqc", "0.11.9")
module_create = nf_core.modules.create.ModuleCreate(
self.nfcore_modules, "fastqc", "@author", "process_low", False, False
Expand All @@ -66,7 +66,7 @@ def test_modules_create_nfcore_modules(self):
def test_modules_create_nfcore_modules_subtool(self):
"""Create a tool/subtool module in a nf-core/modules clone"""
with responses.RequestsMock() as rsps:
mock_anaconda_api_calls(rsps, "star", "2.8.10a")
mock_prefix_api_calls(rsps, "star", "2.8.10a")
mock_biocontainers_api_calls(rsps, "star", "2.8.10a")
module_create = nf_core.modules.create.ModuleCreate(
self.nfcore_modules, "star/index", "@author", "process_medium", False, False
Expand Down
42 changes: 42 additions & 0 deletions tests/modules/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,48 @@ def test_modules_empty_file_in_stub_snapshot(self):
with open(snap_file, "w") as fh:
fh.write(content)

def test_modules_lint_individual_files(self):
"""Test linting individual files passed as arguments"""
for test_case in INDIVIDUAL_FILES:
files, passed, warned, failed = test_case

# Create temporary test files
for file_path in files:
full_path = Path(self.pipeline_dir, file_path)
full_path.parent.mkdir(parents=True, exist_ok=True)
full_path.touch()

module_lint = nf_core.modules.lint.ModuleLint(directory=self.pipeline_dir)
module_lint.lint(print_results=False, files=files)

# Clean up test files
for file_path in files:
Path(self.pipeline_dir, file_path).unlink()

assert len(module_lint.passed) == passed, f"Expected {passed} passes, got {len(module_lint.passed)}"
assert len(module_lint.warned) == warned, f"Expected {warned} warnings, got {len(module_lint.warned)}"
assert len(module_lint.failed) == failed, f"Expected {failed} failures, got {len(module_lint.failed)}"

def test_modules_lint_mixed_files_and_module(self):
"""Test linting when both files and module name are provided"""
files = ["modules/nf-core/bwa/mem/main.nf"]

# Create test file
test_file = Path(self.pipeline_dir, files[0])
test_file.parent.mkdir(parents=True, exist_ok=True)
test_file.touch()

module_lint = nf_core.modules.lint.ModuleLint(directory=self.pipeline_dir)

# Should prioritize files over module name
module_lint.lint(print_results=False, files=files, module="fastqc")

test_file.unlink()

assert len(module_lint.passed) == 1
assert len(module_lint.warned) == 0
assert len(module_lint.failed) == 0


# A skeleton object with the passed/warned/failed list attrs
# Use this in place of a ModuleLint object to test behaviour of
Expand Down
19 changes: 17 additions & 2 deletions tests/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
OLD_TRIMGALORE_BRANCH,
OLD_TRIMGALORE_SHA,
create_tmp_pipeline,
mock_anaconda_api_calls,
mock_biocontainers_api_calls,
mock_biotools_api_calls,
mock_prefix_api_calls,
)


Expand All @@ -49,7 +49,22 @@ def create_modules_repo_dummy(tmp_dir):
yaml.dump(nf_core_yml.model_dump(), fh)
# mock biocontainers and anaconda response and biotools response
with responses.RequestsMock() as rsps:
mock_anaconda_api_calls(rsps, "bpipe", "0.9.13--hdfd78af_0")
# Mock the POST request
rsps.add(
responses.POST,
"https://prefix.dev/api/graphql",
json={}, # or the expected response data
status=200,
)

# Mock the GET request
rsps.add(
responses.GET,
"https://api.biocontainers.pro/ga4gh/trs/v2/tools/bpipe/versions/bpipe-0.9.13",
json={}, # or the expected response data
status=200,
)
mock_prefix_api_calls(rsps, "bpipe", "0.9.13--hdfd78af_0")
mock_biocontainers_api_calls(rsps, "bpipe", "0.9.13--hdfd78af_0")
mock_biotools_api_calls(rsps, "bpipe")
# bpipe is a valid package on bioconda that is very unlikely to ever be added to nf-core/modules
Expand Down
27 changes: 16 additions & 11 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,23 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
return wrapper


def mock_anaconda_api_calls(rsps: responses.RequestsMock, module: str, version: str) -> None:
"""Mock anaconda api calls for module"""
anaconda_api_url = f"https://api.anaconda.org/package/bioconda/{module}"
anaconda_mock = {
"latest_version": version.split("--")[0],
"summary": "",
"doc_url": "http://test",
"dev_url": "http://test",
"files": [{"version": version.split("--")[0]}],
"license": "MIT",
def mock_prefix_api_calls(rsps: responses.RequestsMock, module: str, version: str) -> None:
"""Mock prefix.dev GraphQL API calls for module"""
prefix_api_url = "https://prefix.dev/api/graphql"
prefix_mock = {
"data": {
"package": {
"name": module,
"latestVersion": {
"version": version.split("--")[0],
# TODO We don't need to mock this yet
# "platforms": ["noarch"],
# "urls": [{"url": "http://docs.bpipe.org/", "kind": "HOME", "order": 0}],
},
}
}
}
rsps.get(anaconda_api_url, json=anaconda_mock, status=200)
rsps.post(prefix_api_url, json=prefix_mock, status=200)


def mock_biocontainers_api_calls(rsps: responses.RequestsMock, module: str, version: str) -> None:
Expand Down

0 comments on commit 95549a3

Please sign in to comment.