def testPrefix(self): src_prov = Config.provider({"INCLUDE_PATH": "test/src"}) lib_prov = Config.provider({"INCLUDE_PATH": "test/lib"}) config = { "LOAD_TEMPLATES": [src_prov, lib_prov], "PREFIX_MAP": { "src": "0", "lib": "1", "all": "0, 1" } } self.Expect(DATA, config)
def testProvider(self): dir = "test/src" lib = "test/lib" file = "foo" relfile = os.path.join(".", dir, file) absfile = os.path.join(os.path.abspath(dir), file) newfile = os.path.join(dir, "foobar") def update_file(*args): time.sleep(2) # ensure file time stamps are different f = open(newfile, "w") for arg in args: f.write(str(arg)) f.close() vars = { "file": file, "relfile": relfile, "absfile": absfile, "fixfile": update_file } update_file("This is the old content") #------------------------------------------------------------------------ # instantiate a bunch of providers, using various different techniques, # with different load options but sharing the same parser; then set them # to work fetching some files and check they respond as expected #------------------------------------------------------------------------ parser = Config.parser({"POST_CHOMP": 1}) provinc = Config.provider({ "INCLUDE_PATH": dir, "PARSER": parser, "TOLERANT": 1 }) provabs = Config.provider({ "ABSOLUTE": 1, "PARSER": parser }) provrel = Config.provider({ "RELATIVE": 1, "PARSER": parser }) self.assert_(provinc.parser() is provabs.parser()) self.assert_(provabs.parser() is provrel.parser()) self.assert_(delivered(provinc, file)) self.assert_(declined(provinc, absfile)) self.assert_(declined(provinc, relfile)) self.assert_(declined(provabs, file)) self.assert_(delivered(provabs, absfile)) self.assert_(denied(provabs, relfile)) self.assert_(declined(provrel, file)) self.assert_(denied(provrel, absfile)) self.assert_(delivered(provrel, relfile)) # Test if can fetch from a file handle. ttfile = Template() path = os.path.join(os.path.abspath(dir), "baz") file = open(path) outstr = ttfile.process(file, { "a": "filetest" }) file.close() self.assertEquals("This is the baz file, a: filetest\n", outstr) #------------------------------------------------------------------------ # now we'll fold those providers up into some Template objects that # we can pass to text_expect() to do some template driven testing #------------------------------------------------------------------------ ttinc = Template({ "LOAD_TEMPLATES": [provinc] }) ttabs = Template({ "LOAD_TEMPLATES": [provabs] }) ttrel = Template({ "LOAD_TEMPLATES": [provrel] }) def dpaths(): return [os.path.join(lib, x) for x in "one", "two"] def badpaths(): return [badpaths] ttd1 = Template({ "INCLUDE_PATH": [dpaths, dir], "PARSER": parser }) ttd2 = Template( { "INCLUDE_PATH": [DynamicPaths(os.path.join(lib, "two"), os.path.join(lib, "one")), dir], "PARSER": parser }) ttd3 = Template({ "INCLUDE_PATH": [ badpaths ], "PARSER": parser }) uselist = (("ttinc", ttinc), ("ttabs", ttabs), ("ttrel", ttrel), ("ttd1", ttd1), ("ttd2", ttd2), ("ttdbad", ttd3)) self.Expect(DATA, uselist, vars)
def testProvider(self): dir = "test/src" lib = "test/lib" file = "foo" relfile = os.path.join(".", dir, file) absfile = os.path.join(os.path.abspath(dir), file) newfile = os.path.join(dir, "foobar") def update_file(*args): time.sleep(2) # ensure file time stamps are different f = open(newfile, "w") for arg in args: f.write(str(arg)) f.close() vars = { "file": file, "relfile": relfile, "absfile": absfile, "fixfile": update_file } update_file("This is the old content") #------------------------------------------------------------------------ # instantiate a bunch of providers, using various different techniques, # with different load options but sharing the same parser; then set them # to work fetching some files and check they respond as expected #------------------------------------------------------------------------ parser = Config.parser({"POST_CHOMP": 1}) provinc = Config.provider({ "INCLUDE_PATH": dir, "PARSER": parser, "TOLERANT": 1 }) provabs = Config.provider({"ABSOLUTE": 1, "PARSER": parser}) provrel = Config.provider({"RELATIVE": 1, "PARSER": parser}) self.assertTrue(provinc.parser() is provabs.parser()) self.assertTrue(provabs.parser() is provrel.parser()) self.assertTrue(delivered(provinc, file)) self.assertTrue(declined(provinc, absfile)) self.assertTrue(declined(provinc, relfile)) self.assertTrue(declined(provabs, file)) self.assertTrue(delivered(provabs, absfile)) self.assertTrue(denied(provabs, relfile)) self.assertTrue(declined(provrel, file)) self.assertTrue(denied(provrel, absfile)) self.assertTrue(delivered(provrel, relfile)) # Test if can fetch from a file handle. ttfile = Template() path = os.path.join(os.path.abspath(dir), "baz") file = open(path) outstr = ttfile.process(file, {"a": "filetest"}) file.close() self.assertEqual("This is the baz file, a: filetest\n", outstr) #------------------------------------------------------------------------ # now we'll fold those providers up into some Template objects that # we can pass to text_expect() to do some template driven testing #------------------------------------------------------------------------ ttinc = Template({"LOAD_TEMPLATES": [provinc]}) ttabs = Template({"LOAD_TEMPLATES": [provabs]}) ttrel = Template({"LOAD_TEMPLATES": [provrel]}) def dpaths(): return [os.path.join(lib, x) for x in ["one", "two"]] def badpaths(): return [badpaths] ttd1 = Template({"INCLUDE_PATH": [dpaths, dir], "PARSER": parser}) ttd2 = Template({ "INCLUDE_PATH": [ DynamicPaths(os.path.join(lib, "two"), os.path.join(lib, "one")), dir ], "PARSER": parser }) ttd3 = Template({"INCLUDE_PATH": [badpaths], "PARSER": parser}) uselist = (("ttinc", ttinc), ("ttabs", ttabs), ("ttrel", ttrel), ("ttd1", ttd1), ("ttd2", ttd2), ("ttdbad", ttd3)) self.Expect(DATA, uselist, vars)
def testPrefix(self): src_prov = Config.provider({ "INCLUDE_PATH": "test/src" }) lib_prov = Config.provider({ "INCLUDE_PATH": "test/lib" }) config = { "LOAD_TEMPLATES": [ src_prov, lib_prov ], "PREFIX_MAP": { "src": "0", "lib": "1", "all": "0, 1" } } self.Expect(DATA, config)