2016-02-13 07:48:48 +00:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
|
|
|
import os
|
2016-02-23 21:10:10 +00:00
|
|
|
import stat
|
2016-02-18 02:16:20 +00:00
|
|
|
import uuid
|
2016-02-13 07:48:48 +00:00
|
|
|
import unittest
|
|
|
|
from tempfile import NamedTemporaryFile as ntf
|
|
|
|
|
2016-02-17 03:55:40 +00:00
|
|
|
from tldptesttools import TestToolsFilesystem
|
2016-02-17 03:52:07 +00:00
|
|
|
|
2016-02-13 07:48:48 +00:00
|
|
|
# -- SUT
|
2016-02-17 18:17:19 +00:00
|
|
|
from tldp.utils import makefh, which, execute
|
2016-02-29 19:32:01 +00:00
|
|
|
from tldp.utils import statfile, statfiles, stem_and_ext
|
2016-02-23 21:10:10 +00:00
|
|
|
from tldp.utils import arg_isexecutable, isexecutable
|
|
|
|
from tldp.utils import arg_isreadablefile, isreadablefile
|
2016-02-29 19:32:01 +00:00
|
|
|
from tldp.utils import arg_isdirectory, arg_isloglevel
|
2016-02-23 21:10:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Test_isexecutable_and_friends(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_isexecutable(self):
|
|
|
|
f = ntf(prefix='executable-file')
|
|
|
|
self.assertFalse(isexecutable(f.name))
|
|
|
|
mode = stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR
|
|
|
|
os.chmod(f.name, mode)
|
|
|
|
self.assertTrue(isexecutable(f.name))
|
|
|
|
|
|
|
|
def test_arg_isexecutable(self):
|
|
|
|
f = ntf(prefix='executable-file')
|
|
|
|
self.assertIsNone(arg_isexecutable(f.name))
|
|
|
|
mode = stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR
|
|
|
|
os.chmod(f.name, mode)
|
|
|
|
self.assertEqual(f.name, arg_isexecutable(f.name))
|
|
|
|
|
|
|
|
|
|
|
|
class Test_isreadablefile_and_friends(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_isreadablefile(self):
|
|
|
|
f = ntf(prefix='readable-file')
|
|
|
|
self.assertTrue(isreadablefile(f.name))
|
|
|
|
os.chmod(f.name, 0)
|
|
|
|
self.assertFalse(isreadablefile(f.name))
|
|
|
|
|
|
|
|
def test_arg_isreadablefile(self):
|
|
|
|
f = ntf(prefix='readable-file')
|
|
|
|
self.assertEquals(f.name, arg_isreadablefile(f.name))
|
|
|
|
os.chmod(f.name, 0)
|
|
|
|
self.assertIsNone(arg_isreadablefile(f.name))
|
|
|
|
|
|
|
|
|
|
|
|
class Test_arg_isloglevel(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_arg_isloglevel_integer(self):
|
|
|
|
self.assertEquals(7, arg_isloglevel(7))
|
|
|
|
self.assertEquals(40, arg_isloglevel('frobnitz'))
|
|
|
|
self.assertEquals(20, arg_isloglevel('INFO'))
|
|
|
|
self.assertEquals(10, arg_isloglevel('DEBUG'))
|
2016-02-17 03:52:07 +00:00
|
|
|
|
|
|
|
|
2016-02-29 18:47:30 +00:00
|
|
|
class Test_arg_isdirectory(TestToolsFilesystem):
|
|
|
|
|
|
|
|
def test_arg_isdirectory(self):
|
|
|
|
self.assertTrue(arg_isdirectory(self.tempdir))
|
|
|
|
f = ntf(dir=self.tempdir)
|
|
|
|
self.assertFalse(arg_isdirectory(f.name))
|
|
|
|
|
|
|
|
|
2016-02-17 03:52:07 +00:00
|
|
|
class Test_execute(TestToolsFilesystem):
|
|
|
|
|
|
|
|
def test_execute_returns_zero(self):
|
2016-02-26 20:26:41 +00:00
|
|
|
exe = which('true')
|
2016-02-18 17:13:46 +00:00
|
|
|
result = execute([exe], logdir=self.tempdir)
|
2016-02-17 03:52:07 +00:00
|
|
|
self.assertEqual(0, result)
|
|
|
|
|
|
|
|
def test_execute_returns_nonzero(self):
|
2016-02-26 20:26:41 +00:00
|
|
|
exe = which('false')
|
2016-02-18 17:13:46 +00:00
|
|
|
result = execute([exe], logdir=self.tempdir)
|
2016-02-17 03:52:07 +00:00
|
|
|
self.assertEqual(1, result)
|
|
|
|
|
|
|
|
def test_execute_exception_when_logdir_none(self):
|
2016-02-26 20:26:41 +00:00
|
|
|
exe = which('true')
|
2016-02-19 01:24:00 +00:00
|
|
|
with self.assertRaises(ValueError) as ecm:
|
2016-02-18 17:13:46 +00:00
|
|
|
execute([exe], logdir=None)
|
2016-02-17 03:52:07 +00:00
|
|
|
e = ecm.exception
|
2016-02-19 01:24:00 +00:00
|
|
|
self.assertTrue('logdir must be a directory' in e.message)
|
2016-02-17 03:52:07 +00:00
|
|
|
|
|
|
|
def test_execute_exception_when_logdir_enoent(self):
|
2016-02-26 20:26:41 +00:00
|
|
|
exe = which('true')
|
2016-02-17 03:52:07 +00:00
|
|
|
logdir = os.path.join(self.tempdir, 'nonexistent-directory')
|
|
|
|
with self.assertRaises(IOError) as ecm:
|
2016-02-18 17:13:46 +00:00
|
|
|
execute([exe], logdir=logdir)
|
2016-02-17 03:52:07 +00:00
|
|
|
e = ecm.exception
|
|
|
|
self.assertTrue('nonexistent' in e.filename)
|
2016-02-13 07:48:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Test_which(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_good_which_python(self):
|
|
|
|
python = which('python')
|
|
|
|
self.assertIsInstance(python, str)
|
|
|
|
self.assertTrue(os.path.isfile(python))
|
|
|
|
qualified_python = which(python)
|
|
|
|
self.assertEqual(python, qualified_python)
|
|
|
|
|
|
|
|
def test_bad_silly_name(self):
|
|
|
|
silly = which('silliest-executable-name-which-may-yet-be-possible')
|
|
|
|
self.assertIsNone(silly)
|
|
|
|
|
|
|
|
def test_fq_executable(self):
|
|
|
|
f = ntf(prefix='tldp-which-test', delete=False)
|
|
|
|
f.close()
|
|
|
|
notfound = which(f.name)
|
|
|
|
self.assertIsNone(notfound)
|
|
|
|
os.chmod(f.name, 0755)
|
|
|
|
found = which(f.name)
|
|
|
|
self.assertEqual(f.name, found)
|
|
|
|
os.unlink(f.name)
|
|
|
|
|
2016-02-13 18:54:38 +00:00
|
|
|
|
2016-02-17 19:18:47 +00:00
|
|
|
class Test_statfiles(unittest.TestCase):
|
2016-02-17 03:52:07 +00:00
|
|
|
|
2016-02-19 05:55:56 +00:00
|
|
|
def test_statfiles_dir_in_result(self):
|
2016-02-26 20:26:41 +00:00
|
|
|
'''Assumes that directory ./sample-documents/ exists here'''
|
2016-02-19 05:55:56 +00:00
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
statinfo = statfiles(here, relative=here)
|
|
|
|
self.assertIsInstance(statinfo, dict)
|
2016-02-26 20:26:41 +00:00
|
|
|
self.assertTrue(os.path.basename('sample-documents') in statinfo)
|
2016-02-19 05:55:56 +00:00
|
|
|
|
2016-02-17 19:18:47 +00:00
|
|
|
def test_statfiles_dir_rel(self):
|
2016-02-17 03:52:07 +00:00
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
2016-02-17 19:18:47 +00:00
|
|
|
statinfo = statfiles(here, relative=here)
|
|
|
|
self.assertIsInstance(statinfo, dict)
|
|
|
|
self.assertTrue(os.path.basename(__file__) in statinfo)
|
2016-02-17 03:52:07 +00:00
|
|
|
|
2016-02-17 19:18:47 +00:00
|
|
|
def test_statfiles_dir_abs(self):
|
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
statinfo = statfiles(here)
|
|
|
|
self.assertIsInstance(statinfo, dict)
|
|
|
|
self.assertTrue(__file__ in statinfo)
|
2016-02-17 03:52:07 +00:00
|
|
|
|
2016-02-17 19:18:47 +00:00
|
|
|
def test_statfiles_file_rel(self):
|
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
statinfo = statfiles(__file__, relative=here)
|
|
|
|
self.assertIsInstance(statinfo, dict)
|
|
|
|
self.assertTrue(os.path.basename(__file__) in statinfo)
|
2016-02-17 18:17:19 +00:00
|
|
|
|
2016-02-17 19:18:47 +00:00
|
|
|
def test_statfiles_file_abs(self):
|
|
|
|
statinfo = statfiles(__file__)
|
2016-02-17 18:17:19 +00:00
|
|
|
self.assertIsInstance(statinfo, dict)
|
2016-02-17 19:18:47 +00:00
|
|
|
self.assertTrue(__file__ in statinfo)
|
2016-02-17 18:17:19 +00:00
|
|
|
|
2016-02-18 02:16:20 +00:00
|
|
|
def test_statfiles_nonexistent_file(self):
|
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
this = os.path.join(here, str(uuid.uuid4()))
|
|
|
|
statinfo = statfiles(this)
|
|
|
|
self.assertIsInstance(statinfo, dict)
|
|
|
|
self.assertEquals(0, len(statinfo))
|
|
|
|
|
2016-02-17 18:17:19 +00:00
|
|
|
|
2016-02-29 19:32:01 +00:00
|
|
|
class Test_statfile(TestToolsFilesystem):
|
|
|
|
|
|
|
|
def test_statfile_bogustype(self):
|
2016-03-01 02:05:14 +00:00
|
|
|
with self.assertRaises(TypeError):
|
2016-02-29 19:32:01 +00:00
|
|
|
statfile(0)
|
|
|
|
|
|
|
|
def test_statfile_enoent(self):
|
|
|
|
f = ntf(dir=self.tempdir)
|
2016-03-01 02:05:14 +00:00
|
|
|
self.assertIsNone(statfile(f.name + '-ENOENT_TEST'))
|
2016-02-29 19:32:01 +00:00
|
|
|
|
|
|
|
|
2016-02-19 08:54:16 +00:00
|
|
|
class Test_stem_and_ext(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_stem_and_ext_final_slash(self):
|
|
|
|
r0 = stem_and_ext('/h/q/t/z/Frobnitz-HOWTO')
|
|
|
|
r1 = stem_and_ext('/h/q/t/z/Frobnitz-HOWTO/')
|
|
|
|
self.assertEqual(r0, r1)
|
|
|
|
|
|
|
|
def test_stem_and_ext_rel_abs(self):
|
|
|
|
r0 = stem_and_ext('/h/q/t/z/Frobnitz-HOWTO')
|
|
|
|
r1 = stem_and_ext('Frobnitz-HOWTO/')
|
|
|
|
self.assertEqual(r0, r1)
|
|
|
|
|
|
|
|
|
2016-02-17 18:17:19 +00:00
|
|
|
class Test_att_statinfo(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_max_mtime(self):
|
|
|
|
pass
|
|
|
|
|
2016-02-18 03:36:52 +00:00
|
|
|
|
2016-02-13 07:48:48 +00:00
|
|
|
class Test_makefh(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_makefh(self):
|
|
|
|
f = ntf(prefix='tldp-makefh-openfile-test-', delete=False)
|
|
|
|
# fprime = makefh(f.file)
|
|
|
|
# self.assertIs(f, fprime)
|
|
|
|
# del fprime
|
|
|
|
f.close()
|
|
|
|
fprime = makefh(f.name)
|
|
|
|
self.assertIs(f.name, fprime.name)
|
|
|
|
os.unlink(f.name)
|
|
|
|
|
|
|
|
#
|
|
|
|
# -- end of file
|