|
10 | 10 | import comtypes.server.inprocserver |
11 | 11 | from comtypes import GUID |
12 | 12 | from comtypes.server import register |
13 | | -from comtypes.server.register import RegistryEntries, _get_serverdll |
| 13 | +from comtypes.server.register import Registrar, RegistryEntries, _get_serverdll |
14 | 14 |
|
15 | 15 | HKCR = winreg.HKEY_CLASSES_ROOT |
| 16 | +MULTI_SZ = winreg.REG_MULTI_SZ |
| 17 | +SZ = winreg.REG_SZ |
| 18 | + |
| 19 | + |
| 20 | +class Test_Registrar_nodebug(ut.TestCase): |
| 21 | + @mock.patch.object(register, "winreg") |
| 22 | + def test_calls_openkey_and_deletekey(self, _winreg): |
| 23 | + _winreg.HKEY_CLASSES_ROOT = HKCR |
| 24 | + hkey = mock.Mock(spec=winreg.HKEYType) |
| 25 | + _winreg.OpenKey.return_value = hkey |
| 26 | + reg_clsid = GUID.create_new() |
| 27 | + registrar = Registrar() |
| 28 | + |
| 29 | + class Cls: |
| 30 | + _reg_clsid_ = reg_clsid |
| 31 | + |
| 32 | + registrar.nodebug(Cls) |
| 33 | + _winreg.OpenKey.assert_called_once_with(HKCR, rf"CLSID\{reg_clsid}") |
| 34 | + _winreg.DeleteKey.assert_called_once_with(hkey, "Logging") |
| 35 | + |
| 36 | + @mock.patch.object(register, "winreg") |
| 37 | + def test_ignores_winerror(self, _winreg): |
| 38 | + _winreg.HKEY_CLASSES_ROOT = HKCR |
| 39 | + ERROR_FILE_NOT_FOUND = 2 |
| 40 | + err = OSError(ERROR_FILE_NOT_FOUND, "msg", "filename", ERROR_FILE_NOT_FOUND) |
| 41 | + _winreg.OpenKey.side_effect = err |
| 42 | + reg_clsid = GUID.create_new() |
| 43 | + registrar = Registrar() |
| 44 | + |
| 45 | + class Cls: |
| 46 | + _reg_clsid_ = reg_clsid |
| 47 | + |
| 48 | + registrar.nodebug(Cls) |
| 49 | + _winreg.OpenKey.assert_called_once_with(HKCR, rf"CLSID\{reg_clsid}") |
| 50 | + _winreg.DeleteKey.assert_not_called() |
| 51 | + |
| 52 | + @mock.patch.object(register, "winreg") |
| 53 | + def test_not_ignores_winerror(self, _winreg): |
| 54 | + _winreg.HKEY_CLASSES_ROOT = HKCR |
| 55 | + hkey = mock.Mock(spec=winreg.HKEYType) |
| 56 | + _winreg.OpenKey.return_value = hkey |
| 57 | + ERROR_ACCESS_DENIED = 5 |
| 58 | + err = OSError(ERROR_ACCESS_DENIED, "msg", "filename", ERROR_ACCESS_DENIED) |
| 59 | + _winreg.OpenKey.return_value = hkey |
| 60 | + _winreg.DeleteKey.side_effect = err |
| 61 | + reg_clsid = GUID.create_new() |
| 62 | + registrar = Registrar() |
| 63 | + |
| 64 | + class Cls: |
| 65 | + _reg_clsid_ = reg_clsid |
| 66 | + |
| 67 | + with self.assertRaises(OSError) as e: |
| 68 | + registrar.nodebug(Cls) |
| 69 | + self.assertEqual(e.exception.winerror, ERROR_ACCESS_DENIED) |
| 70 | + _winreg.OpenKey.assert_called_once_with(HKCR, rf"CLSID\{reg_clsid}") |
| 71 | + _winreg.DeleteKey.assert_called_once_with(hkey, "Logging") |
| 72 | + |
| 73 | + |
| 74 | +class Test_Registrar_debug(ut.TestCase): |
| 75 | + @mock.patch.object(register, "winreg") |
| 76 | + def test_calls_createkey_and_sets_format(self, _winreg): |
| 77 | + _winreg.HKEY_CLASSES_ROOT = HKCR |
| 78 | + _winreg.REG_MULTI_SZ = MULTI_SZ |
| 79 | + _winreg.REG_SZ = SZ |
| 80 | + hkey = mock.Mock(spec=winreg.HKEYType) |
| 81 | + _winreg.CreateKey.return_value = hkey |
| 82 | + levels = ["lv=DEBUG"] |
| 83 | + format = "FMT" |
| 84 | + reg_clsid = GUID.create_new() |
| 85 | + registrar = Registrar() |
| 86 | + |
| 87 | + class Cls: |
| 88 | + _reg_clsid_ = reg_clsid |
| 89 | + |
| 90 | + registrar.debug(Cls, levels, format) |
| 91 | + _winreg.CreateKey.assert_called_once_with(HKCR, rf"CLSID\{reg_clsid}\Logging") |
| 92 | + self.assertEqual( |
| 93 | + _winreg.SetValueEx.call_args_list, |
| 94 | + [ |
| 95 | + mock.call(hkey, "levels", None, MULTI_SZ, levels), |
| 96 | + mock.call(hkey, "format", None, SZ, format), |
| 97 | + ], |
| 98 | + ) |
| 99 | + |
| 100 | + @mock.patch.object(register, "winreg") |
| 101 | + def test_calls_createkey_and_deletes_format(self, _winreg): |
| 102 | + _winreg.HKEY_CLASSES_ROOT = HKCR |
| 103 | + _winreg.REG_MULTI_SZ = MULTI_SZ |
| 104 | + hkey = mock.Mock(spec=winreg.HKEYType) |
| 105 | + _winreg.CreateKey.return_value = hkey |
| 106 | + levels = ["lv=DEBUG"] |
| 107 | + reg_clsid = GUID.create_new() |
| 108 | + registrar = Registrar() |
| 109 | + |
| 110 | + class Cls: |
| 111 | + _reg_clsid_ = reg_clsid |
| 112 | + |
| 113 | + registrar.debug(Cls, levels, None) |
| 114 | + _winreg.CreateKey.assert_called_once_with(HKCR, rf"CLSID\{reg_clsid}\Logging") |
| 115 | + _winreg.SetValueEx.assert_called_once_with( |
| 116 | + hkey, "levels", None, MULTI_SZ, levels |
| 117 | + ) |
| 118 | + _winreg.DeleteValue.assert_called_once_with(hkey, "format") |
| 119 | + |
| 120 | + @mock.patch.object(register, "winreg") |
| 121 | + def test_calls_createkey_and_ignores_errors_on_deleting(self, _winreg): |
| 122 | + _winreg.HKEY_CLASSES_ROOT = HKCR |
| 123 | + _winreg.REG_MULTI_SZ = MULTI_SZ |
| 124 | + hkey = mock.Mock(spec=winreg.HKEYType) |
| 125 | + _winreg.CreateKey.return_value = hkey |
| 126 | + ERROR_FILE_NOT_FOUND = 2 |
| 127 | + err = OSError(ERROR_FILE_NOT_FOUND, "msg", "filename", ERROR_FILE_NOT_FOUND) |
| 128 | + _winreg.DeleteValue.side_effect = err |
| 129 | + levels = ["lv=DEBUG"] |
| 130 | + reg_clsid = GUID.create_new() |
| 131 | + registrar = Registrar() |
| 132 | + |
| 133 | + class Cls: |
| 134 | + _reg_clsid_ = reg_clsid |
| 135 | + |
| 136 | + registrar.debug(Cls, levels, None) |
| 137 | + _winreg.CreateKey.assert_called_once_with(HKCR, rf"CLSID\{reg_clsid}\Logging") |
| 138 | + _winreg.SetValueEx.assert_called_once_with( |
| 139 | + hkey, "levels", None, MULTI_SZ, levels |
| 140 | + ) |
| 141 | + _winreg.DeleteValue.assert_called_once_with(hkey, "format") |
| 142 | + |
| 143 | + @mock.patch.object(register, "winreg") |
| 144 | + def test_calls_createkey_and_not_ignores_errors_on_deleting(self, _winreg): |
| 145 | + _winreg.HKEY_CLASSES_ROOT = HKCR |
| 146 | + _winreg.REG_MULTI_SZ = MULTI_SZ |
| 147 | + hkey = mock.Mock(spec=winreg.HKEYType) |
| 148 | + _winreg.CreateKey.return_value = hkey |
| 149 | + ERROR_ACCESS_DENIED = 5 |
| 150 | + err = OSError(ERROR_ACCESS_DENIED, "msg", "filename", ERROR_ACCESS_DENIED) |
| 151 | + _winreg.DeleteValue.side_effect = err |
| 152 | + levels = ["lv=DEBUG"] |
| 153 | + reg_clsid = GUID.create_new() |
| 154 | + registrar = Registrar() |
| 155 | + |
| 156 | + class Cls: |
| 157 | + _reg_clsid_ = reg_clsid |
| 158 | + |
| 159 | + with self.assertRaises(OSError) as e: |
| 160 | + registrar.debug(Cls, levels, None) |
| 161 | + self.assertEqual(e.exception.winerror, ERROR_ACCESS_DENIED) |
| 162 | + _winreg.CreateKey.assert_called_once_with(HKCR, rf"CLSID\{reg_clsid}\Logging") |
| 163 | + _winreg.SetValueEx.assert_called_once_with( |
| 164 | + hkey, "levels", None, MULTI_SZ, levels |
| 165 | + ) |
| 166 | + _winreg.DeleteValue.assert_called_once_with(hkey, "format") |
| 167 | + |
| 168 | + |
| 169 | +class Test_Registrar_register(ut.TestCase): |
| 170 | + def test_calls_cls_register(self): |
| 171 | + cls = mock.Mock(spec=["_register"]) |
| 172 | + registrar = Registrar() |
| 173 | + registrar.register(cls) |
| 174 | + cls._register.assert_called_once_with(registrar) |
| 175 | + |
| 176 | + # The coverage for COM server registration is ensured by the setup |
| 177 | + # of `test_comserver` and `test_dispinterface`, so no additional tests |
| 178 | + # are performed here now. |
| 179 | + |
| 180 | + |
| 181 | +class Test_Registrar_unregister(ut.TestCase): |
| 182 | + def test_calls_cls_unregister(self): |
| 183 | + cls = mock.Mock(spec=["_unregister"]) |
| 184 | + registrar = Registrar() |
| 185 | + registrar.unregister(cls) |
| 186 | + cls._unregister.assert_called_once_with(registrar) |
| 187 | + |
| 188 | + # The coverage for COM server unregistration is ensured by the teardown |
| 189 | + # of `test_comserver` and `test_dispinterface`, so no additional tests |
| 190 | + # are performed here now. |
16 | 191 |
|
17 | 192 |
|
18 | 193 | class Test_get_serverdll(ut.TestCase): |
|
0 commit comments