Skip to content

Commit

Permalink
Merge pull request #178 from desihub/test-redirect
Browse files Browse the repository at this point in the history
increase coverage on redirect module
  • Loading branch information
weaverba137 committed Oct 27, 2021
2 parents 9fe5ceb + f8b2459 commit 6d34c57
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ __pycache__

# Other generated files
htmlcov
.coverage
.coverage*
py/desiutil/.cache
.pytest_cache

Expand Down
2 changes: 2 additions & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ formats: all
python:
version: 3
# system_packages: true
install:
- requirements: doc/rtd-requirements.txt
2 changes: 2 additions & 0 deletions doc/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ Change Log
* Optionally compute the MW dust transmission in the WISE bands (PR `#175`_).
* Do not treat messages printed on STDERR as errors during :command:`desiInstall` (PR `#176`_).
* Add :meth:`desiutil.brick.Bricks.brick_tan_wcs_size` to compute required size of TAN-projection WCS tiles (PR `#177`_).
* Improve some test coverage; RTD fixes (PR `#178`_).

.. _`#175`: https://github.com/desihub/desiutil/pull/175
.. _`#176`: https://github.com/desihub/desiutil/pull/176
.. _`#177`: https://github.com/desihub/desiutil/pull/177
.. _`#178`: https://github.com/desihub/desiutil/pull/178


3.2.2 (2021-06-03)
Expand Down
13 changes: 6 additions & 7 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@

# Configuration for intersphinx, copied from astropy.
intersphinx_mapping = {
'python': ('http://docs.python.org/3/', None),
# 'python3': ('http://docs.python.org/3/', path.abspath(path.join(path.dirname(__file__), 'local/python3links.inv'))),
'numpy': ('http://docs.scipy.org/doc/numpy/', None),
'scipy': ('http://docs.scipy.org/doc/scipy/reference/', None),
'matplotlib': ('http://matplotlib.org/', None),
'astropy': ('http://docs.astropy.org/en/stable/', None),
'h5py': ('http://docs.h5py.org/en/latest/', None)
'python': ('https://docs.python.org/3/', None),
'numpy': ('https://numpy.org/doc/stable/', None),
'scipy': ('https://docs.scipy.org/doc/scipy/reference/', None),
'matplotlib': ('https://matplotlib.org/', None),
'astropy': ('https://docs.astropy.org/en/stable/', None),
'h5py': ('https://docs.h5py.org/en/latest/', None)
}

