Skip to content

Commit 4022994

Browse files
authored
Removed hardcoded PATHs in test (#27)
Some distros like NixOS does not have binaries on `/bin`. Instead of hardcoding them, try to get them from PATH using shutil.which function.
1 parent 18b5c14 commit 4022994

3 files changed

Lines changed: 25 additions & 14 deletions

File tree

lddwrap/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ def list_dependencies(path: pathlib.Path,
173173
"""
174174
Retrieve a list of dependencies of the given binary.
175175
176-
>>> path = pathlib.Path("/bin/ls")
176+
>>> import shutil
177+
>>> ls = shutil.which("ls")
178+
>>> path = pathlib.Path(ls)
177179
>>> deps = list_dependencies(path=path)
178180
>>> deps[0].soname
179181
'linux-vdso.so.1'

tests/test_ldd.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""Test lddwrap."""
33
# pylint: disable=missing-docstring,too-many-public-methods
44
import pathlib
5+
import shutil
56
import tempfile
67
import unittest
78
from typing import Any, List, Optional
@@ -10,6 +11,12 @@
1011

1112
import tests
1213

14+
# Some distros like NixOS does not have binaries on /bin.
15+
# Instead of hardcoding them, try to get them from PATH
16+
# using shutil.which function.
17+
DIR = shutil.which("dir") or "/bin/dir"
18+
PWD = shutil.which("pwd") or "/bin/pwd"
19+
1320

1421
class DependencyDiff:
1522
"""Represent a different between two dependencies."""
@@ -200,7 +207,7 @@ def test_pwd(self):
200207
]),
201208
out_unused=''):
202209
deps = lddwrap.list_dependencies(
203-
path=pathlib.Path('/bin/pwd'), unused=False)
210+
path=pathlib.Path(PWD), unused=False)
204211

205212
expected_deps = [
206213
lddwrap.Dependency(
@@ -249,7 +256,7 @@ def test_bin_dir(self):
249256
out_unused=''):
250257
# pylint: enable=line-too-long
251258
deps = lddwrap.list_dependencies(
252-
path=pathlib.Path('/bin/dir'), unused=False)
259+
path=pathlib.Path(DIR), unused=False)
253260

254261
expected_deps = [
255262
lddwrap.Dependency(
@@ -320,7 +327,7 @@ def test_bin_dir_with_empty_unused(self):
320327
out_unused=''):
321328
# pylint: enable=line-too-long
322329
deps = lddwrap.list_dependencies(
323-
path=pathlib.Path("/bin/dir"), unused=True)
330+
path=pathlib.Path(DIR), unused=True)
324331

325332
unused = [dep for dep in deps if dep.unused]
326333
self.assertListEqual([], unused)
@@ -342,7 +349,7 @@ def test_with_fantasy_unused(self):
342349
):
343350
# pylint: enable=line-too-long
344351
deps = lddwrap.list_dependencies(
345-
path=pathlib.Path("/bin/dir"), unused=True)
352+
path=pathlib.Path(DIR), unused=True)
346353

347354
unused = [dep for dep in deps if dep.unused]
348355

@@ -402,7 +409,7 @@ def test_sorting_by_all_attributes(self) -> None:
402409

403410
for attr in lddwrap.DEPENDENCY_ATTRIBUTES:
404411
deps = lddwrap.list_dependencies(
405-
path=pathlib.Path("/bin/dir"), unused=True)
412+
path=pathlib.Path(DIR), unused=True)
406413

407414
# pylint: disable=protected-access
408415
lddwrap._sort_dependencies_in_place(deps=deps, sort_by=attr)

tests/test_main.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io
55
import json
66
import pathlib
7+
import shutil
78
import textwrap
89
import unittest
910
from typing import List, TextIO, cast
@@ -15,16 +16,18 @@
1516
# pylint: disable=missing-docstring
1617
import tests
1718

19+
LS = shutil.which("ls") or "/bin/ls"
20+
PWD = shutil.which("pwd") or "/bin/pwd"
21+
1822

1923
class TestParseArgs(unittest.TestCase):
2024
def test_single_path(self):
21-
args = lddwrap.main.parse_args(
22-
sys_argv=['some-executable.py', '/bin/ls'])
23-
self.assertEqual(pathlib.Path('/bin/ls'), args.path)
25+
args = lddwrap.main.parse_args(sys_argv=['some-executable.py', LS])
26+
self.assertEqual(pathlib.Path(LS), args.path)
2427

2528
def test_format(self):
2629
args = lddwrap.main.parse_args(
27-
sys_argv=['some-executable.py', '/bin/ls', "--format", "json"])
30+
sys_argv=['some-executable.py', LS, "--format", "json"])
2831
self.assertEqual("json", args.format)
2932

3033

@@ -116,8 +119,7 @@ def test_main(self):
116119
buf = io.StringIO()
117120
stream = cast(TextIO, buf)
118121

119-
args = lddwrap.main.parse_args(
120-
sys_argv=["some-executable.py", "/bin/pwd"])
122+
args = lddwrap.main.parse_args(sys_argv=["some-executable.py", PWD])
121123

122124
with tests.MockLdd(
123125
out="\n".join([
@@ -148,7 +150,7 @@ def test_sorted_without_specific_attribute(self):
148150
stream = cast(TextIO, buf)
149151

150152
args = lddwrap.main.parse_args(
151-
sys_argv=["some-executable.py", "/bin/pwd", "--sorted"])
153+
sys_argv=["some-executable.py", PWD, "--sorted"])
152154

153155
with tests.MockLdd(
154156
out="\n".join([
@@ -179,7 +181,7 @@ def test_sorted_with_specific_attribute(self):
179181
stream = cast(TextIO, buf)
180182

181183
args = lddwrap.main.parse_args(
182-
sys_argv=["some-executable.py", "/bin/pwd", "--sorted", "path"])
184+
sys_argv=["some-executable.py", PWD, "--sorted", "path"])
183185

184186
with tests.MockLdd(
185187
out="\n".join([

0 commit comments

Comments
 (0)