Skip to content

Commit

Permalink
minor QC fixes related to NamedStream tests and use tempdir
Browse files Browse the repository at this point in the history
  • Loading branch information
orbeckst committed Jan 22, 2016
1 parent 92464cd commit b578954
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions testsuite/MDAnalysisTests/test_streamio.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@

import StringIO
import cStringIO
import tempfile
import os

import tempdir


class TestIsstream(TestCase):
def test_hasmethod(self):
Expand Down Expand Up @@ -138,26 +139,24 @@ def test_cStringIO_write(self):
ns.close(force=True)

def test_File_write(self):
fd, outfile = tempfile.mkstemp(suffix=".txt")
os.close(fd)
try:
obj = open(outfile, "w")
ns = util.NamedStream(obj, outfile, close=True)
ns.writelines(self.text)
ns.close()
text = open(outfile).readlines()

assert_equal(ns.name, outfile)
assert_equal(str(ns), outfile)
assert_equal(len(text), len(self.text))
assert_equal("".join(text), "".join(self.text))
finally:
ns.close()
obj.close()
with tempdir.in_tempdir():
outfile = "lookingglas.txt"
try:
os.unlink(outfile)
except OSError:
pass
obj = open(outfile, "w")
ns = util.NamedStream(obj, outfile, close=True)
ns.writelines(self.text)
ns.close()
text = open(outfile).readlines()

assert_equal(ns.name, outfile)
assert_equal(str(ns), outfile)
assert_equal(len(text), len(self.text))
assert_equal("".join(text), "".join(self.text))
finally:
ns.close()
obj.close()


class TestNamedStream_filename_behavior(object):
textname = "~/stories/jabberwock.txt" # with tilde ~ to test regular expanduser()
# note: no setUp() because classes with generators would run it
Expand Down Expand Up @@ -187,12 +186,14 @@ def _test_func(funcname, fn=self.textname, ns=ns):
err_msg=("os.path.{0}() does not work with "
"NamedStream").format(funcname))
# join not included because of different call signature
# but added first argument for the sake of it showing up in the verbose
# nose output
def _test_join(funcname="join", fn=self.textname, ns=ns, path="/tmp/MDAnalysisTests"):
reference = os.path.join(path, fn)
value = os.path.join(path, ns)
assert_equal(value, reference,
err_msg=("os.path.join() does not work with "
"NamedStream"))
err_msg=("os.path.{0}() does not work with "
"NamedStream").format(funcname))
for func in funcs:
yield _test_func, func
yield _test_join, "join"
Expand Down

0 comments on commit b578954

Please sign in to comment.