from .custom import CustomOption from pathlib import Path from os import getcwd, listdir, environ, makedirs from os.path import isfile, join, isdir, dirname from pathlib import Path import json from pytest import fixture, raises from ruamel.yaml import YAML from dotenv import load_dotenv from rougail import RougailConfig, Rougail from rougail.user_data_environment.data import RougailUserDataEnvironment dico_dirs = Path('../rougail/tests/dictionaries') #env_test_folder = '00_6string' # path configuration _here = Path(__file__).resolve().parent envtest = _here / 'tests' / 'envvars' test_ok = set() test_raise = set() for test in listdir(dico_dirs): test_ok.add(test) if test != '__pycache__': test_raise.add(test) #test_ok = [dico_dirs / env_test_folder] excludes = set() test_ok -= excludes @fixture(scope="module", params=test_ok) def test_dir(request): return request.param def _test_dictionaries(test_dir): "rougail config settings" rougailconfig = RougailConfig.copy() 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 test_dictionaries_warning(test_dir): "tests the '00_6string' folder" rougailconfig = _test_dictionaries(test_dir) # populate tests if not already exists dest_dir = Path('tests') / 'envvars' / test_dir.name populate(dest_dir, rougailconfig) # loads the config in the tiramisu's meaning config = Rougail(rougailconfig).run() #Config(optiondescription["option_0"]) config_dict = dict(config.value.get()) user_datas = {'errors': [], 'warnings': [], } # loading the env file envfile = dest_dir / 'env'/ 'all.env' load_dotenv(envfile) # loads the environment variables in the tiramisu config environment = RougailUserDataEnvironment(config, rougailconfig=rougailconfig, user_datas=user_datas, ) environment.run() new_config = environment.config new_config_dict = dict(new_config.value.get()) # expected output with open(Path('tests') / 'makedict' / env_test_folder / 'output.json') as json_file: expected = json.load(json_file) # here is the effective test for key, value in new_config_dict.items(): assert expected["rougail."+key.name()] == value def populate(dest_dir, rougailconfig): for level in ['all', 'mandatories']: environment_file = dest_dir / 'env' / f'{level}.env' makedict_file = dest_dir / 'makedict' / f'{level}.env' if not environment_file.is_file() or not makedict_file.is_file(): config = Rougail(rougailconfig).run() if level == 'all': root_config = config.unrestraint else: root_config = config.unrestraint.value.mandatory() values = ['ROUGAIL_' + variable.path().upper() + '="' + get_value(variable) + '"' for variable in get_variables(root_config)] if not environment_file.is_file(): with environment_file.open('w') as envfh: envfh.write('\n'.join(values) + '\n') if not makedict_file.is_file(): with environment_file.open('w') as envfh: envfh.write('\n'.join(values) + '\n') def get_value(variable): tests = variable.information.get('test', None) if not tests: if variable.type() == 'integer': tests = [1, 2, 3] elif variable.type() == 'float': tests = [1.1, 2.2, 3.3] elif variable.type() == 'port': tests = ['80', '443'] elif variable.type() == 'boolean': tests = [True] elif variable.type() == 'domain name': tests = ['domain1.lan', 'domain2.lan'] elif variable.type() == 'choice': tests = variable.value.list() else: tests = ['string1', 'string2', 'string3'] if not variable.ismulti() or (variable.isfollower() and variable.ismulti() is True): tests = tests[0] variable.value.set(tests) if isinstance(tests, list): tests = ','.join([str(test) for test in tests]) else: tests = str(tests) return tests def get_variables(config): for key in config: if key.isoptiondescription(): yield from get_variables(key) else: yield key 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