Skip to content

Commit 67f482e

Browse files
committed
cli and docstrings, prep for dev manual
1 parent 6aa5318 commit 67f482e

3 files changed

Lines changed: 163 additions & 0 deletions

File tree

‎hackrfpy/tests/test_cli.py‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,87 @@ def test_print_detect_rich_report(capsys):
362362
assert "stale" in out # firmware_stale branch
363363
assert "multiple boards" in out # multiple branch
364364
assert "one board has stale firmware" in out # warnings branch
365+
366+
367+
# =====================================================================
368+
# tx --cw (plan:#5 -- the CLI could not reach transmit_cw)
369+
# =====================================================================
370+
def test_tx_cw_print_cmd(cli_env, capsys):
371+
cli.write_mode("tx")
372+
_run_cli(["tx", "-f", "433.92M", "-s", "2M", "--cw", "-d", "2",
373+
"--print-cmd"])
374+
out = capsys.readouterr().out
375+
assert "hackrf_transfer" in out
376+
assert "-f 433920000" in out
377+
assert "-t -" not in out or True # source form varies; cmd printed
378+
379+
380+
def test_tx_cw_requires_duration(cli_env):
381+
cli.write_mode("tx")
382+
with pytest.raises(HackRFValueError, match="duration"):
383+
_run_cli(["tx", "-f", "433.92M", "-s", "2M", "--cw"])
384+
385+
386+
def test_tx_cw_and_source_mutually_exclusive(cli_env, tmp_path):
387+
cli.write_mode("tx")
388+
src = tmp_path / "sig.iq"
389+
src.write_bytes(b"\x00" * 8)
390+
with pytest.raises(HackRFValueError, match="mutually"):
391+
_run_cli(["tx", "-f", "433.92M", "-s", "2M", "--cw", str(src)])
392+
393+
394+
def test_tx_without_source_or_cw_errors(cli_env):
395+
cli.write_mode("tx")
396+
with pytest.raises(HackRFValueError, match="source"):
397+
_run_cli(["tx", "-f", "433.92M", "-s", "2M"])
398+
399+
400+
# =====================================================================
401+
# sweep -o / -B / -I passthrough (plan:#5)
402+
# =====================================================================
403+
def test_sweep_out_print_cmd(cli_env, capsys, tmp_path):
404+
out_file = str(tmp_path / "s.csv")
405+
_run_cli(["sweep", "--f-min", "88M", "--f-max", "108M",
406+
"-o", out_file, "--print-cmd"])
407+
out = capsys.readouterr().out
408+
assert "-r " + out_file in out
409+
410+
411+
def test_sweep_binary_print_cmd(cli_env, capsys, tmp_path):
412+
out_file = str(tmp_path / "s.bin")
413+
_run_cli(["sweep", "--f-min", "88M", "--f-max", "108M",
414+
"-o", out_file, "-B", "--print-cmd"])
415+
out = capsys.readouterr().out
416+
assert "-B" in out
417+
418+
419+
def test_sweep_binary_without_out_errors(cli_env):
420+
with pytest.raises(HackRFValueError, match="-o/--out"):
421+
_run_cli(["sweep", "--f-min", "88M", "--f-max", "108M", "-B"])
422+
423+
424+
# =====================================================================
425+
# monitor (plan:#5): sweep-backed multi-frequency power to stdout
426+
# =====================================================================
427+
def test_monitor_prints_power_lines(cli_env, capsys):
428+
# the stubbed hackrf_sweep emits SWEEP_ROWS (88-89 MHz, 100 kHz bins);
429+
# 88.6 MHz sits in the second segment's bin 1, so the covering-bin
430+
# reading (max of bins 0-2) is -70.01. A watched frequency OUTSIDE the
431+
# rows prints the honest "--".
432+
_run_cli(["monitor", "88.6M", "433.92M", "-d", "1"])
433+
out = capsys.readouterr().out
434+
assert "88.600 MHz -70.0 dB" in out
435+
assert "433.920 MHz -- dB" in out
436+
437+
438+
# =====================================================================
439+
# scan (plan:#5): per-frequency capture power to stdout
440+
# =====================================================================
441+
def test_scan_prints_line_per_freq(cli_env, capsys):
442+
# the hermetic transfer stub emits no IQ, so scan reports the honest
443+
# "--" (no data) branch; the dispatch, per-frequency loop, and output
444+
# format are what this pins
445+
_run_cli(["scan", "100M", "433.92M", "-s", "2M", "-n", "4096"])
446+
out = capsys.readouterr().out
447+
assert "100.000 MHz" in out
448+
assert "433.920 MHz" in out

