|
1 | 1 | import os |
2 | 2 | import pytest |
| 3 | +import winreg |
3 | 4 |
|
4 | 5 | from pathlib import Path |
5 | 6 |
|
@@ -105,3 +106,134 @@ def test_welcome(assert_log): |
105 | 106 | assert_log(".*Welcome.*", assert_log.end_of_log()) |
106 | 107 | welcome() |
107 | 108 | assert_log(".*Welcome.*", assert_log.end_of_log()) |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +def test_firstrun_command(monkeypatch): |
| 113 | + from manage import commands |
| 114 | + |
| 115 | + called_first_run = False |
| 116 | + called_show_usage = False |
| 117 | + |
| 118 | + def fake_first_run(*args): |
| 119 | + nonlocal called_first_run |
| 120 | + called_first_run = True |
| 121 | + |
| 122 | + def fake_show_usage(*args): |
| 123 | + nonlocal called_show_usage |
| 124 | + called_show_usage = True |
| 125 | + |
| 126 | + monkeypatch.setattr(firstrun, "first_run", fake_first_run) |
| 127 | + monkeypatch.setattr(commands.FirstRun, "confirm", False) |
| 128 | + monkeypatch.setattr(commands.FirstRun, "show_usage", fake_show_usage) |
| 129 | + cmd = commands.find_command(["**first_run"], None) |
| 130 | + cmd.execute() |
| 131 | + assert called_first_run |
| 132 | + assert called_show_usage |
| 133 | + |
| 134 | + |
| 135 | +def test_install_configure_command(monkeypatch): |
| 136 | + from manage import commands |
| 137 | + |
| 138 | + called_first_run = False |
| 139 | + called_show_usage = False |
| 140 | + |
| 141 | + def fake_first_run(*args): |
| 142 | + nonlocal called_first_run |
| 143 | + called_first_run = True |
| 144 | + |
| 145 | + def fake_show_usage(*args): |
| 146 | + nonlocal called_show_usage |
| 147 | + called_show_usage = True |
| 148 | + |
| 149 | + monkeypatch.setattr(firstrun, "first_run", fake_first_run) |
| 150 | + monkeypatch.setattr(commands.FirstRun, "confirm", False) |
| 151 | + monkeypatch.setattr(commands.FirstRun, "show_usage", fake_show_usage) |
| 152 | + cmd = commands.find_command(["install", "--configure"], None) |
| 153 | + cmd.execute() |
| 154 | + assert called_first_run |
| 155 | + assert not called_show_usage |
| 156 | + |
| 157 | + |
| 158 | +def _create_key_read_only(key, subkey, *args, **kwargs): |
| 159 | + return winreg.OpenKeyEx(key, subkey) |
| 160 | + |
| 161 | + |
| 162 | +def _raise_oserror(*args, **kwargs): |
| 163 | + raise OSError("injected error") |
| 164 | + |
| 165 | + |
| 166 | +@pytest.fixture |
| 167 | +def protect_reg(monkeypatch): |
| 168 | + import _native |
| 169 | + monkeypatch.setattr(winreg, "CreateKeyEx", _create_key_read_only) |
| 170 | + monkeypatch.setattr(winreg, "SetValueEx", _raise_oserror) |
| 171 | + monkeypatch.setattr(_native, "broadcast_settings_change", lambda *a: None) |
| 172 | + |
| 173 | + |
| 174 | +def test_do_global_dir_open_fail(protect_reg, fake_config, assert_log, monkeypatch): |
| 175 | + monkeypatch.setattr(winreg, "OpenKeyEx", _raise_oserror) |
| 176 | + firstrun.do_global_dir_on_path(fake_config) |
| 177 | + assert_log(assert_log.skip_until("Failed to update PATH.+")) |
| 178 | + |
| 179 | + |
| 180 | +def test_do_global_dir_read_fail(protect_reg, fake_config, assert_log, monkeypatch): |
| 181 | + monkeypatch.setattr(winreg, "QueryValueEx", _raise_oserror) |
| 182 | + firstrun.do_global_dir_on_path(fake_config) |
| 183 | + assert_log(assert_log.skip_until("Failed to update PATH.+")) |
| 184 | + |
| 185 | + |
| 186 | +def test_do_global_dir_read_kind_fail(protect_reg, fake_config, assert_log, monkeypatch): |
| 187 | + monkeypatch.setattr(winreg, "QueryValueEx", lambda *a: (100, winreg.REG_DWORD)) |
| 188 | + firstrun.do_global_dir_on_path(fake_config) |
| 189 | + assert_log( |
| 190 | + assert_log.skip_until("Initial path: %s", (100,)), |
| 191 | + ("Value kind is %s.+", (winreg.REG_DWORD,)), |
| 192 | + ) |
| 193 | + |
| 194 | + |
| 195 | +def test_do_global_dir_path_already_set(protect_reg, fake_config, assert_log, monkeypatch): |
| 196 | + monkeypatch.setattr(winreg, "QueryValueEx", lambda *a: (f"{fake_config.global_dir};b;c", winreg.REG_SZ)) |
| 197 | + firstrun.do_global_dir_on_path(fake_config) |
| 198 | + assert_log(assert_log.skip_until("Path is already found")) |
| 199 | + |
| 200 | + monkeypatch.setattr(winreg, "QueryValueEx", lambda *a: (f"a;{fake_config.global_dir};c", winreg.REG_SZ)) |
| 201 | + firstrun.do_global_dir_on_path(fake_config) |
| 202 | + assert_log(assert_log.skip_until("Path is already found")) |
| 203 | + |
| 204 | + monkeypatch.setattr(winreg, "QueryValueEx", lambda *a: (f"a;b;{fake_config.global_dir}", winreg.REG_SZ)) |
| 205 | + firstrun.do_global_dir_on_path(fake_config) |
| 206 | + assert_log(assert_log.skip_until("Path is already found")) |
| 207 | + |
| 208 | + |
| 209 | +def test_do_global_dir_path_lost_race(protect_reg, fake_config, assert_log, monkeypatch): |
| 210 | + paths = ["a;b", "a;b;c"] |
| 211 | + monkeypatch.setattr(winreg, "QueryValueEx", lambda *a: (paths.pop(), winreg.REG_SZ)) |
| 212 | + firstrun.do_global_dir_on_path(fake_config) |
| 213 | + assert_log( |
| 214 | + assert_log.skip_until("New path: %s", None), |
| 215 | + "Path is added successfully", |
| 216 | + "PATH has changed.+", |
| 217 | + ) |
| 218 | + |
| 219 | + |
| 220 | +def test_do_global_dir_write_same_kind(protect_reg, fake_config, monkeypatch): |
| 221 | + saved = [] |
| 222 | + monkeypatch.setattr(winreg, "SetValueEx", lambda *a: saved.append(a)) |
| 223 | + |
| 224 | + monkeypatch.setattr(winreg, "QueryValueEx", lambda *a: ("a;", winreg.REG_SZ)) |
| 225 | + firstrun.do_global_dir_on_path(fake_config) |
| 226 | + assert saved[-1][1:] == ("Path", 0, winreg.REG_SZ, f"a;{fake_config.global_dir}") |
| 227 | + |
| 228 | + monkeypatch.setattr(winreg, "QueryValueEx", lambda *a: ("a", winreg.REG_EXPAND_SZ)) |
| 229 | + firstrun.do_global_dir_on_path(fake_config) |
| 230 | + assert saved[-1][1:] == ("Path", 0, winreg.REG_EXPAND_SZ, f"a;{fake_config.global_dir}") |
| 231 | + |
| 232 | + |
| 233 | +def test_do_global_dir_path_fail_broadcast(protect_reg, fake_config, assert_log, monkeypatch): |
| 234 | + import _native |
| 235 | + monkeypatch.setattr(_native, "broadcast_settings_change", _raise_oserror) |
| 236 | + monkeypatch.setattr(winreg, "QueryValueEx", lambda *a: ("a;", winreg.REG_SZ)) |
| 237 | + monkeypatch.setattr(winreg, "SetValueEx", lambda *a: None) |
| 238 | + firstrun.do_global_dir_on_path(fake_config) |
| 239 | + assert_log(assert_log.skip_until("Failed to notify of PATH environment.+")) |
0 commit comments