diff --git a/src/ansible_navigator/initialization.py b/src/ansible_navigator/initialization.py index c9c626d9d..4da4fc77e 100644 --- a/src/ansible_navigator/initialization.py +++ b/src/ansible_navigator/initialization.py @@ -81,7 +81,7 @@ def find_config() -> tuple[list[LogMessage], list[ExitMessage], str | None, C]: message = f"Using settings file at {config_path} set by {cfg_env_var}" messages.append(LogMessage(level=logging.DEBUG, message=message)) elif found_config_path is not None: - config_path = found_config_path + config_path = str(found_config_path) settings_source = C.SEARCH_PATH message = f"Using settings file at {config_path} in search path" messages.append(LogMessage(level=logging.DEBUG, message=message)) diff --git a/src/ansible_navigator/utils/functions.py b/src/ansible_navigator/utils/functions.py index 55a9cd7fa..6594da4b6 100644 --- a/src/ansible_navigator/utils/functions.py +++ b/src/ansible_navigator/utils/functions.py @@ -34,7 +34,7 @@ logger = logging.getLogger(__name__) -def oxfordcomma(listed: Iterable[bool | str], condition: str) -> str: +def oxfordcomma(listed: Iterable[bool | str | Path], condition: str) -> str: """Format a list into a sentence. :param listed: List of string entries to modify @@ -210,7 +210,7 @@ def environment_variable_is_file_path( return messages, exit_messages, file_path -def find_settings_file() -> tuple[list[LogMessage], list[ExitMessage], str | None]: +def find_settings_file() -> tuple[list[LogMessage], list[ExitMessage], Path | None]: """Find the settings file. Find the file at ./ansible-navigator.(.yml,.yaml,.json), @@ -221,27 +221,33 @@ def find_settings_file() -> tuple[list[LogMessage], list[ExitMessage], str | Non messages: list[LogMessage] = [] exit_messages: list[ExitMessage] = [] allowed_extensions = ["yml", "yaml", "json"] - potential_paths: list[list[str]] = [] - found_files: list[str] = [] + potential_paths: list[Path] = [] + found_files: list[Path] = [] - potential_paths.append([os.path.expanduser("~"), ".ansible-navigator"]) - potential_paths.append([os.getcwd(), "ansible-navigator"]) + settings_file_home = Path.home() / ".ansible-navigator" + settings_file_current = Path.cwd() / "ansible-navigator" + potential_paths.append(settings_file_home) + potential_paths.append(settings_file_current) for path in potential_paths: - message = f"Looking in {path[0]}" + message = f"Looking in {path}" messages.append(LogMessage(level=logging.DEBUG, message=message)) - candidates = [os.path.join(path[0], f"{path[1]}.{ext}") for ext in allowed_extensions] + candidates: list[Path] = [] + for ext in allowed_extensions: + p = Path(f"{path}.{ext}") + candidates.append(p) message = f"Looking for {oxfordcomma(candidates, 'and')}" messages.append(LogMessage(level=logging.DEBUG, message=message)) - found = [file for file in candidates if os.path.exists(file)] + found = [file for file in candidates if file.exists()] + message = f"Found {len(found)}: {oxfordcomma(found, 'and')}" messages.append(LogMessage(level=logging.DEBUG, message=message)) if len(found) > 1: exit_msg = f"Only one file among {oxfordcomma(candidates, 'and')}" - exit_msg += f" should be present in {path[0]}" + exit_msg += f" should be present in {path}" exit_msg += f" Found: {oxfordcomma(found, 'and')}" exit_messages.append(ExitMessage(message=exit_msg)) return messages, exit_messages, None diff --git a/tests/defaults.py b/tests/defaults.py index 5d9285362..a5a3dae3a 100644 --- a/tests/defaults.py +++ b/tests/defaults.py @@ -3,17 +3,16 @@ import os from enum import Enum -from pathlib import Path from typing import Any from ansible_navigator.utils.functions import expand_path -FIXTURES_DIR = str(expand_path(os.path.join(os.path.dirname(__file__), "fixtures"))) +FIXTURES_DIR = expand_path(os.path.join(os.path.dirname(__file__), "fixtures")) FIXTURES_COLLECTION_DIR = expand_path( os.path.join(os.path.dirname(__file__), "fixtures", "common", "collections"), ) -FIXTURES_COLLECTION_PATH = Path(FIXTURES_COLLECTION_DIR) +FIXTURES_COLLECTION_PATH = FIXTURES_COLLECTION_DIR class BaseScenario: diff --git a/tests/unit/configuration_subsystem/defaults.py b/tests/unit/configuration_subsystem/defaults.py index 96c87579b..6d8bed195 100644 --- a/tests/unit/configuration_subsystem/defaults.py +++ b/tests/unit/configuration_subsystem/defaults.py @@ -1,8 +1,6 @@ """Defaults for adjacent tests.""" -import os - from tests.defaults import FIXTURES_DIR -TEST_FIXTURE_DIR = os.path.join(FIXTURES_DIR, "unit", "configuration_subsystem") +TEST_FIXTURE_DIR = FIXTURES_DIR / "unit" / "configuration_subsystem" diff --git a/tests/unit/configuration_subsystem/test_internals.py b/tests/unit/configuration_subsystem/test_internals.py index 7090b66eb..11dd42910 100644 --- a/tests/unit/configuration_subsystem/test_internals.py +++ b/tests/unit/configuration_subsystem/test_internals.py @@ -3,6 +3,7 @@ import os from copy import deepcopy +from pathlib import Path import pytest @@ -34,10 +35,10 @@ def test_settings_file_path_file_system(monkeypatch: pytest.MonkeyPatch) -> None args.internals.initializing = True args.application_version = "test" - def getcwd() -> str: + def getcwd() -> Path: return TEST_FIXTURE_DIR - monkeypatch.setattr(os, "getcwd", getcwd) + monkeypatch.setattr(Path, "cwd", getcwd) parse_and_update(params=[], args=args) assert args.internals.settings_file_path == settings_file_path assert args.internals.settings_source == Constants.SEARCH_PATH diff --git a/tests/unit/utils/test_functions.py b/tests/unit/utils/test_functions.py index 6bda4de7e..be7ee098f 100644 --- a/tests/unit/utils/test_functions.py +++ b/tests/unit/utils/test_functions.py @@ -37,9 +37,9 @@ def test_find_many_settings_home(monkeypatch: pytest.MonkeyPatch) -> None: ] def check_path_exists(arg: Any) -> bool: - return arg in paths + return str(arg) in paths - monkeypatch.setattr(os.path, "exists", check_path_exists) + monkeypatch.setattr(Path, "exists", check_path_exists) _messages, exit_messages, _found = find_settings_file() expected = f"Only one file among {oxfordcomma(paths, 'and')}" assert any(expected in exit_msg.message for exit_msg in exit_messages) @@ -53,9 +53,9 @@ def test_find_many_settings_cwd(monkeypatch: pytest.MonkeyPatch) -> None: paths = [os.path.join(os.getcwd(), "ansible-navigator" + ext) for ext in EXTENSIONS] def check_path_exists(arg: Any) -> bool: - return arg in paths + return str(arg) in paths - monkeypatch.setattr(os.path, "exists", check_path_exists) + monkeypatch.setattr(Path, "exists", check_path_exists) _messages, exit_messages, _found = find_settings_file() expected = f"Only one file among {oxfordcomma(paths, 'and')}" assert any(expected in exit_msg.message for exit_msg in exit_messages) @@ -66,13 +66,13 @@ def test_find_many_settings_precedence(monkeypatch: pytest.MonkeyPatch) -> None: :param monkeypatch: The monkeypatch fixture """ - expected = os.path.join(os.getcwd(), "ansible-navigator.yml") - paths = [expected, os.path.join(os.path.expanduser("~"), ".ansible-navigator.json")] + expected = Path.cwd() / "ansible-navigator.yml" + paths = [expected, Path.home() / ".ansible-navigator.json"] def check_path_exists(arg: Any) -> bool: return arg in paths - monkeypatch.setattr(os.path, "exists", check_path_exists) + monkeypatch.setattr(Path, "exists", check_path_exists) _messages, _exit_messages, found = find_settings_file() assert expected == found