Skip to content

Commit

Permalink
Merge pull request #2 from fphammerle/exit-code
Browse files Browse the repository at this point in the history
return exit code 1 if test failed (for use in test pipelines)
  • Loading branch information
blu3r4y authored Aug 29, 2021
2 parents f7ec1ce + ec04133 commit 0ca6a5b
Showing 1 changed file with 19 additions and 3 deletions.
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

0 comments on commit 0ca6a5b

Please sign in to comment.