Skip to content

Commit 2a12879

Browse files
committed
[test] Use central test dir and cleanup
All tests writing to file now use the central test directory from test/util and clean up after they are done.
1 parent 0c257c9 commit 2a12879

14 files changed

+65
-60
lines changed

test/test_doc_integration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55

66
import os
77
import shutil
8-
import tempfile
98
import unittest
109

1110
import odml
11+
from .util import create_test_dir
1212

1313

1414
class TestDocumentIntegration(unittest.TestCase):
1515

1616
def setUp(self):
1717
# Set up test environment
18-
self.tmp_dir = tempfile.mkdtemp(suffix=".odml")
18+
self.tmp_dir = create_test_dir(__file__)
1919

2020
self.json_file = os.path.join(self.tmp_dir, "test.json")
2121
self.xml_file = os.path.join(self.tmp_dir, "test.xml")

test/test_dtypes_integration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
import datetime as dt
88
import os
99
import shutil
10-
import tempfile
1110
import unittest
1211

1312
import odml
13+
from .util import create_test_dir
1414

1515

1616
class TestTypesIntegration(unittest.TestCase):
1717

1818
def setUp(self):
1919
# Set up test environment
20-
self.tmp_dir = tempfile.mkdtemp(suffix=".odml")
20+
self.tmp_dir = create_test_dir(__file__)
2121

2222
self.json_file = os.path.join(self.tmp_dir, "test.json")
2323
self.xml_file = os.path.join(self.tmp_dir, "test.xml")

test/test_format_converter.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import os
2+
import shutil
23
import tempfile
34
import unittest
5+
46
from contextlib import contextmanager
57

68
from odml.tools.converters import FormatConverter
9+
from .util import create_test_dir
710

811
FC = FormatConverter
912

@@ -20,6 +23,11 @@ def setUp(self):
2023
</section>
2124
</odML>
2225
"""
26+
self.tmp_dir = None
27+
28+
def tearDown(self):
29+
if os.path.exists(self.tmp_dir):
30+
shutil.rmtree(self.tmp_dir)
2331

2432
@contextmanager
2533
def assertNotRaises(self, exc_type):
@@ -28,6 +36,13 @@ def assertNotRaises(self, exc_type):
2836
except exc_type:
2937
raise self.failureException('{} raised'.format(exc_type.__name__))
3038

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+
3146
def test_convert(self):
3247
if os.name == 'nt':
3348
raise unittest.SkipTest("Skipping test on Windows")
@@ -50,10 +65,10 @@ def test_convert_dir_no_output_dir(self, recursive=False, func=None):
5065
if os.name == 'nt':
5166
raise unittest.SkipTest("Skipping test on Windows")
5267

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)
5772

5873
if not func:
5974
FC.convert_dir(in_dir, None, recursive, "odml")
@@ -64,7 +79,7 @@ def test_convert_dir_no_output_dir(self, recursive=False, func=None):
6479
func([in_dir, "odml"])
6580

6681
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):
6883
for file_name in file_names:
6984
files.append(os.path.join(dir_path, file_name))
7085

@@ -89,10 +104,11 @@ def test_convert_dir_with_output_dir_specified(self, func=None):
89104
raise unittest.SkipTest("Skipping test on Windows")
90105

91106
# 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)
96112

97113
if not func:
98114
FC.convert_dir(in_dir, out_dir, False, "odml")
@@ -124,15 +140,10 @@ def test_convert_dir_with_output_dir_specified(self, func=None):
124140
in_file.close()
125141
in_file2.close()
126142

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-
133143
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)
136147
with self.assertRaises(ValueError):
137148
FC._check_input_output_directory(None, None)
138149
with self.assertRaises(ValueError):

test/test_links.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
23
from . import test_samplefile as samplefile
34

45

test/test_parser_json.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
import json
77
import os
8-
import tempfile
8+
import shutil
99
import unittest
1010

1111
from odml.tools import dict_parser
1212
from odml.tools.parser_utils import ParserException, InvalidVersionException
13+
from .util import create_test_dir
1314

1415

1516
_INVALID_ATTRIBUTE_HANDLING_DOC = """
@@ -114,16 +115,11 @@ def setUp(self):
114115

115116
self.json_reader = dict_parser.DictReader(show_warnings=False)
116117

117-
dir_name = os.path.basename(os.path.splitext(__file__)[0])
118-
tmp_base_path = os.path.join(tempfile.gettempdir(), "odml_test")
119-
if not os.path.exists(tmp_base_path):
120-
os.mkdir(tmp_base_path)
118+
self.tmp_dir_path = create_test_dir(__file__)
121119

122-
tmp_dir_path = os.path.join(tmp_base_path, dir_name)
123-
if not os.path.exists(tmp_dir_path):
124-
os.mkdir(tmp_dir_path)
125-
126-
self.tmp_dir_path = tmp_dir_path
120+
def tearDown(self):
121+
if os.path.exists(self.tmp_dir_path):
122+
shutil.rmtree(self.tmp_dir_path)
127123

