aboutsummaryrefslogtreecommitdiffstats
path: root/pym/portage/tests/util/test_varExpand.py
blob: 7b528d6dbf32b52de2c81a5eaeb33e31d01403dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# test_varExpand.py -- Portage Unit Testing Functionality
# Copyright 2006-2010 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

from portage.tests import TestCase
from portage.util import varexpand

class VarExpandTestCase(TestCase):
	
	def testVarExpandPass(self):

		varDict = { "a":"5", "b":"7", "c":"-5" }
		for key in varDict:
			result = varexpand( "$%s" % key, varDict )
			
			self.assertFalse( result != varDict[key],
				msg="Got %s != %s, from varexpand( %s, %s )" % \
					( result, varDict[key], "$%s" % key, varDict ) )
			result = varexpand( "${%s}" % key, varDict )
			self.assertFalse( result != varDict[key],
				msg="Got %s != %s, from varexpand( %s, %s )" % \
					( result, varDict[key], "${%s}" % key, varDict ) )

	def testVarExpandBackslashes(self):
		"""
		We want to behave like bash does when expanding a variable
		assignment in a sourced file, in which case it performs
		backslash removal for \\ and \$ but nothing more. It also
		removes escaped newline characters. Note that we don't
		handle escaped quotes here, since getconfig() uses shlex
		to handle that earlier.
		"""

		varDict = {}
		tests = [
			("\\", "\\"),
			("\\\\", "\\"),
			("\\\\\\", "\\\\"),
			("\\\\\\\\", "\\\\"),
			("\\$", "$"),
			("\\\\$", "\\$"),
			("\\a", "\\a"),
			("\\b", "\\b"),
			("\\n", "\\n"),
			("\\r", "\\r"),
			("\\t", "\\t"),
			("\\\n", ""),
			("\\\"", "\\\""),
			("\\'", "\\'"),
		]
		for test in tests:
			result = varexpand( test[0], varDict )
			self.assertFalse( result != test[1],
				msg="Got %s != %s from varexpand( %s, %s )" \
				% ( result, test[1], test[0], varDict ) )

	def testVarExpandDoubleQuotes(self):
		
		varDict = { "a":"5" }
		tests = [ ("\"${a}\"", "\"5\"") ]
		for test in tests:
			result = varexpand( test[0], varDict )
			self.assertFalse( result != test[1],
				msg="Got %s != %s from varexpand( %s, %s )" \
				% ( result, test[1], test[0], varDict ) )

	def testVarExpandSingleQuotes(self):
		
		varDict = { "a":"5" }
		tests = [ ("\'${a}\'", "\'${a}\'") ]
		for test in tests:
			result = varexpand( test[0], varDict )
			self.assertFalse( result != test[1],
				msg="Got %s != %s from varexpand( %s, %s )" \
				% ( result, test[1], test[0], varDict ) )

	def testVarExpandFail(self):

		varDict = { "a":"5", "b":"7", "c":"15" }

		testVars = [ "fail" ]

		for var in testVars:
			result = varexpand( "$%s" % var, varDict )
			self.assertFalse( len(result),
				msg="Got %s == %s, from varexpand( %s, %s )" \
					% ( result, var, "$%s" % var, varDict ) )

			result = varexpand( "${%s}" % var, varDict )
			self.assertFalse( len(result),
				msg="Got %s == %s, from varexpand( %s, %s )" \
					% ( result, var, "${%s}" % var, varDict ) )