1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
from __future__ import unicode_literals
import os
import unittest
from osrf_pycommon.process_utils import impl
this_dir = os.path.dirname(os.path.abspath(__file__))
class TestProcessUtilsImpl(unittest.TestCase):
def test_which(self):
which = impl.which
j = os.path.join
prefix = j(this_dir, 'fixtures', 'impl_which')
paths = os.pathsep.join([
j(prefix, 'usr', 'local', 'bin'),
j(prefix, 'usr', 'bin'),
j(prefix, 'bin'),
])
# bin_only exists +x in bin only
expected = j(prefix, 'bin', 'bin_only.exe')
self.assertEqual(expected, which('bin_only.exe', path=paths))
self.assertEqual(expected, which(expected, path=paths))
# exc1 exists +x in bin and usr/bin
expected = j(prefix, 'usr', 'bin', 'exc1.exe')
self.assertEqual(expected, which('exc1.exe', path=paths))
self.assertEqual(expected, which(expected, path=paths))
# exc2 exists +x in bin and usr/bin, but -x in usr/local/bin
expected = j(prefix, 'usr', 'bin', 'exc2.exe')
self.assertEqual(expected, which('exc2.exe', path=paths))
self.assertEqual(expected, which(expected, path=paths))
# Same as above, with PATH
orig_path = os.environ['PATH']
try:
os.environ['PATH'] = paths
# bin_only exists +x in bin only
expected = j(prefix, 'bin', 'bin_only.exe')
self.assertEqual(expected, which('bin_only.exe'))
self.assertEqual(expected, which(expected))
# exc1 exists +x in bin and usr/bin
expected = j(prefix, 'usr', 'bin', 'exc1.exe')
self.assertEqual(expected, which('exc1.exe'))
self.assertEqual(expected, which(expected))
# exc2 exists +x in bin and usr/bin, but -x in usr/local/bin
expected = j(prefix, 'usr', 'bin', 'exc2.exe')
self.assertEqual(expected, which('exc2.exe'))
self.assertEqual(expected, which(expected))
finally:
os.environ['PATH'] = orig_path
which(str("exc1.exe"), path=paths) # Make sure unicode/str works
|