def test_single_variable_keep_native_type(vars): result = shellexpand("$bool", vars) expected = True assert result == expected result = shellexpand("$list", vars) expected = [1, 2, 3] assert result == expected
def test_single_variable_plus_other_string_convert_to_string(vars): # yes, whitespace counts as extra string result = shellexpand("$bool ", vars) expected = "True " assert result == expected result = shellexpand("$list ", vars) expected = "[1, 2, 3] " assert result == expected
def test_escaped_variables(): query1 = "$FOO.$$BAR" query2 = "${FOO}.$${BAR}" # Notice that BAR is listed as a dependency # Even though it won't actually be used in the full shellexpand d1 = deps(query1) d2 = deps(query2) assert d1 == {"FOO", "BAR"} assert d2 == {"FOO", "BAR"} # for your benefit, the full expansions look like this: # note that BAR is not even defined since it won't be used anyway vars = {"FOO": "bar"} actual1 = shellexpand(query1, vars) actual2 = shellexpand(query2, vars) assert actual1 == "bar.$BAR" assert actual2 == "bar.${BAR}"
def execute(self, params, variables=None): if type(params) != str: raise TypeError( "Expecting a string with the set of variables to load. Instead received: ", params, ) section_name, filename = split_file_component(params) raw_variables = self.loader.get_section(section_name, filename) expanded_variables = shellexpand(raw_variables, variables) return expanded_variables
def test_shellexpand_dict(vars): test_input = { "static": "This should not change.", "dynamic1": "${A}_$B", "dynamic2": "$FOO", } result = shellexpand(test_input, vars) expected = { "static": "This should not change.", "dynamic1": "AA_BBB", "dynamic2": "bar", } assert result == expected
def test_missing_variable_throws_exception(vars): with pytest.raises(VariableNotSet): shellexpand("$NOT_DEFINED", vars)
def test_escaped_dollars(home, vars): result = shellexpand("~/$$A/$${B}/$$${C:C}/$$$$$B", vars) expected = home + "/$A/${B}/$C/$$BBB" assert result == expected
def test_full_expansion(home, vars): result = shellexpand("~/$A/${B}/${C:C}", vars) expected = f"{home}/AA/BBB/C" assert result == expected
def test_curly_defaulted_variable(vars): result = shellexpand("${FOO}_${NOT_DEFINED_YET_HAS_DEFAULT:yes}", vars) expected = "bar_yes" assert result == expected
def test_curly_variable(vars): result = shellexpand("${FOO}", vars) expected = "bar" assert result == expected
def test_tilde_expansion_using_overwritten_home(vars): custom_home = "/totally/different" vars["HOME"] = custom_home result = shellexpand("~/a/b/c", vars) expected = custom_home + "/a/b/c" assert result == expected
def test_tilde_expansion_using_system_home(home, vars): result = shellexpand("~/a/b/c", vars) expected = home + "/a/b/c" assert result == expected
def test_shellexpand_complex(vars): input = {"list": ["$A", "${B}", "${B:99}", {"inner_dict": "$FOO"}]} actual = shellexpand(input, vars) expected = {"list": ["AA", "BBB", "BBB", {"inner_dict": "bar"}]} assert actual == expected
def test_shellexpand_bool(vars): input_value = True actual = shellexpand(input_value, vars) expected = True assert actual == expected
def test_shellexpand_list(vars): input_list = ["same", "$FOO", "$A"] result = shellexpand(input_list, vars) expected = ["same", "bar", "AA"] assert result == expected
def modify_params(self, params, variables=None): return shellexpand(params, variables)