Skip to content

Commit

Permalink
use temporary files when testing struct_print
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverba137 committed Nov 30, 2016
1 parent 6113146 commit 50b9a24
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
5 changes: 4 additions & 1 deletion docs/pydl/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ PyDL Changelog
------------------

* Fixed formatting of TODO document.
* Fixed tests that were failing on 32-bit platforms (`Issue #14`).
* Fixed tests that were failing on 32-bit platforms (`Issue #14`_).
* Use temporary files so that tests can run when astropy is installed
read-only (*e.g.*, with :command:`pip`; `Issue #16`_)

.. _`Issue #14`: https://github.com/weaverba137/pydl/issues/14
.. _`Issue #16`: https://github.com/weaverba137/pydl/issues/16

0.5.2 (2016-08-04)
------------------
Expand Down
4 changes: 2 additions & 2 deletions pydl/pydlutils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,13 @@ def struct_print(array, filename=None, formatcodes=None, alias=None,
if hasattr(filename, 'write'):
f = filename
else:
f = open(filename, 'w')
f = open(filename, 'w+b')
close_file = True
if f is None:
if not silent: # pragma: no cover
print("\n".join(lines)+"\n")
else:
f.write("\n".join(lines)+"\n")
f.write(("\n".join(lines)+"\n").encode('utf-8'))
if close_file:
f.close()
return (lines, css)
31 changes: 14 additions & 17 deletions pydl/pydlutils/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
import os
import numpy as np
import tempfile
from astropy.tests.helper import raises
from .. import PydlutilsException
from ..misc import djs_laxisgen, djs_laxisnum, hogg_iau_name, struct_print
Expand All @@ -12,17 +13,10 @@ class TestMisc(object):
"""

def setup(self):
self.data_dir = os.path.join(os.path.dirname(__file__), 't')
self.struct_print_file = os.path.join(self.data_dir,
'struct_print.txt')
self.struct_print_file2 = os.path.join(self.data_dir,
'struct_print2.txt')
pass

def teardown(self):
if os.path.exists(self.struct_print_file):
os.remove(self.struct_print_file)
if os.path.exists(self.struct_print_file2):
os.remove(self.struct_print_file2)
pass

def test_djs_laxisgen(self):
#
Expand Down Expand Up @@ -184,13 +178,16 @@ def test_struct_print(self):
assert lines[3] == '2 3.46 seven'
assert lines[4] == '3 -4.57 nine '
assert len(css) == 0
lines, css = struct_print(slist, silent=True,
filename=self.struct_print_file)
with open(self.struct_print_file) as f:
data = f.read()
with tempfile.NamedTemporaryFile(delete=False) as spf1:
spf1_name = spf1.name
lines, css = struct_print(slist, silent=True,
filename=spf1_name)
with open(spf1_name, 'rb') as f:
data = f.read().decode('utf-8')
assert "\n".join(lines)+"\n" == data
with open(self.struct_print_file2, 'w') as f:
lines, css = struct_print(slist, silent=True, filename=f)
with open(self.struct_print_file2) as f:
data = f.read()
os.remove(spf1_name)
with tempfile.TemporaryFile() as spf2:
lines, css = struct_print(slist, silent=True, filename=spf2)
spf2.seek(0)
data = spf2.read().decode('utf-8')
assert "\n".join(lines)+"\n" == data

0 comments on commit 50b9a24

Please sign in to comment.