From c0fd4b8e49d70791f60a1a1405b7e3101b9d0e34 Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Mon, 16 Nov 2020 10:19:11 -0500 Subject: [PATCH 1/5] Add check --strict to fail on warnings --- tests/test_check.py | 30 ++++++++++++++++++++++++++++-- twine/commands/check.py | 21 ++++++++++++++++++--- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/tests/test_check.py b/tests/test_check.py index 62650cae..d7028eb3 100644 --- a/tests/test_check.py +++ b/tests/test_check.py @@ -159,8 +159,34 @@ 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)] + + +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() + 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" + ) diff --git a/twine/commands/check.py b/twine/commands/check.py index f7c9dae7..55e73430 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) From c89e4e273d64dffd50fd2fedceb827fa3e2dd8b1 Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Mon, 16 Nov 2020 10:22:39 -0500 Subject: [PATCH 2/5] assert return value from check as well --- tests/test_check.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_check.py b/tests/test_check.py index d7028eb3..f9886bd2 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. " @@ -183,7 +183,7 @@ def test_strict_fails_on_warnings(monkeypatch, capsys): # used to crash with `AttributeError` output_stream = io.StringIO() - check.check(["dist/*"], output_stream=output_stream, strict=True) + 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. " From 8480f1a2cea08a4840db9005c1df5a412f7ced21 Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Mon, 16 Nov 2020 10:25:24 -0500 Subject: [PATCH 3/5] black --- twine/commands/check.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/twine/commands/check.py b/twine/commands/check.py index 55e73430..53a2069c 100644 --- a/twine/commands/check.py +++ b/twine/commands/check.py @@ -102,9 +102,9 @@ def _check_file( def check( - dists: List[str], - output_stream: IO[str] = sys.stdout, - strict: bool = False, + 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. From 49a5279b14fbf954974b4051ea6933fdaae5af86 Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Mon, 16 Nov 2020 10:27:01 -0500 Subject: [PATCH 4/5] update check --help output in docs --- docs/index.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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`` ^^^^^^^^^^^^^^^^^^ From f41ac4e1dbb4d7464a9a20ec294515335677b1a3 Mon Sep 17 00:00:00 2001 From: Brian Rutledge Date: Sun, 22 Nov 2020 14:18:41 -0500 Subject: [PATCH 5/5] Move test near related tests --- tests/test_check.py | 52 ++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/tests/test_check.py b/tests/test_check.py index f9886bd2..3464a3c3 100644 --- a/tests/test_check.py +++ b/tests/test_check.py @@ -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( @@ -164,29 +190,3 @@ def test_main(monkeypatch): assert check.main(["dist/*"]) == check_result assert check_stub.calls == [pretend.call(["dist/*"], strict=False)] - - -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" - )