# Add any paths that contain templates here, relative to this directory.
Expand Down
1 change: 1 addition & 0 deletions doc/rtd-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docutils<0.18
61 changes: 60 additions & 1 deletion py/desiutil/test/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""Test desiutil.install.
"""
import unittest
from unittest.mock import patch
from unittest.mock import patch, call, MagicMock
from os import chdir, environ, getcwd, mkdir, remove, rmdir
from os.path import dirname, isdir, join
from shutil import rmtree
Expand Down Expand Up @@ -183,6 +183,11 @@ def test_identify_branch(self):
options = self.desiInstall.get_options(['plate_layout', 'trunk'])
out = self.desiInstall.get_product_version()
url = self.desiInstall.identify_branch()
self.assertEqual(url,
'https://desi.lbl.gov/svn/code/focalplane/plate_layout/trunk')
options = self.desiInstall.get_options(['plate_layout', 'branches/trunk'])
out = self.desiInstall.get_product_version()
url = self.desiInstall.identify_branch()
self.assertEqual(url,
'https://desi.lbl.gov/svn/code/focalplane/plate_layout/trunk')
options = self.desiInstall.get_options(['plate_layout',
Expand Down Expand Up @@ -370,6 +375,60 @@ def test_nersc_module_dir(self):
self.assertEqual(self.desiInstall.nersc_module_dir,
'/global/cfs/cdirs/desi/test/modulefiles')

@patch('os.path.exists')
@patch('desiutil.install.Popen')
def test_get_extra(self, mock_popen, mock_exists):
"""Test fetching extra data.
"""
options = self.desiInstall.get_options(['desiutil', 'branches/main'])
self.desiInstall.baseproduct = 'desiutil'
self.desiInstall.working_dir = join(self.data_dir, 'desiutil')
mock_exists.return_value = True
mock_proc = mock_popen()
mock_proc.returncode = 0
mock_proc.communicate.return_value = ('out', 'err')
self.desiInstall.get_extra()
mock_popen.assert_has_calls([call([join(self.desiInstall.working_dir, 'etc', 'desiutil_data.sh')], stderr=-1, stdout=-1, universal_newlines=True)],
any_order=True)

@patch('desiutil.install.Popen')
def test_permissions(self, mock_popen):
"""Test the permissions stage of the install.
"""
options = self.desiInstall.get_options(['desiutil', 'branches/main'])
self.desiInstall.install_dir = join(self.data_dir, 'desiutil')
self.desiInstall.is_branch = False
mock_proc = mock_popen()
mock_proc.returncode = 0
mock_proc.communicate.return_value = ('out', 'err')
status = self.desiInstall.permissions()
self.assertEqual(status, 0)
mock_popen.assert_has_calls([call(['fix_permissions.sh', self.desiInstall.install_dir], stderr=-1, stdout=-1, universal_newlines=True),
call(['chmod', '-R', 'a-w', self.desiInstall.install_dir], stderr=-1, stdout=-1, universal_newlines=True)],
any_order=True)
mock_popen.reset_mock()
options = self.desiInstall.get_options(['--test', 'desiutil', 'branches/main'])
status = self.desiInstall.permissions()
self.assertEqual(status, 0)
mock_popen.assert_has_calls([call(['fix_permissions.sh', '-t', self.desiInstall.install_dir], stderr=-1, stdout=-1, universal_newlines=True),
call(['chmod', '-R', 'a-w', self.desiInstall.install_dir], stderr=-1, stdout=-1, universal_newlines=True)],
any_order=True)
mock_popen.reset_mock()
options = self.desiInstall.get_options(['--verbose', 'desiutil', 'branches/main'])
status = self.desiInstall.permissions()
self.assertEqual(status, 0)
mock_popen.assert_has_calls([call(['fix_permissions.sh', '-v', self.desiInstall.install_dir], stderr=-1, stdout=-1, universal_newlines=True),
call(['chmod', '-R', 'a-w', self.desiInstall.install_dir], stderr=-1, stdout=-1, universal_newlines=True)],
any_order=True)
mock_popen.reset_mock()
options = self.desiInstall.get_options(['--verbose', 'desiutil', 'branches/main'])
self.desiInstall.is_branch = True
status = self.desiInstall.permissions()
self.assertEqual(status, 0)
mock_popen.assert_has_calls([call(['fix_permissions.sh', '-v', self.desiInstall.install_dir], stderr=-1, stdout=-1, universal_newlines=True),
call(['chmod', '-R', 'g-w,o-w', self.desiInstall.install_dir], stderr=-1, stdout=-1, universal_newlines=True)],
any_order=True)

def test_cleanup(self):
"""Test the cleanup stage of the install.
"""
Expand Down
55 changes: 53 additions & 2 deletions py/desiutil/test/test_redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import os
import re
import unittest
from unittest.mock import call, patch, MagicMock
import tempfile
import shutil

import subprocess as sp
import desiutil.redirect as r


# This test requires desiutil to be installed for spawned scripts;
# skip if we are doing a test prior to a full installation.
Expand Down Expand Up @@ -72,7 +74,8 @@ def generate_output(error=False):


class TestRedirect(unittest.TestCase):
"""Test desiutil.redirect"""
"""Test desiutil.redirect
"""

@classmethod
def setUpClass(cls):
Expand All @@ -83,6 +86,9 @@ def tearDownClass(cls):
shutil.rmtree(cls.test_dir)

def setUp(self):
r._libc = None
r._c_stdout = None
r._c_stderr = None
self.test_serial_script = os.path.join(self.test_dir, "run_serial.py")
self.test_mpi_script = os.path.join(self.test_dir, "run_mpi.py")
self.n_lines = 5
Expand Down Expand Up @@ -149,6 +155,51 @@ def test_mpi(self):
print(" mpirun -np 2 python {} {} 0".format(self.test_mpi_script, outfile))
print("And then inspect the {} output file\n".format(outfile))

@patch('desiutil.redirect.ctypes')
def test__get_libc_linux(self, mock_ctypes):
"""Test standard library information in a simulated Linux environment.
"""
mock_ctypes.CDLL.return_value = 'Linux'
def side_effect(*args):
return args[1]
mock_ctypes.c_void_p.in_dll.side_effect = side_effect
lib, out, err = r._get_libc()
self.assertEqual(lib, 'Linux')
self.assertEqual(out, 'stdout')
self.assertEqual(err, 'stderr')
mock_ctypes.CDLL.assert_called_once_with(None)
mock_ctypes.c_void_p.in_dll.assert_has_calls([call(lib, "stdout"), call(lib, "stderr")])

@patch('desiutil.redirect.ctypes')
def test__get_libc_darwin(self, mock_ctypes):
"""Test standard library information in a simulated Darwin environment.
"""
mock_ctypes.CDLL.return_value = 'Darwin'
def side_effect(*args):
if args[1] == 'stdout':
raise ValueError("Darwin!")
else:
return args[1]
mock_ctypes.c_void_p.in_dll.side_effect = side_effect
lib, out, err = r._get_libc()
self.assertEqual(lib, 'Darwin')
self.assertEqual(out, '__stdoutp')
self.assertEqual(err, '__stderrp')
mock_ctypes.CDLL.assert_called_once_with(None)
mock_ctypes.c_void_p.in_dll.assert_has_calls([call(lib, "__stdoutp"), call(lib, "__stderrp")])

@patch('desiutil.redirect.ctypes')
def test__get_libc_unknown(self, mock_ctypes):
"""Test standard library information in a simulated Unknown environment.
"""
mock_ctypes.CDLL.return_value = 'Unknown'
mock_ctypes.c_void_p.in_dll.side_effect = ValueError('Unknown!')
lib, out, err = r._get_libc()
self.assertEqual(lib, 'Unknown')
self.assertIsNone(out)
self.assertIsNone(err)
mock_ctypes.CDLL.assert_called_once_with(None)


def test_suite():
"""Allows testing of only this module with the command::
Expand Down

0 comments on commit 6d34c57

Please sign in to comment.