From 2f3cf380734fdfa715f19012cac9457dc189c842 Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Sun, 22 Nov 2020 14:26:02 -0500 Subject: [PATCH] Add strict to check command (#715) * Add check --strict to fail on warnings * assert return value from check as well * black * update check --help output in docs * Move test near related tests Co-authored-by: Brian Rutledge --- docs/index.rst | 3 ++- tests/test_check.py | 32 +++++++++++++++++++++++++++++--- twine/commands/check.py | 21 ++++++++++++++++++--- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 01ea8cb8..972ed926 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -163,13 +163,14 @@ PyPI. .. code-block:: console $ twine check -h - usage: twine check [-h] dist [dist ...] + usage: twine check [-h] [--strict] dist [dist ...] positional arguments: dist The distribution files to check, usually dist/* optional arguments: -h, --help show this help message and exit + --strict Fail on warnings ``twine register`` ^^^^^^^^^^^^^^^^^^ diff --git a/tests/test_check.py b/tests/test_check.py index 62650cae..3464a3c3 100644 --- a/tests/test_check.py +++ b/tests/test_check.py @@ -118,7 +118,7 @@ def test_check_no_description(monkeypatch, capsys): # used to crash with `AttributeError` output_stream = io.StringIO() - check.check(["dist/*"], output_stream=output_stream) + assert not check.check(["dist/*"], output_stream=output_stream) assert output_stream.getvalue() == ( "Checking dist/dist.tar.gz: PASSED, with warnings\n" " warning: `long_description_content_type` missing. " @@ -127,6 +127,32 @@ def test_check_no_description(monkeypatch, capsys): ) +def test_strict_fails_on_warnings(monkeypatch, capsys): + package = pretend.stub( + metadata_dictionary=lambda: { + "description": None, + "description_content_type": None, + } + ) + + monkeypatch.setattr(commands, "_find_dists", lambda a: ["dist/dist.tar.gz"]) + monkeypatch.setattr( + package_file, + "PackageFile", + pretend.stub(from_filename=lambda *a, **kw: package), + ) + + # used to crash with `AttributeError` + output_stream = io.StringIO() + assert check.check(["dist/*"], output_stream=output_stream, strict=True) + assert output_stream.getvalue() == ( + "Checking dist/dist.tar.gz: FAILED, due to warnings\n" + " warning: `long_description_content_type` missing. " + "defaulting to `text/x-rst`.\n" + " warning: `long_description` missing.\n" + ) + + def test_check_failing_distribution(monkeypatch): renderer = pretend.stub(render=pretend.call_recorder(lambda *a, **kw: None)) package = pretend.stub( @@ -159,8 +185,8 @@ def test_check_failing_distribution(monkeypatch): def test_main(monkeypatch): check_result = pretend.stub() - check_stub = pretend.call_recorder(lambda a: check_result) + check_stub = pretend.call_recorder(lambda a, strict=False: check_result) monkeypatch.setattr(check, "check", check_stub) assert check.main(["dist/*"]) == check_result - assert check_stub.calls == [pretend.call(["dist/*"])] + assert check_stub.calls == [pretend.call(["dist/*"], strict=False)] diff --git a/twine/commands/check.py b/twine/commands/check.py index f7c9dae7..53a2069c 100644 --- a/twine/commands/check.py +++ b/twine/commands/check.py @@ -101,7 +101,11 @@ def _check_file( return warnings, is_ok -def check(dists: List[str], output_stream: IO[str] = sys.stdout) -> bool: +def check( + dists: List[str], + output_stream: IO[str] = sys.stdout, + strict: bool = False, +) -> bool: uploads = [i for i in commands._find_dists(dists) if not i.endswith(".asc")] if not uploads: # Return early, if there are no files to check. output_stream.write("No files to check.\n") @@ -126,7 +130,11 @@ def check(dists: List[str], output_stream: IO[str] = sys.stdout) -> bool: output_stream.write(textwrap.indent(error_text, " ")) output_stream.write(textwrap.indent(str(render_warning_stream), " ")) elif warnings: - output_stream.write("PASSED, with warnings\n") + if strict: + failure = True + output_stream.write("FAILED, due to warnings\n") + else: + output_stream.write("PASSED, with warnings\n") else: output_stream.write("PASSED\n") @@ -145,8 +153,15 @@ def main(args: List[str]) -> bool: metavar="dist", help="The distribution files to check, usually dist/*", ) + parser.add_argument( + "--strict", + action="store_true", + default=False, + required=False, + help="Fail on warnings", + ) parsed_args = parser.parse_args(args) # Call the check function with the arguments from the command line - return check(parsed_args.dists) + return check(parsed_args.dists, strict=parsed_args.strict)