Skip to content

Commit aaee57a

Browse files
committed
test: replaced Config error with a warning, and catch it analyze so offline tests can be run
Signed-off-by: Carl Flottmann <[email protected]>
1 parent 905ffb0 commit aaee57a

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/macaron/malware_analyzer/pypi_heuristics/sourcecode/pypi_sourcecode_analyzer.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,18 @@ def _load_defaults(self, resources_path: str) -> tuple[str, str | None, set[str]
131131
]
132132
try:
133133
process = subprocess.run(semgrep_commands, check=True, capture_output=True) # nosec B603
134+
if process.returncode != 0:
135+
warning_msg = (
136+
f"Error running semgrep validation on {custom_rule_path} with arguments" f" {process.args}."
137+
)
138+
logger.warning(warning_msg)
139+
134140
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as semgrep_error:
135-
error_msg = (
141+
warning_msg = (
136142
f"Unable to run semgrep validation on {custom_rule_path} with arguments "
137143
f"{semgrep_commands}: {semgrep_error}."
138144
)
139-
logger.debug(error_msg)
140-
raise ConfigurationError(error_msg) from semgrep_error
141-
142-
if process.returncode != 0:
143-
error_msg = f"Error running semgrep validation on {custom_rule_path} with arguments" f" {process.args}."
144-
logger.debug(error_msg)
145-
raise ConfigurationError(error_msg)
145+
logger.warning(warning_msg)
146146

147147
logger.debug("Including custom ruleset from %s.", custom_rule_path)
148148

tests/malware_analyzer/pypi/test_pypi_sourcecode_analyzer.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test_nonexistent_rule_path(mock_defaults: MagicMock) -> None:
9191

9292

9393
@patch("macaron.malware_analyzer.pypi_heuristics.sourcecode.pypi_sourcecode_analyzer.defaults")
94-
def test_invalid_custom_rules(mock_defaults: MagicMock) -> None:
94+
def test_invalid_custom_rules(mock_defaults: MagicMock, pypi_package_json: MagicMock) -> None:
9595
"""Test for when the provided file is not a valid semgrep rule, so error,"""
9696
# Use this file as an invalid semgrep rule as it is most definitely not a semgrep rule, and does exist.
9797
defaults = {
@@ -103,8 +103,14 @@ def test_invalid_custom_rules(mock_defaults: MagicMock) -> None:
103103
mock_defaults.has_section.side_effect = lambda section: section == "heuristic.pypi"
104104
mock_defaults.__getitem__.side_effect = lambda section: sub_section if section == "heuristic.pypi" else None
105105

106-
with pytest.raises(ConfigurationError):
107-
_ = PyPISourcecodeAnalyzer(resources_path=RESOURCES_PATH)
106+
analyzer = PyPISourcecodeAnalyzer(resources_path=RESOURCES_PATH)
107+
pypi_package_json.package_sourcecode_path = os.path.join(
108+
os.path.dirname(os.path.abspath(__file__)), "resources", "sourcecode_samples"
109+
)
110+
111+
# Semgrep should fail to run when we launch analysis
112+
with pytest.raises(HeuristicAnalyzerValueError):
113+
_ = analyzer.analyze(pypi_package_json)
108114

109115

110116
@patch("macaron.malware_analyzer.pypi_heuristics.sourcecode.pypi_sourcecode_analyzer.defaults")

0 commit comments

Comments
 (0)