Skip to content

Commit

Permalink
Merge pull request #20 from digitronik/tests
Browse files Browse the repository at this point in the history
[RFR] Introduce tests for linkstatus
  • Loading branch information
raukadah authored Dec 17, 2019
2 parents 7c03d7c + 2446fff commit b0ba5c5
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 10 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@ jobs:
python-version: ${{ matrix.python-version }}
architecture: x64

- name: Pre-Commit
- name: Setup Package and Install Devel Dependancies
run: |
python -m pip install pre-commit
pre-commit run -a
python -m pip install .
python -m pip install -Ur dev-requirements.txt
- name: Pre-Commit
run: pre-commit run -a

- name: Analysis (git diff)
if: failure()
run: git diff

- name: Unit Tests
run: py.test tests -v

- name: Build Package
if: matrix.python-version == '3.7' && github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags')
run: |
Expand Down
3 changes: 3 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pre-commit
pytest
ruamel.yaml
8 changes: 8 additions & 0 deletions linkstatus/linkstatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ def link_status(link, timeout):


def all_files(source, recursive=False):
"""Collect all provide file paths
Args:
source: file or directory name
recursive: search recursively directories
Returns:
list of files path
"""
files = []
for src in source:
if os.path.isdir(src):
Expand Down
36 changes: 36 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os

import pytest
from ruamel.yaml import safe_load

BASE_DIR = os.path.dirname(os.path.abspath(__file__))


@pytest.fixture(scope="session")
def data():
with open(os.path.join(BASE_DIR, "data.yaml"), "r") as f:
data = safe_load(f)
return data


@pytest.fixture(
params=["file", "files", "directory", "directories", "file-directory"], scope="module"
)
def sources(request):
SOURCES_TYPE = {
"file": [os.path.join(BASE_DIR, "data", "text_file")],
"files": [
os.path.join(BASE_DIR, "data", "text_file"),
os.path.join(BASE_DIR, "data", "recursive", "recursive_text_file.txt"),
],
"directory": [os.path.join(BASE_DIR, "data")],
"directories": [
os.path.join(BASE_DIR, "data"),
os.path.join(BASE_DIR, "data", "recursive"),
],
"file-directory": [
os.path.join(BASE_DIR, "data", "text_file"),
os.path.join(BASE_DIR, "data"),
],
}
return SOURCES_TYPE[request.param]
63 changes: 63 additions & 0 deletions tests/data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Mapping of dir and files as per links and status.

# Dirs and files with status
data:
parent:
files:
text_file:
'https://github.com/pythonpune/linkstatus':
status: True
line: 'L5'
'http://www.google.com':
status: True,
line: 'L6'
'https://example.com':
status: True,
line: 'L7'
'https://github.com/xyz_foo':
status: False
line: 'L8'
markdown_file.md:
'https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet':
status: True
line: 'L4'
'http://www.google.com':
status: True
line: 'L8'
'https://www.google.com':
status: True
line: 'L10'
'https://github.com/pythonpune/linkstatus':
status: True
line: 'L12'
'http://www.example.com':
status: True
line: 'L24'
'https://github.com/pythonpune/linkcheck':
status: False
line: 'L34'
'https://github.com//pythonpune/':
status: True
line: 'L39'
'http://<hostname>:<port>':
status: False
line: 'L41'
'https://<hostname>:<port>/pages':
status: False
line: 'L43'
recursive:
parent: "data"
files:
recursive_text_file.txt:
'https://github.com/pythonpune/linkstatus':
status: True
line: 'L5'
'http://www.google.com':
status: True,
line: 'L6'
'https://example.com':
status: True,
line: 'L7'
'https://github.com/xyz_foo':
status: False
line: 'L8'
15 changes: 9 additions & 6 deletions tests/dir/links_markdown.md → tests/data/markdown_file.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Markdown sample file

# Markdown sample test file
### Line number and status of link mapped in `tests/data/data.yaml` for testing.
## Links:
[source](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#links)

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

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

[arbitrary case-insensitive reference text]: https://www.mozilla.org <!--noqa-->
[arbitrary case-insensitive reference text]: https://www.mozilla.org

[1]: http://slashdot.org

[link text itself]: http://www.example.com

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

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

[broken link](https://github.com/pythonpune/linkstatus)
## Other link examples

https://github.com//pythonpune/

Expand Down
8 changes: 8 additions & 0 deletions tests/data/recursive/recursive_text_file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Example recursive text file:

Note: Line number and status of link mapped in `tests/data/data.yaml` for testing.

1. linkstatus: https://github.com/pythonpune/linkstatus
2. google: http://www.google.com
3. example: https://example.com
4. broken: https://github.com/xyz_foo
4 changes: 3 additions & 1 deletion tests/links_text → tests/data/text_file
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Links:
Example text file:

Note: Line number and status of link mapped in `tests/data/data.yaml` for testing.

1. linkstatus: https://github.com/pythonpune/linkstatus
2. google: http://www.google.com
Expand Down
30 changes: 30 additions & 0 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os

import pytest

from linkstatus.linkstatus import all_files


@pytest.mark.parametrize("recursive", [True, False], ids=["recursive", "non-recursive"])
def test_all_files(sources, recursive):
collected_files = all_files(sources, recursive=recursive)
expected_files = []

for source_entity in sources:
if os.path.isfile(source_entity):
expected_files.append(source_entity)
else:
inside_entities = [os.path.join(source_entity, e) for e in os.listdir(source_entity)]
inside_files = [f for f in inside_entities if os.path.isfile(f)]

if recursive:
for d in [d for d in inside_entities if os.path.isdir(d) and "__" not in d]:
files = [
os.path.join(d, f)
for f in os.listdir(d)
if os.path.isfile(os.path.join(d, f))
]
inside_files.extend(files)
expected_files.extend(inside_files)

assert set(collected_files) == set(expected_files)

0 comments on commit b0ba5c5

Please sign in to comment.