Skip to content

Commit

Permalink
Fix "exception: 'in <string>' requires string as left operand, not ty…
Browse files Browse the repository at this point in the history
…pe" (#126)

* fix type problem

add also an integration unit test

Fixes #125

* Minor fixups; organize tests

* Update changelog

Co-authored-by: Steven Loria <sloria1@gmail.com>
  • Loading branch information
CarliJoy and sloria committed Jan 11, 2022
1 parent b2baffe commit 050befd
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 29 deletions.
5 changes: 5 additions & 0 deletions README.rst
Expand Up @@ -153,6 +153,11 @@ MIT licensed. See the bundled `LICENSE <https://github.com/sloria/sphinx-issues/
Changelog
*********

3.0.1 (unreleased)
------------------

- Fix regression from 3.0.0: `exception: 'in <string>' requires string as left operand, not type`.

3.0.0 (2022-01-10)
------------------

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -3,7 +3,7 @@

INSTALL_REQUIRES = ["sphinx"]
EXTRAS_REQUIRE = {
"tests": ["pytest"],
"tests": ["pytest>=6.2.0"],
"lint": [
"flake8==3.9.2",
"flake8-bugbear==20.11.1",
Expand Down
22 changes: 10 additions & 12 deletions sphinx_issues.py
Expand Up @@ -333,52 +333,50 @@ def setup(app):
"issues_uri",
default="https://github.com/{group}/{project}/issues/{issue}",
rebuild="html",
types="[str]",
types=[str],
)
app.add_config_value("issues_prefix", default="#", rebuild="html", types="[str]")
app.add_config_value("issues_prefix", default="#", rebuild="html", types=[str])
# Format template for PR URI
# e.g. 'https://github.com/sloria/marshmallow/pull/{issue}
app.add_config_value(
"issues_pr_uri",
default="https://github.com/{group}/{project}/pull/{pr}",
rebuild="html",
types="[str]",
types=[str],
)
app.add_config_value("issues_pr_prefix", default="#", rebuild="html", types="[str]")
app.add_config_value("issues_pr_prefix", default="#", rebuild="html", types=[str])
# Format template for commit URI
# e.g. 'https://github.com/sloria/marshmallow/commits/{commit}
app.add_config_value(
"issues_commit_uri",
default="https://github.com/{group}/{project}/commit/{commit}",
rebuild="html",
types="[str]",
types=[str],
)
app.add_config_value(
"issues_commit_prefix", default="@", rebuild="html", types="[str]"
"issues_commit_prefix", default="@", rebuild="html", types=[str]
)
# There is no seperator config as a format_text function is given

# Default User (Group)/Project eg. 'sloria/marshmallow'
# Called github as the package was working with github only before
app.add_config_value(
"issues_github_path", default=None, rebuild="html", types="[str]"
"issues_github_path", default=None, rebuild="html", types=[str]
)
# Same as above but with new naming to reflect the new functionality
# Only on of both can be set
app.add_config_value(
"issues_default_group_project", default=None, rebuild="html", types="[str]"
"issues_default_group_project", default=None, rebuild="html", types=[str]
)
# Format template for user profile URI
# e.g. 'https://github.com/{user}'
app.add_config_value(
"issues_user_uri",
default="https://github.com/{user}",
rebuild="html",
types="[str]",
)
app.add_config_value(
"issues_user_prefix", default="@", rebuild="html", types="[str]"
types=[str],
)
app.add_config_value("issues_user_prefix", default="@", rebuild="html", types=[str])
app.add_role("issue", issue_role)
app.add_role("pr", pr_role)
app.add_role("user", user_role)
Expand Down
Empty file added tests/source/_static/.gitkeep
Empty file.
64 changes: 64 additions & 0 deletions tests/source/conf.py
@@ -0,0 +1,64 @@
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))


# -- Project information -----------------------------------------------------

project = "sphinx-issues"
copyright = "2022, foobar"
author = "foobar"


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ["sphinx_issues"]

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = "alabaster"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]

#
suppress_warnings = ["app.add_node"]

issues_uri = "https://gitlab.company.com/{group}/{project}/-/issues/{issue}"
issues_prefix = "#"
issues_pr_uri = "https://gitlab.company.com/{group}/{project}/-/merge_requests/{pr}"
issues_pr_prefix = "!"
issues_commit_uri = "https://gitlab.company.com/{group}/{project}/-/commit/{commit}"
issues_commit_prefix = "@"
issues_user_uri = "https://gitlab.company.com/{user}"
issues_user_prefix = "@"
issues_default_group_project = "myteam/super_great_project"
7 changes: 7 additions & 0 deletions tests/source/examples.rst
@@ -0,0 +1,7 @@
Examples:

- See issues :issue:`12,13`

- See other issues :issue:`sloria/konch#45,46`.

