Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return exit code 1 if test failed (for use in test pipelines) #2

Merged
merged 1 commit into from
Aug 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions blinkcheck/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import copy
import fnmatch
import re
import sys
from pathlib import Path
from typing import Dict, Iterator, List

Expand All @@ -17,6 +18,9 @@
T_GREEN = "\033[92m"
T_END = "\033[0m"

# os.EX_* are only available on unix-like system
EXIT_TEST_FAILED = 1


class TestResult:
def __init__(self, url: str, code: int = 0, skip: bool = False, error: str = None):
Expand Down Expand Up @@ -74,8 +78,12 @@ def __init__(
# regex, otherwise, get the full match (the 0th group)
self._regex_group = 1 if self.regex.groups > 0 else 0

def check(self):
def check(self) -> int:
"""
Returns number of urls where test failed.
"""
all_urls = self._get_all_urls()
fails_counter = 0
for file, urls in all_urls.items():
printed_prolog = False

Expand All @@ -89,8 +97,12 @@ def check(self):

print(result)

if not result.ok:
fails_counter += 1

if printed_prolog:
print()
return fails_counter

def _get_all_urls(self) -> Dict[Path, List[str]]:
"""
Expand Down Expand Up @@ -150,7 +162,10 @@ def _ensure_schema(url: str) -> str:


def main():
parser = argparse.ArgumentParser(description="Check for dead links in all files")
parser = argparse.ArgumentParser(
description="Check for dead links in all files."
"\nReturns exit code 1 if the test of an url failed, else 0."
)
parser.add_argument(
"--root",
type=Path,
Expand Down Expand Up @@ -187,7 +202,8 @@ def main():
print()

lc = LinkChecker(**vars(args))
lc.check()
if lc.check() > 0:
sys.exit(EXIT_TEST_FAILED)


if __name__ == "__main__":
Expand Down