From 460a2574b41df71455d8e6b529e56fb59c324fe1 Mon Sep 17 00:00:00 2001 From: Scott Esbrandt Date: Thu, 4 Apr 2024 08:05:51 -0400 Subject: [PATCH] exit with error when specified target is not found in source deps --- bazel2snyk/cli.py | 6 ++++++ bazel2snyk/test/fixtures/__init__.py | 14 ++++++++++++++ bazel2snyk/test/test_cli.py | 9 +++++++++ 3 files changed, 29 insertions(+) diff --git a/bazel2snyk/cli.py b/bazel2snyk/cli.py index 71b8c4a..4da6225 100755 --- a/bazel2snyk/cli.py +++ b/bazel2snyk/cli.py @@ -284,6 +284,12 @@ def main( bazel2snyk.bazel_to_depgraph(parent_node_id=bazel_target, depth=0) + if len(bazel2snyk.dep_graph.graph()["depGraph"]["graph"]["nodes"]) <= 1: + logger.error( + f"No {package_source} dependencies found for given target, please verify --bazel-target exists in the source data" + ) + sys.exit(2) + if prune_all: logger.info("Pruning graph ...") time.sleep(2) diff --git a/bazel2snyk/test/fixtures/__init__.py b/bazel2snyk/test/fixtures/__init__.py index f821201..92f13db 100644 --- a/bazel2snyk/test/fixtures/__init__.py +++ b/bazel2snyk/test/fixtures/__init__.py @@ -27,6 +27,20 @@ "abcdefg", ] +pip_args["bad_target"] = [ + # "--debug", + "--print-deps", + "--package-source", + "pip", + "--bazel-deps-xml", + f"{pip_fixtures['pip']}", + "--bazel-target", + "//snyk/cli:main", + "print-graph", + "--snyk-org-id", + "abcdefg", +] + pip_args["print_graph"] = [ # "--debug", "--print-deps", diff --git a/bazel2snyk/test/test_cli.py b/bazel2snyk/test/test_cli.py index ff205d2..160cc68 100644 --- a/bazel2snyk/test/test_cli.py +++ b/bazel2snyk/test/test_cli.py @@ -14,6 +14,15 @@ def test_bad_args(): assert result.exit_code == 2 +def test_bad_target(): + """ + Test for target not found in source + + """ + result = runner.invoke(cli, pip_args["bad_target"]) + assert result.exit_code == 2 + + def test_pip_command_print_graph(): """ Test for printing the dep graph