Skip to content

Commit d361f15

Browse files
authored
Add type hints + More tests + Better docs (#69)
* minor edits to client scripts * add type hints (and some doc edits) to core scripts * some doc edits to logger script for consistency * fix project name and copyright in docs conf * more tests for the client scripts for better coverage * remove forgotten debug prints * reformat * add mypy check to workflows * automatically get year for copyright * change master to main * pad version and edit changelog * improved datetime import and use
1 parent 3a13e4e commit d361f15

16 files changed

Lines changed: 301 additions & 120 deletions

File tree

.github/workflows/test.yml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,33 @@ jobs:
2424
black --check urlchecker
2525
2626
27-
testing:
27+
type_checking:
2828
needs: formatting
2929
runs-on: ubuntu-latest
30+
steps:
31+
- uses: actions/checkout@v2
32+
33+
- name: Setup mypy environment
34+
run: conda create --quiet --name type_checking mypy
35+
36+
- name: Check types with mypy
37+
run: |
38+
export PATH="/usr/share/miniconda/bin:$PATH"
39+
source activate type_checking
40+
pip install mypy
41+
pip install types-requests
42+
mypy urlchecker
43+
44+
45+
testing:
46+
needs: type_checking
47+
runs-on: ubuntu-latest
3048
steps:
3149
- uses: actions/checkout@v2
3250
- name: Setup testing environment
3351
run: conda create --quiet --name testing pytest
3452

35-
- name: Test
53+
- name: Test
3654
run: |
3755
export PATH="/usr/share/miniconda/bin:$PATH"
3856
source activate testing

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# CHANGELOG
22

3-
This is a manually generated log to track changes to the repository for each release.
4-
Each section should include general headers such as **Implemented enhancements**
3+
This is a manually generated log to track changes to the repository for each release.
4+
Each section should include general headers such as **Implemented enhancements**
55
and **Merged pull requests**. Critical items to know are:
66

77
- renamed commands
@@ -12,6 +12,7 @@ and **Merged pull requests**. Critical items to know are:
1212
Referenced versions in headers are tagged on Github, in parentheses are for pypi.
1313

1414
## [vxx](https://github.com/urlstechie/urlschecker-python/tree/master) (master)
15+
- adding type hints to code, more tests and logging bug fix (0.0.29)
1516
- decrease verbosity when filename is None (0.0.28)
1617
- don't exit and fail if no URLs to check (0.0.27)
1718
- multiprocessing to speed up checks (0.0.26)

docs/source/conf.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
#
1313
import os
1414
import sys
15+
from datetime import datetime
1516

1617
sys.path.insert(0, os.path.abspath("../../"))
1718

1819
# -- Project information -----------------------------------------------------
1920

20-
project = "URLs-checker"
21-
copyright = "2020, urlstechie"
21+
project = "urlchecker"
22+
copyright = "2020-%s, urlstechie" % datetime.now().year
2223
author = "urlstechie"
2324

2425
# The master toctree document.

tests/test_client_check.py

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,121 @@
11
import os
2+
import sys
23
import pytest
3-
import subprocess
4+
import argparse
45
import tempfile
6+
import subprocess
57
import configparser
8+
from urlchecker.client import main as init_main
9+
from urlchecker.client.check import main as check_main
10+
11+
12+
@pytest.mark.parametrize("config_fname", ["./tests/_local_test_config.conf"])
13+
@pytest.mark.parametrize("cleanup", [False, True])
14+
@pytest.mark.parametrize("print_all", [False])
15+
@pytest.mark.parametrize("verbose", [True])
16+
@pytest.mark.parametrize("force_pass", [False, True])
17+
@pytest.mark.parametrize("rcount", [1])
18+
@pytest.mark.parametrize("timeout", [3])
19+
def test_client_init_(
20+
config_fname, cleanup, print_all, verbose, force_pass, rcount, timeout
21+
):
22+
23+
# init config parser
24+
config = configparser.ConfigParser()
25+
config.read(config_fname)
26+
27+
# init env variables
28+
path = config["DEFAULT"]["git_path_test_value"]
29+
file_types = config["DEFAULT"]["file_types_test_values"]
30+
exclude_urls = config["DEFAULT"]["exclude_test_urls"]
31+
exclude_patterns = config["DEFAULT"]["exclude_test_patterns"]
32+
33+
# Generate command
34+
cmd = [
35+
"urlchecker",
36+
"check",
37+
"--branch",
38+
"main",
39+
"--subfolder",
40+
"test_files",
41+
"--file-types",
42+
file_types,
43+
"--exclude-files",
44+
"conf.py",
45+
"--exclude-urls",
46+
exclude_urls,
47+
"--exclude-patterns",
48+
exclude_patterns,
49+
"--retry-count",
50+
str(rcount),
51+
"--timeout",
52+
str(timeout),
53+
]
54+
55+
# Add boolean arguments
56+
if cleanup:
57+
cmd.append("--cleanup")
58+
if print_all:
59+
cmd.append("--print-all")
60+
if force_pass:
61+
cmd.append("--force-pass")
62+
if verbose:
63+
cmd.append("--verbose")
64+
65+
# Add final path
66+
cmd.append(path)
67+
68+
# assign args and run main
69+
sys.argv = cmd
70+
with pytest.raises(SystemExit):
71+
init_main()
72+
73+
74+
@pytest.mark.parametrize(
75+
"command", ["", "--version", "--help", "--unsupported_command"]
76+
)
77+
def test_command(command):
78+
# assign args and run main
79+
sys.argv = ["urlchecker", command]
80+
with pytest.raises(SystemExit):
81+
init_main()
82+
83+
84+
@pytest.mark.parametrize("config_fname", ["./tests/_local_test_config.conf"])
85+
@pytest.mark.parametrize("cleanup", [False, True])
86+
@pytest.mark.parametrize("no_print", [True])
87+
@pytest.mark.parametrize("verbose", [False])
88+
@pytest.mark.parametrize("force_pass", [False])
89+
@pytest.mark.parametrize("rcount", [1])
90+
@pytest.mark.parametrize("timeout", [3])
91+
def test_arguments_from_cli(
92+
config_fname, cleanup, no_print, verbose, force_pass, rcount, timeout
93+
):
94+
95+
# init config parser
96+
config = configparser.ConfigParser()
97+
config.read("./tests/_local_test_config.conf")
98+
99+
# Generate command
100+
cmd = argparse.Namespace(
101+
path=config["DEFAULT"]["git_path_test_value"],
102+
subfolder="test_files",
103+
branch="main",
104+
cleanup=str(cleanup),
105+
file_types=config["DEFAULT"]["file_types_test_values"],
106+
files="",
107+
no_print=str(no_print),
108+
exclude_urls=config["DEFAULT"]["exclude_test_urls"],
109+
exclude_patterns=config["DEFAULT"]["exclude_test_patterns"],
110+
exclude_files="conf.py",
111+
force_pass=str(force_pass),
112+
retry_count=rcount,
113+
save=None,
114+
timeout=timeout,
115+
verbose=verbose,
116+
)
117+
with pytest.raises(SystemExit):
118+
check_main(cmd, None)
6119

7120

8121
@pytest.mark.parametrize("config_fname", ["./tests/_local_test_config.conf"])
@@ -12,8 +125,9 @@
12125
@pytest.mark.parametrize("force_pass", [False, True])
13126
@pytest.mark.parametrize("rcount", [1, 3])
14127
@pytest.mark.parametrize("timeout", [3, 5])
15-
def test_client_general(config_fname, cleanup, print_all, verbose,
16-
force_pass, rcount, timeout):
128+
def test_client_general(
129+
config_fname, cleanup, print_all, verbose, force_pass, rcount, timeout
130+
):
17131

18132
# init config parser
19133
config = configparser.ConfigParser()

tests/test_main_github.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ def test_get_branch():
2828
"""
2929
test getting branch from environment or default
3030
"""
31-
# Unset defaults to master
31+
# Unset defaults to main
3232
os.environ["GITHUB_REF"] = ""
3333
os.putenv("GITHUB_REF", "")
3434
branch = get_branch()
35-
if branch != "master":
35+
if branch != "main":
3636
raise AssertionError
3737

3838
# Set both GitHub input variable and ref (ref takes priority)

urlchecker/client/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def get_parser():
5353
check.add_argument(
5454
"-b",
5555
"--branch",
56-
help="if cloning, specify a branch to use (defaults to master)",
57-
default="master",
56+
help="if cloning, specify a branch to use (defaults to main)",
57+
default="main",
5858
)
5959

6060
check.add_argument(
@@ -152,12 +152,15 @@ def get_parser():
152152

153153

154154
def main():
155-
"""main is the entrypoint urlchecker-python."""
155+
"""
156+
main is the entrypoint urlchecker-python.
157+
"""
156158

157159
parser = get_parser()
158160

159161
def help(return_code=0):
160-
"""print help, including the software version and active client
162+
"""
163+
print help, including the software version and active client
161164
and exit with return code.
162165
"""
163166

urlchecker/client/check.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
"""
2+
23
client/github.py: entrypoint for interaction with a GitHub repostiory.
34
Copyright (c) 2020-2022 Ayoub Malek and Vanessa Sochat
5+
6+
This source code is licensed under the terms of the MIT license.
7+
For a copy, see <https://opensource.org/licenses/MIT>.
8+
49
"""
510

611
import re
@@ -18,14 +23,14 @@
1823

1924
def main(args, extra):
2025
"""
21-
main entrypoint for running a check. We expect an args object with
26+
Main entrypoint for running a check. We expect an args object with
2227
arguments from the main client. From here we determine the path
2328
to parse (or GitHub url to clone) and call the main check function
2429
under main/check.py
2530
26-
Parameters:
27-
- args: the argparse ArgParser with parsed args
28-
- extra: extra arguments not handled by the parser
31+
Args:
32+
- args : the argparse ArgParser with parsed args
33+
- extra : extra arguments not handled by the parser
2934
"""
3035
path = args.path
3136

0 commit comments

Comments
 (0)