Commit | Line | Data |
---|---|---|
66b856f5 FC |
1 | from panda3d.core import load_prc_file_data |
2 | load_prc_file_data('', 'window-type none') | |
8c440734 FC |
3 | from pathlib import Path |
4 | import sys | |
5 | if '' in sys.path: sys.path.remove('') | |
6 | sys.path.append(str(Path(__file__).parent.parent.parent)) | |
d6c157a0 | 7 | from os import remove, makedirs |
8c440734 | 8 | from os.path import exists |
d6c157a0 | 9 | from shutil import rmtree |
8c440734 | 10 | from unittest import TestCase |
d6c157a0 | 11 | from unittest.mock import patch, MagicMock |
8c440734 FC |
12 | from setuptools.dist import Distribution |
13 | from setup import AbsCmd | |
14 | from ya2.build.build import exec_cmd | |
15 | from main import Main | |
16 | import logging | |
17 | from ya2.utils.dictfile import DctFile | |
18 | import importlib | |
19 | import main | |
66b856f5 | 20 | import traceback |
8c440734 FC |
21 | |
22 | ||
23 | class MainTests(TestCase): | |
24 | ||
25 | def setUp(self): | |
26 | self.__optfile = DctFile('options.ini') | |
27 | self.__verbose_val = self.__optfile['development']['verbose_log'] | |
d6c157a0 FC |
28 | makedirs('/tmp/.mount_Pmachines_unit_test/usr/bin', exist_ok=True) |
29 | with open('/tmp/.mount_Pmachines_unit_test/usr/bin/appimage_version.txt', 'w') as f: | |
30 | f.write('version_test') | |
8c440734 FC |
31 | |
32 | def tearDown(self): | |
33 | self.__optfile['development']['verbose_log'] = self.__verbose_val | |
34 | self.__optfile.store() | |
d6c157a0 | 35 | rmtree('/tmp/.mount_Pmachines_unit_test') |
8c440734 FC |
36 | |
37 | def test_logging(self): | |
38 | self.assertEqual(logging.root.handlers[0].formatter._fmt, '%(asctime)s %(message)s') | |
39 | self.assertEqual(logging.root.handlers[0].formatter.datefmt, '%H:%M:%S') | |
40 | self.__optfile['development']['verbose_log'] = 0 | |
41 | self.__optfile.store() | |
42 | importlib.reload(main) | |
43 | self.assertEqual(logging.root.level, logging.INFO) | |
44 | self.__optfile['development']['verbose_log'] = 1 | |
45 | self.__optfile.store() | |
46 | importlib.reload(main) | |
47 | self.assertEqual(logging.root.level, logging.DEBUG) | |
d6c157a0 FC |
48 | |
49 | def test_update(self): | |
50 | with patch.object(sys, 'argv', ['python -m unittest']): | |
51 | main = Main() | |
52 | main._Main__appimage_builder.update = MagicMock() | |
53 | main._Main__pmachines._fsm.demand = MagicMock() | |
54 | main._Main__pmachines.run = MagicMock() | |
55 | main.run() | |
56 | main._Main__pmachines.base.destroy() | |
57 | main._Main__appimage_builder.update.assert_not_called() | |
58 | with patch.object(sys, 'argv', ['python -m unittest', '--update']): | |
59 | main = Main() | |
60 | main._Main__appimage_builder.update = MagicMock() | |
61 | main.run() | |
66b856f5 | 62 | main._Main__pmachines.base.destroy() |
d6c157a0 | 63 | main._Main__appimage_builder.update.assert_called_once() |
66b856f5 FC |
64 | |
65 | def test_version(self): | |
66 | with patch.object(sys, 'argv', ['python -m unittest']): | |
67 | main = Main() | |
68 | main._Main__run_game = MagicMock() | |
69 | main.run() | |
70 | main._Main__pmachines.base.destroy() | |
71 | main._Main__run_game.assert_called_once() | |
72 | with patch.object(sys, 'argv', ['python -m unittest', '--version']): | |
73 | main = Main() | |
74 | main._Main__run_game = MagicMock() | |
75 | main.run() | |
76 | main._Main__pmachines.base.destroy() | |
77 | main._Main__run_game.assert_not_called() | |
78 | ||
79 | def test_run_game(self): | |
80 | with patch.object(sys, 'argv', ['python -m unittest']): | |
81 | main = Main() | |
82 | main._Main__pmachines.run = MagicMock() | |
83 | main.run() | |
84 | main._Main__pmachines.base.destroy() | |
85 | main._Main__pmachines.run.assert_called_once() | |
86 | ||
87 | def test_run_game_exception(self): | |
88 | with patch.object(sys, 'argv', ['python -m unittest']): | |
89 | with patch.object(traceback, 'print_exc') as print_exc_mock: | |
90 | main = Main() | |
91 | main._Main__pmachines.run = MagicMock(side_effect=Exception) | |
92 | main.run() | |
93 | main._Main__pmachines.base.destroy() | |
94 | main._Main__pmachines.run.trowed_exception() | |
95 | print_exc_mock.assert_called_once() |