|
| 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 __main__ |
| 16 | +from unittest import TestCase |
| 17 | +from unittest.mock import patch |
| 18 | + |
| 19 | +from veadk.utils.misc import get_agents_dir |
| 20 | + |
| 21 | + |
| 22 | +class GetAgentsDirTest(TestCase): |
| 23 | + @patch.object(__main__, "__file__", "/test/path/app/main.py") |
| 24 | + def test_get_agents_dir_from_main(self): |
| 25 | + """Test get_agents_dir using __main__.__file__""" |
| 26 | + self.assertEqual(get_agents_dir(), "/test/path") |
| 27 | + |
| 28 | + def test_get_agents_dir_from_argv(self): |
| 29 | + """Test get_agents_dir using sys.argv""" |
| 30 | + with patch("veadk.utils.misc.sys") as mock_sys: |
| 31 | + mock_sys.argv = ["/test/path/app/main.py"] |
| 32 | + with patch("veadk.utils.misc.hasattr") as mock_hasattr: |
| 33 | + mock_hasattr.return_value = False |
| 34 | + self.assertEqual(get_agents_dir(), "/test/path") |
| 35 | + |
| 36 | + @patch("veadk.utils.misc.os.getcwd") |
| 37 | + @patch("veadk.utils.misc.sys") |
| 38 | + def test_get_agents_dir_from_cwd(self, mock_sys, mock_getcwd): |
| 39 | + """Test get_agents_dir using os.getcwd""" |
| 40 | + mock_sys.argv = [""] |
| 41 | + mock_getcwd.return_value = "/test/path/app" |
| 42 | + with patch("veadk.utils.misc.hasattr") as mock_hasattr: |
| 43 | + mock_hasattr.return_value = False |
| 44 | + self.assertEqual(get_agents_dir(), "/test/path") |
| 45 | + |
| 46 | + @patch("veadk.utils.misc.os.getcwd") |
| 47 | + @patch("veadk.utils.misc.sys") |
| 48 | + def test_get_agents_dir_from_cwd_empty_argv(self, mock_sys, mock_getcwd): |
| 49 | + """Test get_agents_dir using os.getcwd with empty argv""" |
| 50 | + mock_sys.argv = [] |
| 51 | + mock_getcwd.return_value = "/test/path/app" |
| 52 | + with patch("veadk.utils.misc.hasattr") as mock_hasattr: |
| 53 | + mock_hasattr.return_value = False |
| 54 | + self.assertEqual(get_agents_dir(), "/test/path") |
0 commit comments