Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TargetPath instances for configutil.parse #743

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions dissect/target/helpers/configutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 27 additions & 1 deletion tests/helpers/test_configutil.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
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,
Indentation,
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``"""
Expand Down Expand Up @@ -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"))
Loading