Skip to content

Commit

Permalink
Use OutputFormat in tests instead of strings (semgrep#3051)
Browse files Browse the repository at this point in the history
  • Loading branch information
mschwager authored May 6, 2021
1 parent ecadf44 commit 5c6b03f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 27 deletions.
10 changes: 5 additions & 5 deletions semgrep/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ You can add any additional CLI flag you'd like like so:

To call semgrep without the `--json` flag:

```run_semgrep_in_tmp("r2c/python", output_format="normal")```
```run_semgrep_in_tmp("r2c/python", output_format=OutputFormat.TEXT)```

To call semgrep without the `--junit-xml` flag:
To call semgrep with the `--junit-xml` flag:

```run_semgrep_in_tmp("r2c/python", output_format="junit-xml")```
```run_semgrep_in_tmp("r2c/python", output_format=OutputFormat.JUNIT_XML)```

To call semgrep with `--sarif` instead of `--json`:
To call semgrep with the `--sarif` flag instead of `--json`:

```run_semgrep_in_tmp("r2c/python", output_format="sarif")```
```run_semgrep_in_tmp("r2c/python", output_format=OutputFormat.SARIF)```
13 changes: 7 additions & 6 deletions semgrep/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pytest

from . import public_repos
from semgrep.constants import OutputFormat

TESTS_PATH = Path(__file__).parent

Expand Down Expand Up @@ -61,7 +62,7 @@ def _run_semgrep(
*,
target_name: str = "basic",
options: Optional[List[Union[str, Path]]] = None,
output_format: str = "json",
output_format: OutputFormat = OutputFormat.JSON,
stderr: bool = False,
strict: bool = True,
) -> str:
Expand All @@ -70,7 +71,7 @@ def _run_semgrep(
:param config: what to pass as --config's value
:param target_name: which directory within ./e2e/targets/ to scan
:param options: additional CLI flags to add
:param output_format: which format to use, valid options are normal, json, junit_xml and sarif
:param output_format: which format to use
:param stderr: whether to merge stderr into the returned string
"""
if options is None:
Expand All @@ -88,11 +89,11 @@ def _run_semgrep(
else:
options.extend(["--config", config])

if output_format == "json":
if output_format == OutputFormat.JSON:
options.append("--json")
elif output_format == "junit-xml":
elif output_format == OutputFormat.JUNIT_XML:
options.append("--junit-xml")
elif output_format == "sarif":
elif output_format == OutputFormat.SARIF:
options.append("--sarif")

output = subprocess.check_output(
Expand All @@ -101,7 +102,7 @@ def _run_semgrep(
stderr=subprocess.STDOUT if stderr else subprocess.PIPE,
)

if output_format in {"json", "sarif"} and not stderr:
if output_format.is_json() and not stderr:
output = _clean_output_json(output)

return output
Expand Down
18 changes: 10 additions & 8 deletions semgrep/tests/e2e/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

from semgrep import __VERSION__
from semgrep.constants import OutputFormat

GITHUB_TEST_GIST_URL = (
"https://raw.githubusercontent.com/returntocorp/semgrep-rules/develop/template.yaml"
Expand Down Expand Up @@ -73,7 +74,8 @@ def test_basic_rule__absolute(run_semgrep_in_tmp, snapshot):

def test_terminal_output(run_semgrep_in_tmp, snapshot):
snapshot.assert_match(
run_semgrep_in_tmp("rules/eqeq.yaml", output_format="normal"), "output.txt"
run_semgrep_in_tmp("rules/eqeq.yaml", output_format=OutputFormat.TEXT),
"output.txt",
)


Expand All @@ -85,7 +87,7 @@ def test_multiline(run_semgrep_in_tmp, snapshot):


def test_junit_xml_output(run_semgrep_in_tmp, snapshot):
output = run_semgrep_in_tmp("rules/eqeq.yaml", output_format="junit-xml")
output = run_semgrep_in_tmp("rules/eqeq.yaml", output_format=OutputFormat.JUNIT_XML)
result = etree_to_dict(cElementTree.XML(output))

filename = snapshot.snapshot_dir / "results.xml"
Expand All @@ -96,7 +98,7 @@ def test_junit_xml_output(run_semgrep_in_tmp, snapshot):

def test_sarif_output(run_semgrep_in_tmp, snapshot):
sarif_output = json.loads(
run_semgrep_in_tmp("rules/eqeq.yaml", output_format="sarif")
run_semgrep_in_tmp("rules/eqeq.yaml", output_format=OutputFormat.SARIF)
)

# rules are logically a set so the JSON list's order doesn't matter
Expand Down Expand Up @@ -142,7 +144,7 @@ def test_hidden_rule__implicit(run_semgrep_in_tmp, snapshot):
snapshot.assert_match(excinfo.value.stdout, "error.json")

with pytest.raises(CalledProcessError) as excinfo:
run_semgrep_in_tmp("rules/hidden", output_format="normal")
run_semgrep_in_tmp("rules/hidden", output_format=OutputFormat.TEXT)
assert excinfo.value.returncode == 7
snapshot.assert_match(excinfo.value.stderr, "error.txt")

Expand Down Expand Up @@ -353,7 +355,7 @@ def test_spacegrep_timeout(run_semgrep_in_tmp, snapshot):
"--timeout",
"1",
],
output_format="text",
output_format=OutputFormat.TEXT,
stderr=True,
strict=False, # don't fail due to timeout
),
Expand All @@ -375,7 +377,7 @@ def test_max_memory(run_semgrep_in_tmp, snapshot):
snapshot.assert_match(
run_semgrep_in_tmp(
"rules/long.yaml",
output_format="normal",
output_format=OutputFormat.TEXT,
options=["--verbose", "--max-memory", "1"],
target_name="equivalence",
strict=False,
Expand All @@ -399,7 +401,7 @@ def test_timeout_threshold(run_semgrep_in_tmp, snapshot):
snapshot.assert_match(
run_semgrep_in_tmp(
"rules/multiple-long.yaml",
output_format="normal",
output_format=OutputFormat.TEXT,
options=["--verbose", "--timeout", "1", "--timeout-threshold", "1"],
target_name="equivalence",
strict=False,
Expand All @@ -411,7 +413,7 @@ def test_timeout_threshold(run_semgrep_in_tmp, snapshot):
snapshot.assert_match(
run_semgrep_in_tmp(
"rules/multiple-long.yaml",
output_format="normal",
output_format=OutputFormat.TEXT,
options=["--verbose", "--timeout", "1", "--timeout-threshold", "2"],
target_name="equivalence",
strict=False,
Expand Down
11 changes: 7 additions & 4 deletions semgrep/tests/e2e/test_cli_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from semgrep.constants import OutputFormat


def test_cli_test_basic(run_semgrep_in_tmp, snapshot):
results = run_semgrep_in_tmp(
"rules/cli_test/basic/",
options=["--test"],
target_name="cli_test/basic/",
output_format="json",
output_format=OutputFormat.JSON,
)
snapshot.assert_match(
results,
Expand All @@ -16,7 +19,7 @@ def test_cli_test_yaml_language(run_semgrep_in_tmp, snapshot):
"rules/cli_test/language/",
options=["--test"],
target_name="cli_test/language/",
output_format="json",
output_format=OutputFormat.JSON,
)
snapshot.assert_match(
results,
Expand All @@ -29,7 +32,7 @@ def test_cli_test_suffixes(run_semgrep_in_tmp, snapshot):
"rules/cli_test/suffixes/",
options=["--test"],
target_name="cli_test/suffixes/",
output_format="json",
output_format=OutputFormat.JSON,
)
snapshot.assert_match(
results,
Expand All @@ -42,7 +45,7 @@ def test_cli_test_multiline_annotations(run_semgrep_in_tmp, snapshot):
"rules/cli_test/multiple_annotations/",
options=["--test"],
target_name="cli_test/multiple_annotations/",
output_format="text",
output_format=OutputFormat.TEXT,
)
snapshot.assert_match(
results,
Expand Down
5 changes: 4 additions & 1 deletion semgrep/tests/e2e/test_debugging_json.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from semgrep.constants import OutputFormat


def test_debugging_json(run_semgrep_in_tmp, snapshot):
snapshot.assert_match(
run_semgrep_in_tmp(
"rules/eqeq.yaml",
target_name="basic/stupid.py",
options=["--debug"],
output_format="json",
output_format=OutputFormat.JSON,
),
"results.json",
)
6 changes: 4 additions & 2 deletions semgrep/tests/e2e/test_rule_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest

from ..conftest import TESTS_PATH
from semgrep.constants import OutputFormat

syntax_dir = TESTS_PATH / "e2e" / "rules" / "syntax"
syntax_passes = [f.with_suffix("").name for f in syntax_dir.glob("good*.yaml")]
Expand Down Expand Up @@ -47,7 +48,7 @@ def test_rule_parser__failure__error_messages(run_semgrep_in_tmp, snapshot, file
run_semgrep_in_tmp(
f"rules/syntax/{filename}.yaml",
options=["--force-color"],
output_format="normal",
output_format=OutputFormat.TEXT,
)

snapshot.assert_match(
Expand All @@ -71,6 +72,7 @@ def test_rule_parser_cli_pattern(run_semgrep_in_tmp, snapshot):
# Check pretty print output
with pytest.raises(CalledProcessError) as excinfo:
run_semgrep_in_tmp(
options=["-e", "#include<asdf><<>>><$X>", "-l", "c"], output_format="normal"
options=["-e", "#include<asdf><<>>><$X>", "-l", "c"],
output_format=OutputFormat.TEXT,
)
snapshot.assert_match(excinfo.value.stderr, "error.txt")
4 changes: 3 additions & 1 deletion semgrep/tests/e2e/test_semgrep_core_parse_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import pytest

from semgrep.constants import OutputFormat


@pytest.mark.parametrize(
"filename",
Expand All @@ -15,7 +17,7 @@ def test_rule_parser__failure__error_messages(run_semgrep_in_tmp, snapshot, file
config="rules/eqeq-python.yaml",
target_name=f"bad/{filename}",
options=["--verbose"],
output_format="text",
output_format=OutputFormat.TEXT,
stderr=True,
)
snapshot.assert_match(excinfo.value.output, "error.txt")

0 comments on commit 5c6b03f

Please sign in to comment.