Add unit tests for which.py (#1686)

* Add unit tests for which.py

* Change ls path to /bin/ls

* Update tests.sh
This commit is contained in:
Gulshan Singh 2023-04-24 00:45:27 -07:00 committed by GitHub
parent 7720b81cc7
commit 89b22f4cf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,36 @@
import sys
from unittest.mock import MagicMock
# Replace `pwndbg.commands` module with a mock to prevent import errors, as well
# as the `load_commands` function
module_name = "pwndbg.commands"
module = MagicMock(__name__=module_name, load_commands=lambda: None)
sys.modules[module_name] = module
import os
import tempfile
# Load the mocks for the `gdb` and `gdblib` modules
import mocks.gdb
import mocks.gdblib # noqa: F401
# We must import the function under test after all the mocks are imported
from pwndbg.lib.which import which
def test_basic():
assert which("ls") == "/bin/ls"
def test_nonexistent():
assert which("definitely-not-a-real-command") is None
def test_dir():
with tempfile.TemporaryDirectory() as tempdir:
path = os.path.join(tempdir, "test_file")
with open(path, "w") as f:
f.write("test")
os.chmod(path, 0o755)
assert which(path) == path