diff --git a/evennia/utils/batchprocessors.py b/evennia/utils/batchprocessors.py index d798d19d58..dde9dba189 100644 --- a/evennia/utils/batchprocessors.py +++ b/evennia/utils/batchprocessors.py @@ -145,7 +145,7 @@ Example batch.py file ``` #HEADER -from django.config import settings +from django.conf import settings from evennia.utils import create from types import basetypes diff --git a/evennia/utils/tests/test_batchprocessors.py b/evennia/utils/tests/test_batchprocessors.py index d89f54fe0f..89d9d72f17 100644 --- a/evennia/utils/tests/test_batchprocessors.py +++ b/evennia/utils/tests/test_batchprocessors.py @@ -86,3 +86,107 @@ class TestBatchCommandProcessor(TestCase): mock.call('foopath', file_ending='.ev'), mock.call('x', file_ending='.ev')]) + +class TestBatchCodeProcessor(TestCase): + + @mock.patch.object(batchprocessors, 'read_batchfile') + def test_parses_one_codeblock(self, mocked_read): + mocked_read.return_value = textwrap.dedent( + r""" + print("Hello") + """) + commands = batchprocessors.BATCHCODE.parse_file('foopath') + self.assertEqual([ + '# batchcode code:\n\nprint("Hello")\n'], + commands) + + @mock.patch.object(batchprocessors, 'read_batchfile') + def test_parses_codeblocks(self, mocked_read): + mocked_read.return_value = textwrap.dedent( + 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) + + @mock.patch.object(batchprocessors, 'read_batchfile') + def test_parses_header_and_two_codeblock(self, mocked_read): + mocked_read.return_value = textwrap.dedent( + r""" + #HEADER + a = 100 + #CODE + a += 100 + #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) + + @mock.patch.object(batchprocessors, 'read_batchfile') + def test_parses_INSERT(self, mocked_read): + mocked_read.side_effect = [ + textwrap.dedent(r""" + a = 1 + #INSERT another.py + """), + textwrap.dedent(r""" + print("Hello") + """) + ] + 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')]) + + @mock.patch.object(batchprocessors, 'read_batchfile') + def test_parses_INSERT_raises_IOError(self, mocked_read): + mocked_read.side_effect = [ + textwrap.dedent(r""" + #INSERT x + """), + IOError + ] + with self.assertRaises(IOError, msg='#INSERT x failed.'): + commands = 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') + def test_execs_codeblock(self, mocked_exec): + err = batchprocessors.BATCHCODE.code_exec( + '# batchcode code:\n\nprint("Hello")\n', + extra_environ={}) + self.assertIsNone(err) + + @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")\n', + extra_environ={}) + self.assertIsNotNone(err)