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

Consider underscores in the keys of setup.cfg #13

Merged
merged 4 commits into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 14 additions & 5 deletions src/pyscaffoldext/markdown/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pyscaffold import file_system as fs
from pyscaffold.actions import Action, ActionParams, ScaffoldOpts, Structure
from pyscaffold.extensions import Extension
from pyscaffold.identification import dasherize
from pyscaffold.log import logger
from pyscaffold.operations import FileContents, FileOp, no_overwrite
from pyscaffold.structure import merge, reify_leaf, reject
Expand All @@ -19,6 +20,8 @@
__copyright__ = "Florian Wilhelm"
__license__ = "MIT"

DESC_KEY = "long_description"
TYPE_KEY = "long_description_content_type"

DOC_REQUIREMENTS = ["recommonmark"]

Expand All @@ -38,13 +41,19 @@ def add_long_desc(content: str) -> str:
updater = ConfigUpdater()
updater.read_string(content)
metadata = updater["metadata"]
metadata["long-description"].value = "file: README.md"
long_desc_type = "long-description-content-type"

long_desc = metadata.get(dasherize(DESC_KEY)) or metadata.get(DESC_KEY)
long_desc.name = DESC_KEY # dash-separated keys are deprecated in setuptools
abravalheri marked this conversation as resolved.
Show resolved Hide resolved
long_desc.value = "file: README.md"
long_desc_value = "text/markdown; charset=UTF-8; variant=GFM"
if long_desc_type not in metadata:
metadata["long-description"].add_after.option(long_desc_type, long_desc_value)

long_desc_type = metadata.get(dasherize(TYPE_KEY)) or metadata.get(TYPE_KEY)
if long_desc_type:
long_desc_type.name = TYPE_KEY
long_desc_type.value = long_desc_value
else:
metadata[long_desc_type].value = long_desc_value
long_desc.add_after.option(TYPE_KEY, long_desc_value)

return str(updater)


Expand Down
42 changes: 40 additions & 2 deletions tests/test_markdown.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from configparser import ConfigParser
from textwrap import dedent

import pytest
from pyscaffold import __version__ as pyscaffold_version
from pyscaffold import api, cli

from pyscaffoldext.markdown.extension import DOC_REQUIREMENTS, Markdown
from pyscaffoldext.markdown.extension import DOC_REQUIREMENTS, Markdown, add_long_desc

CONV_FILES = [
"README",
Expand All @@ -17,6 +18,42 @@
]


def test_underscore_keys():
# Dash-separated keys for setup.cfg were deprecated by setuptools, so it is good to
# ensure they don't occur
examples = [
"""\
[metadata]
long-description: a-pkg
long-description-content-type: text/x-rst
""",
"""\
[metadata]
long_description: a-pkg
long_description_content_type: text/x-rst
""",
"""\
[metadata]
long-description: a-pkg
""",
"""\
[metadata]
long_description: a-pkg
""",
]

for example in examples:
modified = add_long_desc(dedent(example))
cfg = ConfigParser()
cfg.read_string(modified)
options = list(cfg["metadata"].keys())
print(modified)
assert "long_description" in options
assert "long_description_content_type" in options
assert "long-description" not in options
assert "long-description-content-type" not in options


@pytest.mark.slow
def test_create_project_with_markdown(tmpfolder):
# Given options with the markdown extension,
Expand All @@ -42,7 +79,8 @@ def test_create_project_with_markdown(tmpfolder):
existing_setup = (tmpfolder / "proj/setup.cfg").read_text()
cfg = ConfigParser()
cfg.read_string(existing_setup)
assert "text/markdown" in str(cfg["metadata"]["long-description-content-type"])
assert "text/markdown" in str(cfg["metadata"]["long_description_content_type"])
assert "file: README" in str(cfg["metadata"]["long_description"])

# and new doc requirements should be added to docs/requirements.txt
requirements_txt = (tmpfolder / "proj/docs/requirements.txt").read_text()
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ setenv =
TOXINIDIR = {toxinidir}
passenv =
HOME
deps:
setuptools_scm<6
abravalheri marked this conversation as resolved.
Show resolved Hide resolved
extras =
testing
commands =
Expand Down