1
1
import os
2
+ import shutil
2
3
import tempfile
3
4
import unittest
5
+
4
6
from contextlib import contextmanager
5
7
6
8
from odml .tools .converters import FormatConverter
9
+ from .util import create_test_dir
7
10
8
11
FC = FormatConverter
9
12
@@ -20,6 +23,11 @@ def setUp(self):
20
23
</section>
21
24
</odML>
22
25
"""
26
+ self .tmp_dir = None
27
+
28
+ def tearDown (self ):
29
+ if os .path .exists (self .tmp_dir ):
30
+ shutil .rmtree (self .tmp_dir )
23
31
24
32
@contextmanager
25
33
def assertNotRaises (self , exc_type ):
@@ -28,6 +36,13 @@ def assertNotRaises(self, exc_type):
28
36
except exc_type :
29
37
raise self .failureException ('{} raised' .format (exc_type .__name__ ))
30
38
39
+ def _create_open_file (self , in_dir ):
40
+ in_file = tempfile .NamedTemporaryFile (mode = 'a+' , suffix = ".xml" , dir = in_dir )
41
+ in_file .write (self .doc )
42
+ in_file .seek (0 )
43
+
44
+ return in_file
45
+
31
46
def test_convert (self ):
32
47
if os .name == 'nt' :
33
48
raise unittest .SkipTest ("Skipping test on Windows" )
@@ -50,10 +65,10 @@ def test_convert_dir_no_output_dir(self, recursive=False, func=None):
50
65
if os .name == 'nt' :
51
66
raise unittest .SkipTest ("Skipping test on Windows" )
52
67
53
- work_dir = tempfile . mkdtemp ( )
54
- in_dir = tempfile .mkdtemp (dir = work_dir )
55
- in_file = self .create_open_file (in_dir )
56
- in_file2 = self .create_open_file (in_dir )
68
+ self . tmp_dir = create_test_dir ( __file__ )
69
+ in_dir = tempfile .mkdtemp (dir = self . tmp_dir )
70
+ in_file = self ._create_open_file (in_dir )
71
+ in_file2 = self ._create_open_file (in_dir )
57
72
58
73
if not func :
59
74
FC .convert_dir (in_dir , None , recursive , "odml" )
@@ -64,7 +79,7 @@ def test_convert_dir_no_output_dir(self, recursive=False, func=None):
64
79
func ([in_dir , "odml" ])
65
80
66
81
files = []
67
- for dir_path , dir_names , file_names in os .walk (work_dir ):
82
+ for dir_path , dir_names , file_names in os .walk (self . tmp_dir ):
68
83
for file_name in file_names :
69
84
files .append (os .path .join (dir_path , file_name ))
70
85
@@ -89,10 +104,11 @@ def test_convert_dir_with_output_dir_specified(self, func=None):
89
104
raise unittest .SkipTest ("Skipping test on Windows" )
90
105
91
106
# Testing FC.convert_dir(in_dir, out_dir, False, "odml")
92
- in_dir = tempfile .mkdtemp ()
93
- out_dir = tempfile .mkdtemp ()
94
- in_file = self .create_open_file (in_dir )
95
- in_file2 = self .create_open_file (in_dir )
107
+ self .tmp_dir = create_test_dir (__file__ )
108
+ in_dir = tempfile .mkdtemp (dir = self .tmp_dir )
109
+ out_dir = tempfile .mkdtemp (dir = self .tmp_dir )
110
+ in_file = self ._create_open_file (in_dir )
111
+ in_file2 = self ._create_open_file (in_dir )
96
112
97
113
if not func :
98
114
FC .convert_dir (in_dir , out_dir , False , "odml" )
@@ -124,15 +140,10 @@ def test_convert_dir_with_output_dir_specified(self, func=None):
124
140
in_file .close ()
125
141
in_file2 .close ()
126
142
127
- def create_open_file (self , in_dir ):
128
- in_file = tempfile .NamedTemporaryFile (mode = 'a+' , suffix = ".xml" , dir = in_dir )
129
- in_file .write (self .doc )
130
- in_file .seek (0 )
131
- return in_file
132
-
133
143
def test_check_io_directory (self ):
134
- out_dir = tempfile .mkdtemp ()
135
- in_dir = tempfile .mkdtemp ()
144
+ self .tmp_dir = create_test_dir (__file__ )
145
+ out_dir = tempfile .mkdtemp (dir = self .tmp_dir )
146
+ in_dir = tempfile .mkdtemp (dir = self .tmp_dir )
136
147
with self .assertRaises (ValueError ):
137
148
FC ._check_input_output_directory (None , None )
138
149
with self .assertRaises (ValueError ):
0 commit comments