128124
def _prepare_doc(self, file_name, file_content):
129125
file_path = os.path.join(self.tmp_dir_path, file_name)

test/test_parser_odml.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
import os
77
import shutil
8-
import tempfile
98
import unittest
109

1110
from odml import Document, Section, Property
1211
from odml.tools import odmlparser
12+
from .util import create_test_dir
1313

1414

1515
class TestOdmlParser(unittest.TestCase):
@@ -19,7 +19,7 @@ def setUp(self):
1919
dir_path = os.path.dirname(os.path.realpath(__file__))
2020
self.basefile = os.path.join(dir_path, "resources", "example.odml")
2121

22-
self.tmp_dir = tempfile.mkdtemp(suffix=".odml")
22+
self.tmp_dir = create_test_dir(__file__)
2323

2424
self.json_file = os.path.join(self.tmp_dir, "test.json")
2525
self.xml_file = os.path.join(self.tmp_dir, "test.xml")

test/test_parser_yaml.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
"""
55

66
import os
7-
import tempfile
7+
import shutil
88
import unittest
99
import yaml
1010

1111
from odml.tools import dict_parser
1212
from odml.tools.parser_utils import ParserException, InvalidVersionException
13+
from .util import create_test_dir
1314

1415

1516
_INVALID_ATTRIBUTE_HANDLING_DOC = """
@@ -74,16 +75,11 @@ def setUp(self):
7475

7576
self.yaml_reader = dict_parser.DictReader(show_warnings=False)
7677

77-
dir_name = os.path.basename(os.path.splitext(__file__)[0])
78-
tmp_base_path = os.path.join(tempfile.gettempdir(), "odml_test")
79-
if not os.path.exists(tmp_base_path):
80-
os.mkdir(tmp_base_path)
78+
self.tmp_dir_path = create_test_dir(__file__)
8179

82-
tmp_dir_path = os.path.join(tmp_base_path, dir_name)
83-
if not os.path.exists(tmp_dir_path):
84-
os.mkdir(tmp_dir_path)
85-
86-
self.tmp_dir_path = tmp_dir_path
80+
def tearDown(self):
81+
if os.path.exists(self.tmp_dir_path):
82+
shutil.rmtree(self.tmp_dir_path)
8783

8884
def _prepare_doc(self, file_name, file_content):
8985
file_path = os.path.join(self.tmp_dir_path, file_name)

test/test_property_integration.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55

66
import os
77
import shutil
8-
import tempfile
98
import unittest
109

1110
import odml
1211

12+
from .util import create_test_dir
13+
1314

1415
class TestPropertyIntegration(unittest.TestCase):
1516

1617
def setUp(self):
1718
# Set up test environment
18-
self.tmp_dir = tempfile.mkdtemp(suffix=".odml")
19+
self.tmp_dir = create_test_dir(__file__)
1920

2021
self.json_file = os.path.join(self.tmp_dir, "test.json")
2122
self.xml_file = os.path.join(self.tmp_dir, "test.xml")

test/test_samplefile.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import re
33
import sys
4-
import tempfile
54
import unittest
65

76
try:
@@ -13,6 +12,7 @@
1312

1413
from odml.info import FORMAT_VERSION
1514
from odml.tools import xmlparser
15+
from .util import create_test_dir
1616

1717
try:
1818
unicode = unicode
@@ -162,7 +162,7 @@ def test_xml_writer_version(self):
162162
# self.assertEqual(doc._xml_version, FORMAT_VERSION)
163163

164164
def test_save(self):
165-
base_path = tempfile.gettempdir()
165+
base_path = create_test_dir(__file__)
166166
for module in [xmlparser.XMLWriter]:
167167
path = os.path.join(base_path, "temp.odml")
168168
doc = module(self.doc)
@@ -369,7 +369,8 @@ def test_get_property_by_path(self):
369369
self.assertRaises(ValueError, sec1.get_property_by_path, wrong_path)
370370

371371
def test_save_version(self):
372-
tmp_file = os.path.join(tempfile.gettempdir(), "example.odml")
372+
tmp_dir = create_test_dir(__file__)
373+
tmp_file = os.path.join(tmp_dir, "example.odml")
373374

374375
self.doc.version = '2.4'
375376
writer = xmlparser.XMLWriter(self.doc)

test/test_section_integration.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55

66
import os
77
import shutil
8-
import tempfile
98
import unittest
109

1110
import odml
1211

12+
from .util import create_test_dir
13+
1314

1415
class TestSectionIntegration(unittest.TestCase):
1516

1617
def setUp(self):
1718
# Set up test environment
18-
self.tmp_dir = tempfile.mkdtemp(suffix=".odml")
19+
self.tmp_dir = create_test_dir(__file__)
1920

2021
self.json_file = os.path.join(self.tmp_dir, "test.json")
2122
self.xml_file = os.path.join(self.tmp_dir, "test.xml")

0 commit comments

Comments
 (0)