Spaces:
Runtime error

File size: 2,254 Bytes
2b4b6d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from pathlib import Path

import pytest

from amo.workspace import WORKSPACE_ROOT, compile_workspace_python, list_workspace, read_workspace_file, write_workspace_file, resolve_workspace_path


def test_workspace_write_read_and_backup(tmp_path, monkeypatch):
    monkeypatch.setattr("amo.workspace.WORKSPACE_ROOT", tmp_path)
    tmp_path.mkdir(exist_ok=True)
    first = write_workspace_file("demo.py", "def f():\n    return 1\n")
    assert first.startswith("تم حفظ")
    assert "return 1" in read_workspace_file("demo.py")
    second = write_workspace_file("demo.py", "def f():\n    return 2\n")
    assert "النسخة السابقة محفوظة" in second
    assert list((tmp_path / ".amo_backups").iterdir())


def test_path_traversal_is_rejected(tmp_path, monkeypatch):
    monkeypatch.setattr("amo.workspace.WORKSPACE_ROOT", tmp_path)
    tmp_path.mkdir(exist_ok=True)
    with pytest.raises(ValueError):
        resolve_workspace_path("../escape.txt")
    with pytest.raises(ValueError):
        resolve_workspace_path("/tmp/secret.txt")


def test_syntax_check_reports_invalid_python(tmp_path, monkeypatch):
    monkeypatch.setattr("amo.workspace.WORKSPACE_ROOT", tmp_path)
    tmp_path.mkdir(exist_ok=True)
    (tmp_path / "bad.py").write_text("def broken(:\n    pass\n", encoding="utf-8")
    result = compile_workspace_python(".")
    assert "SyntaxError" in result or "invalid syntax" in result


def test_workspace_unittest_runs_inseparate_process(tmp_path, monkeypatch):
    from amo.workspace import run_workspace_unittests
    monkeypatch.setattr("amo.workspace.WORKSPACE_ROOT", tmp_path)
    (tmp_path / "test_demo.py").write_text(
        "import unittest\nclass Demo(unittest.TestCase):\n    def test_ok(self):\n        self.assertEqual(2 + 2, 4)\n",
        encoding="utf-8",
    )
    result = run_workspace_unittests(".")
    assert "Ran 1 test" in result
    assert "OK" in result


def test_linux_tool_is_allowlisted(monkeypatch, tmp_path):
    from amo.workspace import linux_safe_task
    monkeypatch.setattr("amo.workspace.WORKSPACE_ROOT", tmp_path)
    result = linux_safe_task("python_version")
    assert "python_version" in result
    denied = linux_safe_task("rm -rf /")
    assert "غير مسموحة" in denied