Skip to content

Commit

Permalink
fix: fix issue when generating unit tests for rules with directory ou…
Browse files Browse the repository at this point in the history
…tput (#1385)

* fix: fix issue when generating unit tests for rules with directory output

* fix
  • Loading branch information
johanneskoester committed Feb 9, 2022
1 parent cccffc4 commit 7db614f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
14 changes: 11 additions & 3 deletions snakemake/unit_tests/__init__.py
Expand Up @@ -54,6 +54,15 @@ def generate(dag, path, deploy=["conda", "singularity"], configfiles=None):
)

for rulename, jobs in groupby(dag.jobs, key=lambda job: job.rule.name):
jobs = list(jobs)
if jobs[0].rule.norun:
logger.info(
"Skipping rule {} because it does not execute anything.".format(
rulename
)
)
continue

testpath = path / "test_{}.py".format(rulename)

if testpath.exists():
Expand All @@ -79,12 +88,11 @@ def copy_files(files, content_type):
if parent.is_absolute():
root = str(f.parents[len(f.parents) - 1])
parent = str(parent)[len(root) :]
print(parent)
target = path / rulename / content_type / parent
os.makedirs(target, exist_ok=True)
if f.is_dir():
shutil.copytree(f, target)
shutil.copytree(f, target / f.name)
else:
os.makedirs(target, exist_ok=True)
shutil.copy(f, target)
if not files:
os.makedirs(path / rulename / content_type, exist_ok=True)
Expand Down
2 changes: 1 addition & 1 deletion snakemake/unit_tests/templates/ruletest.py.jinja2
Expand Up @@ -30,7 +30,7 @@ def test_{{ ruletest.name }}():
"-m",
"snakemake",
"{{ ruletest.target }}",
"-F",
"-f",
"-j1",
"--keep-target-files",
{% if configfiles %}
Expand Down
20 changes: 20 additions & 0 deletions tests/test_github_issue1384/Snakefile
@@ -0,0 +1,20 @@
rule all:
input:
"results/checksum.txt"

rule foo:
output:
directory("results/dir")
shell:
"""
mkdir results/dir
echo hi >> results/dir/something
"""

rule bar:
input:
rules.foo.output
output:
"results/checksum.txt"
shell:
"ls {input}/* >> {output}"
Empty file.
15 changes: 15 additions & 0 deletions tests/tests.py
Expand Up @@ -1412,3 +1412,18 @@ def test_default_target():

def test_cache_multioutput():
run(dpath("test_cache_multioutput"), shouldfail=True)


@skip_on_windows
def test_github_issue1384():
try:
tmpdir = run(dpath("test_github_issue1384"), cleanup=False)
shell(
"""
cd {tmpdir}
python -m snakemake --generate-unit-tests
pytest -v .tests/unit
"""
)
finally:
shutil.rmtree(tmpdir)

0 comments on commit 7db614f

Please sign in to comment.