def test_tag_not_in_context_should_throw(): with pytest.raises(KeyNotInContextError) as err: context = Context({'key1': 'value1'}) context['input_string'] = '{key1} this is {key2} string' context.get_formatted('input_string') assert repr(err.value) == ( "KeyNotInContextError(\"Unable to format '{key1} this is " "{key2} string' at context['input_string'] with {key2}, because " "context['key2'] doesn't exist\",)")
def test_pyimport_with_pystring(): """Pystring evals with the globals namespace from pyimport.""" context = Context({'a': -1, 'b': 'xx', 'c': 4, 'pyImport': 'import math', 'out': PyString('abs(a) + len(b) + math.sqrt(c)')}) pyimport.run_step(context) assert context.get_formatted('out') == 5
def run_step(context: Context): print("filespec: %s" % str(context)) context.assert_key_has_value("vlnv", "filespec") context.assert_key_exists("out", "filespec") vlnv = context.get_formatted("vlnv") dbm = CoreDbMgr.inst() deps = dbm.get_depends(vlnv) for e in context["out"]: if "name" not in e.keys(): raise KeyNotInContextError("Missing 'name'") if "type" not in e.keys(): raise KeyNotInContextError("Missing 'type'") name = e["name"] file_types = set() for t in e["type"]: file_types.add(t.strip()) flags = {} if "flags" in e.keys(): for f in e["flags"]: flags[f] = True is_include = False if "include" in e.keys(): is_include = bool(e["include"]) files = dbm.collect_files(deps, file_types, flags, is_include) if name in context.keys(): if isinstance(context[name], list): context[name].extend(files) elif isinstance(context[name], str): context[name] += " ".join(files) else: raise Exception("Target for files is an unsupported type %s" % str(type(context[name]))) else: context[name] = files
def run_step(context: Context): context.assert_key_exists("cmd", "run_step") cmd_r = context.get_formatted("cmd") print("cmd=%s" % cmd_r) cmd_l = cmd_r.split() print("cmd_l=%s" % str(cmd_l)) cmd = [sys.executable, '-m'] for a in cmd_l: if a[0] == '"': cmd.append(a[1:len(a) - 1]) else: cmd.append(a) print("cmd=%s" % str(cmd)) if "out" in context.keys(): try: out = subprocess.check_output(cmd) except Exception as e: print("Exception: %s" % str(e)) traceback.print_exc() raise e context[context["out"]] = out.decode().strip() else: try: res = subprocess.run(cmd, stdout=sys.stdout, stderr=sys.stderr) except Exception as e: raise e if res.returncode != 0: raise Exception("Run failed") pass
def test_context_item_list_should_iterate(): context = Context({'key1': 'value1'}) context['input_string'] = ['string1', '{key1}', 'string3'] val = context.get_formatted('input_string') assert val == ['string1', 'value1', 'string3']
def test_context_item_not_a_string_should_return_as_is(): context = Context({'key1': 'value1'}) context['input_string'] = 77 val = context.get_formatted('input_string') assert val == 77
def test_single_curly_should_throw(): with pytest.raises(ValueError): context = Context({'key1': 'value1'}) context['input_string'] = '{key1} this { is {key2} string' context.get_formatted('input_string')
def test_string_interpolate_sic(): context = Context({'key1': 'down', 'key2': 'valleys', 'key3': 'value3'}) context['input_string'] = '[sic]"Piping {key1} the {key2} wild"' output = context.get_formatted('input_string') assert output == 'Piping {key1} the {key2} wild', ( "string interpolation incorrect")
def test_string_interpolate_escapes_double_curly_pair(): context = Context({'key1': 'down', 'key2': 'valleys', 'key3': 'value3'}) context['input_string'] = 'Piping {{down}} the valleys wild' output = context.get_formatted('input_string') assert output == 'Piping {down} the valleys wild', ( "string interpolation incorrect")
def test_string_interpolate_works_with_no_swaps(): context = Context({'key1': 'down', 'key2': 'valleys', 'key3': 'value3'}) context['input_string'] = 'Piping down the valleys wild' output = context.get_formatted('input_string') assert output == 'Piping down the valleys wild', ( "string interpolation incorrect")