From 763788858aa3a559e72d4215635c3e4aca7a64c6 Mon Sep 17 00:00:00 2001 From: gwen Date: Tue, 24 Sep 2024 16:42:03 +0200 Subject: [PATCH] add test --- src/rougail/user_data_environment/data.py | 7 +-- tests/custom.py | 7 +++ tests/envvars/00_10_env_var/env | 8 +++ tests/test_load.py | 61 ++++++++++------------- 4 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 tests/custom.py create mode 100644 tests/envvars/00_10_env_var/env diff --git a/src/rougail/user_data_environment/data.py b/src/rougail/user_data_environment/data.py index a1a9406..8347b13 100644 --- a/src/rougail/user_data_environment/data.py +++ b/src/rougail/user_data_environment/data.py @@ -67,7 +67,7 @@ class RougailUserDataEnvironment: if option.isoptiondescription(): self.parse(option) else: - # FIXME gerer les erreurs + # FIXME handle exceptions # try except # ValueError suivant le type # PropertyError -> hidden ou frozen... @@ -76,6 +76,7 @@ class RougailUserDataEnvironment: self.load_environment(option) # except Exception as exc: # print(exc) + def load_environment(self, option): option_name = option.path() # the bash variable are in upper case @@ -94,7 +95,7 @@ class RougailUserDataEnvironment: option_bash_value = self.rougail_environment_vars.pop(option_name) if ismulti: # here we expect the bash option value of a multi to be coma separated: - option_bash_value = [opt.strip() for opt in option_bash_value.split(",")] + option_bash_value = [opt.strip() for opt in option_bash_value.split(",")] if type_obj: option_bash_value = [type_obj(opt) for opt in option_bash_value] elif type_obj: @@ -103,4 +104,4 @@ class RougailUserDataEnvironment: option.value.set(option_bash_value) except Exception as exc: print(exc) - + diff --git a/tests/custom.py b/tests/custom.py new file mode 100644 index 0000000..eb0e07f --- /dev/null +++ b/tests/custom.py @@ -0,0 +1,7 @@ +from os import environ +environ['TIRAMISU_LOCALE'] = 'en' +from tiramisu import StrOption + + +class CustomOption(StrOption): + pass diff --git a/tests/envvars/00_10_env_var/env b/tests/envvars/00_10_env_var/env new file mode 100644 index 0000000..5444afb --- /dev/null +++ b/tests/envvars/00_10_env_var/env @@ -0,0 +1,8 @@ +ROUGAIL_VARNAME2="tata" +ROUGAIL_VARNAME1="titi" +ROUGAIL_VARNAME23=" 18, 25 " +ROUGAIL_VARINT="5" +ROUGAIL_VARNAME34=" 58, 22 " +ROUGAIL_IDONTEXIST="blah" +ROUGAIL_IDONTEXIST2="hum" +ROUGAIL_myfamily.varname3='spam' diff --git a/tests/test_load.py b/tests/test_load.py index f826f8a..9321e05 100644 --- a/tests/test_load.py +++ b/tests/test_load.py @@ -1,24 +1,20 @@ from .custom import CustomOption from pathlib import Path +import os from pytest import fixture # , raises from ruamel.yaml import YAML +from dotenv import load_dotenv from rougail import RougailConfig, Rougail #from tiramisu import Config - -#from rougail.output_doc import RougailOutputDoc +from rougail.user_data_environment.data import RougailUserDataEnvironment dico_dirs = Path('../rougail/tests/dictionaries') #test_ok = set() -#excludes = set([ -# '60_5family_dynamic_unknown_suffix', -# '60_5family_dynamic_variable_outside_sub_suffix', -#]) - #for test in dico_dirs.iterdir(): # if (test / 'tiramisu').is_dir() and test.name not in excludes: # test_ok.add(test) @@ -26,7 +22,7 @@ dico_dirs = Path('../rougail/tests/dictionaries') #test_ok = list(test_ok) #test_ok.sort() -test_ok = [dico_dirs / '60_0family_dynamic_variable_suffix'] +test_ok = [dico_dirs / '00_10_env_var'] @fixture(scope="module", params=test_ok) @@ -36,46 +32,39 @@ def test_dir(request): def _test_dictionaries(test_dir): rougailconfig = RougailConfig.copy() -# rougailconfig['step.output'] = 'environment' + rougailconfig['step.user_data'] = ['environment'] + rougailconfig['main_namespace'] = None dirs = [str(test_dir / 'dictionaries' / 'rougail')] rougailconfig['custom_types']['custom'] = CustomOption rougailconfig['main_dictionaries'] = dirs return rougailconfig - -#def option_value(parent, key_is_option=False): -# for option, value in parent.items(): -# if option.isoptiondescription(): -# if not key_is_option and option.isleadership(): -# ret = [] -# for idx, datas in enumerate(option_value(value, key_is_option=True)): -# sub_option, sub_value = datas -# if not idx: -# sub_option = sub_option.path() -# key = sub_option -# for val in sub_value: -# ret.append({sub_option: val}) -# else: -# index = sub_option.index() -# sub_option = sub_option.path() -# ret[index][sub_option] = sub_value -# yield key, ret -# else: -# yield from option_value(value, key_is_option) -# elif key_is_option: -# yield option, value -# else: -# yield option.path(), value def test_dictionaries_warning(test_dir): rougailconfig = _test_dictionaries(test_dir) # loads the config in the tiramisu's meaning config = Rougail(rougailconfig).run() #Config(optiondescription["option_0"]) - print(type(config)) -# config_dict = dict(option_value(config.value.get())) -# print(config_dict) + config_dict = dict(config.value.get()) + #print(config_dict) + user_datas = {'errors': [], + 'warnings': [], + } + # loading the env file + envfile = Path('tests') / 'envvars' / '00_10_env_var' / 'env' + load_dotenv(envfile) + environment = RougailUserDataEnvironment(config, + rougailconfig=rougailconfig, + user_datas=user_datas, + ) + environment.run() + new_config = environment.config + #print(user_datas) + #print(os.getenv("ROUGAIL_VARNAME23")) + new_config_dict = dict(new_config.value.get()) + # print(new_config_dict) + assert str(new_config_dict) == """{: ['18', '25'], : [58, 22], : 'titi', : 2, : 'tata', : {: 'egg'}}""" #def test_dictionaries_asciidoc(test_dir): # _test_dictionaries(test_dir, 'asciidoc', True)