Fix examine using wrong cmdset target for accounts. Resolve #1886

This commit is contained in:
Griatch 2019-10-17 00:15:09 +02:00
parent 0ef2e667e4
commit bc5210eb9c
6 changed files with 139 additions and 106 deletions

View file

@ -233,7 +233,7 @@ def read_batchfile(pythonpath, file_ending=".py"):
continue
break
if not text and decoderr:
raise UnicodeDecodeError("\n".join(decoderr), bytearray(), 0, 0, '')
raise UnicodeDecodeError("\n".join(decoderr), bytearray(), 0, 0, "")
return text

View file

@ -8,26 +8,24 @@ import textwrap
class TestBatchprocessorErrors(TestCase):
@mock.patch.object(utils, 'pypath_to_realpath', return_value=[])
@mock.patch.object(utils, "pypath_to_realpath", return_value=[])
def test_read_batchfile_raises_IOError(self, _):
with self.assertRaises(IOError):
batchprocessors.read_batchfile('foopath')
batchprocessors.read_batchfile("foopath")
@mock.patch.object(utils, 'pypath_to_realpath', return_value=['pathfoo'])
@mock.patch.object(codecs, 'open', side_effect=ValueError('decodeerr'))
@mock.patch.object(batchprocessors, '_ENCODINGS', ['fooencoding'])
@mock.patch.object(utils, "pypath_to_realpath", return_value=["pathfoo"])
@mock.patch.object(codecs, "open", side_effect=ValueError("decodeerr"))
@mock.patch.object(batchprocessors, "_ENCODINGS", ["fooencoding"])
def test_read_batchfile_raises_UnicodeDecodeError(self, *_):
with self.assertRaises(UnicodeDecodeError, msg='decodeerr'):
batchprocessors.read_batchfile('foopath')
with self.assertRaises(UnicodeDecodeError, msg="decodeerr"):
batchprocessors.read_batchfile("foopath")
class TestBatchCommandProcessor(TestCase):
@mock.patch.object(batchprocessors, 'read_batchfile')
@mock.patch.object(batchprocessors, "read_batchfile")
def test_parses_2_commands(self, mocked_read):
mocked_read.return_value = textwrap.dedent(
r"""
r"""
@create rock
#
@ -35,93 +33,102 @@ class TestBatchCommandProcessor(TestCase):
A big rock. You can tell is ancient.
#
""")
commands = batchprocessors.BATCHCMD.parse_file('foopath')
self.assertEqual([
'@create rock', '@set rock/desc =\nA big rock. You can tell is ancient.'],
commands)
"""
)
commands = batchprocessors.BATCHCMD.parse_file("foopath")
self.assertEqual(
["@create rock", "@set rock/desc =\nA big rock. You can tell is ancient."], commands
)
@mock.patch.object(batchprocessors, 'read_batchfile')
@mock.patch.object(batchprocessors, "read_batchfile")
def test_parses_INSERT(self, mocked_read):
mocked_read.side_effect = [
textwrap.dedent(r"""
textwrap.dedent(
r"""
@create sky
#
#INSERT another.ev
#
@create sun
#
"""),
textwrap.dedent(r"""
"""
),
textwrap.dedent(
r"""
@create bird
#
@create cloud
#
""")
"""
),
]
commands = batchprocessors.BATCHCMD.parse_file('foopath')
commands = batchprocessors.BATCHCMD.parse_file("foopath")
self.assertEqual(commands, ["@create sky", "@create bird", "@create cloud", "@create sun"])
self.assertEqual(
commands,
['@create sky', '@create bird', '@create cloud', '@create sun'])
self.assertEqual(mocked_read.mock_calls, [
mock.call('foopath', file_ending='.ev'),
mock.call('another.ev', file_ending='.ev')])
mocked_read.mock_calls,
[mock.call("foopath", file_ending=".ev"), mock.call("another.ev", file_ending=".ev")],
)
@mock.patch.object(batchprocessors, 'read_batchfile')
@mock.patch.object(batchprocessors, "read_batchfile")
def test_parses_INSERT_raises_IOError(self, mocked_read):
mocked_read.side_effect = [
textwrap.dedent(r"""
textwrap.dedent(
r"""
@create sky
#
#INSERT x
#
@create sun
#
"""),
IOError
"""
),
IOError,
]
with self.assertRaises(IOError, msg='#INSERT x failed.'):
batchprocessors.BATCHCMD.parse_file('foopath')
self.assertEqual(mocked_read.mock_calls, [
mock.call('foopath', file_ending='.ev'),
mock.call('x', file_ending='.ev')])
with self.assertRaises(IOError, msg="#INSERT x failed."):
batchprocessors.BATCHCMD.parse_file("foopath")
self.assertEqual(
mocked_read.mock_calls,
[mock.call("foopath", file_ending=".ev"), mock.call("x", file_ending=".ev")],
)
class TestBatchCodeProcessor(TestCase):
@mock.patch.object(batchprocessors, 'read_batchfile')
@mock.patch.object(batchprocessors, "read_batchfile")
def test_parses_one_codeblock(self, mocked_read):
mocked_read.return_value = textwrap.dedent(
r"""
r"""
print("Hello")
""")
commands = batchprocessors.BATCHCODE.parse_file('foopath')
self.assertEqual([
'# batchcode code:\n\nprint("Hello")\n'],
commands)
"""
)
commands = batchprocessors.BATCHCODE.parse_file("foopath")
self.assertEqual(['# batchcode code:\n\nprint("Hello")\n'], commands)
@mock.patch.object(batchprocessors, 'read_batchfile')
@mock.patch.object(batchprocessors, "read_batchfile")
def test_parses_codeblocks(self, mocked_read):
mocked_read.return_value = textwrap.dedent(
r"""
r"""
#CODE
print("Hello")
#CODE
a = 1
b = [1,
2, 3]
""")
commands = batchprocessors.BATCHCODE.parse_file('foopath')
self.assertEqual([
'# batchcode code:\n\n',
'# batchcode code:\n\nprint("Hello")\n',
'# batchcode code:\n\na = 1\nb = [1,\n2, 3]\n'],
commands)
"""
)
commands = batchprocessors.BATCHCODE.parse_file("foopath")
self.assertEqual(
[
"# batchcode code:\n\n",
'# batchcode code:\n\nprint("Hello")\n',
"# batchcode code:\n\na = 1\nb = [1,\n2, 3]\n",
],
commands,
)
@mock.patch.object(batchprocessors, 'read_batchfile')
@mock.patch.object(batchprocessors, "read_batchfile")
def test_parses_header_and_two_codeblock(self, mocked_read):
mocked_read.return_value = textwrap.dedent(
r"""
r"""
#HEADER
a = 100
#CODE
@ -129,71 +136,86 @@ class TestBatchCodeProcessor(TestCase):
#CODE
a += 100
a == 100
""")
commands = batchprocessors.BATCHCODE.parse_file('foopath')
self.assertEqual([
'# batchcode header:\n\na = 100\n\n\n# batchcode code:\n\n',
'# batchcode header:\n\na = 100\n\n\n# batchcode code:\n\na += 100\n',
'# batchcode header:\n\na = 100\n\n\n# batchcode code:\n\na += 100\na == 100\n'],
commands)
"""
)
commands = batchprocessors.BATCHCODE.parse_file("foopath")
self.assertEqual(
[
"# batchcode header:\n\na = 100\n\n\n# batchcode code:\n\n",
"# batchcode header:\n\na = 100\n\n\n# batchcode code:\n\na += 100\n",
"# batchcode header:\n\na = 100\n\n\n# batchcode code:\n\na += 100\na == 100\n",
],
commands,
)
@mock.patch.object(batchprocessors, 'read_batchfile')
@mock.patch.object(batchprocessors, "read_batchfile")
def test_parses_INSERT(self, mocked_read):
mocked_read.side_effect = [
textwrap.dedent(r"""
textwrap.dedent(
r"""
a = 1
#INSERT another.py
"""),
textwrap.dedent(r"""
"""
),
textwrap.dedent(
r"""
print("Hello")
""")
"""
),
]
commands = batchprocessors.BATCHCODE.parse_file('foopath')
commands = batchprocessors.BATCHCODE.parse_file("foopath")
self.assertEqual(
commands, [
'# batchcode code:\n'
'\n'
'a = 1\n'
'# batchcode insert (another.py):# batchcode code:\n'
'\n'
'print("Hello")\n'
'\n'])
self.assertEqual(mocked_read.mock_calls, [
mock.call('foopath', file_ending='.py'),
mock.call('another.py', file_ending='.py')])
commands,
[
"# batchcode code:\n"
"\n"
"a = 1\n"
"# batchcode insert (another.py):# batchcode code:\n"
"\n"
'print("Hello")\n'
"\n"
],
)
self.assertEqual(
mocked_read.mock_calls,
[mock.call("foopath", file_ending=".py"), mock.call("another.py", file_ending=".py")],
)
@mock.patch.object(batchprocessors, 'read_batchfile')
@mock.patch.object(batchprocessors, "read_batchfile")
def test_parses_INSERT_raises_IOError(self, mocked_read):
mocked_read.side_effect = [
textwrap.dedent(r"""
textwrap.dedent(
r"""
#INSERT x
"""),
IOError
"""
),
IOError,
]
with self.assertRaises(IOError, msg='#INSERT x failed.'):
batchprocessors.BATCHCODE.parse_file('foopath')
self.assertEqual(mocked_read.mock_calls, [
mock.call('foopath', file_ending='.py'),
mock.call('x', file_ending='.py')])
with self.assertRaises(IOError, msg="#INSERT x failed."):
batchprocessors.BATCHCODE.parse_file("foopath")
self.assertEqual(
mocked_read.mock_calls,
[mock.call("foopath", file_ending=".py"), mock.call("x", file_ending=".py")],
)
@mock.patch('builtins.exec')
@mock.patch("builtins.exec")
def test_execs_codeblock(self, mocked_exec):
err = batchprocessors.BATCHCODE.code_exec(
'# batchcode code:\n\nprint("Hello")\n',
extra_environ={})
'# batchcode code:\n\nprint("Hello")\n', extra_environ={}
)
self.assertIsNone(err)
@mock.patch('builtins.exec')
@mock.patch("builtins.exec")
def test_execs_codeblock_with_extra_environ(self, mocked_exec):
err = batchprocessors.BATCHCODE.code_exec(
'# batchcode code:\n\nprint("Hello")\n',
extra_environ={'foo': 'bar', 'baz': True})
'# batchcode code:\n\nprint("Hello")\n', extra_environ={"foo": "bar", "baz": True}
)
self.assertIsNone(err)
@mock.patch('builtins.exec')
@mock.patch("builtins.exec")
def test_execs_codeblock_raises(self, mocked_exec):
mocked_exec.side_effect = Exception
err = batchprocessors.BATCHCODE.code_exec(
'# batchcode code:\n\nprint("Hello")\nprint("Evennia")',
extra_environ={})
'# batchcode code:\n\nprint("Hello")\nprint("Evennia")', extra_environ={}
)
self.assertIsNotNone(err)