Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_included jinja variable #72

Merged
merged 1 commit into from
Feb 13, 2019
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 conda_devenv/devenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
import six


def render_jinja(contents, filename):
def render_jinja(contents, filename, is_included):
import jinja2
import sys
import platform

jinja_dict = {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did put this in alphabetical order and added the "is_included" variable.

"root": os.path.dirname(os.path.abspath(filename)),
"is_included": is_included,
"os": os,
"sys": sys,
"platform": platform,
"root": os.path.dirname(os.path.abspath(filename)),
"sys": sys,
}

return jinja2.Template(contents).render(**jinja_dict)
Expand Down Expand Up @@ -49,7 +50,7 @@ def handle_includes(root_filename, root_yaml):
filename=filename
))
with open(included_filename, "r") as f:
jinja_contents = render_jinja(f.read(), included_filename)
jinja_contents = render_jinja(f.read(), included_filename, is_included=True)
included_yaml_dict = yaml.safe_load(jinja_contents)
if included_yaml_dict is None:
raise ValueError("The file '{included_filename}' which was"
Expand Down Expand Up @@ -182,7 +183,7 @@ def merge_dependencies_version_specifications(yaml_dict, key_to_merge):
def load_yaml_dict(filename):
with open(filename, "r") as f:
contents = f.read()
rendered_contents = render_jinja(contents, filename)
rendered_contents = render_jinja(contents, filename, is_included=False)

import yaml
root_yaml = yaml.load(rendered_contents)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def obtain_yaml_dicts(root_yaml_filename):
contents = open(root_yaml_filename, "r").read()
contents = render_jinja(contents, filename=root_yaml_filename)
contents = render_jinja(contents, filename=root_yaml_filename, is_included=False)
root_yaml = yaml.load(contents)
dicts = handle_includes(root_yaml_filename, root_yaml).values()
dicts = list(dicts)
Expand Down
34 changes: 19 additions & 15 deletions tests/test_jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@


def test_jinja_root():
assert render_jinja("{{root}}", filename="path/to/file") == os.path.abspath("path/to")
assert render_jinja(
"{{root}}",
filename="path/to/file",
is_included=False,
) == os.path.abspath("path/to")


def test_jinja_os(monkeypatch):
Expand All @@ -22,13 +26,13 @@ def test_jinja_os(monkeypatch):
{%- endif %}
""").strip()

assert render_jinja(template, filename="") == "variable is not set"
assert render_jinja(template, filename="", is_included=False) == "variable is not set"

monkeypatch.setenv('ENV_VARIABLE', '1')
assert render_jinja(template, filename="") == "variable is set"
assert render_jinja(template, filename="", is_included=False) == "variable is set"

monkeypatch.setenv('ENV_VARIABLE', '2')
assert render_jinja(template, filename="") == "variable is not set"
assert render_jinja(template, filename="", is_included=False) == "variable is not set"


def test_jinja_sys(monkeypatch):
Expand All @@ -43,27 +47,27 @@ def test_jinja_sys(monkeypatch):
""").strip()

monkeypatch.setattr(sys, 'platform', 'linux')
assert render_jinja(template, filename="") == "linux!"
assert render_jinja(template, filename="", is_included=False) == "linux!"

monkeypatch.setattr(sys, 'platform', 'windows')
assert render_jinja(template, filename="") == "windows!"
assert render_jinja(template, filename="", is_included=False) == "windows!"

monkeypatch.setattr(sys, 'platform', 'darwin')
assert render_jinja(template, filename="") == "others!"
assert render_jinja(template, filename="", is_included=False) == "others!"


def test_jinja_platform(monkeypatch):
template = "{{ platform.python_revision() }}"
assert render_jinja(template, filename="") == platform.python_revision()
assert render_jinja(template, filename="", is_included=False) == platform.python_revision()


def test_jinja_invalid_template():
# TODO: change this to pytest's nicer syntax: with pytest.raises()
try:
render_jinja(textwrap.dedent("""\
with pytest.raises(jinja2.exceptions.TemplateSyntaxError):
render_jinja(
textwrap.dedent("""\
{%- if os.environ['ENV_VARIABLE'] == '1' %}
{% %}
"""), filename="")
pytest.fail("Should raise an exception")
except jinja2.exceptions.TemplateSyntaxError as e:
pass
"""),
filename="",
is_included=False,
)
31 changes: 31 additions & 0 deletions tests/test_load_yaml_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,34 @@ def test_get_env_name(mocker, tmpdir, cmd_line_name):
assert name == 'foo'
else:
assert name == 'bar'


def test_is_included_var(datadir):
import six
import textwrap
a_env_file = datadir / 'a.devenv.yml'
a_env_file.write_text(six.text_type(textwrap.dedent('''\
name: a
includes:
- {{root}}/b.devenv.yml
environment:
VARIABLE: value_a
IS_A_INCLUDED: {{is_included}}
''')))
b_env_file = datadir / 'b.devenv.yml'
b_env_file.write_text(six.text_type(textwrap.dedent('''\
name: b
environment:
{% if not is_included %}
VARIABLE: value_b
{% endif %}
IS_B_INCLUDED: {{is_included}}
''')))

conda_env, os_env = load_yaml_dict(str(a_env_file))
assert conda_env == {'name': 'a'}
assert os_env == {
'IS_A_INCLUDED': False,
'IS_B_INCLUDED': True,
'VARIABLE': 'value_a',
}