Skip to content

Commit bf74285

Browse files
committed
Add tests
1 parent ce8d2d3 commit bf74285

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tests/test_local_python_executor.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from smolagents.local_python_executor import (
2525
InterpreterError,
2626
PrintContainer,
27+
check_module_authorized,
2728
evaluate_python_code,
2829
fix_final_answer_code,
2930
get_safe_module,
@@ -1139,3 +1140,39 @@ def test_len(self):
11391140
pc = PrintContainer()
11401141
pc.append("Hello")
11411142
assert len(pc) == 5
1143+
1144+
1145+
@pytest.mark.parametrize(
1146+
"module,authorized_imports,expected",
1147+
[
1148+
("os", ["*"], True),
1149+
("AnyModule", ["*"], True),
1150+
("os", ["os"], True),
1151+
("AnyModule", ["AnyModule"], True),
1152+
("Module.os", ["Module"], False),
1153+
("Module.os", ["Module", "os"], True),
1154+
("os.path", ["os"], True),
1155+
("os", ["os.path"], False),
1156+
],
1157+
)
1158+
def test_check_module_authorized(module: str, authorized_imports: list[str], expected: bool):
1159+
dangerous_patterns = (
1160+
"_os",
1161+
"os",
1162+
"subprocess",
1163+
"_subprocess",
1164+
"pty",
1165+
"system",
1166+
"popen",
1167+
"spawn",
1168+
"shutil",
1169+
"sys",
1170+
"pathlib",
1171+
"io",
1172+
"socket",
1173+
"compile",
1174+
"eval",
1175+
"exec",
1176+
"multiprocessing",
1177+
)
1178+
assert check_module_authorized(module, authorized_imports, dangerous_patterns) == expected

0 commit comments

Comments
 (0)