From ede313dcd31ea5f136b3b8f743e2265331475342 Mon Sep 17 00:00:00 2001 From: stydodt <56256130+stydodt@users.noreply.github.com> Date: Sat, 27 Nov 2021 08:25:36 +0100 Subject: [PATCH] fix: SameFileError #1153 (#1220) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixes https://github.com/snakemake/snakemake/issues/1153. Path.parent replaces target path when full paths are provided, matching file and target locations. * Turn absolute path into relative one instead of avoiding file path altogether. Co-authored-by: Benjamin Essigman Co-authored-by: Johannes Köster Co-authored-by: Johannes Köster --- snakemake/unit_tests/__init__.py | 7 ++++++- tests/test_generate_unit_tests/Snakefile | 4 ++++ tests/tests.py | 16 +++++++++------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/snakemake/unit_tests/__init__.py b/snakemake/unit_tests/__init__.py index 2725af273..50111fe25 100644 --- a/snakemake/unit_tests/__init__.py +++ b/snakemake/unit_tests/__init__.py @@ -75,7 +75,12 @@ def generate(dag, path, deploy=["conda", "singularity"], configfiles=None): def copy_files(files, content_type): for f in files: f = Path(f) - target = path / rulename / content_type / f.parent + parent = f.parent + 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) diff --git a/tests/test_generate_unit_tests/Snakefile b/tests/test_generate_unit_tests/Snakefile index 6d03b9d19..335d78985 100644 --- a/tests/test_generate_unit_tests/Snakefile +++ b/tests/test_generate_unit_tests/Snakefile @@ -1,8 +1,12 @@ +import os + rule all: input: expand("test/{sample}.tsv", sample=range(3)) rule a: + input: + os.environ["UNIT_TEST_TMPFILE"] output: temp("test/{sample}.txt") shell: diff --git a/tests/tests.py b/tests/tests.py index f11dcd806..78a5fcd72 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -1192,13 +1192,15 @@ def test_github_issue640(): @skip_on_windows # TODO check whether this might be enabled later def test_generate_unit_tests(): - tmpdir = run( - dpath("test_generate_unit_tests"), - generate_unit_tests=".tests/unit", - check_md5=False, - cleanup=False, - ) - sp.check_call(["pytest", ".tests", "-vs"], cwd=tmpdir) + with tempfile.NamedTemporaryFile() as tmpfile: + os.environ["UNIT_TEST_TMPFILE"] = tmpfile.name + tmpdir = run( + dpath("test_generate_unit_tests"), + generate_unit_tests=".tests/unit", + check_md5=False, + cleanup=False, + ) + sp.check_call(["pytest", ".tests", "-vs"], cwd=tmpdir) @skip_on_windows