|
| 1 | +# Copyright © 2011-2026 Splunk, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# 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, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from splunklib.searchcommands.environment import ( |
| 20 | + _find_app_root, # pyright: ignore[reportPrivateUsage] |
| 21 | +) |
| 22 | + |
| 23 | +_SPLUNK_HOME = os.path.join(os.sep, "opt", "splunk") |
| 24 | +_APPS_DIRECTORY = os.path.join(_SPLUNK_HOME, "etc", "apps") |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize( |
| 28 | + ("app_file_parts", "expected_app_root_parts"), |
| 29 | + [ |
| 30 | + # A script located directly in the app's bin directory |
| 31 | + (("my_app", "bin", "command.py"), ("my_app",)), |
| 32 | + # A script located in a subdirectory of bin, including one that happens to be |
| 33 | + # named "bin" itself; the app root is still $SPLUNK_HOME/etc/apps/my_app |
| 34 | + (("my_app", "bin", "foo", "bin", "command.py"), ("my_app",)), |
| 35 | + ], |
| 36 | +) |
| 37 | +def test_find_app_root( |
| 38 | + app_file_parts: tuple[str, ...], expected_app_root_parts: tuple[str, ...] |
| 39 | +) -> None: |
| 40 | + app_file = os.path.join(_APPS_DIRECTORY, *app_file_parts) |
| 41 | + expected_app_root = os.path.join(_APPS_DIRECTORY, *expected_app_root_parts) |
| 42 | + assert _find_app_root(app_file, _SPLUNK_HOME) == expected_app_root |
| 43 | + |
| 44 | + |
| 45 | +def test_find_app_root_falls_back_on_nonstandard_splunk_home() -> None: |
| 46 | + app_file = os.path.join("some", "other", "layout", "command.py") |
| 47 | + expected_app_root = os.path.dirname(os.path.abspath(os.path.dirname(app_file))) |
| 48 | + assert _find_app_root(app_file, _SPLUNK_HOME) == expected_app_root |
0 commit comments