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