Skip to content

Commit

Permalink
add tests of lstat, islink
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverba137 committed May 5, 2022
1 parent e843493 commit d96a3bc
Showing 1 changed file with 67 additions and 60 deletions.
127 changes: 67 additions & 60 deletions hpsspy/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
class SaveArgs(object):
"""Save a function call's arguments for later inspection.
"""
def __init__(self, return_value):
self.return_value = return_value
def __init__(self, return_values):
self.counter = 0
self.return_values = return_values
self.args = list()

def __call__(self, *args):
self.args = list(args)
return self.return_value
self.args.append(tuple(args))
r = self.return_values[self.counter]
self.counter += 1
return r


@pytest.fixture
Expand All @@ -37,43 +41,43 @@ def mock_hsi():
def test_chmod(monkeypatch, mock_hsi):
"""Test the chmod() function.
"""
m = mock_hsi('All good!')
m = mock_hsi(['All good!'])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
chmod('/home/b/bweaver/foo.txt', 0o664)
assert m.args == ['chmod', '436', '/home/b/bweaver/foo.txt']
assert m.args[0] == ('chmod', '436', '/home/b/bweaver/foo.txt')


def test_chmod_error(monkeypatch, mock_hsi):
"""Test the chmod() throwing an error.
"""
m = mock_hsi('** Error!')
m = mock_hsi(['** Error!'])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
with pytest.raises(HpssOSError) as err:
chmod('/home/b/bweaver/foo.txt', 0o664)
assert err.value.args[0] == "** Error!"
assert m.args == ['chmod', '436', '/home/b/bweaver/foo.txt']
assert m.args[0] == ('chmod', '436', '/home/b/bweaver/foo.txt')


def test_listdir_error(monkeypatch, mock_hsi):
"""Test the listdir() function throwing an error.
"""
m = mock_hsi('** Error!')
m = mock_hsi(['** Error!'])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
with pytest.raises(HpssOSError) as err:
files = listdir('/home/b/bweaver')
assert err.value.args[0] == '** Error!'
assert m.args == ['ls', '-la', '/home/b/bweaver']
assert m.args[0] == ('ls', '-la', '/home/b/bweaver')


def test_listdir_bad_line(monkeypatch, mock_hsi):
"""Test the listdir() function with bad data.
"""
m = mock_hsi('/home/b/bweaver:\nGarbage line')
m = mock_hsi(['/home/b/bweaver:\nGarbage line'])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
with pytest.raises(HpssOSError) as err:
files = listdir('/home/b/bweaver')
assert err.value.args[0] == "Could not match line!\nGarbage line"
assert m.args == ['ls', '-la', '/home/b/bweaver']
assert m.args[0] == ('ls', '-la', '/home/b/bweaver')


