-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathtest_utils_environment.py
More file actions
128 lines (93 loc) · 5.1 KB
/
test_utils_environment.py
File metadata and controls
128 lines (93 loc) · 5.1 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
import platform
import pytest
from kaos.path import KaosPath
@pytest.mark.skipif(platform.system() == "Windows", reason="Skipping test on Windows")
async def test_environment_detection(monkeypatch):
monkeypatch.setattr(platform, "system", lambda: "Linux")
monkeypatch.setattr(platform, "machine", lambda: "x86_64")
monkeypatch.setattr(platform, "version", lambda: "5.15.0-123-generic")
async def _mock_is_file(self: KaosPath) -> bool:
return str(self) == "/usr/bin/bash"
monkeypatch.setattr(KaosPath, "is_file", _mock_is_file)
from kimi_cli.utils.environment import Environment
env = await Environment.detect()
assert env.os_kind == "Linux"
assert env.os_arch == "x86_64"
assert env.os_version == "5.15.0-123-generic"
assert env.shell_name == "bash"
assert str(env.shell_path) == "/usr/bin/bash"
@pytest.mark.skipif(platform.system() == "Windows", reason="Skipping test on Windows")
async def test_environment_detection_windows(monkeypatch):
monkeypatch.setattr(platform, "system", lambda: "Windows")
monkeypatch.setattr(platform, "machine", lambda: "AMD64")
monkeypatch.setattr(platform, "version", lambda: "10.0.19044")
monkeypatch.setenv("SYSTEMROOT", r"C:\Windows")
monkeypatch.delenv("ProgramW6432", raising=False)
monkeypatch.setattr("kimi_cli.utils.environment.shutil.which", lambda *_a, **_k: None)
expected = os.path.join(
r"C:\Windows", "System32", "WindowsPowerShell", "v1.0", "powershell.exe"
)
async def _mock_is_file(self: KaosPath) -> bool:
return os.path.normcase(str(self)) == os.path.normcase(expected)
monkeypatch.setattr(KaosPath, "is_file", _mock_is_file)
from kimi_cli.utils.environment import Environment
env = await Environment.detect()
assert env.os_kind == "Windows"
assert env.os_arch == "AMD64"
assert env.os_version == "10.0.19044"
assert env.shell_name == "Windows PowerShell"
assert os.path.normcase(str(env.shell_path)) == os.path.normcase(expected)
@pytest.mark.skipif(platform.system() == "Windows", reason="Skipping test on Windows")
async def test_environment_detection_windows_prefers_pwsh_from_path(monkeypatch):
monkeypatch.setattr(platform, "system", lambda: "Windows")
monkeypatch.setattr(platform, "machine", lambda: "AMD64")
monkeypatch.setattr(platform, "version", lambda: "10.0.19044")
monkeypatch.setenv("SYSTEMROOT", r"C:\Windows")
expected = os.path.join(r"C:\Program Files", "PowerShell", "7", "pwsh.exe")
def _which(cmd: str, path: str | None = None) -> str | None:
if cmd == "pwsh":
return expected
return None
monkeypatch.setattr("kimi_cli.utils.environment.shutil.which", _which)
async def _mock_is_file(self: KaosPath) -> bool:
return os.path.normcase(str(self)) == os.path.normcase(expected)
monkeypatch.setattr(KaosPath, "is_file", _mock_is_file)
from kimi_cli.utils.environment import Environment
env = await Environment.detect()
assert env.shell_name == "Windows PowerShell"
assert os.path.normcase(str(env.shell_path)) == os.path.normcase(expected)
@pytest.mark.skipif(platform.system() == "Windows", reason="Skipping test on Windows")
async def test_environment_detection_windows_prefers_pwsh_from_program_files(monkeypatch):
monkeypatch.setattr(platform, "system", lambda: "Windows")
monkeypatch.setattr(platform, "machine", lambda: "AMD64")
monkeypatch.setattr(platform, "version", lambda: "10.0.19044")
monkeypatch.setenv("SYSTEMROOT", r"C:\Windows")
monkeypatch.delenv("ProgramW6432", raising=False)
monkeypatch.setenv("ProgramFiles", r"C:\Program Files")
monkeypatch.setattr("kimi_cli.utils.environment.shutil.which", lambda *_a, **_k: None)
expected = os.path.join(r"C:\Program Files", "PowerShell", "7", "pwsh.exe")
async def _mock_is_file(self: KaosPath) -> bool:
return os.path.normcase(str(self)) == os.path.normcase(expected)
monkeypatch.setattr(KaosPath, "is_file", _mock_is_file)
from kimi_cli.utils.environment import Environment
env = await Environment.detect()
assert env.shell_name == "Windows PowerShell"
assert os.path.normcase(str(env.shell_path)) == os.path.normcase(expected)
@pytest.mark.skipif(platform.system() == "Windows", reason="Skipping test on Windows")
async def test_environment_detection_windows_fallback(monkeypatch):
monkeypatch.setattr(platform, "system", lambda: "Windows")
monkeypatch.setattr(platform, "machine", lambda: "AMD64")
monkeypatch.setattr(platform, "version", lambda: "10.0.19044")
monkeypatch.setenv("SYSTEMROOT", r"C:\Windows")
monkeypatch.setattr("kimi_cli.utils.environment.shutil.which", lambda *_a, **_k: None)
async def _mock_is_file(self: KaosPath) -> bool:
return False
monkeypatch.setattr(KaosPath, "is_file", _mock_is_file)
from kimi_cli.utils.environment import Environment
env = await Environment.detect()
assert env.os_kind == "Windows"
assert env.os_arch == "AMD64"
assert env.os_version == "10.0.19044"
assert env.shell_name == "Windows PowerShell"
assert str(env.shell_path) == "powershell.exe"