Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add strict to check command #715

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating the docs!


``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 Down Expand Up @@ -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()
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"
)
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)