- See PR :pr:`58`, thanks :user:`kound`
7 changes: 7 additions & 0 deletions tests/source/index.rst
@@ -0,0 +1,7 @@

Welcome to sphinx-issues's documentation!
=========================================


.. include:: examples.rst
.. include:: ../README.rst
97 changes: 81 additions & 16 deletions test_sphinx_issues.py → tests/test_sphinx_issues.py
@@ -1,19 +1,18 @@
from tempfile import mkdtemp
import subprocess
import sys
from pathlib import Path
from shutil import rmtree
from tempfile import mkdtemp
from unittest.mock import Mock

from sphinx.application import Sphinx
from sphinx_issues import (
issue_role,
user_role,
pr_role,
cve_role,
cwe_role,
commit_role,
setup as issues_setup,
)

import pytest
import sphinx.application
from sphinx_issues import commit_role, cve_role, cwe_role, issue_role, pr_role
from sphinx_issues import setup as issues_setup
from sphinx_issues import user_role


BASE_DIR = Path(__file__).parent.absolute()


@pytest.fixture(
Expand All @@ -30,8 +29,8 @@
)
def app(request):
src, doctree, confdir, outdir = [mkdtemp() for _ in range(4)]
Sphinx._log = lambda self, message, wfile, nonl=False: None
app = Sphinx(
sphinx.application.Sphinx._log = lambda self, message, wfile, nonl=False: None
app = sphinx.application.Sphinx(
srcdir=src, confdir=None, outdir=outdir, doctreedir=doctree, buildername="html"
)
issues_setup(app)
Expand Down Expand Up @@ -180,8 +179,8 @@ def test_issue_role_multiple_with_external(inliner):
@pytest.fixture
def app_custom_uri():
src, doctree, confdir, outdir = [mkdtemp() for _ in range(4)]
Sphinx._log = lambda self, message, wfile, nonl=False: None
app = Sphinx(
sphinx.application.Sphinx._log = lambda self, message, wfile, nonl=False: None
app = sphinx.application.Sphinx(
srcdir=src, confdir=None, outdir=outdir, doctreedir=doctree, buildername="html"
)
issues_setup(app)
Expand Down Expand Up @@ -291,3 +290,69 @@ def test_roles_custom_uri(
link = result[0][0]
assert link.astext() == expected_text
assert link.attributes["refuri"] == expected_url


@pytest.fixture
def tmp_doc_build_folder(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
"""Generate a temporary source folder and chdir in it. Return the build folder"""
source = tmp_path / "source"
build = tmp_path / "build"
static = source / "_static"
for folder in (source, build, static):
folder.mkdir()
conf_py = BASE_DIR / "source" / "conf.py"
examples_rst = BASE_DIR / "source" / "examples.rst"

source.joinpath("conf.py").write_bytes(conf_py.read_bytes())
source.joinpath("index.rst").write_bytes(examples_rst.read_bytes())

monkeypatch.chdir(source)
return build


def test_sphinx_build_integration(tmp_doc_build_folder: Path):
"""Ensure that a simulated complete sphinx run works as expected"""
subprocess.run(
[
Path(sys.executable).parent.joinpath("sphinx-build"),
"-b",
"html",
"-W", # turn warnings into errors
"-E", # force rebuild of environment (even if we work in tmp)
".",
str(tmp_doc_build_folder),
],
check=True,
)

created = tmp_doc_build_folder / "index.html"
assert created.exists() and created.is_file()
content = created.read_text()
issue_url = "https://gitlab.company.com/myteam/super_great_project/-/issues/"
other_issue_url = "https://gitlab.company.com/sloria/konch/-/issues/"
pr_url = "https://gitlab.company.com/myteam/super_great_project/-/merge_requests/"
user_url = "https://gitlab.company.com/"

# We could do something fancy like an HTML parser or regex:
# Instead we keep it simple
expected_strings = (
(
f"See issues "
f'<a class="reference external" href="{issue_url}12">#12</a>, '
f'<a class="reference external" href="{issue_url}13">#13</a>'
),
(
f"See other issues "
f'<a class="reference external" href="{other_issue_url}45">sloria/konch#45</a>,'
f' <a class="reference external" href="{issue_url}46">#46</a>'
),
(
f'See PR <a class="reference external" href="{pr_url}58">!58</a>, '
f'thanks <a class="reference external" href="{user_url}kound">&#64;kound</a>'
),
)
# Ensure that we do no check character wise but line wise
assert len(expected_strings) == 3

for expected in expected_strings:
assert expected in content

0 comments on commit 050befd

Please sign in to comment.