Skip to content

Commit 1256997

Browse files
committed
using pytest tmpdir in fixtures
1 parent 8b97712 commit 1256997

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

nipype/testing/fixtures.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,23 @@ def nifti_image_files(outdir, filelist, shape):
2727

2828

2929
@pytest.fixture()
30-
def create_files_in_directory(request):
31-
outdir = os.path.realpath(mkdtemp())
30+
def create_files_in_directory(request, tmpdir):
31+
outdir = str(tmpdir)
3232
cwd = os.getcwd()
3333
os.chdir(outdir)
3434
filelist = ['a.nii', 'b.nii']
3535
nifti_image_files(outdir, filelist, shape=(3,3,3,4))
3636

37-
def clean_directory():
38-
if os.path.exists(outdir):
39-
rmtree(outdir)
37+
def change_directory():
4038
os.chdir(cwd)
4139

42-
request.addfinalizer(clean_directory)
40+
request.addfinalizer(change_directory)
4341
return (filelist, outdir)
4442

4543

4644
@pytest.fixture()
47-
def create_files_in_directory_plus_dummy_file(request):
48-
outdir = os.path.realpath(mkdtemp())
45+
def create_files_in_directory_plus_dummy_file(request, tmpdir):
46+
outdir = str(tmpdir)
4947
cwd = os.getcwd()
5048
os.chdir(outdir)
5149
filelist = ['a.nii', 'b.nii']
@@ -55,29 +53,25 @@ def create_files_in_directory_plus_dummy_file(request):
5553
fp.write('dummy file')
5654
filelist.append('reg.dat')
5755

58-
def clean_directory():
59-
if os.path.exists(outdir):
60-
rmtree(outdir)
56+
def change_directory():
6157
os.chdir(cwd)
6258

63-
request.addfinalizer(clean_directory)
59+
request.addfinalizer(change_directory)
6460
return (filelist, outdir)
6561

6662

6763
@pytest.fixture()
68-
def create_surf_file_in_directory(request):
69-
outdir = os.path.realpath(mkdtemp())
64+
def create_surf_file_in_directory(request, tmpdir):
65+
outdir = str(tmpdir)
7066
cwd = os.getcwd()
7167
os.chdir(outdir)
7268
surf = 'lh.a.nii'
7369
nifti_image_files(outdir, filelist=surf, shape=(1,100,1))
7470

75-
def clean_directory():
76-
if os.path.exists(outdir):
77-
rmtree(outdir)
71+
def change_directory():
7872
os.chdir(cwd)
7973

80-
request.addfinalizer(clean_directory)
74+
request.addfinalizer(change_directory)
8175
return (surf, outdir)
8276

8377

@@ -93,10 +87,10 @@ def set_output_type(fsl_output_type):
9387
return prev_output_type
9488

9589
@pytest.fixture(params=[None]+list(Info.ftypes))
96-
def create_files_in_directory_plus_output_type(request):
90+
def create_files_in_directory_plus_output_type(request, tmpdir):
9791
func_prev_type = set_output_type(request.param)
9892

99-
testdir = os.path.realpath(mkdtemp())
93+
testdir = str(tmpdir)
10094
origdir = os.getcwd()
10195
os.chdir(testdir)
10296
filelist = ['a.nii', 'b.nii']
@@ -105,8 +99,6 @@ def create_files_in_directory_plus_output_type(request):
10599
out_ext = Info.output_type_to_ext(Info.output_type())
106100

107101
def fin():
108-
if os.path.exists(testdir):
109-
rmtree(testdir)
110102
set_output_type(func_prev_type)
111103
os.chdir(origdir)
112104

0 commit comments

Comments
 (0)