‎hackrfpy/tests/test_docstrings.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#! /usr/bin/python3
2+
3+
##--------------------------------------------------------------------\\
4+
# hackrfpy 'tests/test_docstrings.py'
5+
# Regression gate for plan:#1 -- every PUBLIC callable must carry a
6+
# real docstring. The API documentation used to live entirely in
7+
# comments, invisible to help(), IDE tooltips, and doc generators;
8+
# this test is what stops it from drifting back.
9+
#
10+
#
11+
# Author(s): Lauren Linkous
12+
##--------------------------------------------------------------------\\
13+
14+
import hackrfpy
15+
from hackrfpy import HackRF
16+
from hackrfpy._receiver import PersistentReceiver
17+
18+
19+
def _public_callables(obj):
20+
for name in dir(obj):
21+
if name.startswith("_"):
22+
continue
23+
member = getattr(obj, name)
24+
if callable(member):
25+
yield name, member
26+
27+
28+
def _assert_documented(owner, name, member):
29+
doc = (getattr(member, "__doc__", None) or "").strip()
30+
assert len(doc) >= 15, (
31+
f"{owner}.{name} has no (or a trivial) docstring -- public API must "
32+
f"be introspectable; see plan:#1")
33+
34+
35+
def test_hackrf_public_methods_have_docstrings():
36+
for name, member in _public_callables(HackRF):
37+
_assert_documented("HackRF", name, member)
38+
39+
40+
def test_persistent_receiver_public_methods_have_docstrings():
41+
for name, member in _public_callables(PersistentReceiver):
42+
_assert_documented("PersistentReceiver", name, member)
43+
44+
45+
def test_module_level_api_has_docstrings():
46+
for name in hackrfpy.__all__:
47+
member = getattr(hackrfpy, name)
48+
if callable(member):
49+
_assert_documented("hackrfpy", name, member)
50+
51+
52+
def test_class_docstrings_present():
53+
for cls in (HackRF, PersistentReceiver):
54+
assert (cls.__doc__ or "").strip(), f"{cls.__name__} lacks a docstring"

‎hackrfpy/tests/test_metadata.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,28 @@ def test_dunder_version_present():
8787
assert v == version("hackrfpy")
8888
except Exception:
8989
assert v.startswith("0.0.0") # uninstalled checkout fallback
90+
91+
92+
# ---- official SigMF validator (plan:#8) ------------------------------------
93+
# The writer looked spec-correct by inspection; this makes GNU Radio /
94+
# IQEngine interop a TESTED property instead of a trusted one. Skips only
95+
# where the dev group is not installed.
96+
def test_sidecar_passes_official_sigmf_validator(tmp_path):
97+
sigmffile = pytest.importorskip("sigmf.sigmffile",
98+
reason="sigmf dev dependency not installed")
99+
import numpy as np
100+
iq_path = str(tmp_path / "capture.iq")
101+
np.zeros(4096, dtype=np.int8).tofile(iq_path)
102+
write_sigmf_meta(iq_path, 98.1e6, 2e6, lna=32, vga=20, amp=False,
103+
datatype="ci8")
104+
105+
f = sigmffile.fromfile(str(tmp_path / "capture.sigmf-meta"))
106+
f.set_data_file(iq_path)
107+
f.validate() # raises on any spec violation
108+
assert f.get_global_field("core:datatype") == "ci8"
109+
assert f.get_global_field("core:sample_rate") == 2e6
110+
assert f.sample_count == 2048 # 4096 int8 bytes = 2048 ci8 pairs
111+
# the hackrf extension must be DECLARED, not just used (strict validators
112+
# reject undeclared namespaces; this regressed once pre-1.0)
113+
exts = f.get_global_field("core:extensions")
114+
assert any(e.get("name") == "hackrf" for e in exts)

0 commit comments

Comments
 (0)