|
| 1 | +import pretend # type: ignore |
| 2 | +import pytest |
| 3 | + |
| 4 | +import pip_audit._cli |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.parametrize( |
| 8 | + "args, vuln_count, pkg_count, expected", |
| 9 | + [ |
| 10 | + ([], 1, 1, "Found 1 known vulnerability in 1 package"), |
| 11 | + ([], 2, 1, "Found 2 known vulnerabilities in 1 package"), |
| 12 | + ([], 2, 2, "Found 2 known vulnerabilities in 2 packages"), |
| 13 | + (["--fix"], 1, 1, "fixed 1 vulnerability in 1 package"), |
| 14 | + (["--fix"], 2, 1, "fixed 2 vulnerabilities in 1 package"), |
| 15 | + (["--fix"], 2, 2, "fixed 2 vulnerabilities in 2 packages"), |
| 16 | + ], |
| 17 | +) |
| 18 | +def test_plurals(capsys, monkeypatch, args, vuln_count, pkg_count, expected): |
| 19 | + dummysource = pretend.stub(fix=lambda a: None) |
| 20 | + monkeypatch.setattr(pip_audit._cli, "PipSource", lambda *a, **kw: dummysource) |
| 21 | + |
| 22 | + parser = pip_audit._cli._parser() |
| 23 | + monkeypatch.setattr(pip_audit._cli, "_parse_args", lambda x: parser.parse_args(args)) |
| 24 | + |
| 25 | + result = [ |
| 26 | + ( |
| 27 | + pretend.stub( |
| 28 | + is_skipped=lambda: False, |
| 29 | + name="something" + str(i), |
| 30 | + canonical_name="something" + str(i), |
| 31 | + version=1, |
| 32 | + ), |
| 33 | + [pretend.stub(fix_versions=[2], id="foo")] * (vuln_count // pkg_count), |
| 34 | + ) |
| 35 | + for i in range(pkg_count) |
| 36 | + ] |
| 37 | + |
| 38 | + auditor = pretend.stub(audit=lambda a: result) |
| 39 | + monkeypatch.setattr(pip_audit._cli, "Auditor", lambda *a, **kw: auditor) |
| 40 | + |
| 41 | + resolve_fix_versions = [pretend.stub(is_skipped=lambda: False, dep=spec) for spec, _ in result] |
| 42 | + monkeypatch.setattr(pip_audit._cli, "resolve_fix_versions", lambda *a: resolve_fix_versions) |
| 43 | + |
| 44 | + try: |
| 45 | + pip_audit._cli.audit() |
| 46 | + except SystemExit: |
| 47 | + pass |
| 48 | + |
| 49 | + captured = capsys.readouterr() |
| 50 | + assert expected in captured.err |
0 commit comments