| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
"""Check links inserted into internal documentation. |
| 18 |
|
| 19 |
Reason for doing this here: |
| 20 |
- Some links don't appear to be being picked up by Cylc-doc linkcheck. |
| 21 |
- As we have more links it's worth checking them here, rather than waiting |
| 22 |
for them to show up in Cylc. |
| 23 |
""" |
| 24 |
|
| 25 |
import os |
| 26 |
import re |
| 27 |
import pytest |
| 28 |
import fnmatch |
| 29 |
|
| 30 |
from time import sleep |
| 31 |
from pathlib import Path |
| 32 |
from urllib import request |
| 33 |
from urllib.error import HTTPError |
| 34 |
|
| 35 |
EXCLUDE = [ |
| 36 |
r'*//www.gnu.org/licenses/', |
| 37 |
r'*//my-site.com/*', |
| 38 |
r'*//ahost/%(owner)s/notes/%(workflow)s', |
| 39 |
r'*//www.h2g2.com/*', |
| 40 |
r'*//web.archive.org/*' |
| 41 |
] |
| 42 |
|
| 43 |
|
| 44 |
def get_links(): |
| 45 |
searchdir = Path(__file__).parent.parent.parent / 'cylc' / 'flow' |
| 46 |
return sorted({ |
| 47 |
url |
| 48 |
for file_ in searchdir.rglob('*.py') |
| 49 |
for url in re.findall( |
| 50 |
r'(https?:\/\/.*?)[\n\s\>`"\',]', file_.read_text() |
| 51 |
) |
| 52 |
if not any( |
| 53 |
fnmatch.fnmatch(url, pattern) for pattern in EXCLUDE |
| 54 |
) |
| 55 |
}) |
| 56 |
|
| 57 |
|
| 58 |
def make_request(link): |
| 59 |
"""Make an HTTP request, using GITHUB_TOKEN for GitHub URLs if available. |
| 60 |
|
| 61 |
The GITHUB_TOKEN environment variable contains a GitHub Actions token. |
| 62 |
This is used to authenticate the workflow requests to github.com, which |
| 63 |
helps avoid rate limiting (unauthenticated requests are limited to 60/hour, |
| 64 |
authenticated to 5000/hour). |
| 65 |
""" |
| 66 |
req = request.Request(link) |
| 67 |
github_token = os.environ.get('GITHUB_TOKEN') |
| 68 |
if github_token and 'github.com' in link: |
| 69 |
req.add_header('Authorization', f'token {github_token}') |
| 70 |
return request.urlopen(req).getcode() |
| 71 |
|
| 72 |
|
| 73 |
@pytest.mark.linkcheck |
| 74 |
@pytest.mark.parametrize('link', get_links()) |
| 75 |
def test_embedded_url(link): |
| 76 |
"""Check links in the source code are not broken. |
| 77 |
|
| 78 |
TIP: use `--dist=load` when running pytest to enable parametrized tests |
| 79 |
to run in parallel |
| 80 |
""" |
| 81 |
try: |
| 82 |
make_request(link) |
| 83 |
except HTTPError as exc: |
| 84 |
# Allowing 403 (forbidden) & 429 (rate-limited) as the link |
| 85 |
# is probably valid, but we are blocked. |
| 86 |
if exc.code in {403, 429}: |
| 87 |
pytest.skip(f'{exc} | {link}') |
| 88 |
# Sleep and retry to reduce risk of flakiness: |
| 89 |
sleep(10) |
| 90 |
try: |
| 91 |
make_request(link) |
| 92 |
except HTTPError as exc: |
| 93 |
raise Exception(f'{exc} | {link}') |