Skip to content

Commit

Permalink
feat(jinja): add configuration options to jinja Environment (#181)
Browse files Browse the repository at this point in the history
Close #181
  • Loading branch information
Toilal committed Jan 18, 2021
1 parent 10b7a72 commit 092d9d0
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 5 deletions.
15 changes: 11 additions & 4 deletions ddb/feature/jinja/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ def initialize(self):
self._rootpath = self.template_finder.rootpath
self._migrationpath = get_single_temporary_file_directory("ddb", "migration", "jinja")

self.env = Environment(
loader=FileSystemLoader([self._rootpath, self._migrationpath]),
undefined=StrictUndefined
)
effective_options = {
"loader": FileSystemLoader([self._rootpath, self._migrationpath]),
"undefined": StrictUndefined,
"keep_trailing_newline": True
}

options = config.data.get('jinja.options')
if options:
effective_options.update(options)

self.env = Environment(**effective_options)

self.env.filters.update(custom_filters)
self.env.tests.update(custom_tests)
Expand Down
1 change: 1 addition & 0 deletions ddb/feature/jinja/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ class JinjaSchema(FeatureSchema):
extensions = fields.List(fields.String(), default=[".*", ""])
includes = fields.List(fields.String()) # default is build automatically from suffixes value
excludes = fields.List(fields.String())
options = fields.Dict(fields.String(required=True), fields.Field(required=True), allow_none=True, default=None)
1 change: 1 addition & 0 deletions docs/features/jinja.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using ddb configuration.
| `suffixes` | string[]<br>`['.jinja']` | A list of filename suffix to include. |
| `extensions` | string[]<br>`['.*', '']` | A list of glob of supported extension. |
| `excludes` | string[]<br>`[]` | A list of glob of filepath to exclude. |
| `options` | dict[string, object] | Additional options to pass to [Jinja Environment](https://jinja.palletsprojects.com/en/2.11.x/api/#jinja2.Environment). |

=== "Advanced"
| Property | Type | Description |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test: trailing
63 changes: 63 additions & 0 deletions tests/feature/jinja/test_jinja.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

from ddb.__main__ import load_registered_features, register_actions_in_event_bus
from ddb.config import config
from ddb.feature import features
from ddb.feature.core import CoreFeature
from ddb.feature.file import FileFeature, FileWalkAction
Expand Down Expand Up @@ -135,3 +136,65 @@ def test_project5(self, project_loader):
foo = f.read()

assert foo == 'env: dev'

def test_project_keep_trailing_lines_false(self, project_loader):
project_loader("trailing-newline")

features.register(CoreFeature())
features.register(FileFeature())
features.register(JinjaFeature())
load_registered_features()
register_actions_in_event_bus(True)

config.data['jinja.options'] = {"keep_trailing_newline": False}

action = FileWalkAction()
action.initialize()
action.execute()

assert os.path.exists('foo.yml')
with open('foo.yml', 'r') as f:
foo = f.read()

assert foo == 'test: trailing'

def test_project_keep_trailing_lines_default(self, project_loader):
project_loader("trailing-newline")

features.register(CoreFeature())
features.register(FileFeature())
features.register(JinjaFeature())
load_registered_features()
register_actions_in_event_bus(True)

action = FileWalkAction()
action.initialize()
action.execute()

assert os.path.exists('foo.yml')
with open('foo.yml', 'r') as f:
foo = f.read()

assert foo == 'test: trailing\n'

def test_project_keep_trailing_lines_true(self, project_loader):
project_loader("trailing-newline")

features.register(CoreFeature())
features.register(FileFeature())
features.register(JinjaFeature())
load_registered_features()
register_actions_in_event_bus(True)

config.data['jinja.options'] = {"keep_trailing_newline": True}

action = FileWalkAction()
action.initialize()
action.execute()

assert os.path.exists('foo.yml')
with open('foo.yml', 'r') as f:
foo = f.read()

assert foo == 'test: trailing\n'

2 changes: 1 addition & 1 deletion tests/it/test_files_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_ensure_chaining(self, project_loader):
assert not os.path.exists("test.dev.yml.jinja")
with open("test.dev.yml.jsonnet", "r") as dockerfile:
data = dockerfile.read()
assert data == '{["e" + "n" + "v"]: "dev"}'
assert data == '{["e" + "n" + "v"]: "dev"}\n'

assert os.path.exists("test.dev.yml")
with open("test.dev.yml", "r") as dockerfile:
Expand Down

0 comments on commit 092d9d0

Please sign in to comment.