Skip to content

Commit 2408492

Browse files
committed
Clean up tests.
1 parent bd1273d commit 2408492

2 files changed

Lines changed: 7 additions & 35 deletions

File tree

tests/test_via_gnumeric.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,13 @@
44
import os
55
import subprocess
66
import csv
7-
import shutil
87

98
import decimal
109
import datetime
1110

1211
import odswriter as ods
1312

1413

15-
class TempDir(object):
16-
"""
17-
A simple context manager for temporary directories.
18-
"""
19-
20-
def __init__(self):
21-
self.path = tempfile.mkdtemp()
22-
23-
def __enter__(self):
24-
return self
25-
26-
def __exit__(self, *args, **kwargs):
27-
shutil.rmtree(self.path)
2814

2915

3016
def command_is_executable(args):
@@ -40,10 +26,10 @@ def launder_through_gnumeric(rows):
4026
Saves rows into an ods, uses ssconvert (based on gnumeric) to convert to a CSV and loads
4127
the rows from that CSV.
4228
"""
43-
with TempDir() as d:
29+
with tempfile.TemporaryDirectory() as temp_dir:
4430
# Make an ODS
45-
temp_ods = os.path.join(d.path, "test.ods")
46-
temp_csv = os.path.join(d.path, "test.csv")
31+
temp_ods = os.path.join(temp_dir, "test.ods")
32+
temp_csv = os.path.join(temp_dir, "test.csv")
4733
with open(temp_ods, "wb") as temp_ods_file:
4834
with ods.writer(temp_ods_file) as odsfile:
4935
odsfile.writerows(rows)

tests/test_via_libreoffice.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,12 @@
44
import os
55
import subprocess
66
import csv
7-
import shutil
87

98
import decimal
109
import datetime
1110

1211
import odswriter as ods
1312

14-
class TempDir(object):
15-
"""
16-
A simple context manager for temporary directories.
17-
"""
18-
19-
def __init__(self):
20-
self.path = tempfile.mkdtemp()
21-
22-
def __enter__(self):
23-
return self
24-
25-
def __exit__(self, *args, **kwargs):
26-
shutil.rmtree(self.path)
2713

2814
def command_is_executable(args):
2915
try:
@@ -37,22 +23,22 @@ def launder_through_lo(rows):
3723
Saves rows into an ods, uses LibreOffice to convert to a CSV and loads
3824
the rows from that CSV.
3925
"""
40-
with TempDir() as d:
26+
with tempfile.TemporaryDirectory() as temp_dir:
4127
# Make an ODS
42-
temp_ods = os.path.join(d.path, "test.ods")
28+
temp_ods = os.path.join(temp_dir, "test.ods")
4329
with open(temp_ods, "wb") as temp_ods_file:
4430
with ods.writer(temp_ods_file) as odsfile:
4531
odsfile.writerows(rows)
4632

4733
# Convert it to a CSV
4834
p = subprocess.Popen(["libreoffice", "--headless", "--convert-to",
4935
"csv", "test.ods"],
50-
cwd=d.path)
36+
cwd=temp_dir)
5137

5238
p.wait()
5339

5440
# Read the CSV
55-
temp_csv = os.path.join(d.path,"test.csv")
41+
temp_csv = os.path.join(temp_dir, "test.csv")
5642
with open(temp_csv) as temp_csv_file:
5743
csvfile = csv.reader(temp_csv_file)
5844
return list(csvfile)

0 commit comments

Comments
 (0)