Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test that checks README.rst against usbsdmux -h output #65

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Install python dependencies
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade pytest
python -m pip install --upgrade pytest pytest-mock
- name: Run pytest
run: |
python -m pytest -vv
23 changes: 13 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,23 @@ command invocations:
.. code-block:: text

$ usbsdmux -h
usage: usbsdmux [-h] SG {get,dut,client,host,off}
usage: usbsdmux [-h] [--json] SG {get,dut,client,host,off,gpio,info} ...

positional arguments:
SG /dev/sg* to use
{get,dut,client,host,off}
Action:
get - return selected mode
dut - set to dut mode
client - set to dut mode (alias for dut)
host - set to host mode
off - set to off mode

optional arguments:
{get,dut,client,host,off,gpio,info}
Supply one of the following commands to interact with the device
get Read the current state of the USB-SD-Mux
dut Switch to the DUT
client Switch to the DUT
host Switch to the host
off Disconnect from host and DUT
gpio Manipulate a GPIO (open drain output only)
info Show information about the SD card

options:
-h, --help show this help message and exit
--json Format output as json. Useful for scripting.

Using as root
-------------
Expand Down
43 changes: 43 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os

import pytest

import usbsdmux.__main__


def test_usage(capsys, mocker):
"test that the usage output include the command name"
mocker.patch("sys.argv", ["usbsdmux"])
with pytest.raises(SystemExit):
usbsdmux.__main__.main()
captured = capsys.readouterr()
assert captured.out == ""
assert captured.err.startswith("usage: usbsdmux")


def test_help_in_readme(capsys, mocker):
"test that the help output matches the readme"
mocker.patch("sys.argv", ["usbsdmux", "-h"])
with pytest.raises(SystemExit):
usbsdmux.__main__.main()
captured = capsys.readouterr()
assert captured.out.startswith("usage: usbsdmux")
assert captured.err == ""

readme_path = os.path.join(os.path.dirname(__file__), "../", "README.rst")
readme_lines = None
for line in open(readme_path).readlines():
line = line.rstrip()
if line == " $ usbsdmux -h":
readme_lines = []
elif readme_lines is not None:
if line and not line.startswith(" "):
break
readme_lines.append(line)

assert readme_lines is not None
del readme_lines[-1] # remove trailing empty line

output_lines = [f" {line}".rstrip() for line in captured.out.splitlines()]

assert output_lines == readme_lines
Loading