Skip to content
Snippets Groups Projects
Commit 277828f5d232 authored by Bob Ippolito's avatar Bob Ippolito
Browse files

Merge pull request #214 from simplejson/tempfile-py25

Fix tests in Python 2.5.
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,15 @@
"".encode(),
stderr).strip()
def open_temp_file():
if sys.version_info >= (2, 6):
file = tempfile.NamedTemporaryFile(delete=False)
filename = file.name
else:
fd, filename = tempfile.mkstemp()
file = os.fdopen(fd, 'w+b')
return file, filename
class TestTool(unittest.TestCase):
data = """
......@@ -71,8 +80,8 @@
self.expect.splitlines())
def test_infile_stdout(self):
infile = tempfile.NamedTemporaryFile(delete=False)
infile, infile_name = open_temp_file()
try:
infile.write(self.data.encode())
infile.close()
self.assertEqual(
......@@ -75,7 +84,7 @@
try:
infile.write(self.data.encode())
infile.close()
self.assertEqual(
self.runTool(args=[infile.name]),
self.runTool(args=[infile_name]),
self.expect.splitlines())
finally:
......@@ -80,5 +89,5 @@
self.expect.splitlines())
finally:
os.unlink(infile.name)
os.unlink(infile_name)
def test_infile_outfile(self):
......@@ -83,8 +92,8 @@
def test_infile_outfile(self):
infile = tempfile.NamedTemporaryFile(delete=False)
infile, infile_name = open_temp_file()
try:
infile.write(self.data.encode())
infile.close()
# outfile will get overwritten by tool, so the delete
# may not work on some platforms. Do it manually.
......@@ -86,9 +95,9 @@
try:
infile.write(self.data.encode())
infile.close()
# outfile will get overwritten by tool, so the delete
# may not work on some platforms. Do it manually.
outfile = tempfile.NamedTemporaryFile(delete=False)
outfile, outfile_name = open_temp_file()
try:
outfile.close()
self.assertEqual(
......@@ -92,5 +101,5 @@
try:
outfile.close()
self.assertEqual(
self.runTool(args=[infile.name, outfile.name]),
self.runTool(args=[infile_name, outfile_name]),
[])
......@@ -96,7 +105,7 @@
[])
with open(outfile.name, 'rb') as f:
with open(outfile_name, 'rb') as f:
self.assertEqual(
f.read().decode('utf8').splitlines(),
self.expect.splitlines()
)
finally:
......@@ -98,7 +107,7 @@
self.assertEqual(
f.read().decode('utf8').splitlines(),
self.expect.splitlines()
)
finally:
os.unlink(outfile.name)
os.unlink(outfile_name)
finally:
......@@ -104,2 +113,2 @@
finally:
os.unlink(infile.name)
os.unlink(infile_name)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment