-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rather than maintaining our own parser, jinja2 can be used. Miguel G. pointed out that we can simply change the glyphs used to denote a variable declaration to avoid conflicts with Jira syntax. This has numerous advantages. Contrast to 2 commits ago: - variable declartions -> @@var@@ to {@var@} - No way to specify an optional value (that's OK I think) - Use Jinja2 templating by passing raw text from reading the template instead of the dict loaded by yaml.safe_load()
- Loading branch information
Showing
5 changed files
with
157 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,46 @@ | ||
#!/usr/bin/python3 | ||
|
||
from jirate.template_vars import apply_values, _populate_defaults | ||
from jirate.template_vars import apply_values | ||
|
||
import pytest # NOQA | ||
import types | ||
|
||
|
||
def test_populate_defaults_simple(): | ||
inp = '@@a:b@@' | ||
values = {} | ||
def test_apply_var_type1(): | ||
inp = """ | ||
{% set var=var or "1.0" %} | ||
fork: {@var@} | ||
""" | ||
exp = 'fork: 1.0' | ||
assert apply_values(inp).strip() == exp.strip() | ||
|
||
_populate_defaults(inp, values) | ||
assert values == {'a': 'b'} | ||
|
||
def test_apply_var_type2(): | ||
inp = """ | ||
fork: {@var|default('1.0')@} | ||
""" | ||
exp = 'fork: 1.0' | ||
assert apply_values(inp).strip() == exp.strip() | ||
|
||
def test_ok_variable_no_default(): | ||
inp = '@@a@@' | ||
values = {'a': 'b'} | ||
|
||
ret = apply_values(inp, values) | ||
assert ret == 'b' | ||
|
||
|
||
def test_populate_defaults_no_overwrite(): | ||
inp = '@@a:b@@' | ||
values = {'a': 'c'} | ||
|
||
ret = apply_values(inp, values) | ||
assert ret == 'c' | ||
|
||
|
||
def test_populate_defaults_first_value(): | ||
inp = '@@a:b@@ @@a:c@@' | ||
values = {} | ||
|
||
ret = apply_values(inp, values) | ||
assert ret == 'b b' | ||
|
||
|
||
def test_populate_defaults_no_overwrite2(): | ||
inp = '@@a:b@@ @@a:c@@' | ||
values = {'a': 'd'} | ||
|
||
ret = apply_values(inp, values) | ||
assert ret == 'd d' | ||
|
||
|
||
def test_populate_defaults_second_def(): | ||
inp = '@@a@@ @@a:c@@' | ||
values = {} | ||
|
||
ret = apply_values(inp, values) | ||
assert ret == 'c c' | ||
|
||
|
||
def test_multi_str1(): | ||
inp = 'abc@@a@@def@@b@@ghi@@a@@jkl@@c@@' | ||
values = {'a': '1', 'b': '2', 'c': '3'} | ||
|
||
ret = apply_values(inp, values) | ||
assert ret == 'abc1def2ghi1jkl3' | ||
|
||
|
||
def test_list1(): | ||
inp = ['one', '@@ver@@', 'two'] | ||
values = {'ver': '1.0'} | ||
|
||
ret = apply_values(inp, values) | ||
assert ret == ['one', '1.0', 'two'] | ||
|
||
|
||
def test_dict1(): | ||
inp = {'top': '@@ver@@', 'bottom': '@@old:0.1@@'} | ||
values = {'ver': '1.0'} | ||
|
||
ret = apply_values(inp, values) | ||
assert ret == {'top': '1.0', 'bottom': '0.1'} | ||
|
||
|
||
def test_complex1(): | ||
inp = {'top': '@@ver@@', 'bottom': ['@@old:0.1@@', '@@date@@', {'pork': '@@bacon@@'}]} | ||
values = {'ver': '1.0', 'date': '2024-07-25', 'bacon': 'yum'} | ||
|
||
ret = apply_values(inp, values) | ||
assert ret == {'top': '1.0', 'bottom': ['0.1', '2024-07-25', {'pork': 'yum'}]} | ||
|
||
|
||
def test_invalid_var(): | ||
inp = {'fun': '@@bar@@'} | ||
values = {'baz': '123'} | ||
|
||
def test_missing_var(): | ||
inp = """ | ||
fork: {@var|default('1.0')@} | ||
knife: {@butter@} | ||
""" | ||
with pytest.raises(ValueError): | ||
ret = apply_values(inp, values) | ||
apply_values(inp) | ||
|
||
|
||
def test_provided_var(): | ||
inp = """ | ||
fork: {@var|default('1.0')@} | ||
knife: {@butter@} | ||
""" | ||
exp = """ | ||
fork: 2.0 | ||
knife: salted | ||
""" | ||
values = {'var': '2.0', | ||
'butter': 'salted'} | ||
assert apply_values(inp, values).strip() == exp.strip() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ editor>=1.2.1 | |
PyYAML | ||
rich | ||
jira>=3.8.0 | ||
Jinja2>=3.0.0 | ||
python-dateutil | ||
toolchest | ||
prettytable | ||
|