Skip to content

Commit 2f3cf38

Browse files
altendkybhrutledge
andauthored
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 <[email protected]>
1 parent 668f4db commit 2f3cf38

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

docs/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,14 @@ PyPI.
163163
.. code-block:: console
164164
165165
$ twine check -h
166-
usage: twine check [-h] dist [dist ...]
166+
usage: twine check [-h] [--strict] dist [dist ...]
167167
168168
positional arguments:
169169
dist The distribution files to check, usually dist/*
170170
171171
optional arguments:
172172
-h, --help show this help message and exit
173+
--strict Fail on warnings
173174
174175
``twine register``
175176
^^^^^^^^^^^^^^^^^^

tests/test_check.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def test_check_no_description(monkeypatch, capsys):
118118

119119
# used to crash with `AttributeError`
120120
output_stream = io.StringIO()
121-
check.check(["dist/*"], output_stream=output_stream)
121+
assert not check.check(["dist/*"], output_stream=output_stream)
122122
assert output_stream.getvalue() == (
123123
"Checking dist/dist.tar.gz: PASSED, with warnings\n"
124124
" warning: `long_description_content_type` missing. "
@@ -127,6 +127,32 @@ def test_check_no_description(monkeypatch, capsys):
127127
)
128128

129129

130+
def test_strict_fails_on_warnings(monkeypatch, capsys):
131+
package = pretend.stub(
132+
metadata_dictionary=lambda: {
133+
"description": None,
134+
"description_content_type": None,
135+
}
136+
)
137+
138+
monkeypatch.setattr(commands, "_find_dists", lambda a: ["dist/dist.tar.gz"])
139+
monkeypatch.setattr(
140+
package_file,
141+
"PackageFile",
142+
pretend.stub(from_filename=lambda *a, **kw: package),
143+
)
144+
145+
# used to crash with `AttributeError`
146+
output_stream = io.StringIO()
147+
assert check.check(["dist/*"], output_stream=output_stream, strict=True)
148+
assert output_stream.getvalue() == (
149+
"Checking dist/dist.tar.gz: FAILED, due to warnings\n"
150+
" warning: `long_description_content_type` missing. "
151+
"defaulting to `text/x-rst`.\n"
152+
" warning: `long_description` missing.\n"
153+
)
154+
155+
130156
def test_check_failing_distribution(monkeypatch):
131157
renderer = pretend.stub(render=pretend.call_recorder(lambda *a, **kw: None))
132158
package = pretend.stub(
@@ -159,8 +185,8 @@ def test_check_failing_distribution(monkeypatch):
159185

160186
def test_main(monkeypatch):
161187
check_result = pretend.stub()
162-
check_stub = pretend.call_recorder(lambda a: check_result)
188+
check_stub = pretend.call_recorder(lambda a, strict=False: check_result)
163189
monkeypatch.setattr(check, "check", check_stub)
164190

165191
assert check.main(["dist/*"]) == check_result
166-
assert check_stub.calls == [pretend.call(["dist/*"])]
192+
assert check_stub.calls == [pretend.call(["dist/*"], strict=False)]

twine/commands/check.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ def _check_file(
101101
return warnings, is_ok
102102

103103

104-
def check(dists: List[str], output_stream: IO[str] = sys.stdout) -> bool:
104+
def check(
105+
dists: List[str],
106+
output_stream: IO[str] = sys.stdout,
107+
strict: bool = False,
108+
) -> bool:
105109
uploads = [i for i in commands._find_dists(dists) if not i.endswith(".asc")]
106110
if not uploads: # Return early, if there are no files to check.
107111
output_stream.write("No files to check.\n")
@@ -126,7 +130,11 @@ def check(dists: List[str], output_stream: IO[str] = sys.stdout) -> bool:
126130
output_stream.write(textwrap.indent(error_text, " "))
127131
output_stream.write(textwrap.indent(str(render_warning_stream), " "))
128132
elif warnings:
129-
output_stream.write("PASSED, with warnings\n")
133+
if strict:
134+
failure = True
135+
output_stream.write("FAILED, due to warnings\n")
136+
else:
137+
output_stream.write("PASSED, with warnings\n")
130138
else:
131139
output_stream.write("PASSED\n")
132140

@@ -145,8 +153,15 @@ def main(args: List[str]) -> bool:
145153
metavar="dist",
146154
help="The distribution files to check, usually dist/*",
147155
)
156+
parser.add_argument(
157+
"--strict",
158+
action="store_true",
159+
default=False,
160+
required=False,
161+
help="Fail on warnings",
162+
)
148163

149164
parsed_args = parser.parse_args(args)
150165

151166
# Call the check function with the arguments from the command line
152-
return check(parsed_args.dists)
167+
return check(parsed_args.dists, strict=parsed_args.strict)

0 commit comments

Comments
 (0)