|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import sys |
| 17 | +import types |
| 18 | + |
| 19 | +from veadk.utils.misc import get_agents_dir, get_agent_dir |
| 20 | + |
| 21 | + |
| 22 | +class GetAgentsDirTest: |
| 23 | + def test_get_agents_dir_from_main_file(monkeypatch): |
| 24 | + """ |
| 25 | + Case 1: __main__.__file__ exists (common in CLI or uv run environments) |
| 26 | + """ |
| 27 | + fake_main = types.SimpleNamespace(__file__="/tmp/project/testapp/agent.py") |
| 28 | + monkeypatch.setitem(sys.modules, "__main__", fake_main) |
| 29 | + |
| 30 | + result = get_agents_dir() |
| 31 | + assert result == "/tmp/project" |
| 32 | + result = get_agent_dir() |
| 33 | + assert result == "/tmp/project/testapp" |
| 34 | + |
| 35 | + def test_get_agents_dir_from_sys_argv(monkeypatch): |
| 36 | + """ |
| 37 | + Case 2: Fallback to sys.argv[0] |
| 38 | + """ |
| 39 | + fake_main = types.SimpleNamespace() |
| 40 | + monkeypatch.setitem(sys.modules, "__main__", fake_main) |
| 41 | + monkeypatch.setattr(sys, "argv", ["/tmp/project/testapp/agent.py"]) |
| 42 | + |
| 43 | + result = get_agents_dir() |
| 44 | + assert result == "/tmp/project" |
| 45 | + result = get_agent_dir() |
| 46 | + assert result == "/tmp/project/testapp" |
| 47 | + |
| 48 | + def test_get_agents_dir_from_cwd(monkeypatch, tmp_path): |
| 49 | + """ |
| 50 | + Case 3: Fallback to current working directory (REPL or no file context) |
| 51 | + """ |
| 52 | + fake_main = types.SimpleNamespace() |
| 53 | + monkeypatch.setitem(sys.modules, "__main__", fake_main) |
| 54 | + monkeypatch.setattr(sys, "argv", []) |
| 55 | + |
| 56 | + fake_cwd = tmp_path / "some_dir" |
| 57 | + fake_cwd.mkdir() |
| 58 | + |
| 59 | + monkeypatch.setattr(os, "getcwd", lambda: str(fake_cwd)) |
| 60 | + result = get_agents_dir() |
| 61 | + |
| 62 | + # should return the parent of fake_cwd |
| 63 | + assert result == str(tmp_path) |
| 64 | + result = get_agent_dir() |
| 65 | + assert result == str(tmp_path / "some_dir") |
0 commit comments