Skip to content

Commit b0ba5c5

Browse files
authored
Merge pull request #20 from digitronik/tests
[RFR] Introduce tests for linkstatus
2 parents 7c03d7c + 2446fff commit b0ba5c5

9 files changed

Lines changed: 169 additions & 10 deletions

File tree

.github/workflows/main.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@ jobs:
1919
python-version: ${{ matrix.python-version }}
2020
architecture: x64
2121

22-
- name: Pre-Commit
22+
- name: Setup Package and Install Devel Dependancies
2323
run: |
24-
python -m pip install pre-commit
25-
pre-commit run -a
24+
python -m pip install .
25+
python -m pip install -Ur dev-requirements.txt
26+
27+
- name: Pre-Commit
28+
run: pre-commit run -a
2629

2730
- name: Analysis (git diff)
2831
if: failure()
2932
run: git diff
3033

34+
- name: Unit Tests
35+
run: py.test tests -v
36+
3137
- name: Build Package
3238
if: matrix.python-version == '3.7' && github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags')
3339
run: |

dev-requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pre-commit
2+
pytest
3+
ruamel.yaml

linkstatus/linkstatus.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ def link_status(link, timeout):
3434

3535

3636
def all_files(source, recursive=False):
37+
"""Collect all provide file paths
38+
Args:
39+
source: file or directory name
40+
recursive: search recursively directories
41+
42+
Returns:
43+
list of files path
44+
"""
3745
files = []
3846
for src in source:
3947
if os.path.isdir(src):

tests/conftest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
3+
import pytest
4+
from ruamel.yaml import safe_load
5+
6+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
7+
8+
9+
@pytest.fixture(scope="session")
10+
def data():
11+
with open(os.path.join(BASE_DIR, "data.yaml"), "r") as f:
12+
data = safe_load(f)
13+
return data
14+
15+
16+
@pytest.fixture(
17+
params=["file", "files", "directory", "directories", "file-directory"], scope="module"
18+
)
19+
def sources(request):
20+
SOURCES_TYPE = {
21+
"file": [os.path.join(BASE_DIR, "data", "text_file")],
22+
"files": [
23+
os.path.join(BASE_DIR, "data", "text_file"),
24+
os.path.join(BASE_DIR, "data", "recursive", "recursive_text_file.txt"),
25+
],
26+
"directory": [os.path.join(BASE_DIR, "data")],
27+
"directories": [
28+
os.path.join(BASE_DIR, "data"),
29+
os.path.join(BASE_DIR, "data", "recursive"),
30+
],
31+
"file-directory": [
32+
os.path.join(BASE_DIR, "data", "text_file"),
33+
os.path.join(BASE_DIR, "data"),
34+
],
35+
}
36+
return SOURCES_TYPE[request.param]

tests/data.yaml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Mapping of dir and files as per links and status.
2+
3+
# Dirs and files with status
4+
data:
5+
parent:
6+
files:
7+
text_file:
8+
'https://github.com/pythonpune/linkstatus':
9+
status: True
10+
line: 'L5'
11+
'http://www.google.com':
12+
status: True,
13+
line: 'L6'
14+
'https://example.com':
15+
status: True,
16+
line: 'L7'
17+
'https://github.com/xyz_foo':
18+
status: False
19+
line: 'L8'
20+
markdown_file.md:
21+
'https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet':
22+
status: True
23+
line: 'L4'
24+
'http://www.google.com':
25+
status: True
26+
line: 'L8'
27+
'https://www.google.com':
28+
status: True
29+
line: 'L10'
30+
'https://github.com/pythonpune/linkstatus':
31+
status: True
32+
line: 'L12'
33+
'http://www.example.com':
34+
status: True
35+
line: 'L24'
36+
'https://github.com/pythonpune/linkcheck':
37+
status: False
38+
line: 'L34'
39+
'https://github.com//pythonpune/':
40+
status: True
41+
line: 'L39'
42+
'http://<hostname>:<port>':
43+
status: False
44+
line: 'L41'
45+
'https://<hostname>:<port>/pages':
46+
status: False
47+
line: 'L43'
48+
recursive:
49+
parent: "data"
50+
files:
51+
recursive_text_file.txt:
52+
'https://github.com/pythonpune/linkstatus':
53+
status: True
54+
line: 'L5'
55+
'http://www.google.com':
56+
status: True,
57+
line: 'L6'
58+
'https://example.com':
59+
status: True,
60+
line: 'L7'
61+
'https://github.com/xyz_foo':
62+
status: False
63+
line: 'L8'
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Markdown sample file
2-
1+
# Markdown sample test file
2+
### Line number and status of link mapped in `tests/data/data.yaml` for testing.
33
## Links:
44
[source](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#links)
55

@@ -25,13 +25,16 @@ http://www.example.com.
2525

2626
Some text to show that the reference links can follow later.
2727

28-
[arbitrary case-insensitive reference text]: https://www.mozilla.org <!--noqa-->
28+
[arbitrary case-insensitive reference text]: https://www.mozilla.org
29+
30+
[1]: http://slashdot.org
31+
32+
[link text itself]: http://www.example.com
2933

30-
[1]: http://slashdot.org <!--noqa-->
34+
[broken link](https://github.com/pythonpune/linkcheck)
3135

32-
[link text itself]: http://www.reddit.com <!--noqa-->
3336

34-
[broken link](https://github.com/pythonpune/linkstatus)
37+
## Other link examples
3538

3639
https://github.com//pythonpune/
3740

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Example recursive text file:
2+
3+
Note: Line number and status of link mapped in `tests/data/data.yaml` for testing.
4+
5+
1. linkstatus: https://github.com/pythonpune/linkstatus
6+
2. google: http://www.google.com
7+
3. example: https://example.com
8+
4. broken: https://github.com/xyz_foo
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Links:
1+
Example text file:
2+
3+
Note: Line number and status of link mapped in `tests/data/data.yaml` for testing.
24

35
1. linkstatus: https://github.com/pythonpune/linkstatus
46
2. google: http://www.google.com

tests/test_methods.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
3+
import pytest
4+
5+
from linkstatus.linkstatus import all_files
6+
7+
8+
@pytest.mark.parametrize("recursive", [True, False], ids=["recursive", "non-recursive"])
9+
def test_all_files(sources, recursive):
10+
collected_files = all_files(sources, recursive=recursive)
11+
expected_files = []
12+
13+
for source_entity in sources:
14+
if os.path.isfile(source_entity):
15+
expected_files.append(source_entity)
16+
else:
17+
inside_entities = [os.path.join(source_entity, e) for e in os.listdir(source_entity)]
18+
inside_files = [f for f in inside_entities if os.path.isfile(f)]
19+
20+
if recursive:
21+
for d in [d for d in inside_entities if os.path.isdir(d) and "__" not in d]:
22+
files = [
23+
os.path.join(d, f)
24+
for f in os.listdir(d)
25+
if os.path.isfile(os.path.join(d, f))
26+
]
27+
inside_files.extend(files)
28+
expected_files.extend(inside_files)
29+
30+
assert set(collected_files) == set(expected_files)

0 commit comments

Comments
 (0)