def test_listdir(monkeypatch, mock_hsi):
Expand All @@ -83,69 +87,69 @@ def test_listdir(monkeypatch, mock_hsi):
-rw-rw---- 1 bweaver desi 29956061184 May 15 2014 cosmos_nvo.tar
-rw-rw---- 1 bweaver desi 61184 May 15 2014 cosmos_nvo.tar.idx
'''
m = mock_hsi(foo)
m = mock_hsi([foo])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
files = listdir('/home/b/bweaver')
assert files[0].ishtar
assert m.args == ['ls', '-la', '/home/b/bweaver']
assert m.args[0] == ('ls', '-la', '/home/b/bweaver')


def test_makedirs_error(monkeypatch, mock_hsi):
"""Test the makedirs() function throwing an error.
"""
m = mock_hsi('** Error!')
m = mock_hsi(['** Error!'])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
with pytest.raises(HpssOSError) as err:
makedirs('/home/b/bweaver', '2775')
assert err.value.args[0] == '** Error!'
assert m.args == ['mkdir', '-p', '-m', '2775', '/home/b/bweaver']
assert m.args[0] == ('mkdir', '-p', '-m', '2775', '/home/b/bweaver')


def test_makedirs_with_mode(monkeypatch, mock_hsi):
"""Test the makedirs() function setting the mode.
"""
m = mock_hsi('All good!')
m = mock_hsi(['All good!'])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
makedirs('/home/b/bweaver', '2775')
assert m.args == ['mkdir', '-p', '-m', '2775', '/home/b/bweaver']
assert m.args[0] == ('mkdir', '-p', '-m', '2775', '/home/b/bweaver')


def test_makedirs(monkeypatch, mock_hsi):
"""Test the makedirs() function.
"""
m = mock_hsi('All good!')
m = mock_hsi(['All good!'])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
makedirs('/home/b/bweaver')
assert m.args == ['mkdir', '-p', '/home/b/bweaver']
assert m.args[0] == ('mkdir', '-p', '/home/b/bweaver')


def test_mkdir_error(monkeypatch, mock_hsi):
"""Test the mkdir() function throwing an error.
"""
m = mock_hsi('** Error!')
m = mock_hsi(['** Error!'])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
with pytest.raises(HpssOSError) as err:
mkdir('/home/b/bweaver', '2775')
assert err.value.args[0] == '** Error!'
assert m.args == ['mkdir', '-m', '2775', '/home/b/bweaver']
assert m.args[0] == ('mkdir', '-m', '2775', '/home/b/bweaver')


def test_mkdir_with_mode(monkeypatch, mock_hsi):
"""Test the mkdir() function setting the mode.
"""
m = mock_hsi('All good!')
m = mock_hsi(['All good!'])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
mkdir('/home/b/bweaver', '2775')
assert m.args == ['mkdir', '-m', '2775', '/home/b/bweaver']
assert m.args[0] == ('mkdir', '-m', '2775', '/home/b/bweaver')


def test_mkdir(monkeypatch, mock_hsi):
"""Test the makedirs() function.
"""
m = mock_hsi('All good!')
m = mock_hsi(['All good!'])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
mkdir('/home/b/bweaver')
assert m.args == ['mkdir', '/home/b/bweaver']
assert m.args[0] == ('mkdir', '/home/b/bweaver')


# def test_stat(self):
Expand Down Expand Up @@ -204,32 +208,37 @@ def test_mkdir(monkeypatch, mock_hsi):
# self.assertTrue(s.isdir)
# h.assert_has_calls([call('ls', '-ld', 'cosmo'),
# call('ls', '-ld', 'cosmo.old')])
#
# def test_lstat(self):
# """Test the lstat() function.
# """
# with patch('hpsspy.os._os.hsi') as h:
# h.side_effect = [('lrwxrwxrwx 1 bweaver bweaver ' +
# '21 Aug 22 2014 cosmo@ -> ' +
# '/nersc/projects/cosmo\n'),
# ('drwxrws--- 6 nugent cosmo ' +
# '512 Dec 16 2016 cosmo')]
# s = lstat("cosmo")
# self.assertTrue(s.islink)
# with patch('hpsspy.os._os.hsi') as h:
# h.return_value = ('drwxr-sr-x 3 bweaver bweaver ' +
# '512 Oct 4 2010 test')
# s = lstat("test")
# self.assertFalse(s.islink)


def test_lstat_is_link(monkeypatch, mock_hsi):
"""Test the lstat() function.
"""
m = mock_hsi(['lrwxrwxrwx 1 bweaver bweaver 21 Aug 22 2014 cosmo@ -> /nersc/projects/cosmo\n',
'drwxrws--- 6 nugent cosmo 512 Dec 16 2016 cosmo'])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
s = lstat('cosmo')
assert s.islink
assert m.args[0] == ('ls', '-ld', 'cosmo')
# assert m.args[1] == ('ls', '-ld', '/nersc/projects/cosmo')


def test_lstat_not_link(monkeypatch, mock_hsi):
"""Test the lstat() function for non-links.
"""
m = mock_hsi(['drwxr-sr-x 3 bweaver bweaver 512 Oct 4 2010 test',])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
s = lstat('test')
assert not s.islink
assert m.args[0] == ('ls', '-ld', 'test')


def test_isdir(monkeypatch, mock_hsi):
"""Test the isdir() function.
"""
m = mock_hsi('drwxr-sr-x 3 bweaver bweaver 512 Oct 4 2010 test')
m = mock_hsi(['drwxr-sr-x 3 bweaver bweaver 512 Oct 4 2010 test'])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
assert isdir('test')
assert m.args == ['ls', '-ld', 'test']
assert m.args[0] == ('ls', '-ld', 'test')


def test_isfile(monkeypatch, mock_hsi):
Expand All @@ -238,25 +247,23 @@ def test_isfile(monkeypatch, mock_hsi):
foo = '''desi:
-rw-rw---- 1 bweaver desi 29956061184 May 15 2014 cosmos_nvo.tar
'''
m = mock_hsi(foo)
m = mock_hsi([foo])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
assert isfile('desi/cosmos_nvo.tar')
assert m.args == ['ls', '-ld', 'desi/cosmos_nvo.tar']
assert m.args[0] == ('ls', '-ld', 'desi/cosmos_nvo.tar')


def test_islink(monkeypatch, mock_hsi):
"""Test the islink() function.
"""
m = mock_hsi(['lrwxrwxrwx 1 bweaver bweaver 21 Aug 22 2014 cosmo@ -> /nersc/projects/cosmo',
'/nersc/projects:\ndrwxrwxr-x 1 bweaver bweaver 21 Aug 22 2014 cosmo'])
monkeypatch.setattr('hpsspy.os._os.hsi', m)
assert islink('cosmo')
assert m.args[0] == ('ls', '-ld', 'cosmo')
# assert m.args[1] == ('ls', '-ld', '/nersc/projects/cosmo')


# def test_islink(self):
# """Test the islink() function.
# """
# with patch('hpsspy.os._os.hsi') as h:
# h.side_effect = [('lrwxrwxrwx 1 bweaver bweaver ' +
# '21 Aug 22 2014 cosmo@ -> ' +
# '/nersc/projects/cosmo\n'),
# ('/nersc/projects:\n' +
# 'drwxrwxr-x 1 bweaver bweaver ' +
# '21 Aug 22 2014 cosmo')]
# self.assertTrue(islink('cosmo'))
# h.assert_has_calls([call('ls', '-ld', 'cosmo')])
#
# def test_walk(self):
# """Test the walk() function.
# """
Expand Down

0 comments on commit d96a3bc

Please sign in to comment.