Skip to content
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
11 changes: 6 additions & 5 deletions src/orgparse/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import itertools
import re
from collections.abc import Iterable, Iterator, Sequence
from pathlib import Path
from typing import Any, cast

from .date import (
Expand Down Expand Up @@ -348,7 +349,7 @@ def __init__(
self,
todos: Sequence[str] | None = None,
dones: Sequence[str] | None = None,
filename: str = '<undefined>',
filename: str | Path = '<undefined>',
) -> None:
if dones is None:
dones = ['DONE']
Expand All @@ -357,7 +358,7 @@ def __init__(
self._todos = list(todos)
self._dones = list(dones)
self._todo_not_specified_in_comment = True
self._filename = filename
self._filename = str(filename)
self._nodes: list[OrgBaseNode] = []

@property
Expand Down Expand Up @@ -429,10 +430,10 @@ def all_todo_keys(self):
@property
def filename(self) -> str:
"""
Return a path to the source file or similar information.
Return the source filename as a string.

If the org objects are not loaded from a file, this value
will be a string of the form ``<SOME_TEXT>``.
A :class:`pathlib.Path` passed to ``OrgEnv`` is converted to a string.
Documents loaded without a filename use a placeholder such as ``<string>``.
"""
return self._filename

Expand Down
38 changes: 38 additions & 0 deletions src/orgparse/tests/test_load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Regression tests for loading documents with an explicit environment."""

from pathlib import Path

import pytest

from .. import OrgEnv, load


@pytest.mark.parametrize('path_type', [str, Path])
@pytest.mark.parametrize('filename_type', [str, Path])
def test_load_with_env(
*, tmp_path: Path, path_type: type[str] | type[Path], filename_type: type[str] | type[Path]
) -> None:
"""Accept matching Path/string filenames and preserve the environment's custom TODO states."""
path = tmp_path / 'notes.org'
path.write_text('* NEXT Task\n', encoding='utf-8')
env = OrgEnv(filename=filename_type(path), todos=['NEXT'])

root = load(path_type(path), env=env)

assert root.env is env
assert root.env.filename == str(path)
[node] = root.children
assert node.env is env
assert node.todo == 'NEXT'
assert node.heading == 'Task'


@pytest.mark.parametrize('filename_type', [str, Path])
def test_load_with_mismatched_env(*, tmp_path: Path, filename_type: type[str] | type[Path]) -> None:
"""Reject different source filenames, even when the basename matches."""
path = tmp_path / 'notes.org'
path.write_text('* Task\n', encoding='utf-8')
env = OrgEnv(filename=filename_type(tmp_path / 'other' / 'notes.org'))

with pytest.raises(ValueError, match='If env is specified, filename must match'):
load(path, env=env)
Loading