Commit Graph
37 Commits
Author SHA1 Message Date
François Freitag e32301cbbb Merge pull request #10757 from n-peugnet/linkcheck-raw-source-url
linkcheck: Check the source URL of raw directives that use the `url` option
2022-08-23 14:26:16 +02:00
François Freitag 5851344934 Merge pull request #9894 from croth1/linkcheck_ignore_docs
linkcheck: Exclude links from matched documents
2021-12-08 21:57:38 +01:00
François Freitag 70e0c314ca Fix linkcheck_auth link to Requests authentication
The link was directing to
https://www.sphinx-doc.org/en/master/usage/requests-auth>. Prefer using
the intersphinx module to generate the link, it’s more robust than
directly linking to the page.
2021-10-25 15:04:15 +02:00
François Freitag b12a0f33ef Formalize linkcheck CheckResult into a NamedTuple 2021-02-04 21:53:49 +01:00
François Freitag c7eb7cd127 Linkcheck: Use app.build() to trigger the test build
Instead of using application members to access the builder and trigger a
build, use the main app interface.
It ensures the builder setup is realistic, builder cleanups are executed
and the build-finished events are emitted.
2021-01-27 18:55:36 +01:00
François Freitag 227955cbe8 linkcheck: Raise on unknown status in process_result()
Helps catching programming errors. The else clause should never be
reached.
2021-01-27 17:36:10 +01:00
François Freitag 1121e5e8bc Linkcheck: Derive number of links from the post-transform result
The number of links to check is the number of links in self.hyperlinks,
populated by the post-transform.
2021-01-24 12:24:52 +01:00
François Freitag 54df51e86f Linkcheck: Don’t repeatedly open/close log files
Opening and closing a file requires processing from the operating
system. Repeatedly opening and closing wastes system resources and
hinders buffering, causing a flush (disk I/O) after each write
operation.

Using a context manager ensures the logs are flushed to disk and files
are properly closed whether the program exists successfully or an
exception occurs. Compared to the previous implementation, a brutal
shutdown of the machine (e.g. power cord disconnected) could cause some
log lines not to be written. That should not be an issue in practice.

Now, files are created and truncated when linkcheck submitted the links
to check to the threads and is ready to process the results, instead of
when the builder is constructed. It keeps the file operations closer to
their use.
2021-01-24 12:15:07 +01:00
François Freitag aa5e4e2da0 Deprecate linkcheck builder {broken,good,redirected}
These attributes were used to cache checked links and avoid issuing
another web request to the same URI.

Since 82ef497a8c, links are pre-processed
to ensure uniqueness. This caching the results of checked links is no
longer useful.
2021-01-21 17:06:51 +01:00
François Freitag 52fde7e7b1 Match linkcheck deprecation warning version with deprecated.rst
Deprecated.rst states the node_line_or_0 helper will be removed in
Sphinx 5.0, use a RemovedInSphinx50Warning.
2021-01-21 16:39:40 +01:00
François Freitag a1b8b1febb Ensure linkcheck items are comparable
Linkcheck organizes the URLs to checks in a PriorityQueue. The items are
tuples (priority, url, docname, lineno).

