Package portage :: Package tests :: Package util :: Module test_varExpand
[hide private]

Source Code for Module portage.tests.util.test_varExpand

 1  # test_varExpand.py -- Portage Unit Testing Functionality 
 2  # Copyright 2006-2010 Gentoo Foundation 
 3  # Distributed under the terms of the GNU General Public License v2 
 4   
 5  from portage.tests import TestCase 
 6  from portage.util import varexpand 
 7   
8 -class VarExpandTestCase(TestCase):
9
10 - def testVarExpandPass(self):
11 12 varDict = {"a": "5", "b": "7", "c": "-5"} 13 for key in varDict: 14 result = varexpand("$%s" % key, varDict) 15 16 self.assertFalse(result != varDict[key], 17 msg="Got %s != %s, from varexpand(%s, %s)" % 18 (result, varDict[key], "$%s" % key, varDict)) 19 result = varexpand("${%s}" % key, varDict) 20 self.assertFalse(result != varDict[key], 21 msg="Got %s != %s, from varexpand(%s, %s)" % 22 (result, varDict[key], "${%s}" % key, varDict))
23
25 """ 26 We want to behave like bash does when expanding a variable 27 assignment in a sourced file, in which case it performs 28 backslash removal for \\ and \$ but nothing more. It also 29 removes escaped newline characters. Note that we don't 30 handle escaped quotes here, since getconfig() uses shlex 31 to handle that earlier. 32 """ 33 34 varDict = {} 35 tests = [ 36 ("\\", "\\"), 37 ("\\\\", "\\"), 38 ("\\\\\\", "\\\\"), 39 ("\\\\\\\\", "\\\\"), 40 ("\\$", "$"), 41 ("\\\\$", "\\$"), 42 ("\\a", "\\a"), 43 ("\\b", "\\b"), 44 ("\\n", "\\n"), 45 ("\\r", "\\r"), 46 ("\\t", "\\t"), 47 ("\\\n", ""), 48 ("\\\"", "\\\""), 49 ("\\'", "\\'"), 50 ] 51 for test in tests: 52 result = varexpand(test[0], varDict) 53 self.assertFalse(result != test[1], 54 msg="Got %s != %s from varexpand(%s, %s)" 55 % (result, test[1], test[0], varDict))
56
58 59 varDict = {"a": "5"} 60 tests = [("\"${a}\"", "\"5\"")] 61 for test in tests: 62 result = varexpand(test[0], varDict) 63 self.assertFalse(result != test[1], 64 msg="Got %s != %s from varexpand(%s, %s)" 65 % (result, test[1], test[0], varDict))
66
68 69 varDict = {"a": "5"} 70 tests = [("\'${a}\'", "\'${a}\'")] 71 for test in tests: 72 result = varexpand(test[0], varDict) 73 self.assertFalse(result != test[1], 74 msg="Got %s != %s from varexpand(%s, %s)" 75 % (result, test[1], test[0], varDict))
76
77 - def testVarExpandFail(self):
78 79 varDict = {"a": "5", "b": "7", "c": "15"} 80 81 testVars = ["fail"] 82 83 for var in testVars: 84 result = varexpand("$%s" % var, varDict) 85 self.assertFalse(len(result), 86 msg="Got %s == %s, from varexpand(%s, %s)" 87 % (result, var, "$%s" % var, varDict)) 88 89 result = varexpand("${%s}" % var, varDict) 90 self.assertFalse(len(result), 91 msg="Got %s == %s, from varexpand(%s, %s)" 92 % (result, var, "${%s}" % var, varDict))
93