Skip to content

Commit 8ced43a

Browse files
authored
feat: add startingwith to jinja whitelist (#1679)
1 parent cf1a238 commit 8ced43a

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

docs/reference/jinja.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,28 @@ You can also check for the existence of an environment variable:
199199
- `${{ env.exists("MY_ENV_VAR") }}` will return `true` if the environment
200200
variable `MY_ENV_VAR` is set and `false` otherwise.
201201

202+
## Tests
203+
204+
You can write tests using minijinja to check whether objects have certain properties.
205+
The syntax for a filter is `{{ variable is test_name }}`.
206+
207+
- `undefined`: Check whether a variable is undefined.
208+
- `defined`: Check whether a variable is defined.
209+
- `none`: Check whether a variable is none.
210+
- `safe`: Check whether a variable is safe.
211+
- `escaped`: Check whether a variable is escaped. Same as `is safe`.
212+
- `odd`: Check whether a number is odd.
213+
- `even`: Check whether a number is even.
214+
- `number`: Check whether a variable is a number.
215+
- `integer`: Check whether a variable is an integer.
216+
- `int`: Check whether a variable is an integer. Same as `is integer`.
217+
- `float`: Check whether a variable is a float.
218+
- `string`: Check whether a variable is a string.
219+
- `sequence`: Check whether a variable is a sequence.
220+
- `boolean`: Check whether a variable is a boolean.
221+
- `startingwith`: Check whether a variable is starting with another string: `{{ python is startingwith('3.12') }}`
222+
- `endingwith`: Check whether a variable is starting with another string: `{{ python is endingwith('.*') }}`
223+
202224
## Filters
203225

204226
A feature of `jinja` is called "filters". Filters are functions that can be

src/recipe/jinja.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ fn default_tests(env: &mut Environment) {
309309
env.add_test("string", minijinja::tests::is_string);
310310
env.add_test("sequence", minijinja::tests::is_sequence);
311311
env.add_test("boolean", minijinja::tests::is_boolean);
312+
env.add_test("startingwith", minijinja::tests::is_startingwith);
313+
env.add_test("endingwith", minijinja::tests::is_endingwith);
312314

313315
// operators
314316
env.add_test("eq", minijinja::tests::is_eq);

test-data/recipes/jinja-types/recipe.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ context:
3232
# quoted bool is quoted in variants.yaml and thus should be a string
3333
is_quoted_true_var: ${{ quoted_bool is string }}
3434
is_quoted_int_var: ${{ quoted_int is string }}
35+
starting_with: ${{ xstring is startingwith('blah') }}
36+
not_starting_with: ${{ xstring is startingwith('foo') }}
37+
ending_with: ${{ xstring is startingwith('blah') }}
38+
not_ending_with: ${{ xstring is startingwith('foo') }}
3539

3640
package:
3741
name: testtypes

test/end-to-end/__snapshots__/test_simple/test_jinja_types.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"cquoted": "5",
88
"cquoted_bool": "true",
99
"cstring": "blah",
10+
"ending_with": true,
1011
"float": "1.23",
1112
"inline_list": [
1213
"a",
@@ -35,5 +36,8 @@
3536
"b",
3637
"c"
3738
],
39+
"not_ending_with": false,
40+
"not_starting_with": false,
41+
"starting_with": true,
3842
"string": "blah"
3943
}

0 commit comments

Comments
 (0)