Skip to content

Commit b3fbce0

Browse files
committed
Implement test case for pathnode.
1 parent dcc5958 commit b3fbce0

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

src/_pytask/nodes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def __attrs_post_init__(self: Task) -> None:
6464
self.short_name = self.name
6565

6666
def state(self, hash: bool = False) -> str | None: # noqa: A002
67+
"""Return the state of the node."""
6768
if hash and self.path.exists():
6869
return hashlib.sha256(self.path.read_bytes()).hexdigest()
6970
if not hash and self.path.exists():
@@ -102,6 +103,7 @@ def value(self) -> Path:
102103

103104
@value.setter
104105
def value(self, value: Path) -> None:
106+
"""Set path and if other attributes are not set, set sensible defaults."""
105107
if not isinstance(value, Path):
106108
raise TypeError("'value' must be a 'pathlib.Path'.")
107109
if not self.name:
@@ -133,6 +135,17 @@ def state(self) -> str | None:
133135
def load(self) -> Path:
134136
return self.value
135137

138+
def save(self, value: bytes | str) -> None:
139+
"""Save strings or bytes to file."""
140+
if isinstance(value, str):
141+
self.path.write_text(value)
142+
elif isinstance(value, bytes):
143+
self.path.write_bytes(value)
144+
else:
145+
raise TypeError(
146+
f"'PathNode' can only save 'str' and 'bytes', not {type(value)}"
147+
)
148+
136149

137150
@define(kw_only=True)
138151
class PythonNode(Node):

tests/test_execute.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,6 @@ def test_error_with_multiple_different_dep_annotations(runner, tmp_path):
532532
from pathlib import Path
533533
from typing_extensions import Annotated
534534
from pytask import Product, PythonNode, PathNode
535-
from typing import Any
536535
537536
def task_example(
538537
dependency: Annotated[Any, PythonNode(), PathNode()] = "hello",
@@ -547,6 +546,25 @@ def task_example(
547546
assert "Parameter 'dependency'" in result.output
548547

549548

549+
@pytest.mark.end_to_end()
550+
def test_return_with_pathnode_annotation_as_return(runner, tmp_path):
551+
source = """
552+
from pathlib import Path
553+
from typing import Any
554+
from typing_extensions import Annotated
555+
from pytask import PathNode
556+
557+
node = PathNode.from_path(Path(__file__).parent.joinpath("file.txt"))
558+
559+
def task_example() -> Annotated[str, node]:
560+
return "Hello, World!"
561+
"""
562+
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
563+
result = runner.invoke(cli, [tmp_path.as_posix()])
564+
assert result.exit_code == ExitCode.OK
565+
assert tmp_path.joinpath("file.txt").read_text() == "Hello, World!"
566+
567+
550568
@pytest.mark.end_to_end()
551569
def test_return_with_custom_type_annotation_as_return(runner, tmp_path):
552570
source = """

0 commit comments

Comments
 (0)