def test_subst_overriding_gvars(self): """Test scons_subst(): supplying an overriding gvars dictionary""" env = DummyEnv({'XXX' : 'xxx'}) result = scons_subst('$XXX', env, gvars=env.Dictionary()) assert result == 'xxx', result result = scons_subst('$XXX', env, gvars={'XXX' : 'yyy'}) assert result == 'yyy', result
def test_subst_type_errors(self): """Test scons_subst(): handling type errors""" env = DummyEnv(self.loc) try: scons_subst("${NONE[2]}", env, gvars={'NONE':None}) except SCons.Errors.UserError as e: expect = [ # Python 2.7 and later "TypeError `'NoneType' object is not subscriptable' trying to evaluate `${NONE[2]}'", # Python 2.7 and later under Fedora "TypeError `'NoneType' object has no attribute '__getitem__'' trying to evaluate `${NONE[2]}'", ] assert str(e) in expect, e else: raise AssertionError("did not catch expected UserError") try: def func(a, b, c): pass scons_subst("${func(1)}", env, gvars={'func':func}) except SCons.Errors.UserError as e: expect = [ # Python 3.5 (and 3.x?) "TypeError `func() missing 2 required positional arguments: 'b' and 'c'' trying to evaluate `${func(1)}'", # Python 3.10 "TypeError `scons_subst_TestCase.test_subst_type_errors.<locals>.func() missing 2 required positional arguments: 'b' and 'c'' trying to evaluate `${func(1)}'", ] assert str(e) in expect, repr(str(e)) else: raise AssertionError("did not catch expected UserError")
def test_subst_raw_function(self): """Test scons_subst(): fetch function with SUBST_RAW plus conv""" # Test that the combination of SUBST_RAW plus a pass-through # conversion routine allows us to fetch a function through the # dictionary. CommandAction uses this to allow delayed evaluation # of $SPAWN variables. env = DummyEnv(self.loc) gvars = env.Dictionary() x = lambda x: x r = scons_subst("$CALLABLE1", env, mode=SUBST_RAW, conv=x, gvars=gvars) assert r is self.callable_object_1, repr(r) r = scons_subst("$CALLABLE1", env, mode=SUBST_RAW, gvars=gvars) assert r == 'callable-1', repr(r) # Test how we handle overriding the internal conversion routines. def s(obj): return obj n1 = self.MyNode('n1') env = DummyEnv({'NODE' : n1}) gvars = env.Dictionary() node = scons_subst("$NODE", env, mode=SUBST_RAW, conv=s, gvars=gvars) assert node is n1, node node = scons_subst("$NODE", env, mode=SUBST_CMD, conv=s, gvars=gvars) assert node is n1, node node = scons_subst("$NODE", env, mode=SUBST_SIG, conv=s, gvars=gvars) assert node is n1, node
def test_subst_SUBST_modes(self): """Test scons_subst(): SUBST_* modes""" env = DummyEnv(self.loc) subst_cases = self.subst_cases[:] gvars = env.Dictionary() failed = 0 while subst_cases: input, eraw, ecmd, esig = subst_cases[:4] result = scons_subst(input, env, mode=SUBST_RAW, gvars=gvars) if result != eraw: if failed == 0: print() print(" input %s => RAW %s did not match %s" % (repr(input), repr(result), repr(eraw))) failed = failed + 1 result = scons_subst(input, env, mode=SUBST_CMD, gvars=gvars) if result != ecmd: if failed == 0: print() print(" input %s => CMD %s did not match %s" % (repr(input), repr(result), repr(ecmd))) failed = failed + 1 result = scons_subst(input, env, mode=SUBST_SIG, gvars=gvars) if result != esig: if failed == 0: print() print(" input %s => SIG %s did not match %s" % (repr(input), repr(result), repr(esig))) failed = failed + 1 del subst_cases[:4] assert failed == 0, "%d subst() mode cases failed" % failed
def test_subst_syntax_errors(self): """Test scons_subst(): handling syntax errors""" env = DummyEnv(self.loc) try: scons_subst('$foo.bar.3.0', env) except SCons.Errors.UserError as e: expect = [ # Python 2.5 and later "SyntaxError `invalid syntax (<string>, line 1)' trying to evaluate `$foo.bar.3.0'", ] assert str(e) in expect, e else: raise AssertionError("did not catch expected UserError")
def test_subst_balance_errors(self): """Test scons_subst(): handling syntax errors""" env = DummyEnv(self.loc) try: scons_subst('$(', env, mode=SUBST_SIG) except SCons.Errors.UserError as e: assert str(e) == "Unbalanced $(/$) in: $(", str(e) else: raise AssertionError("did not catch expected UserError") try: scons_subst('$)', env, mode=SUBST_SIG) except SCons.Errors.UserError as e: assert str(e) == "Unbalanced $(/$) in: $)", str(e) else: raise AssertionError("did not catch expected UserError")
def test_subst_env(self): """Test scons_subst(): expansion dictionary""" # The expansion dictionary no longer comes from the construction # environment automatically. env = DummyEnv(self.loc) s = scons_subst('$AAA', env) assert s == '', s
def create_asr_experiment(target, source, env): # the first three sources are the original configuration dictionaries files, directories, parameters = [x.read() for x in source[:3]] files = {k : env.File(v) for k, v in files.iteritems()} directories = {k : env.Dir(os.path.abspath(v)) for k, v in directories.iteritems()} # the remainder are template files templates = source[3:6] # create one big configuration dictionary config = {k : v for k, v in sum([list(y) for y in [files.iteritems(), directories.iteritems(), parameters.iteritems()]], [])} config["GRAPH_OFILE"] = env.File(os.path.join(config["ASR_OUTPUT_PATH"].rstr(), "dnet.bin.gz")) config["CTM_OPATH"] = env.Dir(os.path.abspath(os.path.join(config["ASR_OUTPUT_PATH"].rstr(), "ctm"))) config["LAT_OPATH"] = env.Dir(os.path.abspath(os.path.join(config["ASR_OUTPUT_PATH"].rstr(), "lat"))) config["DATABASE_FILE"] = config["SEGMENTATION_FILE"] # print dictionary for debugging logging.debug("%s", "\n".join(["%s = %s" % (k, v) for k, v in config.iteritems()])) # perform substitution on each template file, write to appropriate location for template, final in zip(templates, target): with open(template.rstr()) as ifd, open(final.rstr(), "w") as ofd: ofd.write(scons_subst(ifd.read(), env=env, lvars=config)) return None
def test_subst_attribute_errors(self): """Test scons_subst(): handling attribute errors""" env = DummyEnv(self.loc) try: class Foo: pass scons_subst('${foo.bar}', env, gvars={'foo':Foo()}) except SCons.Errors.UserError as e: expect = [ "AttributeError `bar' trying to evaluate `${foo.bar}'", "AttributeError `Foo instance has no attribute 'bar'' trying to evaluate `${foo.bar}'", "AttributeError `'Foo' instance has no attribute 'bar'' trying to evaluate `${foo.bar}'", "AttributeError `'Foo' object has no attribute 'bar'' trying to evaluate `${foo.bar}'", ] assert str(e) in expect, e else: raise AssertionError("did not catch expected UserError")
def test_subst_callable_with_default_expansion(self): """Test scons_subst(): expanding a callable with a default value arg""" env = DummyEnv(self.loc) gvars = env.Dictionary() newcom = scons_subst("test $CallableWithDefault $SOURCES $TARGETS", env, target=self.MyNode('t'), source=self.MyNode('s'), gvars=gvars) assert newcom == "test CallableWithDefault: default s t", newcom
def test_subst_callable_expansion(self): """Test scons_subst(): expanding a callable""" env = DummyEnv(self.loc) gvars = env.Dictionary() newcom = scons_subst("test $CMDGEN1 $SOURCES $TARGETS", env, target=self.MyNode('t'), source=self.MyNode('s'), gvars=gvars) assert newcom == "test foo bar with spaces.out s t", newcom
def test_subst_partial_callable_with_no_default_expansion(self): """Test scons_subst(): expanding a functools.partial callable which sets the value for extraneous function argument""" env = DummyEnv(self.loc) gvars = env.Dictionary() newcom = scons_subst("test $PartialCallableNoDefault $SOURCES $TARGETS", env, target=self.MyNode('t'), source=self.MyNode('s'), gvars=gvars) assert newcom == "test CallableWithNoDefault: partialNoDefault s t", newcom
def test_subst_target_source(self): """Test scons_subst(): target= and source= arguments""" env = DummyEnv(self.loc) t1 = self.MyNode('t1') t2 = self.MyNode('t2') s1 = self.MyNode('s1') s2 = self.MyNode('s2') result = scons_subst("$TARGET $SOURCES", env, target=[t1, t2], source=[s1, s2]) assert result == "t1 s1 s2", result result = scons_subst("$TARGET $SOURCES", env, target=[t1, t2], source=[s1, s2], gvars={}) assert result == "t1 s1 s2", result result = scons_subst("$TARGET $SOURCES", env, target=[], source=[]) assert result == " ", result result = scons_subst("$TARGETS $SOURCE", env, target=[], source=[]) assert result == " ", result
def test_CLVar(self): """Test scons_subst() and scons_subst_list() with CLVar objects""" loc = {} loc['FOO'] = 'foo' loc['BAR'] = SCons.Util.CLVar('bar') loc['CALL'] = lambda target, source, env, for_signature: 'call' env = DummyEnv(loc) cmd = SCons.Util.CLVar("test $FOO $BAR $CALL test") newcmd = scons_subst(cmd, env, gvars=env.Dictionary()) assert newcmd == ['test', 'foo', 'bar', 'call', 'test'], newcmd cmd_list = scons_subst_list(cmd, env, gvars=env.Dictionary()) assert len(cmd_list) == 1, cmd_list assert cmd_list[0][0] == "test", cmd_list[0][0] assert cmd_list[0][1] == "foo", cmd_list[0][1] assert cmd_list[0][2] == "bar", cmd_list[0][2] assert cmd_list[0][3] == "call", cmd_list[0][3] assert cmd_list[0][4] == "test", cmd_list[0][4]
def generator(target, source, env, for_signature): args = source[-1].read() return scons_subst(command, env, target=target, source=source, lvars=args, gvars=env.gvars())