def setUp(self): tools = {"a": 1, "b": 2} self.__env = Env({"FOO": "foo"}) self.__env.setFuns(DEFAULT_STRING_FUNS.copy()) self.__env.setFunArgs({"sandbox": False, "__tools": tools}) self.__parser = IfExpressionParser()
class TestIfExpressionParser(TestCase): def setUp(self): tools = {"a": 1, "b": 2} self.__env = Env({"FOO": "foo"}) self.__env.setFuns(DEFAULT_STRING_FUNS.copy()) self.__env.setFunArgs({"sandbox": False, "__tools": tools}) self.__parser = IfExpressionParser() def tearDown(self): self.__parser = None def parse(self, expr): return self.__parser.evalExpression(expr, self.__env) def testEqual(self): self.assertRaises(BobError, self.parse, "x ==") self.assertTrue(self.parse('"${FOO}" == "foo"')) self.assertTrue(self.parse('"a" == "a"')) self.assertFalse(self.parse('"a" == "b"')) def testNotEqual(self): self.assertRaises(BobError, self.parse, "x !=") self.assertFalse(self.parse('"a" != "a"')) self.assertTrue(self.parse('"a" != "b"')) def testNot(self): self.assertRaises(BobError, self.parse, "!") self.assertFalse(self.parse('!"true"')) self.assertFalse(self.parse("!'TRUE'")) self.assertFalse(self.parse("!'1'")) self.assertFalse(self.parse("!'foobar'")) self.assertTrue(self.parse("!''")) self.assertTrue(self.parse("!'0'")) self.assertTrue(self.parse("!'false'")) self.assertTrue(self.parse("!'FaLsE'")) def testOr(self): self.assertTrue(self.parse('"true" || "false"')) self.assertTrue(self.parse('"false" || "true"')) self.assertFalse(self.parse('"false" || "false"')) self.assertTrue(self.parse('"1" || "2" || "3" || "4"')) self.assertFalse(self.parse('"0" || "0"|| "0" || "0"')) self.assertFalse(self.parse('"0" || "" || "false"')) def testAnd(self): self.assertTrue(self.parse('"true" && "true"')) self.assertFalse(self.parse('"true" && "false"')) self.assertFalse(self.parse('"false" && "true"')) self.assertTrue(self.parse('"true" && "true" && "true"')) self.assertTrue(self.parse('"true" && "1" && "abq"')) self.assertFalse(self.parse('"true" && ""')) def testFuns(self): self.assertFalse(self.parse('is-sandbox-enabled()')) self.assertTrue(self.parse('is-tool-defined("a")')) self.assertFalse(self.parse('is-tool-defined("c")')) self.assertFalse(self.parse('match( "string", "pattern")'))
def testDel(self): """Test to delete a key""" o = ScmOverride({'del': ['branch']}) match, scm = o.mangle(self.scm, Env()) self.assertEqual(scm, { 'scm': "git", 'url': "[email protected]:foo/bar.git", }) o = ScmOverride({'del': ["${DEL}"]}) e = Env({"DEL": "branch"}) match, scm = o.mangle(self.scm, e) self.assertEqual(scm, { 'scm': "git", 'url': "[email protected]:foo/bar.git", })
def testOverwrite(self): """Test to overwrite existing key""" o = ScmOverride({'set': {'branch': "master"}}) match, scm = o.mangle(self.scm, Env()) self.assertEqual(scm, { 'scm': "git", 'url': "[email protected]:foo/bar.git", 'branch': "master" }) # test substitution o = ScmOverride({'set': {'branch': "${BRANCH}"}}) e = Env({"BRANCH": "master"}) match, scm = o.mangle(self.scm, e) self.assertEqual(scm, { 'scm': "git", 'url': "[email protected]:foo/bar.git", 'branch': "master" })
def testReplaceInvalid(self): """Test that invalid regexes are handled gracefully""" o = ScmOverride( {'replace': { 'url': { 'pattern': "*", 'replacement': "foo" } }}) self.assertRaises(ParseError, o.mangle, self.scm, Env())
def parseAndPrepare(self, name, recipe, classes={}, allRelocatable=None): cwd = os.getcwd() recipeSet = MagicMock() recipeSet.loadBinary = MagicMock() recipeSet.scriptLanguage = ScriptLanguage.BASH recipeSet.getPolicy = lambda x: allRelocatable if x == 'allRelocatable' else None cc = { n: Recipe(recipeSet, r, [], n + ".yaml", cwd, n, n, {}, False) for n, r in classes.items() } recipeSet.getClass = lambda x, cc=cc: cc[x] r = recipe.copy() r["root"] = True ret = Recipe(recipeSet, recipe, [], name + ".yaml", cwd, name, name, {}) ret.resolveClasses(Env()) return ret.prepare(Env(), False, {})[0].refDeref([], {}, None, None)
def testAdd(self): """Test to add a new key""" o = ScmOverride({'set': {'commit': '1234'}}) match, scm = o.mangle(self.scm, Env()) self.assertEqual( scm, { 'scm': "git", 'url': "[email protected]:foo/bar.git", 'branch': "develop", 'commit': "1234" }) o = ScmOverride({'set': {'commit': "${COMMIT}"}}) e = Env({"COMMIT": "4321"}) match, scm = o.mangle(self.scm, e) self.assertEqual( scm, { 'scm': "git", 'url': "[email protected]:foo/bar.git", 'branch': "develop", 'commit': "4321" })
def testReplace(self): """Test replacement via regex""" o = ScmOverride({ 'replace': { 'url': { 'pattern': "@.*:", 'replacement': "@acme.test:" } } }) match, scm = o.mangle(self.scm, Env()) self.assertEqual(scm, { 'scm': "git", 'url': "[email protected]:foo/bar.git", 'branch': "develop" }) # test substitiution o = ScmOverride({ 'replace': { 'url': { 'pattern': "@${FROM_HOST}:", 'replacement': "@${TO_HOST}:" } } }) match, scm = o.mangle( self.scm, Env({ "FROM_HOST": "git.com", "TO_HOST": "acme.test" })) self.assertEqual(scm, { 'scm': "git", 'url': "[email protected]:foo/bar.git", 'branch': "develop" })
def testReplace(self): """Test replacement via regex""" o = ScmOverride({ 'replace': { 'url': { 'pattern': "@.*:", 'replacement': "@acme.test:" } } }) match, scm = o.mangle(self.scm, Env()) self.assertEqual(scm, { 'scm': "git", 'url': "[email protected]:foo/bar.git", 'branch': "develop" })
def testMatchGlob(self): """Test that matching uses globbing""" o = ScmOverride({ 'match': { 'url': "*git.com*" }, 'set': { 'url': "mirror" } }) match, scm = o.mangle(self.scm, Env()) self.assertEqual(scm, { 'scm': "git", 'url': "mirror", 'branch': "develop" })
def testMatch(self): """Test matching (multiple) keys""" # match single key o = ScmOverride({ 'match': { 'branch': "develop" }, 'set': { 'branch': "master" } }) match, scm = o.mangle(self.scm, Env()) self.assertEqual(scm, { 'scm': "git", 'url': "[email protected]:foo/bar.git", 'branch': "master" }) # mismatch single key o = ScmOverride({ 'match': { 'branch': "upstream" }, 'set': { 'branch': "master" } }) match, scm = o.mangle(self.scm, Env()) self.assertEqual(scm, { 'scm': "git", 'url': "[email protected]:foo/bar.git", 'branch': "develop" }) # match multiple keys o = ScmOverride({ 'match': { 'branch': "develop", 'url': "[email protected]:foo/bar.git", }, 'set': { 'branch': "master" } }) match, scm = o.mangle(self.scm, Env()) self.assertEqual(scm, { 'scm': "git", 'url': "[email protected]:foo/bar.git", 'branch': "master" }) # mismatch one out of two keys o = ScmOverride({ 'match': { 'branch': "develop", 'url': "asdfadgag", }, 'set': { 'branch': "master" } }) match, scm = o.mangle(self.scm, Env()) self.assertEqual(scm, { 'scm': "git", 'url': "[email protected]:foo/bar.git", 'branch': "develop" }) # test substitution o = ScmOverride({ 'match': { 'url': "git@${SERVER}:foo/${NAME}.git", }, 'set': { 'branch': "master" } }) e = Env({"SERVER": "git.com", "NAME": "bar"}) match, scm = o.mangle(self.scm, e) self.assertEqual(match, True) self.assertEqual(scm, { 'scm': "git", 'url': "[email protected]:foo/bar.git", 'branch': "master" })
class TestIfExpressionParser(TestCase): def setUp(self): tools = {"a": 1, "b": 2} self.__env = Env({"FOO": "foo"}) self.__env.setFuns(DEFAULT_STRING_FUNS.copy()) self.__env.setFunArgs({"sandbox": False, "__tools": tools}) def evalExpr(self, expr): return IfExpression(expr).evalExpression(self.__env) def testLiteral(self): self.assertRaises(BobError, self.evalExpr, "x") self.assertTrue(self.evalExpr('"true"')) self.assertTrue(self.evalExpr("'TRUE'")) self.assertTrue(self.evalExpr("'1'")) self.assertTrue(self.evalExpr("'foobar'")) self.assertFalse(self.evalExpr("''")) self.assertFalse(self.evalExpr("'0'")) self.assertFalse(self.evalExpr("'false'")) self.assertFalse(self.evalExpr("'FaLsE'")) def testEqual(self): self.assertRaises(BobError, self.evalExpr, '"x" ==') self.assertTrue(self.evalExpr('"${FOO}" == "foo"')) self.assertTrue(self.evalExpr('"a" == "a"')) self.assertFalse(self.evalExpr('"a" == "b"')) def testNotEqual(self): self.assertRaises(BobError, self.evalExpr, "x !=") self.assertFalse(self.evalExpr('"a" != "a"')) self.assertTrue(self.evalExpr('"a" != "b"')) def testNot(self): self.assertRaises(BobError, self.evalExpr, "!") self.assertFalse(self.evalExpr('!"true"')) self.assertFalse(self.evalExpr("!'TRUE'")) self.assertFalse(self.evalExpr("!'1'")) self.assertFalse(self.evalExpr("!'foobar'")) self.assertTrue(self.evalExpr("!''")) self.assertTrue(self.evalExpr("!'0'")) self.assertTrue(self.evalExpr("!'false'")) self.assertTrue(self.evalExpr("!'FaLsE'")) def testOr(self): self.assertTrue(self.evalExpr('"true" || "false"')) self.assertTrue(self.evalExpr('"false" || "true"')) self.assertFalse(self.evalExpr('"false" || "false"')) self.assertTrue(self.evalExpr('"1" || "2" || "3" || "4"')) self.assertFalse(self.evalExpr('"0" || "0"|| "0" || "0"')) self.assertFalse(self.evalExpr('"0" || "" || "false"')) def testAnd(self): self.assertTrue(self.evalExpr('"true" && "true"')) self.assertFalse(self.evalExpr('"true" && "false"')) self.assertFalse(self.evalExpr('"false" && "true"')) self.assertTrue(self.evalExpr('"true" && "true" && "true"')) self.assertTrue(self.evalExpr('"true" && "1" && "abq"')) self.assertFalse(self.evalExpr('"true" && ""')) def testFuns(self): self.assertFalse(self.evalExpr('is-sandbox-enabled()')) self.assertTrue(self.evalExpr('is-tool-defined("a")')) self.assertFalse(self.evalExpr('is-tool-defined("c")')) self.assertFalse(self.evalExpr('match( "string", "pattern")')) self.assertRaises(BobError, self.evalExpr, "!does-not-exist()") def testCompare(self): """Equality comparison should work on the actual expression""" self.assertEqual(IfExpression('"true"'), IfExpression('"true"')) self.assertNotEqual(IfExpression('"true"'), IfExpression('"false"')) self.assertEqual(IfExpression('! "true"'), IfExpression('! "true"')) self.assertNotEqual(IfExpression('! "true"'), IfExpression('! "false"')) self.assertNotEqual(IfExpression('! "true"'), IfExpression('"true"')) self.assertEqual(IfExpression('"true" && "true"'), IfExpression('"true" && "true"')) self.assertNotEqual(IfExpression('"true" && "true"'), IfExpression('"true" && "false"')) self.assertNotEqual(IfExpression('"true" && "true"'), IfExpression('"true" == "true"')) self.assertEqual(IfExpression('"a" < "b"'), IfExpression('"a" < "b"')) self.assertNotEqual(IfExpression('"a" < "b"'), IfExpression('"a" <= "b"')) self.assertEqual(IfExpression('is-tool-defined("a")'), IfExpression('is-tool-defined("a")')) self.assertNotEqual(IfExpression('is-tool-defined("a")'), IfExpression('is-tool-defined("b")')) self.assertNotEqual(IfExpression('is-tool-defined("a")'), IfExpression('match("a", "b")')) self.assertEqual( str( IfExpression( 'call("a", call("b", "c")) && !foo() || ("zz" < bar())')), '((call("a", call("b", "c"))) && (!(foo()))) || (("zz") < (bar()))' )