8
8
9
9
import pytest
10
10
11
- import macaron
11
+ from macaron import MACARON_PATH
12
12
from macaron .errors import ConfigurationError , HeuristicAnalyzerValueError
13
13
from macaron .malware_analyzer .pypi_heuristics .heuristics import HeuristicResult
14
14
from macaron .malware_analyzer .pypi_heuristics .sourcecode .pypi_sourcecode_analyzer import PyPISourcecodeAnalyzer
15
15
16
+ RESOURCES_PATH = os .path .join (MACARON_PATH , "resources" )
17
+
16
18
17
19
def test_no_resources () -> None :
18
20
"""Test for when the semgrep rules can't be found, so error."""
@@ -25,22 +27,22 @@ def test_no_defaults_section(mock_defaults: MagicMock) -> None:
25
27
"""Test for when the heuristics.pypi in defaults isn't defined at all, so error."""
26
28
mock_defaults .has_section .side_effect = lambda _ : False
27
29
with pytest .raises (ConfigurationError ):
28
- _ = PyPISourcecodeAnalyzer (resources_path = os . path . join ( os . path . dirname ( macaron . __file__ ), "resources" ) )
30
+ _ = PyPISourcecodeAnalyzer (resources_path = RESOURCES_PATH )
29
31
30
32
31
33
@patch ("macaron.malware_analyzer.pypi_heuristics.sourcecode.pypi_sourcecode_analyzer.defaults" )
32
34
def test_no_custom_path (mock_defaults : MagicMock ) -> None :
33
35
"""Test for when a default path isn't provided, so the custom rule path should be None."""
34
36
mock_defaults .has_section .side_effect = lambda section : section == "heuristic.pypi"
35
37
mock_defaults .__getitem__ .side_effect = lambda _ : (MagicMock (get = MagicMock (return_value = None )))
36
- analyzer = PyPISourcecodeAnalyzer (resources_path = os . path . join ( os . path . dirname ( macaron . __file__ ), "resources" ) )
38
+ analyzer = PyPISourcecodeAnalyzer (resources_path = RESOURCES_PATH )
37
39
assert analyzer .custom_rule_path is None
38
40
39
41
mock_defaults .has_section .side_effect = lambda section : section == "heuristic.pypi"
40
42
mock_defaults .__getitem__ .side_effect = lambda section : (
41
43
MagicMock (get = MagicMock (return_value = "" if section == "heuristic.pypi" else None ))
42
44
)
43
- analyzer = PyPISourcecodeAnalyzer (resources_path = os . path . join ( os . path . dirname ( macaron . __file__ ), "resources" ) )
45
+ analyzer = PyPISourcecodeAnalyzer (resources_path = RESOURCES_PATH )
44
46
assert analyzer .custom_rule_path is None
45
47
46
48
@@ -52,7 +54,7 @@ def test_nonexistent_rule_path(mock_defaults: MagicMock) -> None:
52
54
MagicMock (get = MagicMock (return_value = "some_random_path" if section == "heuristic.pypi" else None ))
53
55
)
54
56
with pytest .raises (ConfigurationError ):
55
- _ = PyPISourcecodeAnalyzer (resources_path = os . path . join ( os . path . dirname ( macaron . __file__ ), "resources" ) )
57
+ _ = PyPISourcecodeAnalyzer (resources_path = RESOURCES_PATH )
56
58
57
59
58
60
@patch ("macaron.malware_analyzer.pypi_heuristics.sourcecode.pypi_sourcecode_analyzer.defaults" )
@@ -64,12 +66,12 @@ def test_invalid_custom_rules(mock_defaults: MagicMock) -> None:
64
66
MagicMock (get = MagicMock (return_value = os .path .abspath (__file__ ) if section == "heuristic.pypi" else None ))
65
67
)
66
68
with pytest .raises (ConfigurationError ):
67
- _ = PyPISourcecodeAnalyzer (resources_path = os . path . join ( os . path . dirname ( macaron . __file__ ), "resources" ) )
69
+ _ = PyPISourcecodeAnalyzer (resources_path = RESOURCES_PATH )
68
70
69
71
70
72
def test_no_sourcecode (pypi_package_json : MagicMock ) -> None :
71
73
"""Test for when there is no source code available, so error."""
72
- analyzer = PyPISourcecodeAnalyzer (resources_path = os . path . join ( os . path . dirname ( macaron . __file__ ), "resources" ) )
74
+ analyzer = PyPISourcecodeAnalyzer (resources_path = RESOURCES_PATH )
73
75
74
76
pypi_package_json .package_sourcecode_path = ""
75
77
@@ -103,7 +105,7 @@ def test_rules(
103
105
MagicMock (get = MagicMock (return_value = "" if section == "heuristic.pypi" else None ))
104
106
)
105
107
106
- analyzer = PyPISourcecodeAnalyzer (resources_path = os . path . join ( os . path . dirname ( macaron . __file__ ), "resources" ) )
108
+ analyzer = PyPISourcecodeAnalyzer (resources_path = RESOURCES_PATH )
107
109
108
110
pypi_package_json .package_sourcecode_path = sample_path
109
111
analyzer .default_rule_path = os .path .join (analyzer .default_rule_path , rule_file )
0 commit comments