Skip to content

Commit effe7f8

Browse files
authored
Fix test side effects: isolate file I/O in tmp dirs
1 parent 42335b2 commit effe7f8

8 files changed

Lines changed: 31 additions & 21 deletions

File tree

src/jcvi/assembly/hic.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,10 @@ def bam2mat(args):
946946
"--seqids",
947947
help="Use a given seqids file, a single line with seqids joined by comma",
948948
)
949+
p.add_argument(
950+
"--outdir",
951+
help="Output directory for generated files (default: same dir as input BAM)",
952+
)
949953
opts, args = p.parse_args(args)
950954

951955
if len(args) != 1:
@@ -954,7 +958,8 @@ def bam2mat(args):
954958
(bamfilename,) = args
955959
pf = bamfilename.rsplit(".", 1)[0]
956960
N = opts.resolution
957-
pf += f".resolution_{N}"
961+
pf_name = op.basename(pf) + f".resolution_{N}"
962+
pf = op.join(opts.outdir, pf_name) if opts.outdir else pf + f".resolution_{N}"
958963
bins = 1500 # Distance distribution bins
959964
minsize = 100 # Record distance if it is at least minsize
960965
seqids = opts.seqids

src/jcvi/compara/synteny.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,7 @@ def liftover(args):
19301930
"""
19311931
p = OptionParser(liftover.__doc__)
19321932
p.set_stripnames()
1933+
p.set_outfile()
19331934

19341935
blast_file, anchor_file, dist, opts = add_arguments(p, args)
19351936
qbed, sbed, qorder, sorder, is_self = check_beds(blast_file, p, opts)
@@ -1961,7 +1962,7 @@ def liftover(args):
19611962
lifted += 1
19621963

19631964
logger.debug("%d new pairs found (dist=%d).", lifted, dist)
1964-
newanchorfile = anchor_file.rsplit(".", 1)[0] + ".lifted.anchors"
1965+
newanchorfile = opts.outfile or anchor_file.rsplit(".", 1)[0] + ".lifted.anchors"
19651966
if accepted:
19661967
ac.filter_blocks(accepted)
19671968
ac.print_to_file(filename=newanchorfile)

tests/assembly/hic.py/tests.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
bam2mat:
22
action: bam2mat
3-
opts:
3+
opts: --outdir=__TMP__
44
args: __DIR__/inputs/test.bam
5-
outputs: [__DIR__/inputs/test.resolution_500000.json]
5+
outputs: [__TMP__/test.resolution_500000.json]
66
references: [references/test.resolution_500000.json]
77

88
bam2mat_seqids:
99
action: bam2mat
10-
opts: --seqids=__DIR__/inputs/seqids
10+
opts: --seqids=__DIR__/inputs/seqids --outdir=__TMP__
1111
args: __DIR__/inputs/test.bam
12-
outputs: [__DIR__/inputs/test.resolution_500000.json]
12+
outputs: [__TMP__/test.resolution_500000.json]
1313
references: [references/test.resolution_500000.seqids.json]

tests/assembly/test_allmaps.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import os
21
import os.path as op
3-
from jcvi.apps.base import mkdir
42
from jcvi.assembly.allmaps import path
53

64

@@ -34,14 +32,13 @@ def test_liftover(tmp_path, monkeypatch):
3432

3533

3634
def test_path(tmp_path, monkeypatch):
35+
import shutil
36+
3737
monkeypatch.chdir(tmp_path)
38-
bedfile = datafile("inputs/JM-2.bed")
38+
bedfile = str(tmp_path / "JM-2.bed")
39+
shutil.copy(datafile("inputs/JM-2.bed"), bedfile)
3940
fastafile = datafile("inputs/scaffolds.fasta.gz")
4041
weightsfile = datafile("inputs/weights.txt")
41-
output_image = "chr23.pdf"
42-
testdir = str(tmp_path / "chr23")
43-
mkdir(testdir)
44-
os.chdir(testdir)
4542
path(
4643
[
4744
bedfile,
@@ -50,4 +47,4 @@ def test_path(tmp_path, monkeypatch):
5047
weightsfile,
5148
]
5249
)
53-
assert op.exists(output_image)
50+
assert op.exists("chr23.pdf")

tests/compara/synfind.py/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
synfind:
22
script: synfind
33
action: main
4-
opts: -o __DIR__/inputs/testtigs.testchr.synfind
4+
opts: -o __TMP__/testtigs.testchr.synfind
55
args: __DIR__/inputs/testtigs.testchr.last
6-
outputs: [__DIR__/inputs/testtigs.testchr.synfind]
6+
outputs: [__TMP__/testtigs.testchr.synfind]
77
references: [references/testtigs.testchr.synfind]

tests/compara/synteny.py/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ depth:
1414

1515
liftover:
1616
action: liftover
17-
opts: --qbed=__DIR__/inputs/test_empty_blocks.a.bed --sbed=__DIR__/inputs/test_empty_blocks.b.bed
17+
opts: --qbed=__DIR__/inputs/test_empty_blocks.a.bed --sbed=__DIR__/inputs/test_empty_blocks.b.bed --outfile=__TMP__/test_empty_blocks.lifted.anchors
1818
args: __DIR__/inputs/test_empty_blocks.last __DIR__/inputs/test_empty_blocks.anchors
19-
outputs: [__DIR__/inputs/test_empty_blocks.lifted.anchors]
19+
outputs: [__TMP__/test_empty_blocks.lifted.anchors]
2020
references: [references/test_empty_blocks.lifted.anchors]

tests/config.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"""
1414

1515
import sys
16+
import os
1617
import os.path as op
1718
import glob
1819
import re
@@ -134,8 +135,13 @@ def test_script(
134135
func = getattr(module, action)
135136
args = " ".join((opts, args)).split()
136137

137-
with Capturing(stdout) as output:
138-
func(args)
138+
original_dir = os.getcwd()
139+
os.chdir(tmp_dir)
140+
try:
141+
with Capturing(stdout) as output:
142+
func(args)
143+
finally:
144+
os.chdir(original_dir)
139145

140146
if not fail:
141147
for output, reference in zip(outputs, references):

tests/graphics/test_glyph.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
from jcvi.graphics.glyph import demo
55

66

7-
def test_demo():
7+
def test_demo(tmp_path, monkeypatch):
8+
monkeypatch.chdir(tmp_path)
89
demo([])

0 commit comments

Comments
 (0)