Skip to content

Commit

Permalink
return code 22 if any link bad
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Feb 28, 2021
1 parent 259c8c8 commit bfbe3ad
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ pip install -e linkchecker-markdown

## Usage

The static site generator does NOT have to be running for these tests--it looks at the .md files directly.
The static site generator does NOT have to be running for these tests.
This program looks at the Markdown .md files directly.

If any local or remote links are determined to be missing, the following happens:

* the file containing the bad link and the link is printed to "stdout"
* the program will exit with code 22 instead of 0 after all files are checked

The bad links are printed to stdout since the normal operation of this program is to check for errors.
Due to the fast, concurrent checking and numerous pages checked, there may be diagnostics printed to stderr.
That way library error messages can be kept separate from the missing page locations printed on stdout.


The examples assume webpage Markdown files have top-level directory ~/web.
*If using the linkchecker on an MkDocs documentation project, Markdown files
are typically found in a `~/docs` directory.*
Expand Down
8 changes: 3 additions & 5 deletions examples/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ if not shutil.which("linkcheckMarkdown"):

ok = True
for p in ["content/posts"]:
stdout = subprocess.check_output(
["linkcheckMarkdown", "-local", p], universal_newlines=True
)
if stdout:
print(stdout)
out = subprocess.check_output(["linkcheckMarkdown", "-local", p], text=True)
if out:
print(out)
ok = False

if not ok:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = linkcheckmd
version = 1.3.1
version = 1.4.0
author = Michael Hirsch, Ph.D.
author_email = [email protected]
url = https://github.com/scivision/linkchecker-markdown
Expand Down
7 changes: 6 additions & 1 deletion src/linkcheckmd/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def main():
logging.basicConfig(level=logging.INFO)

tic = time.monotonic()
check_links(
bad = check_links(
P.path,
ext=P.ext,
domain=P.domain,
Expand All @@ -51,6 +51,11 @@ def main():

print(f"{time.monotonic() - tic:0.3} seconds to check links")

if bad:
# using 22 following cURL
# https://everything.curl.dev/usingcurl/returns
raise SystemExit(22)


if __name__ == "__main__":
main()
10 changes: 5 additions & 5 deletions src/linkcheckmd/tests/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ def test_mod(use_async):


@pytest.mark.parametrize("use_async", [True, False])
def test_script(use_async):
def test_script(use_async, capfd):

args = []
if not use_async:
pytest.importorskip("requests", reason="Synchronous requires requests")
args.append("--sync")

with importlib.resources.path("linkcheckmd.tests", "badlink.md") as file:
ret = subprocess.check_output(
["linkcheckMarkdown", str(file), "github.invalid"] + args, text=True
)
assert "github.invalid" in ret
ret = subprocess.run(["linkcheckMarkdown", str(file), "github.invalid"] + args, text=True)

assert ret.returncode == 22
assert "github.invalid" in capfd.readouterr().out

0 comments on commit bfbe3ad

Please sign in to comment.