diff --git a/dissect/target/helpers/configutil.py b/dissect/target/helpers/configutil.py index 8be76beda..bb808c656 100644 --- a/dissect/target/helpers/configutil.py +++ b/dissect/target/helpers/configutil.py @@ -748,13 +748,13 @@ def parse(path: Union[FilesystemEntry, TargetPath], hint: Optional[str] = None, FileNotFoundError: If the ``path`` is not a file. """ - if not path.is_file(follow_symlinks=True): - raise FileNotFoundError(f"Could not parse {path} as a dictionary.") - entry = path if isinstance(path, TargetPath): entry = path.get() + if not entry.is_file(follow_symlinks=True): + raise FileNotFoundError(f"Could not parse {path} as a dictionary.") + options = ParserOptions(*args, **kwargs) return parse_config(entry, hint, options) diff --git a/tests/helpers/test_configutil.py b/tests/helpers/test_configutil.py index b97cfe95b..49fe54d4e 100644 --- a/tests/helpers/test_configutil.py +++ b/tests/helpers/test_configutil.py @@ -1,10 +1,13 @@ +from __future__ import annotations + import textwrap from io import StringIO from pathlib import Path -from typing import Union +from typing import TYPE_CHECKING, Union import pytest +from dissect.target.exceptions import FileNotFoundError from dissect.target.helpers.configutil import ( ConfigurationParser, Default, @@ -12,9 +15,14 @@ Json, ScopeManager, SystemD, + parse, ) from tests._utils import absolute_path +if TYPE_CHECKING: + from dissect.target import Target + from dissect.target.filesystem import VirtualFilesystem + def parse_data(parser_type: type[ConfigurationParser], data_to_read: str, *args, **kwargs) -> dict: """Initializes parser_type as a parser which parses ``data_to_read``""" @@ -258,3 +266,21 @@ def test_json_syntax(data_string: str, expected_data: Union[dict, list]) -> None parser.parse_file(StringIO(data_string)) assert parser.parsed_data == expected_data + + +def test_parse(target_linux: Target, fs_linux: VirtualFilesystem, tmp_path: Path) -> None: + # File does not exist on the system in the first place + with pytest.raises(FileNotFoundError): + parse(target_linux.fs.path("/path/to/file")) + + file_path = tmp_path.joinpath("path/to/file") + file_path.parent.mkdir(parents=True) + file_path.touch() + + fs_linux.map_dir("/", tmp_path.absolute()) + + # Trying to read a directory + with pytest.raises(FileNotFoundError): + parse(target_linux.fs.path("/path/to")) + + parse(target_linux.fs.path("/path/to/file"))