diff --git a/docs/pydl/changes.rst b/docs/pydl/changes.rst index 6a90b33d..915ad46f 100644 --- a/docs/pydl/changes.rst +++ b/docs/pydl/changes.rst @@ -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) ------------------ diff --git a/pydl/pydlutils/misc.py b/pydl/pydlutils/misc.py index c8681f55..77730d2a 100644 --- a/pydl/pydlutils/misc.py +++ b/pydl/pydlutils/misc.py @@ -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) diff --git a/pydl/pydlutils/tests/test_misc.py b/pydl/pydlutils/tests/test_misc.py index d2314c92..d31f5454 100644 --- a/pydl/pydlutils/tests/test_misc.py +++ b/pydl/pydlutils/tests/test_misc.py @@ -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 @@ -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): # @@ -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