Skip to content

Commit

Permalink
Update get_log_paths to reference config directly instead of test s…
Browse files Browse the repository at this point in the history
…ome well-known values

Update unit test for `get_log_paths`
  • Loading branch information
NeonDaniel committed Jul 18, 2024
1 parent 2c96bdb commit 89db894
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
19 changes: 15 additions & 4 deletions ovos_utils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def get_log_path(service: str, directories: Optional[List[str]] = None) \
return path


def get_log_paths() -> Set[str]:
def get_log_paths(config: Optional[dict] = None) -> Set[str]:
"""
Get all log paths for all service logs
Different services may have different log paths
Expand All @@ -379,9 +379,20 @@ def get_log_paths() -> Set[str]:
set of paths to log directories
"""
paths = set()
svc_names = ALL_SERVICES.union({s.replace("-", "_") for s in ALL_SERVICES})
for service in svc_names:
paths.add(get_log_path(service))
if not config:
try:
from ovos_config import Configuration
config = Configuration()
except ImportError:
LOG.warning("ovos_config not available. Falling back to defaults")
config = dict()

for name, service_config in config.get("logging", {}).items():
if not isinstance(service_config, dict) or name == "logs":
continue
if service_config.get("path"):
paths.add(service_config.get("path"))
paths.add(get_log_path(""))

return paths

Expand Down
31 changes: 14 additions & 17 deletions test/unittests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,27 +272,24 @@ def test_get_log_path(self, get_config):
self.assertEqual(get_log_path("test"), self.test_dir)
get_config.assert_called_once_with(service_name="test")

@patch('ovos_utils.log.get_log_path')
def test_get_log_paths(self, get_log_path):
from ovos_utils.log import get_log_paths, ALL_SERVICES
@patch('ovos_config.Configuration')
def test_get_log_paths(self, config):
from ovos_utils.log import get_log_paths

def mock_get_path_different(service) -> str:
return service
config_no_modules = {"logging": {"logs": {"path": "default_path"}}}

def mock_get_path_same(service) -> str:
return "log_path"
# Test default config path from Configuration (no module overrides)
config.return_value = config_no_modules
self.assertEqual(get_log_paths(), {"default_path"})

# Test services with different configured paths
get_log_path.side_effect = mock_get_path_different
paths = get_log_paths()
for svc in ALL_SERVICES:
self.assertIn(svc, paths)
if '-' in svc:
self.assertIn(svc.replace('-', '_'), paths)

# Test services with same path
get_log_path.side_effect = mock_get_path_same
self.assertEqual(get_log_paths(), {"log_path"})
config_multi_modules = {"logging": {"logs": {"path": "default_path"},
"module_1": {"path": "path_1"},
"module_2": {"path": "path_2"},
"module_3": {"path": "path_1"}}}
self.assertEqual(get_log_paths(config_multi_modules),
{"default_path", "path_1", "path_2"})


@patch('ovos_utils.log.get_log_paths')
def test_get_available_logs(self, get_log_paths):
Expand Down

0 comments on commit 89db894

Please sign in to comment.