Skip to content

Commit

Permalink
Fix logging of headers to stderr when --quiet is on (#7134)
Browse files Browse the repository at this point in the history
  • Loading branch information
underyx committed Feb 13, 2023
1 parent c038f4b commit 5aefc01
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
3 changes: 3 additions & 0 deletions changelog.d/quiet-please.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The 1.11.0 release started printing log lines to stderr even when --quiet was on,
making it impossible to get well-formed JSON output when mixing stdout and stderr.
These lines are now gone, and output is again restricted to just scan results.
8 changes: 7 additions & 1 deletion cli/src/semgrep/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from attr import define

from semgrep.console import console
from semgrep.env import Env


Expand Down Expand Up @@ -35,7 +36,12 @@ def configure(
quiet: bool = True,
force_color: bool = False,
) -> None:
"""Set the relevant logging levels"""
"""Set the relevant logging levels.
Affects also the configuration of the rich console."""

console.quiet = quiet

# Assumes only one of verbose, debug, quiet is True
logger = logging.getLogger("semgrep")
logger.handlers = [] # Reset to no handlers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ Findings:
=== end of stdout - plain

=== stderr - plain


╭─────────╮
│ Results │
╰─────────╯



╭──────────────╮
│ Scan Summary │
╰──────────────╯


=== end of stderr - plain

Expand All @@ -61,17 +49,5 @@ Findings:
=== end of stdout - color

=== stderr - color


╭─────────╮
│ Results │
╰─────────╯



╭──────────────╮
│ Scan Summary │
╰──────────────╯


=== end of stderr - color
19 changes: 18 additions & 1 deletion cli/tests/e2e/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ def test_yaml_metavariables(run_semgrep_in_tmp, snapshot):
snapshot.assert_match(stdout, "report.json")


@pytest.mark.quick
def test_quiet_mode_has_empty_stderr(run_semgrep_in_tmp, snapshot):
"""
Test that quiet mode doesn't print anything to stderr.
This is because some contexts e.g. Kubernetes jobs force-mix stdout and stderr,
and --quiet is the only way to get valid JSON output in that case.
"""
stdout, stderr = run_semgrep_in_tmp(
"rules/yaml_key.yaml",
target_name="yaml/target.yaml",
output_format=OutputFormat.JSON,
options=["--quiet"],
)
assert stderr == ""
json.loads(stdout) # stdout must be parseable JSON


# junit-xml is tested in a test_junit_xml_output due to ambiguous XML attribute ordering
@pytest.mark.kinda_slow
@pytest.mark.parametrize(
Expand Down Expand Up @@ -261,7 +279,6 @@ def test_sarif_output_with_nosemgrep_and_error(run_semgrep_in_tmp, snapshot):

@pytest.mark.kinda_slow
def test_sarif_output_with_autofix(run_semgrep_in_tmp, snapshot):

snapshot.assert_match(
run_semgrep_in_tmp(
"rules/autofix/autofix.yaml",
Expand Down

0 comments on commit 5aefc01

Please sign in to comment.