Skip to content

Commit

Permalink
Merge pull request #695 from emmo-repo/redirectioncheck
Browse files Browse the repository at this point in the history
Added redirection checking tool
  • Loading branch information
jesper-friis authored Dec 21, 2023
2 parents 41cafbb + b6594d1 commit 04f145f
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ Pygments>=2.7.4,<3
pyparsing>=2.4.7
PyYAML>=5.4.1,<7
rdflib>=4.2.1,<8
requests>=2.31,<3
semver>=2.8.1,<4
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def fglob(patt):
"tools/ontoconvert",
"tools/ontoversion",
"tools/excel2onto",
"tools/redirectioncheck",
],
package_data={
"ontopy.factpluspluswrapper.java.lib.so": ["*"],
Expand Down
5 changes: 5 additions & 0 deletions tests/tools/input/expected_redirections.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
redirections:
https://example.com: https://example.com/
http://emmo.info/emmo: https://raw.githubusercontent.com/emmo-repo/EMMO/master/emmo.ttl
https://w3id.org/emmo/1.0.0-beta5/: https://raw.githubusercontent.com/emmo-repo/EMMO/1.0.0-beta5/emmo.ttl
28 changes: 28 additions & 0 deletions tests/tools/test_redirectioncheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Test the `redirectioncheck` tool."""
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from pathlib import Path
from types import ModuleType
from typing import Callable


def test_run(get_tool: "Callable[[str], ModuleType]") -> None:
"""Check that running `redirectioncheck` works.
Parameters:
get_tool: Local module fixture to load a named tool as a module.
See the current folder's `conftest.py` file.
"""
from pathlib import Path

yamlfile = (
Path(__file__).resolve().parent / "input" / "expected_redirections.yaml"
)
redirectioncheck = get_tool("redirectioncheck")

# Make output more readable when running pytest with -s option
print("\n\n")

redirectioncheck.main([str(yamlfile), "-v"])
105 changes: 105 additions & 0 deletions tools/redirectioncheck
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/env python3
"""Script for testing URL redirections.
The expected redirections are specified in a YAML file maping URLs to
expected redirections:
---
redirections:
https://example.com: |
https://example.com
http://emmo.info/emmo: |
https://raw.githubusercontent.com/emmo-repo/EMMO/master/emmo.ttl
https://w3id.org/emmo/: |
https://raw.githubusercontent.com/emmo-repo/EMMO/master/emmo.ttl
"""
import sys
import argparse

import yaml
import requests


def test_all(yamlfile, conf=None):
"""Test all redirections specified in `yamlfile`.
Arguments:
yamlfile: File specifying redirections.
conf: Dict with additional testing configurations.
Returns:
ok: Whether the redirection was successful.
"""
with open(yamlfile, "r") as f:
specifications = yaml.safe_load(f)

ok = True
for url, redirected_url in specifications["redirections"].items():
ok &= test_redirection(url, redirected_url, conf=conf)

return ok


def test_redirection(url, redirected_url, conf=None):
"""Test a redirection.
Arguments:
url: URL to test.
redirected_url: Expected redirection.
conf: Dict with additional testing configurations.
Returns:
ok: Whether the redirection was successful.
"""
verbose = conf.get("verbose")
r = requests.get(url, timeout=5)
ok, reason, got = r.ok, r.reason, None

if r.ok and r.url != redirected_url:
ok, reason, got = False, "Invalid redirection", r.url

if verbose:
print(f"{url:60} {reason}")
if (got and verbose > 1) or verbose > 2:
print(f" got: {got}")
print(f" expected: {redirected_url}")
print()

return ok


def main(argv: list = None):
"""Main function for running redirection tests."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"yamlfile",
nargs="?",
default="redirections.yaml",
help="Name of YAML file specifying redirections to test.",
)
parser.add_argument(
"--verbose",
"-v",
action="count",
default=1,
help="Whether to print verbose output (default).",
)
parser.add_argument(
"--silent",
"--quiet",
"-q",
action="store_const",
const=0,
dest="verbose",
help="Whether to be silent..",
)
args = parser.parse_args(args=argv)
conf = {"verbose": args.verbose}

return test_all(args.yamlfile, conf=conf)


if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)

0 comments on commit 04f145f

Please sign in to comment.