improve test to not use hardcoded test directory

This commit is contained in:
Ilan Schnell 2012-09-13 10:57:48 -05:00
commit 5df7c4d159

View file

@ -4,8 +4,10 @@ LLVM tests
import os
import sys
import math
import shutil
import unittest
import subprocess
import tempfile
is_py3k = bool(sys.version_info[0] == 3)
@ -28,6 +30,12 @@ tests = []
# ---------------------------------------------------------------------------
class TestAsm(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.tmpdir)
def test_asm(self):
# create a module
m = Module.new('module1')
@ -36,18 +44,18 @@ class TestAsm(unittest.TestCase):
# write it's assembly representation to a file
asm = str(m)
with open("/tmp/testasm.ll", "w") as fout:
testasm_ll = os.path.join(self.tmpdir, 'testasm.ll')
with open(testasm_ll, "w") as fout:
fout.write(asm)
# read it back into a module
with open("/tmp/testasm.ll") as fin:
with open(testasm_ll) as fin:
m2 = Module.from_assembly(fin)
# The default `m.id` is '<string>'.
m2.id = m.id # Copy the name from `m`
self.assertEqual(str(m2).strip(), asm.strip())
def test_bitcode(self):
# create a module
m = Module.new('module1')
@ -56,11 +64,12 @@ class TestAsm(unittest.TestCase):
# write it's assembly representation to a file
asm = str(m)
with open("/tmp/testasm.bc", "wb") as fout:
testasm_bc = os.path.join(self.tmpdir, 'testasm.bc')
with open(testasm_bc, "wb") as fout:
m.to_bitcode(fout)
# read it back into a module
with open("/tmp/testasm.bc", "rb") as fin:
with open(testasm_bc, "rb") as fin:
m2 = Module.from_bitcode(fin)
# The default `m.id` is '<string>'.
m2.id = m.id # Copy the name from `m`