def test_dollar_symbol_not_followed_by_bracket(): subst = parse_substitution('$0 $1') assert len(subst) == 1 assert subst[0].perform(None) == '$0 $1' subst = parse_substitution("$(test '$0 $1')") assert len(subst) == 1 assert subst[0].perform(None) == '$0 $1'
def test_env_subst(): subst = parse_substitution('$(env asd bsd)') assert len(subst) == 1 env = subst[0] assert isinstance(env, EnvironmentVariable) assert 'asd' == ''.join([x.perform(None) for x in env.name]) assert 'bsd' == ''.join([x.perform(None) for x in env.default_value]) subst = parse_substitution('$(env asd)') assert len(subst) == 1 env = subst[0] assert isinstance(env, EnvironmentVariable) assert 'asd' == ''.join([x.perform(None) for x in env.name]) assert '' == ''.join([x.perform(None) for x in env.default_value])
def evaluate(self, context: LaunchContext) -> Path: """Evaluate and return a parameter file path.""" if self.__evaluated_param_file is not None: return self.__evaluated_param_file param_file = self.__param_file if isinstance(param_file, list): # list of substitutions param_file = perform_substitutions(context, self.__param_file) allow_substs = perform_typed_substitution(context, self.__allow_substs, data_type=bool) param_file_path: Path = Path(param_file) if allow_substs: with open(param_file_path, 'r') as f, NamedTemporaryFile(mode='w', prefix='launch_params_', delete=False) as h: parsed = perform_substitutions(context, parse_substitution(f.read())) try: yaml.safe_load(parsed) except Exception: raise SubstitutionFailure( 'The substituted parameter file is not a valid yaml file' ) h.write(parsed) param_file_path = Path(h.name) self.__created_tmp_file = True self.__evaluated_param_file = param_file_path return param_file_path
def test_text_only(): subst = parse_substitution("'yes'") assert len(subst) == 1 assert subst[0].perform(None) == "'yes'" subst = parse_substitution('"yes"') assert len(subst) == 1 assert subst[0].perform(None) == '"yes"' subst = parse_substitution('10') assert len(subst) == 1 assert subst[0].perform(None) == '10' subst = parse_substitution('10e4') assert len(subst) == 1 assert subst[0].perform(None) == '10e4' subst = parse_substitution('10e4') assert len(subst) == 1 assert subst[0].perform(None) == '10e4'
def test_text_with_embedded_substitutions(): subst = parse_substitution('why_$(test asd)_asdasd_$(test bsd)') assert len(subst) == 4 assert subst[0].perform(None) == 'why_' assert subst[1].perform(None) == 'asd' assert subst[2].perform(None) == '_asdasd_' assert subst[3].perform(None) == 'bsd'
def test_nested_substitutions(): subst = parse_substitution('$(env what/$(test asd) 10) 10 10)') assert len(subst) == 2 assert len(subst[0].name) == 2 assert subst[0].name[0].perform(None) == 'what/' assert subst[0].name[1].perform(None) == 'asd' assert subst[0].default_value[0].perform(None) == '10' assert subst[1].perform(None) == ' 10 10)'
def test_env_subst(): subst = parse_substitution('$(env asd bsd)') assert len(subst) == 1 env = subst[0] assert isinstance(env, EnvironmentVariable) assert 'asd' == perform_substitutions_without_context(env.name) assert 'bsd' == perform_substitutions_without_context(env.default_value) subst = parse_substitution("$(env asd '')") assert len(subst) == 1 env = subst[0] assert isinstance(env, EnvironmentVariable) assert 'asd' == perform_substitutions_without_context(env.name) assert '' == perform_substitutions_without_context(env.default_value) subst = parse_substitution('$(env asd)') assert len(subst) == 1 env = subst[0] assert isinstance(env, EnvironmentVariable) assert 'asd' == perform_substitutions_without_context(env.name) assert env.default_value is None
def test_get_log_dir_frontend(log_dir): """Test log_dir frontend substitution.""" launch.logging.reset() launch.logging.launch_config.log_dir = log_dir subst = parse_substitution('$(log_dir)') assert len(subst) == 1 result = subst[0] assert isinstance(result, TextSubstitution) assert result.text == log_dir
def test_quoted_nested_substitution(): subst = parse_substitution('go_to_$(env WHERE asd)_of_$(env ' "'something $(test 10)')") assert len(subst) == 4 assert subst[0].perform(None) == 'go_to_' assert subst[1].name[0].perform(None) == 'WHERE' assert subst[1].default_value[0].perform(None) == 'asd' assert subst[2].perform(None) == '_of_' assert subst[3].name[0].perform(None) == 'something ' assert subst[3].name[1].perform(None) == '10' assert subst[3].default_value is None
def test_combining_quotes_nested_substitution(): subst = parse_substitution( '$(env "asd_bsd_qsd_$(test \'asd_bds\')" \'$(env DEFAULT)_qsd\')') context = LaunchContext() assert len(subst) == 1 assert len(subst[0].name) == 2 assert subst[0].name[0].perform(context) == 'asd_bsd_qsd_' assert subst[0].name[1].perform(context) == "'asd_bds'" assert len(subst[0].default_value) == 2 assert subst[0].default_value[0].name[0].perform(context) == 'DEFAULT' assert subst[0].default_value[0].default_value is None assert subst[0].default_value[1].perform(context) == '_qsd'
def test_double_quoted_nested_substitution(): subst = parse_substitution( r'$(env "asd_bsd_qsd_$(test \"asd_bds\")" "$(env DEFAULT)_qsd")') context = LaunchContext() assert len(subst) == 1 assert len(subst[0].name) == 2 assert subst[0].name[0].perform(context) == 'asd_bsd_qsd_' assert subst[0].name[1].perform(context) == '"asd_bds"' assert len(subst[0].default_value) == 2 assert subst[0].default_value[0].name[0].perform(context) == 'DEFAULT' assert subst[0].default_value[0].default_value is None assert subst[0].default_value[1].perform(context) == '_qsd'
def test_escaped_characters(): subst = parse_substitution(r'$(env what/\$\(test asd\\\)) 10 10)') assert len(subst) == 2 assert subst[0].name[0].perform(None) == 'what/$(test' assert subst[0].default_value[0].perform(None) == r'asd\)' assert subst[1].perform(None) == ' 10 10)'
def test_substitution_with_multiple_arguments(): subst = parse_substitution('$(env what heck)') assert len(subst) == 1 subst = subst[0] assert subst.name[0].perform(None) == 'what' assert subst.default_value[0].perform(None) == 'heck'
def test_no_text(): subst = parse_substitution('') assert len(subst) == 1 assert subst[0].perform(None) == ''
def parse_substitution(self, value: Text) -> SomeSubstitutionsType: return parse_substitution(value)
def test_eval_subst(): subst = parse_substitution(r'$(eval "\'asd\' + \'bsd\'")') assert len(subst) == 1 expr = subst[0] assert isinstance(expr, PythonExpression) assert 'asdbsd' == expr.perform(LaunchContext())
def test_dirname_subst(): subst = parse_substitution('$(dirname)') assert len(subst) == 1 assert isinstance(subst[0], ThisLaunchFileDir)