1010from jinja2 .exceptions import UndefinedError
1111from pytest_shell import fs
1212
13- from opentaskpy .config .loader import ConfigLoader
13+ from opentaskpy .config .loader import ConfigLoader , LazyResolvedDict
1414from opentaskpy .exceptions import VariableResolutionTooDeepError
1515
1616GLOBAL_VARIABLES : str | None = None
@@ -312,7 +312,13 @@ def test_custom_plugin(tmpdir):
312312 [
313313 {
314314 f"{ tmpdir } /variables.json.j2" : {
315- "content" : '{"test": "{{ lookup(\' test_plugin\' ) }}"}'
315+ "content" : (
316+ "{"
317+ '"DD": "{{ now().strftime(\' %d\' ) }}",'
318+ '"YYYY": "{{ now().strftime(\' %Y\' ) }}",'
319+ '"NESTED_VAR": {"NESTED_VAR1": "{{ YYYY }}"},'
320+ '"test": "{{ lookup(\' test_plugin\' , dd=DD, yyyy=NESTED_VAR.NESTED_VAR1) }}"}'
321+ )
316322 }
317323 },
318324 ]
@@ -325,8 +331,120 @@ def test_custom_plugin(tmpdir):
325331
326332 # Test that the global variables are loaded correctly
327333 config_loader = ConfigLoader (tmpdir )
334+ global_variables = config_loader .get_global_variables ()
335+
336+ dd = datetime .now ().strftime ("%d" )
337+ yyyy = datetime .now ().strftime ("%Y" )
338+
339+ assert global_variables ["DD" ] == dd
340+ assert global_variables ["YYYY" ] == yyyy
341+ assert global_variables ["NESTED_VAR" ] == {"NESTED_VAR1" : yyyy }
342+ assert global_variables ["test" ] == f"hello { int (dd )+ 1 } { int (yyyy )+ 1 } "
343+
344+
345+ def test_custom_plugin_lazy_load (tmpdir ):
346+ os .environ ["OTF_LAZY_LOAD_VARIABLES" ] = "1"
347+
348+ fs .create_files (
349+ [
350+ {
351+ f"{ tmpdir } /variables.json.j2" : {
352+ "content" : (
353+ "{"
354+ '"DD": "{{ now().strftime(\' %d\' ) }}",'
355+ '"YYYY": "{{ now().strftime(\' %Y\' ) }}",'
356+ '"NESTED_VAR": {"NESTED_VAR1": "{{ YYYY }}"},'
357+ '"test": "{{ lookup(\' test_plugin\' , dd=DD, yyyy=NESTED_VAR.NESTED_VAR1) }}"}'
358+ )
359+ }
360+ },
361+ {f"{ tmpdir } /task.json" : {"content" : '{"test": "{{ test }}"}' }},
362+ ]
363+ )
364+
365+ os .symlink (
366+ os .path .join (os .path .dirname (__file__ ), "../test/cfg" , "plugins" ),
367+ f"{ tmpdir } /plugins" ,
368+ )
369+
370+ config_loader = ConfigLoader (tmpdir )
371+
372+ del os .environ ["OTF_LAZY_LOAD_VARIABLES" ]
373+
374+ dd = datetime .now ().strftime ("%d" )
375+ yyyy = datetime .now ().strftime ("%Y" )
376+
377+ assert config_loader .load_task_definition ("task" , cache = False ) == {
378+ "test" : f"hello { int (dd )+ 1 } { int (yyyy )+ 1 } "
379+ }
380+
381+
382+ def test_resolve_lookup_value_variants (tmpdir ):
383+ fs .create_files (
384+ [
385+ {
386+ f"{ tmpdir } /variables.json.j2" : {
387+ "content" : json .dumps (
388+ {
389+ "YYYY" : "2026" ,
390+ "MM" : "03" ,
391+ }
392+ )
393+ }
394+ },
395+ ]
396+ )
397+
398+ config_loader = ConfigLoader (tmpdir )
399+
400+ assert config_loader ._resolve_lookup_value ({"year" : "{{ YYYY }}" }) == {
401+ "year" : "2026"
402+ }
403+ assert config_loader ._resolve_lookup_value (["{{ YYYY }}" , "{{ MM }}" , 7 ]) == [
404+ "2026" ,
405+ "03" ,
406+ 7 ,
407+ ]
408+ assert (
409+ config_loader ._resolve_lookup_value (
410+ "{{ lookup('file', path='/tmp/skip.txt') }}" , resolve_lookups = False
411+ )
412+ == "{{ lookup('file', path='/tmp/skip.txt') }}"
413+ )
414+ assert config_loader ._resolve_lookup_value (1234 ) == 1234
415+
416+
417+ def test_lazy_resolved_dict_accessors (tmpdir ):
418+ fs .create_files (
419+ [
420+ {
421+ f"{ tmpdir } /variables.json.j2" : {
422+ "content" : json .dumps (
423+ {
424+ "YYYY" : "2026" ,
425+ "MM" : "03" ,
426+ }
427+ )
428+ }
429+ },
430+ ]
431+ )
432+
433+ config_loader = ConfigLoader (tmpdir )
434+ lazy_dict = LazyResolvedDict (
435+ {
436+ "nested" : {"value" : "{{ YYYY }}" },
437+ "items" : ["{{ MM }}" , 2 ],
438+ "lookup" : "{{ lookup('file', path='/tmp/skip.txt') }}" ,
439+ },
440+ config_loader ._resolve_lookup_value ,
441+ resolve_lookups = False ,
442+ )
328443
329- assert config_loader .get_global_variables () == {"test" : "hello" }
444+ assert lazy_dict ["nested" ]["value" ] == "2026"
445+ assert lazy_dict ["items" ] == ["03" , 2 ]
446+ assert lazy_dict .get ("lookup" ) == "{{ lookup('file', path='/tmp/skip.txt') }}"
447+ assert lazy_dict .get ("missing" , "fallback" ) == "fallback"
330448
331449
332450def test_default_filters (tmpdir ):
0 commit comments