/tests/test-interp.py
Python | 196 lines | 175 code | 9 blank | 12 comment | 9 complexity | 7a11f4df5725806341fe81984bd5c669 MD5 | raw file
Possible License(s): GPL-2.0
- # test-interp.py - shell interpreter unit tests
- #
- # Copyright 2007 Patrick Mezard
- #
- # This software may be used and distributed according to the terms
- # of the GNU General Public License, incorporated herein by reference.
- import unittest
- from pysh.interp import *
- def remove_empty_parts(wtree):
- """Perform word tree cleanup by removing empty elements which are semantically
- useless.
- """
- result = []
- for part in wtree:
- if isinstance(part, list):
- part = remove_empty_parts(part)
- result.append(part)
- result[1:-1] =[s for s in result[1:-1] if s]
- if len(result)==2:
- result = []
- if len(result)==3 and isinstance(result[1], list):
- return result[1]
- return result
- class TestInterpreter(unittest.TestCase):
- def setUp(self):
- if not getattr(self, 'cwd', None):
- self.cwd = os.getcwd()
- open('test.py', 'w').close()
-
- def tearDown(self):
- os.chdir(self.cwd)
- os.remove('test.py')
- def test_env(self):
- env = Environment('.')
- # Check get method availability
- env.get('PWD', 'default')
-
- # Test positional arguments
- tests = [
- [],
- ['p1'],
- ['p1', 'p2', 'p3'],
- ]
-
- for args in tests:
- env.set_positional_args(args)
- self.assertEqual(env.get_positional_args(), args)
- def test_splitfields(self):
- ip = Interpreter('/')
- env = ip.get_env()
- self.assertEqual(env.split_fields('['), ['['])
-
- tests = [
- # No split
- (['', 'foo', ''], [['', 'foo', '']]),
- (['', '[', ''], [['', '[', '']]),
- # Basic split + leading/trailing whitespaces removal
- (['', ' foo bar ', ''], [['', 'foo', ''], ['', 'bar', '']]),
- # Ignore escaped blanks
- (['', 'foo', ['\\', ' ', ''], 'bar', ''], [['', 'foo', ['\\', ' ', ''], 'bar', '']]),
- # Test with some quote
- (['', 'foo bar ', ['"', 'quoted blank', '"'], ''], [['', 'foo', ''], ['', 'bar', ''], ['"', 'quoted blank', '"']]),
- (['', 'fii', 'BAR BAZ', '-baz', ''], [['', 'fiiBAR', ''], ['', 'BAZ-baz', '']]),
- # Double-quotes
- (['"', 'a b', 'c ', 'd', '"'], [['"', 'a b', 'c ', 'd', '"']]),
- # Positional arguments
- (['"', 'foo', ['@', 'p1', 'p2', 'p3', ''], 'bar', '"'], [['"', 'foo', 'p1', '"'], ['"', 'p2', '"'], ['"', 'p3', 'bar', '"']]),
- (['', 'fuu', ['"', 'foo', ['@', 'p1', 'p2', 'p3', ''], 'bar', '"'], 'baz', ''], [['', 'fuu', ['"', 'foo', 'p1', '"'], ''], ['"', 'p2', '"'], ['', ['"', 'p3', 'bar', '"'], 'baz', '']]),
- ]
-
- for test, expected in tests:
- result = ip._split_fields(test)
- result = map(remove_empty_parts, result)
- self.assertEqual(result, expected)
-
- def test_expandparameter(self):
- self.assert_(is_name('FOO'))
- ip = Interpreter('/')
-
- self.assertEqual(ip.expand_token(('WORD', '`echo foo`')), ['foo'])
- ip.get_env()['FOO'] = 'BAR'
- self.assertEqual(ip.expand_token(('WORD', 'fii$FOO-baz')), ['fiiBAR-baz'])
- # Handle non-parameter expansion
- self.assertEqual(ip.expand_token(('WORD', '$.')), ['$.'])
- # Parameter expansion + field splitting
- ip.get_env()['FOO'] = 'BAR BAZ'
- self.assertEqual(ip.expand_token(('WORD', 'fii$FOO-baz')), ['fiiBAR', 'BAZ-baz'])
- self.assertEqual(ip.expand_token(('WORD', 'a')), ['a'])
-
- # Positional arguments
- ip.get_env()['FOO'] = 'BAR BAZ'
- ip.get_env().set_positional_args(['p1', 'p2', 'p3'])
- self.assertEqual(ip.expand_token(('WORD', '$@')), ['p1', 'p2', 'p3'])
- self.assertEqual(ip.expand_token(('WORD', 'foo$@bar')), ['foop1', 'p2', 'p3bar'])
- self.assertEqual(ip.expand_token(('WORD', 'fuu"foo$@bar"baz')), ['fuufoop1', 'p2', 'p3barbaz'])
-
- self.assertEqual(ip.expand_token(('WORD', '$*')), ['p1', 'p2', 'p3'])
- self.assertEqual(ip.expand_token(('WORD', 'foo$*bar')), ['foop1', 'p2', 'p3bar'])
- self.assertEqual(ip.expand_token(('WORD', 'fuu"foo$*bar"baz')), ['fuufoop1 p2 p3barbaz'])
-
- # With no arguments
- ip.get_env().set_positional_args([])
- self.assertEqual(ip.expand_token(('WORD', '$@')), [])
- self.assertEqual(ip.expand_token(('WORD', '"$@"')), [])
- self.assertEqual(ip.expand_token(('WORD', 'foo$@bar')), ['foobar'])
- self.assertEqual(ip.expand_token(('WORD', 'fuu"foo$@bar"baz')), ['fuufoobarbaz'])
-
- self.assertEqual(ip.expand_token(('WORD', '[')), ['['])
-
- # Empty arguments
- self.assertEqual(ip.expand_token(('WORD', '""')), [''])
-
- def test_expandheredocument(self):
- ip = Interpreter('/')
- ip.get_env()['FOO'] = 'BAR'
- word = "try to expand this: '\\${FOO}=$FOO' \\\non the same line"
- self.assertEqual(ip.expand_here_document(('WORD', word)), "try to expand this: '${FOO}=BAR' on the same line")
- word = "\\n";
- self.assertEqual(ip.expand_here_document(('WORD', word)), "\\n")
-
- def test_expandpathnames(self):
- ip = Interpreter(os.path.abspath('.'))
-
- #FIX: find a more generic 'other' location
- os.chdir(tempfile.gettempdir())
- res1 = ip._expand_pathname(['', 'te*', '.py', ''])
- self.assert_(len(res1)>0)
- # Test quoting on literal values (no effect)
- res2 = ip._expand_pathname(['', 'te*', ['"', '.py', '"'], ''])
- self.assert_(len(res2)>0)
- self.assertEqual(res1, res2)
-
- # No expansion
- res = ip._expand_pathname(['', 'foo', 'bar', ''])
- self.assertEqual(res, [['', 'foo', 'bar', '']])
-
- # Test quoting on special characters
- wtree = ['', 'te', ['"', '*.py', '"'], '']
- res = ip._expand_pathname(wtree)
- self.assertEqual(res, [wtree])
-
- def test_removequotes(self):
- ip = Interpreter('/')
-
- tests = [
- (['', 'a', ''], ['', 'a', '']),
- # Unquoted
- (['', 'foo', 'bar', ''], ['', 'foobar', '']),
- # Quotes in quotes
- (['', 'foo', ['"', 'ba', ['\\', 'r', ''], '"'], 'baz', ''], ['', 'foobarbaz', '']),
- # Quoted backslashes
- (['"', 'foo', ['\\', '\\', ''], 'nbaz', '"'], ['', 'foo\\nbaz', '']),
- ]
-
- for test, expected in tests:
- self.assertEqual(ip._remove_quotes(test), expected)
- def test_redirs(self):
- def checkopened(path):
- try:
- os.remove(path)
- return False
- except:
- return True
-
- # Test simple open/close sequence
- tmpfd, tmppath = tempfile.mkstemp()
- f = os.fdopen(tmpfd, 'w')
- w = FileWrapper('w', f, True)
- dup = w.dup()
- dup.close()
- self.assert_(checkopened(tmppath))
- w.close()
- os.remove(tmppath)
- # Test redirection clone
- tmpfd, tmppath = tempfile.mkstemp()
- f = os.fdopen(tmpfd, 'w')
- w = FileWrapper('w', f, True)
- r = Redirections(w, w.dup(), w.dup())
- self.assert_(checkopened(tmppath))
- # Duplicate one descriptor into another
- r.add(None, '>&', '2', '1')
- self.assert_(checkopened(tmppath))
- r.close()
- os.remove(tmppath)
-
-
-
- if __name__=='__main__':
- unittest.main()