Skip to content

Commit

Permalink
Merge pull request #21 from par-tec/ioggstream-20
Browse files Browse the repository at this point in the history
Fix: #20. Refactor test directories
  • Loading branch information
DrPlumcake authored Nov 15, 2023
2 parents 515312b + 688c6e1 commit 3b34c06
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 41 deletions.
File renamed without changes.
10 changes: 5 additions & 5 deletions tests/json/bandit_error.json → tests/data/bandit_error.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"reason": "syntax error while parsing AST from file"
},
{
"filename": "tests/data/py2.py",
"filename": "tests/data/python-01/py2.py",
"reason": "syntax error while parsing AST from file"
}
],
Expand Down Expand Up @@ -43,7 +43,7 @@
"results": [
{
"code": "2 \n3 print(randint(0, 10))\n4 \n",
"filename": "tests/canary.py",
"filename": "tests/data/python-01/canary.py",
"issue_confidence": "HIGH",
"issue_severity": "LOW",
"issue_text": "Standard pseudo-random generators are not suitable for security/cryptographic purposes.",
Expand All @@ -57,7 +57,7 @@
},
{
"code": "4 \n5 password = \"secret\"\n6 \n7 a = eval(\"\"\"\n",
"filename": "tests/canary.py",
"filename": "tests/data/python-01/canary.py",
"issue_confidence": "MEDIUM",
"issue_severity": "LOW",
"issue_text": "Possible hardcoded password: 'secret'",
Expand All @@ -72,7 +72,7 @@
},
{
"code": "6 \n7 a = eval(\"\"\"\n8 3 + 2 \\\n9 + randint(0, 10)\n10 \n11 \"\"\")\n12 assert a\n",
"filename": "tests/canary.py",
"filename": "tests/data/python-01/canary.py",
"issue_confidence": "HIGH",
"issue_severity": "MEDIUM",
"issue_text": "Use of possibly insecure function - consider using safer ast.literal_eval.",
Expand All @@ -90,7 +90,7 @@
},
{
"code": "11 \"\"\")\n12 assert a\n",
"filename": "tests/canary.py",
"filename": "tests/data/python-01/canary.py",
"issue_confidence": "HIGH",
"issue_severity": "LOW",
"issue_text": "Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions tests/data/semgrep-01.annotations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Semgrep Comments",
"head_sha": "stuff",
"completed_at": "2023-11-09T15:29:33.821590Z",
"conclusion": "failure",
"output": {
"title": "Semgrep: ",
"summary": "Semgrep statistics: {\n \"Total_errors\": 1,\n \"Semgrep_Version\": \"1.34.0\",\n \"paths_scanned\": 36\n}",
"text": "<add --verbose for a list of skipped paths>",
"annotations": [
{
"path": ".github/workflows/test.yml",
"start_line": 31,
"end_line": 31,
"start_column": 114,
"end_column": 117,
"annotation_level": "warning",
"title": "Syntax error",
"message": " When parsing a snippet as Bash for metavariable-pattern in rule 'yaml.github-actions.security.curl-eval.curl-eval'"
}
]
}
}
File renamed without changes.
10 changes: 5 additions & 5 deletions tests/test_bandit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
from parse_scripts.util import json_load

TEST_DIR = Path(__file__).parent
JSON_DIR = TEST_DIR / "json"
DATA_DIR = TEST_DIR / "data"


def test_errors():
results = json_load(JSON_DIR / "bandit_error.json")
results = json_load(DATA_DIR / "bandit_error.json")
errors = [bandit.bandit_error(error) for error in results["errors"]]
assert errors[0]["path"] == "LICENSE"
assert errors[1] == {
"path": "tests/data/py2.py",
"path": "tests/data/python-01/py2.py",
"start_line": 2,
"end_line": 2,
"annotation_level": "failure",
Expand All @@ -22,13 +22,13 @@ def test_errors():


def test_annotations():
data = json_load(JSON_DIR / "bandit.json")
data = json_load(DATA_DIR / "bandit.json")
annotations = bandit.bandit_annotations(data)
assert annotations[0]["path"] == "canary.py"
assert annotations[0]["start_line"] == 3


def test_run_check():
data = json_load(JSON_DIR / "bandit.json")
data = json_load(DATA_DIR / "bandit.json")
run_check_body = bandit.bandit_run_check(data)
assert run_check_body["conclusion"] == "failure"
16 changes: 12 additions & 4 deletions tests/test_checkov.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
from pathlib import Path

import pytest

from parse_scripts import checkov
from parse_scripts.util import json_load

TEST_DIR = Path(__file__).parent
JSON_DIR = TEST_DIR / "json"
DATA_DIR = TEST_DIR / "data"


def test_parse():
expected_comments = json_load(DATA_DIR / "checkov_input.json")
data = json_load(JSON_DIR / "checkov.json")
@pytest.mark.parametrize(
"infile,expected",
[
(f, f.with_suffix(".annotations.json"))
for f in DATA_DIR.glob("checkov-[0-9][0-9].json")
],
)
def test_parse(infile, expected):
expected_comments = json_load(expected)
data = json_load(infile)
actual_comments = checkov.checkov_results(log=data, github_sha="stuff")
actual_comments["completed_at"] = "00:00"
assert expected_comments == actual_comments
40 changes: 13 additions & 27 deletions tests/test_semgrep.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
from pathlib import Path

import pytest

import parse_scripts.semgrep
from parse_scripts.util import json_load

DATA_DIR = Path(__file__).parent / "json"

expected_results = {
"name": "Semgrep Comments",
"head_sha": "stuff",
"completed_at": "2023-11-09T15:29:33.821590Z",
"conclusion": "failure",
"output": {
"title": "Semgrep: ",
"summary": 'Semgrep statistics: {\n "Total_errors": 1,\n "Semgrep_Version": "1.34.0",\n "paths_scanned": 36\n}',
"text": "<add --verbose for a list of skipped paths>",
"annotations": [
{
"path": ".github/workflows/test.yml",
"start_line": 31,
"end_line": 31,
"start_column": 114,
"end_column": 117,
"annotation_level": "warning",
"title": "Syntax error",
"message": " When parsing a snippet as Bash for metavariable-pattern in rule 'yaml.github-actions.security.curl-eval.curl-eval'",
}
],
},
}
DATA_DIR = Path(__file__).parent / "data"


def test_parse_data():
data = json_load(DATA_DIR / "semgrep.json")
@pytest.mark.parametrize(
"infile,expected",
[
(f, f.with_suffix(".annotations.json"))
for f in DATA_DIR.glob("semgrep-[0-9][0-9].json")
],
)
def test_parse_data(infile, expected):
data = json_load(infile)
expected_results = json_load(expected)
actual_results = parse_scripts.semgrep.parse_data(data, "stuff")
actual_results["completed_at"] = "2023-11-09T15:29:33.821590Z"
assert expected_results == actual_results

0 comments on commit 3b34c06

Please sign in to comment.