def test_retrieve_namespace(data_regression):
    app_env = create_sphinx_app(confoverrides={"extensions": ["sphinxcontrib.bibtex"]})
    roles, directives = retrieve_namespace(app_env)
    data_regression.check(
        {
            "roles": list(sorted(roles.keys())),
            "directives": list(sorted(directives.keys())),
        }
    )
Ejemplo n.º 2
0
 def _update_env(self):
     """Update the sphinx application."""
     # TODO how to watch conf.py for changes? (or at least have command to update)
     # TODO use self.source_roots to find conf path?
     # TODO allow source directory to be different to conf path
     conf_path = self._config.settings.get("conf_path", None)
     logger.debug(f"Settings: {self._config.settings}")
     if conf_path and not os.path.exists(conf_path):
         self.server.show_message(
             f"The path set in `rst_lsp.conf_path` does not exist: {conf_path}",
             msg_type=MessageType.Error,
         )
         conf_path = None
     elif conf_path:
         conf_path = os.path.realpath(conf_path)
     logger.debug(f"Using conf dir: {conf_path}")
     try:
         self._app_env = create_sphinx_app(
             conf_dir=os.path.dirname(conf_path) if conf_path else None,
             doctree_dir=create_default_cache_path(self._root_uri_hash,
                                                   "doctrees"),
             output_dir=create_default_cache_path(self._root_uri_hash,
                                                  "outputs"),
         )
     except Exception as err:
         self.server.show_message(
             ("An error occurred creating a sphinx application from "
              f"`rst_lsp.conf_path`: {conf_path}.\n\n"
              f"{err}"),
             msg_type=MessageType.Error,
         )
         conf_path = None
         self._app_env = create_sphinx_app(
             conf_dir=None,
             doctree_dir=create_default_cache_path(self._root_uri_hash,
                                                   "doctrees"),
             output_dir=create_default_cache_path(self._root_uri_hash,
                                                  "outputs"),
         )
     roles, directives = retrieve_namespace(self._app_env)
     self._db.update_conf_file(conf_path, datetime.datetime.utcnow(), roles,
                               directives)
def test_section_levels(get_test_file_content, data_regression):
    content = get_test_file_content("test_sections.rst")
    app_env = create_sphinx_app()
    results = assess_source(content, app_env)
    data_regression.check(
        {
            "positions": results.positions,
            "references": results.references,
            "targets": results.targets,
            "linting": results.linting,
        }
    )
def test_basic_database(get_test_file_content, data_regression):
    content = get_test_file_content("test_basic.rst")
    app_env = create_sphinx_app(confoverrides={"extensions": ["sphinxcontrib.bibtex"]})
    results = assess_source(content, app_env)
    data_regression.check(
        {
            "positions": results.positions,
            "references": results.references,
            "targets": results.targets,
            "linting": results.linting,
        }
    )
def test_update_conf_file(tmp_path, data_regression):
    cache = DocutilsCache(str(tmp_path), echo=False)
    app_env = create_sphinx_app()
    roles, directives = retrieve_namespace(app_env)
    cache.update_conf_file(
        "conf.py",
        mtime=datetime(2019, 12, 30, 0, 0, 0),
        roles=roles,
        directives=directives,
    )
    cache.update_conf_file(
        "conf.py",
        mtime=datetime(2019, 12, 30, 0, 0, 1),
        roles=roles,
        directives=directives,
    )
    data_regression.check(
        cache.to_dict(order_by={
            "roles": "name",
            "directives": "name"
        }))
def test_sphinx_elements(file_regression, data_regression):
    from textwrap import dedent

    source = dedent(
        """\
        .. _title:

        Title
        -----

        :ref:`title`
        :ref:`fig1`
        :ref:`tbl1`
        :eq:`eq1`
        :numref:`code1`
        :cite:`citation`
        :unknown:`abc`

        .. versionadded:: 1.0

            A note about |RST|

        .. figure:: abc.png
           :name: fig1

        .. table:: Truth table for "not"
            :widths: auto
            :name: tbl1

            =====  =====
            A      not A
            =====  =====
            False  True
            True   False
            =====  =====

        .. math::
            :nowrap:
            :label: eq1

            \\begin{eqnarray}
                y    & = & ax^2 + bx + c \\\\
                f(x) & = & x^2 + 2xy + y^2
            \\end{eqnarray}

        .. code-block:: python::
            :name: code1

            pass

        .. |RST| replace:: ReStructuredText
        """
    )
    app_env = create_sphinx_app(confoverrides={"extensions": ["sphinxcontrib.bibtex"]})
    results = assess_source(source, app_env)
    file_regression.check(results.doctree.pformat())
    data_regression.check(
        {
            "lints": results.linting,
            "positions": results.positions,
            "references": results.references,
            "pending_xrefs": results.pending_xrefs,
            "targets": results.targets,
        }
    )
def test_basic_linting(get_test_file_content, data_regression):
    content = get_test_file_content("test_basic.rst")
    app_env = create_sphinx_app(confoverrides={"extensions": ["sphinxcontrib.bibtex"]})
    results = assess_source(content, app_env)
    # TODO inline errors from docutils refers to wrong line, if after line break
    data_regression.check(results.linting)
def test_basic_doctree(get_test_file_content, file_regression):
    content = get_test_file_content("test_basic.rst")
    app_env = create_sphinx_app(confoverrides={"extensions": ["sphinxcontrib.bibtex"]})
    results = assess_source(content, app_env)
    file_regression.check(results.doctree.pformat())