Tuples where the lineno is `None` are not comparable with tuples that
have an integer lineno, and PriorityQueue items must be comparable (see
https://bugs.python.org/issue31145).

Fixes an issue when a document contains two links to the same URL, one
with an int line number and the other without line number metadata (such
as an image :target: attribute).

Using 0 instead of None to represent no line number should not lead to
observable changes, the result logger only logs the line number when it
is truthy.

Close #8565
2020-12-22 21:18:31 +01:00
François Freitag 236afac7fd Unset env variables in tests by del os.environ
The `unsetenv()` operation does not update `os.environ`.
https://docs.python.org/3/library/os.html#os.unsetenv
2020-11-26 21:12:28 +01:00
François Freitag 6b90a63f08 Fix #6629: linkcheck: Handle rate-limiting
Follow the Retry-After header if present, otherwise use an exponential
back-off.
2020-11-25 17:34:55 +01:00
François Freitag 419e145e36 Make tests/ a Python package
Allows relative imports.
2020-11-20 19:43:02 +01:00
François Freitag 3c4e1c30b1 linkcheck: Specify subjectAltName in test self-signed certificate
Clears warning:

tests/test_build_linkcheck.py::test_connect_to_selfsigned_with_tls_cacerts
tests/test_build_linkcheck.py::test_connect_to_selfsigned_with_requests_env_var
  …/venv/lib/python3.8/site-packages/urllib3/connection.py:455: SubjectAltNameWarning: Certificate for localhost has no `subjectAltName`, falling back to check for a `commonName` for now. This feature is being removed by major browsers and deprecated by RFC 2818. (See https://github.com/urllib3/urllib3/issues/497 for details.)
    warnings.warn(
2020-11-15 18:25:39 +01:00
François Freitag 5eb74d5c8e tests: Always cleanup when exiting modify_env context manager
Previously, an exception would leave the environment changed.
2020-11-15 10:25:36 +01:00
François Freitag d453088b58 linkcheck: Prevent REQUESTS_CA_BUNDLE leak in tests
Test `test_connect_to_selfsigned_nonexistent_cert_file` serves two
purposes:
- verify the behavior when the CA bundle file path is incorrect
- flag a leak of REQUESTS_CA_BUNDLE

Assumes that test runs after
`test_connect_to_selfsigned_with_requests_env_var`. Not great, but
better than no testing.
2020-11-14 19:58:02 +01:00
François Freitag 10e11f3133 linkcheck: Verify behavior with self-signed certs 2020-11-14 12:59:29 +01:00
François Freitag 99c8765822 linkcheck: Remove unused argument from test_invalid_ssl
Not updated after c90eef1f67.
2020-11-14 12:49:47 +01:00
François Freitag 683635f5b4 linkcheck: Remove call to is_ssl_error()
This method always returns False, it is dead code. The exception
checking stopped working because Requests library wraps SSL errors in a
`requests.exceptions.SSLError` and no longer throws an
`urllib3.exceptions.SSLError`. The first argument to that exception is
an `urllib3.exceptions.MaxRetryError`.
2020-11-12 19:58:04 +01:00
François Freitag 040a1e7743 linkcheck: test invalid SSL is reported as broken
`linkcheck` logic suggests that SSL errors were originally expected to
be ignored.

Blaming the corresponding lines point to issue #3008: linkcheck to
website with self-signed certificates. That issue was fixed by commit
4c7bec6460, which ignored SSL errors.
Probably because back then, users could not specify a CA bundle.

A broken SSL certificate is a real issue, it should not be ignored.
Users wishing to ignore issues for a specific link can use the
`linkcheck_ignore` option.

The current behavior is to report the site as broken, keep it.
2020-11-12 19:58:04 +01:00
François Freitag ce9d4c1e68 linkcheck: Rewrite headers tests without mocking
Makes the test more realistic by issuing an HTTP request.
Reduces coupling between test and the code under test.
2020-11-11 21:24:06 +01:00
François Freitag 341b98f794 linkcheck: Rewrite auth tests without mocking
Makes the test more realistic by issuing an HTTP request.
Reduces coupling between test and the code under test.
2020-11-11 21:22:59 +01:00
François Freitag 640cc40b7e linkcheck: Set allow_redirects in requests.head() call
Following redirects is the default for other methods.
https://requests.readthedocs.io/en/latest/api/#requests.request

Define the option closer to its use.
2020-11-11 18:32:54 +01:00
François Freitag 53ca608bc0 linkcheck: Test redirect following for HEAD and GET 2020-11-11 18:32:53 +01:00
François Freitag 6ea0a123c6 Rename test-linkcheck-localserver{,-anchor}
Precise the `linkcheck_anchors` option is enabled. Frees the name
`test-linkcheck-localserver` for future tests without that option.
2020-11-11 18:32:52 +01:00
François Freitag 22a4ed531c Run tests with Python Development mode
Helps catching more issues with the test suite.
https://docs.python.org/3/library/devmode.html#devmode
2020-11-11 14:20:18 +01:00
François Freitag c90eef1f67 linkcheck: Remove unused arguments from tests 2020-11-11 13:48:45 +01:00
François Freitag 0949735210 Sort imports with isort
Keep imports alphabetically sorted and their order homogeneous across
Python source files.

The isort project has more feature and is more active than the
flake8-import-order plugin.

Most issues caught were simply import ordering from the same module.
Where imports were purposefully placed out of order, tag with
isort:skip.
2020-11-11 13:19:05 +01:00
François Freitag 0178437f3c Rewrite test_inspect_main_url to avoid mocking
Makes the test more realistic by issuing an HTTP request.
Reduces coupling between test and the code under test.

The `http_server` helper was factored out into a new tests.utils module.
2020-11-08 22:30:49 +01:00
François Freitag 342c808af3 Fix flake8 issue 2020-11-07 13:32:37 +01:00
François Freitag b1caecda39 Remove additional mentions of Python 3.5 2020-11-07 11:56:20 +01:00
François Freitag fa8bec76e7 Drop Python 3.4 compatibility code
Python 3.4 support was dropped in Sphinx 2.0.
2020-11-07 11:21:34 +01:00
François Freitag 55f7919531 Linkcheck: Use Thread daemon argument
Instead of using a separate call.
2020-10-11 11:41:42 +02:00
François Freitag bd49c3c2ef Outdated comment in docs config for intersphinx
Intersphinx is in use since 6a396c7eb8.
2020-10-05 08:34:26 +02:00
François Freitag 5ea8ee133d Fix #8268: make linkcheck report HTTP errors 2020-10-03 14:33:29 +02:00
François Freitag 5166dd194c Strip trailing whitespaces and normalize line endings
Trailing whitespace do not have impact on the result, they are just
unused bytes. Most text editors are configured to strip trailing
whitespaces. Remove them all in one go.

Update a handful of files to use the UNIX line ending.
2020-10-03 13:47:01 +02:00