|
16 | 16 | # You can download the latest version of this tool from: |
17 | 17 | # https://github.com/theypsilon/Update_All_MiSTer |
18 | 18 |
|
| 19 | +import os |
| 20 | +import tempfile |
19 | 21 | import unittest |
| 22 | +import zipfile |
| 23 | +from unittest.mock import patch |
20 | 24 |
|
21 | | -from update_all.other import any_to_bool, any_to_nonfalsy_str, calculate_overscan, TerminalSize, OverscanDim, calculate_outer_box |
| 25 | +from update_all.other import any_to_bool, any_to_nonfalsy_str, calculate_overscan, TerminalSize, OverscanDim, calculate_outer_box, \ |
| 26 | + current_update_all_archive_path, is_mister_scripts_menu_fb_launch |
22 | 27 |
|
23 | 28 |
|
24 | 29 | class TestOther(unittest.TestCase): |
@@ -47,6 +52,28 @@ def test_any_to_nonfalsy_str(self): |
47 | 52 | with self.subTest(value=value, expected=expected): |
48 | 53 | self.assertEqual(expected, any_to_nonfalsy_str(value)) |
49 | 54 |
|
| 55 | + def test_current_update_all_archive_path___when_argv0_is_zipapp_with_any_extension___returns_path(self): |
| 56 | + path = _temp_zipapp_with_suffix('.sh') |
| 57 | + try: |
| 58 | + with patch('update_all.other.sys.argv', [path]): |
| 59 | + self.assertEqual(path, current_update_all_archive_path()) |
| 60 | + finally: |
| 61 | + _remove(path) |
| 62 | + |
| 63 | + def test_current_update_all_archive_path___when_argv0_is_not_zipapp___returns_none(self): |
| 64 | + with tempfile.NamedTemporaryFile(delete=False) as file: |
| 65 | + path = file.name |
| 66 | + file.write(b'#!/bin/bash\n') |
| 67 | + try: |
| 68 | + with patch('update_all.other.sys.argv', [path]): |
| 69 | + self.assertIsNone(current_update_all_archive_path()) |
| 70 | + finally: |
| 71 | + _remove(path) |
| 72 | + |
| 73 | + def test_current_update_all_archive_path___when_argv0_is_missing___returns_none(self): |
| 74 | + with patch('update_all.other.sys.argv', ['/tmp/missing_update_all']): |
| 75 | + self.assertIsNone(current_update_all_archive_path()) |
| 76 | + |
50 | 77 | def test_calculate_overscan(self): |
51 | 78 | def _size(columns=100, lines=50): |
52 | 79 | return TerminalSize(columns=columns, lines=lines) |
@@ -85,8 +112,47 @@ def test_calculate_outer_box___keeps_zero_line_overscan_outside_screen(self): |
85 | 112 | screen_dims = _ScreenDims(TerminalSize(columns=80, lines=40), OverscanDim(cols=1, lines=0)) |
86 | 113 | self.assertEqual((-1, 40, 0, 79), calculate_outer_box(screen_dims)) |
87 | 114 |
|
| 115 | + def test_is_mister_scripts_menu_fb_launch___when_tty2_with_script_wrapper_and_mister_ancestor___returns_true(self): |
| 116 | + with patch('update_all.other._current_tty', return_value='/dev/tty2'), \ |
| 117 | + patch('update_all.other._ancestor_process_descriptions', return_value=[ |
| 118 | + ('bash', '/bin/bash /tmp/script'), |
| 119 | + ('agetty', '/sbin/agetty -l /tmp/script tty2'), |
| 120 | + ('MiSTer', '/media/fat/MiSTer'), |
| 121 | + ]): |
| 122 | + self.assertTrue(is_mister_scripts_menu_fb_launch()) |
| 123 | + |
| 124 | + def test_is_mister_scripts_menu_fb_launch___when_tty2_without_mister_ancestor___returns_false(self): |
| 125 | + with patch('update_all.other._current_tty', return_value='/dev/tty2'), \ |
| 126 | + patch('update_all.other._ancestor_process_descriptions', return_value=[ |
| 127 | + ('bash', '/bin/bash /tmp/script'), |
| 128 | + ('agetty', '/sbin/agetty -l /tmp/script tty2'), |
| 129 | + ]): |
| 130 | + self.assertFalse(is_mister_scripts_menu_fb_launch()) |
| 131 | + |
| 132 | + def test_is_mister_scripts_menu_fb_launch___when_not_tty2___returns_false(self): |
| 133 | + with patch('update_all.other._current_tty', return_value='/dev/pts/0'), \ |
| 134 | + patch('update_all.other._ancestor_process_descriptions') as ancestors: |
| 135 | + self.assertFalse(is_mister_scripts_menu_fb_launch()) |
| 136 | + ancestors.assert_not_called() |
| 137 | + |
88 | 138 |
|
89 | 139 | class _ScreenDims: |
90 | 140 | def __init__(self, term_size: TerminalSize, overscan_dim: OverscanDim): |
91 | 141 | self.term_size = term_size |
92 | 142 | self.overscan_dim = overscan_dim |
| 143 | + |
| 144 | + |
| 145 | +def _temp_zipapp_with_suffix(suffix: str) -> str: |
| 146 | + with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as file: |
| 147 | + path = file.name |
| 148 | + with zipfile.ZipFile(path, 'w') as archive: |
| 149 | + archive.writestr('__main__.py', '') |
| 150 | + return path |
| 151 | + |
| 152 | + |
| 153 | +def _remove(*paths: str) -> None: |
| 154 | + for path in paths: |
| 155 | + try: |
| 156 | + os.remove(path) |
| 157 | + except FileNotFoundError: |
| 158 | + pass |
0 commit comments