Skip to content

Commit

Permalink
Adds a type check to ensure the description type provided is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
Bachmann1234 committed Jul 7, 2019
1 parent add0aea commit 76efb49
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,31 @@ def test_check_no_description(monkeypatch, capsys):
)


def test_check_invalid_description(monkeypatch, capsys):
package = pretend.stub(metadata_dictionary=lambda: {
'description': "Project",
'long_description': "My amazing project::",
'description_content_type': '"text/rst"', # close, but not quite
})

monkeypatch.setattr(check, "_find_dists", lambda a: ["dist/dist.tar.gz"])
monkeypatch.setattr(
check,
"PackageFile",
pretend.stub(from_filename=lambda *a, **kw: package),
)

output_stream = check.StringIO()
check.check("dist/*", output_stream=output_stream)
assert output_stream.getvalue() == (
'Checking distribution dist/dist.tar.gz: '
'warning; `long_description_content_type` invalid.\n'
'It must be one of the following types: '
'[text/markdown, text/plain, text/x-rst].\n'
'Passed\n'
)


def test_check_failing_distribution(monkeypatch):
renderer = pretend.stub(
render=pretend.call_recorder(lambda *a, **kw: None)
Expand Down
8 changes: 8 additions & 0 deletions twine/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"text/markdown": None, # Rendering cannot fail
}

_supported_readme_types = sorted([key for key in _RENDERERS.keys() if key])

# Regular expression used to capture and reformat doctuils warnings into
# something that a human can understand. This is loosely borrowed from
Expand Down Expand Up @@ -88,6 +89,13 @@ def check(dists, output_stream=sys.stdout):
)
description_content_type = 'text/x-rst'

if description_content_type not in _supported_readme_types:
output_stream.write(
'warning; `long_description_content_type` invalid.\n'
'It must be one of the following types: [{}].\n'
.format(", ".join(_supported_readme_types))
)

content_type, params = cgi.parse_header(description_content_type)
renderer = _RENDERERS.get(content_type, _RENDERERS[None])

Expand Down

0 comments on commit 76efb49

Please sign in to comment.