mirror of https://github.com/tLDP/python-tldp
beefing up some testing for new functions
This commit is contained in:
parent
2fef28a299
commit
8c69e05f94
|
@ -2,6 +2,7 @@
|
|||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
import os
|
||||
import stat
|
||||
import uuid
|
||||
import unittest
|
||||
from tempfile import NamedTemporaryFile as ntf
|
||||
|
@ -11,6 +12,51 @@ from tldptesttools import TestToolsFilesystem
|
|||
# -- SUT
|
||||
from tldp.utils import makefh, which, execute
|
||||
from tldp.utils import statfiles, stem_and_ext
|
||||
from tldp.utils import arg_isexecutable, isexecutable
|
||||
from tldp.utils import arg_isreadablefile, isreadablefile
|
||||
from tldp.utils import arg_isdirectory
|
||||
from tldp.utils import arg_isloglevel
|
||||
|
||||
|
||||
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'))
|
||||
|
||||
|
||||
class Test_execute(TestToolsFilesystem):
|
||||
|
|
|
@ -28,7 +28,7 @@ logger = getLogger()
|
|||
def firstfoundfile(locations):
|
||||
'''return the first existing file from a list of filenames (or None)'''
|
||||
for option in locations:
|
||||
if os.path.isfile(option):
|
||||
if isreadablefile(option):
|
||||
return option
|
||||
return None
|
||||
|
||||
|
@ -40,19 +40,19 @@ def arg_isloglevel(l):
|
|||
level = getattr(logging, l.upper(), None)
|
||||
try:
|
||||
logging.getLogger().setLevel(level)
|
||||
except ValueError:
|
||||
except (TypeError, ValueError):
|
||||
level = logging.ERROR
|
||||
return level
|
||||
|
||||
|
||||
def arg_isfile(f):
|
||||
if os.path.exists(f):
|
||||
def arg_isreadablefile(f):
|
||||
if isreadablefile(f):
|
||||
return f
|
||||
return None
|
||||
|
||||
|
||||
def arg_isdirectory(d):
|
||||
if os.path.exists(d):
|
||||
if os.path.isdir(d):
|
||||
return d
|
||||
return None
|
||||
|
||||
|
@ -152,9 +152,14 @@ def execute(cmd, stdin=None, stdout=None, stderr=None,
|
|||
return result
|
||||
|
||||
|
||||
def isexecutable(fpath):
|
||||
def isexecutable(f):
|
||||
'''True if argument is executable'''
|
||||
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
|
||||
return os.path.isfile(f) and os.access(f, os.X_OK)
|
||||
|
||||
|
||||
def isreadablefile(f):
|
||||
'''True if argument is readable file'''
|
||||
return os.path.isfile(f) and os.access(f, os.R_OK)
|
||||
|
||||
|
||||
def which(program):
|
||||
|
|
Loading…
Reference in New Issue