Skip to content

Commit

Permalink
feat: fix handling of template files and template literal
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Neto <[email protected]>
  • Loading branch information
oleoneto committed Feb 19, 2025
1 parent bea476d commit 3e46203
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
9 changes: 7 additions & 2 deletions geny/core/filesystem/directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@ def tree(self, **kwargs):
return tree

def path(self, parent: Path = None) -> Path:
value = Path(self.name) if parent is None else parent / self.name
return value
if parent is None:
return Path(self.name)

if type(parent) is str:
parent = Path(parent)

return parent / self.name

@halt_on_error
def create(self, parent: Path = None, after_hooks: list[Callable] = None, **kwargs):
Expand Down
23 changes: 13 additions & 10 deletions geny/core/filesystem/files.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# core:filesystem
from typing import Callable
import re
from pathlib import Path

from geny.core.logger import logger
Expand All @@ -24,19 +24,22 @@ def template(self) -> str:
return self._template

def contents(self, **kwargs) -> str:
if self.template != "":
self.context.update(**kwargs)
is_content_literal = self._content != ""
is_template_literal = re.match(".*{{.+}}.*", self.template)
is_template_file = self.template != "" and not is_template_literal

content = TemplateParser().parse_file(
filepath=self.template,
variables=self.context,
)
if (is_template_literal and is_content_literal) or (is_template_file and is_content_literal):
raise AssertionError("cannot specify both template and content for File")

return content
self.context.update(**kwargs)

content = TemplateParser().parse_string(self._content, variables=self.context)
if is_template_file:
return TemplateParser().parse_file(filepath=self.template, variables=self.context)

return content
return TemplateParser().parse_string(
self.template if is_template_literal else self._content,
variables=self.context
)

def path(self, parent: Path = None) -> Path:
value = Path(self.name) if parent is None else parent / self.name
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ maintainers = [{ name = "Leo Neto", email = "[email protected]" }]
requires-python = ">=3.6"
license = { file = "LICENSE" }
readme = "README.md"
version = "0.1.3"
version = "0.1.4"
keywords = [
"automation",
"files",
Expand Down
17 changes: 12 additions & 5 deletions tests/core/filesystem/test_directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,31 @@ def test_create_directory_with_files(self):
name,
children=[
File(name="file1.txt"),
File(name="file2.md"),
File(name="file2.md", template="# {{ name }}"),
Directory("folder"),
],
)

d.create()
d.create(**{"name": "Bookstore"})

self.assertTrue(d.path().exists())
self.assertTrue(d.path().is_dir())

# Sub-directories
self.assertEqual(1, len(d.dirs))
self.assertEqual(2, len(d.files))

self.assertEqual("folder", d.dirs[0].name)
self.assertTrue(d.dirs[0].path(d.path()).exists())

# Files
self.assertEqual(2, len(d.files))
self.assertEqual("file1.txt", d.files[0].name)
self.assertEqual("file2.md", d.files[1].name)

self.assertEqual(sorted(["file1.txt", "folder", "file2.md"]), sorted(os.listdir(name)))
for file in d.files:
self.assertTrue(file.path(d.path()).exists())
self.assertTrue(file.path(d.path()).is_file())

self.assertEqual("# Bookstore\n", d.files[1].path(d.path()).read_text())

def test_create_directory_with_template_files(self):
with runner.isolated_filesystem():
Expand Down
5 changes: 3 additions & 2 deletions tests/core/filesystem/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_create_file(self):

self.assertTrue(file.path().exists())
self.assertTrue(file.path().is_file())
self.assertEqual("Hello world!", file.contents())
self.assertEqual("Hello world!\n", file.path().read_text())

def test_create_file_and_intermittent_directories(self):
with runner.isolated_filesystem():
Expand All @@ -31,7 +31,7 @@ def test_create_file_and_intermittent_directories(self):

self.assertTrue(file.path().exists())
self.assertTrue(file.path().is_file())
self.assertEqual("Hello world!", file.contents())
self.assertEqual("Hello world!\n", file.path().read_text())

def test_delete_file(self):
with runner.isolated_filesystem():
Expand All @@ -43,6 +43,7 @@ def test_delete_file(self):
file.create()

self.assertTrue(file.path().exists())
self.assertEqual("Hello world!\n", file.path().read_text())

file.destroy()

Expand Down

0 comments on commit 3e46203

Please sign in to comment.