diff --git a/bigquery_etl/cli/query.py b/bigquery_etl/cli/query.py index 8a5eb9c5ec6..6d118d9755c 100644 --- a/bigquery_etl/cli/query.py +++ b/bigquery_etl/cli/query.py @@ -426,6 +426,8 @@ def info(ctx, name, sql_dir, project_id, cost, last_updated): ignore=["derived_view_schemas", "stable_views"], ) query_files = paths_matching_name_pattern(name, ctx.obj["TMP_DIR"], project_id) + if query_files == []: + raise click.ClickException(f"No queries matching `{name}` were found.") for query_file in query_files: query_file_path = Path(query_file) @@ -719,6 +721,8 @@ def backfill( ignore=["derived_view_schemas", "stable_views", "country_code_lookup"], ) query_files = paths_matching_name_pattern(name, ctx.obj["TMP_DIR"], project_id) + if query_files == []: + raise click.ClickException(f"No queries matching `{name}` were found.") for query_file in query_files: query_file_path = Path(query_file) @@ -895,6 +899,8 @@ def run( ignore=["derived_view_schemas", "stable_views", "country_code_lookup"], ) query_files = paths_matching_name_pattern(name, ctx.obj["TMP_DIR"], project_id) + if query_files == []: + raise click.ClickException(f"No queries matching `{name}` were found.") _run_query( query_files, @@ -1992,6 +1998,8 @@ def deploy( query_files = paths_matching_name_pattern( name, ctx.obj["TMP_DIR"], project_id, ["query.*"] ) + if not query_files: + raise click.ClickException(f"No queries matching `{name}` were found.") def _deploy(query_file): if respect_dryrun_skip and str(query_file) in DryRun.skipped_files(): @@ -2253,6 +2261,8 @@ def validate_schema( ignore=["derived_view_schemas", "stable_views"], ) query_files = paths_matching_name_pattern(name, ctx.obj["TMP_DIR"], project_id) + if query_files == []: + raise click.ClickException(f"No queries matching `{name}` were found.") _validate_schema = partial( _validate_schema_from_path, diff --git a/tests/test_entrypoint.py b/tests/test_entrypoint.py index e55f494799d..438c731ed18 100644 --- a/tests/test_entrypoint.py +++ b/tests/test_entrypoint.py @@ -131,5 +131,10 @@ def test_run_query_write_to_table( @pytest.mark.integration def test_run_query_no_query_file(self): - result = subprocess.check_output([ENTRYPOINT_SCRIPT, "query"]) - assert b"No files matching:" in result + with pytest.raises(subprocess.CalledProcessError) as e: + subprocess.run( + [ENTRYPOINT_SCRIPT, "query"], + check=True, + capture_output=True, + ) + assert b"No queries matching" in e.value.stderr