Skip to content

Commit

Permalink
Introduce pytest (#6)
Browse files Browse the repository at this point in the history
Introduce pytest with some basic tests for the CLI. We will expand on
the testing now that we have it setup.

Added it to the workflow for GitHub as well.
  • Loading branch information
fzakaria authored Sep 1, 2023
1 parent e026ffe commit caff5a7
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ jobs:
name: fzakaria
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
- run: nix develop --command make lint
- run: nix develop --command make test
- run: nix build --print-build-logs
- run: nix run . -- --help
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ lint: ## Run pep8, black, mypy linters.
# TODO(fzakaria): without pythonpath it picks up the wrong python
# and then does not find the venv for the imports
pyright --pythonpath $(shell which python)
nixpkgs-fmt --check .
nixpkgs-fmt --check .

.PHONY: test
test: ## Run pytest primarily.
pytest
68 changes: 67 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ black = "^23.7.0"
isort = "^5.12.0"
flake8 = "^6.1.0"
pyright = "^1.1.325"
pytest = "^7.4.0"

[build-system]
requires = ["poetry-core"]
Expand Down
14 changes: 11 additions & 3 deletions sqlelf/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import os
import sys
from functools import reduce

import apsw
Expand All @@ -10,7 +11,14 @@
from .elf import dynamic, header, instruction, section, strings, symbol


def start():
def start(args=sys.argv[1:], stdin=sys.stdin):
"""
Start the main CLI
Args:
args: the command line arguments to parse
stdin: the stdin to use if invoking the shell
"""
parser = argparse.ArgumentParser(
prog="sqlelf",
description="Analyze ELF files with the power of SQL",
Expand All @@ -23,7 +31,7 @@ def start():
"-s", "--sql", help="Potential SQL to execute. Omitting this enters the REPL."
)

args = parser.parse_args()
args = parser.parse_args(args)

# Iterate through our arguments and if one of them is a directory explode it out
filenames: list[str] = reduce(
Expand Down Expand Up @@ -52,7 +60,7 @@ def start():
strings.register(connection, binaries)
instruction.register(connection, binaries)

shell = apsw.shell.Shell(db=connection)
shell = apsw.shell.Shell(db=connection, stdin=stdin)

if args.sql:
shell.process_complete_line(args.sql)
Expand Down
Empty file added tests/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from sqlelf import cli
import pytest
from io import StringIO

def test_cli_bad_arguments():
with pytest.raises(SystemExit):
cli.start(["--does-not-exist"])

def test_cli_no_arguments():
with pytest.raises(SystemExit):
cli.start([])

def test_cli_single_file_arguments():
stdin = StringIO("")
cli.start(["/bin/ls"], stdin)

def test_cli_prompt_single_file_arguments():
stdin = StringIO(".exit 56\n")
with pytest.raises(SystemExit) as err:
cli.start(["/bin/ls"], stdin)
assert err.value.code == 56

0 comments on commit caff5a7

Please sign in to comment.