Skip to content

Commit

Permalink
make mock commands easier to use
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverba137 committed Aug 10, 2017
1 parent 1c0b636 commit 509383f
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 41 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ nosetests.xml

# Mac OSX
.DS_Store

# don't ignore test/bin
!test/bin
File renamed without changes.
File renamed without changes.
34 changes: 12 additions & 22 deletions hpsspy/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,28 @@
# import json
# from pkg_resources import resource_filename
import os
from ..os import chmod
from ..os import chmod, lstat, stat
from ..os.path import isdir, isfile, islink
from .test_util import MockHpss


class TestOs(unittest.TestCase):
class TestOs(MockHpss):
"""Test the functions in the os subpackage.
"""

@classmethod
def setUpClass(cls):
def test_isdir(self):
"""Test the isdir() function.
"""
pass

@classmethod
def tearDownClass(cls):
def test_isfile(self):
"""Test the isfile() function.
"""
pass

def setUp(self):
# Store the original value of env variables, if present.
# self.env = {'TMPDIR': None, 'HPSS_DIR': None}
# for e in self.env:
# if e in os.environ:
# self.env[e] = os.environ['TMPDIR']
pass

def tearDown(self):
# Restore the original value of env variables, if they were present.
# for e in self.env:
# if self.env[e] is None:
# if e in os.environ:
# del os.environ[e]
# else:
# os.environ[e] = self.env[e]
def test_islink(self):
"""Test the islink() function.
"""
pass


Expand Down
2 changes: 1 addition & 1 deletion hpsspy/test/test_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def test_validate_configuration(self):
self.assertLog(-2, "foo.bar might not be a JSON file!")
self.assertLog(-1, "foo.bar does not exist. Try again.")
# invalid file
invalid = resource_filename('hpsspy.test', 't/hsi')
invalid = resource_filename('hpsspy.test', 'bin/hsi')
status = validate_configuration(invalid)
self.assertEqual(status, 1)
self.assertLog(-2, "{0} might not be a JSON file!".format(invalid))
Expand Down
25 changes: 8 additions & 17 deletions hpsspy/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
from ..util import HpssFile, get_hpss_dir, get_tmpdir, hsi, htar


class TestUtil(unittest.TestCase):
"""Test the functions in the util subpackage.
class MockHpss(unittest.TestCase):
"""Provide access to mock HPSS commands.
"""

@classmethod
Expand All @@ -38,7 +38,9 @@ def setUp(self):
self.env = {'TMPDIR': None, 'HPSS_DIR': None}
for e in self.env:
if e in os.environ:
self.env[e] = os.environ['TMPDIR']
self.env[e] = os.environ[e]
hsi = resource_filename('hpsspy.test', 'bin/hsi')
os.environ['HPSS_DIR'] = os.path.dirname(os.path.dirname(hsi))

def tearDown(self):
# Restore the original value of env variables, if they were present.
Expand All @@ -49,17 +51,10 @@ def tearDown(self):
else:
os.environ[e] = self.env[e]

def setup_bin(self, command):
real_command = resource_filename('hpsspy.test', 't/'+command)
hpss_dir = os.path.dirname(real_command)
os.environ['HPSS_DIR'] = hpss_dir
os.mkdir(os.path.join(hpss_dir, 'bin'))
os.symlink(real_command, os.path.join(hpss_dir, 'bin', command))

def remove_bin(self, command):
hpss_dir = os.environ['HPSS_DIR']
os.remove(os.path.join(hpss_dir, 'bin', command))
os.rmdir(os.path.join(hpss_dir, 'bin'))
class TestUtil(MockHpss):
"""Test the functions in the util subpackage.
"""

def test_HpssFile(self):
"""Test the HpssFile object.
Expand Down Expand Up @@ -155,19 +150,16 @@ def test_get_tmpdir(self):
def test_hsi(self):
"""Test passing arguments to the hsi command.
"""
self.setup_bin('hsi')
os.environ['TMPDIR'] = os.environ['HPSS_DIR']
pre_command = ['-O', os.path.join(os.environ['TMPDIR'], 'hsi.txt'),
'-s', 'archive']
command = ['ls', '-l', 'foo']
out = hsi(*command)
self.assertEqual(out.strip(), ' '.join(command))
self.remove_bin('hsi')

def test_htar(self):
"""Test passing arguments to the htar command.
"""
self.setup_bin('htar')
command = ['-cvf', 'foo/bar.tar', '-H', 'crc:verify=all', 'bar']
out, err = htar(*command)
if self.PY3:
Expand All @@ -176,7 +168,6 @@ def test_htar(self):
else:
self.assertEqual(out.strip(), ' '.join(command))
self.assertEqual(err.strip(), '')
self.remove_bin('htar')


def test_suite():
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
setup_keywords['use_2to3'] = True
setup_keywords['packages'] = find_packages()
setup_keywords['cmdclass'] = {'sdist': DistutilsSdist}
setup_keywords['package_data'] = {'hpsspy': ['data/*.json'], 'hpsspy.test': ['t/*']}
setup_keywords['package_data'] = {'hpsspy': ['data/*.json',],
'hpsspy.test': ['t/*', 'bin/*']}
#
# Autogenerate command-line scripts.
#
Expand Down

0 comments on commit 509383f

Please sign in to comment.