Merge pull request #332 from hartym/211_forbid_fs_related_nodes_to_join_cwd_silently
Do not allow to provide absolute paths as filesystem will join it sil…
This commit is contained in:
@ -1,6 +1,15 @@
|
|||||||
from bonobo.config import Configurable, ContextProcessor, Option, Service
|
from bonobo.config import Configurable, ContextProcessor, Option, Service
|
||||||
|
|
||||||
|
|
||||||
|
def filesystem_path(path: str):
|
||||||
|
if path.startswith("/"):
|
||||||
|
raise ValueError(
|
||||||
|
"File path should not be absolute. If you really need to provide absolute paths, then you must pass a "
|
||||||
|
"filesystem instance that is bound to your filesystem root and provide a relative path from there."
|
||||||
|
)
|
||||||
|
return str(path)
|
||||||
|
|
||||||
|
|
||||||
class FileHandler(Configurable):
|
class FileHandler(Configurable):
|
||||||
"""Abstract component factory for file-related components.
|
"""Abstract component factory for file-related components.
|
||||||
|
|
||||||
@ -13,39 +22,12 @@ class FileHandler(Configurable):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
path = Option(
|
path = Option(
|
||||||
str,
|
filesystem_path, required=True, positional=True, __doc__="Path to use within the provided filesystem."
|
||||||
required=True,
|
|
||||||
positional=True,
|
|
||||||
__doc__="""
|
|
||||||
Path to use within the provided filesystem.
|
|
||||||
""",
|
|
||||||
) # type: str
|
|
||||||
eol = Option(
|
|
||||||
str,
|
|
||||||
default="\n",
|
|
||||||
__doc__="""
|
|
||||||
Character to use as line separator.
|
|
||||||
""",
|
|
||||||
) # type: str
|
|
||||||
mode = Option(
|
|
||||||
str,
|
|
||||||
__doc__="""
|
|
||||||
What mode to use for open() call.
|
|
||||||
""",
|
|
||||||
) # type: str
|
|
||||||
encoding = Option(
|
|
||||||
str,
|
|
||||||
default="utf-8",
|
|
||||||
__doc__="""
|
|
||||||
Encoding.
|
|
||||||
""",
|
|
||||||
) # type: str
|
|
||||||
fs = Service(
|
|
||||||
"fs",
|
|
||||||
__doc__="""
|
|
||||||
The filesystem instance to use.
|
|
||||||
""",
|
|
||||||
) # type: str
|
) # type: str
|
||||||
|
eol = Option(str, default="\n", __doc__="Character to use as line separator.") # type: str
|
||||||
|
mode = Option(str, __doc__="What mode to use for open() call.") # type: str
|
||||||
|
encoding = Option(str, default="utf-8", __doc__="Encoding.") # type: str
|
||||||
|
fs = Service("fs", __doc__="The filesystem instance to use.") # type: str
|
||||||
|
|
||||||
@ContextProcessor
|
@ContextProcessor
|
||||||
def file(self, context, *, fs):
|
def file(self, context, *, fs):
|
||||||
|
|||||||
12
tests/nodes/io/test_io_base.py
Normal file
12
tests/nodes/io/test_io_base.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from bonobo.nodes.io.base import filesystem_path
|
||||||
|
|
||||||
|
|
||||||
|
def test_filesystem_path_absolute():
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
filesystem_path("/this/is/absolute")
|
||||||
|
|
||||||
|
|
||||||
|
def test_filesystem_path_relative():
|
||||||
|
assert filesystem_path("this/is/relative") == "this/is/relative"
|
||||||
Reference in New Issue
Block a user