Skip to content

Commit

Permalink
Add strict to check command (#715)
Browse files Browse the repository at this point in the history
* 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 <brian@bhrutledge.com>
  • Loading branch information
altendky and bhrutledge committed Nov 22, 2020
1 parent 668f4db commit 2f3cf38
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
3 changes: 2 additions & 1 deletion docs/index.rst
Expand Up @@ -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``
^^^^^^^^^^^^^^^^^^
Expand Down
32 changes: 29 additions & 3 deletions tests/test_check.py
Expand Up @@ -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. "
Expand All @@ -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(
Expand Down Expand Up @@ -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)]
21 changes: 18 additions & 3 deletions twine/commands/check.py
Expand Up @@ -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")
Expand All @@ -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")

Expand All @@ -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)

0 comments on commit 2f3cf38

Please sign in to comment.