diff --git a/linkstatus/linkstatus.py b/linkstatus/linkstatus.py index 3bf2b41..3f2517d 100644 --- a/linkstatus/linkstatus.py +++ b/linkstatus/linkstatus.py @@ -68,7 +68,12 @@ def main(source, recursive, timeout, retry): down_count = 0 skip_count = 0 - files = all_files(source, recursive=recursive) + if source: + files = all_files(source, recursive=recursive) + else: + click.echo("Source Not Found") + click.echo("Run 'linkstatus --help' for more information.") + exit(1) for f in files: links = parse_file(f) @@ -114,18 +119,22 @@ def main(source, recursive, timeout, retry): ) # Print summary - columns = get_terminal_size().columns - click.echo("=" * columns) - click.echo(click.style("Links Status Summary".center(columns), bold=True)) - click.echo(click.style("Links UP: {}".format(up_count).center(columns), fg="green")) - click.echo(click.style("Links SKIP: {}".format(skip_count).center(columns), fg="blue")) - click.echo(click.style("Links DOWN: {}".format(down_count).center(columns), fg="red")) - - if exit_code == 1: - click.echo( - "Warning: Use `noqa` inline comment to skip link check. " - "like, response code 403 due to header restrictions etc..." - ) - - click.echo("=" * columns) + total_links = up_count + skip_count + down_count + + if total_links: + columns = get_terminal_size().columns + click.echo("=" * columns) + click.echo(click.style("Links Status Summary".center(columns), bold=True)) + click.echo(click.style("Links UP: {}".format(up_count).center(columns), fg="green")) + click.echo(click.style("Links SKIP: {}".format(skip_count).center(columns), fg="blue")) + click.echo(click.style("Links DOWN: {}".format(down_count).center(columns), fg="red")) + + if exit_code == 1: + click.echo( + "Warning: Use `noqa` inline comment to skip link check. " + "like, response code 403 due to header restrictions etc..." + ) + click.echo("=" * columns) + else: + click.echo("No link found") exit(exit_code) diff --git a/tests/test_linkstatus.py b/tests/test_linkstatus.py index 4f91df4..1360cca 100644 --- a/tests/test_linkstatus.py +++ b/tests/test_linkstatus.py @@ -1,6 +1,18 @@ import os +import subprocess + +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) def test_linkstatus_command(): - return_code = os.system("linkstatus") - assert return_code == 0 + result = subprocess.run("linkstatus", stdout=subprocess.PIPE) + assert result.returncode == 1 + assert "Source Not Found" in result.stdout.decode() + assert "Run 'linkstatus --help' for more information." in result.stdout.decode() + + +def test_linkstatus_command_with_source(): + src = os.path.join(BASE_DIR, "conftest.py") + result = subprocess.run(["linkstatus", src], stdout=subprocess.PIPE) + assert result.returncode == 0 + assert result.stdout.decode().strip() == "No link found"