|
1 | 1 | import os.path |
2 | 2 | import re |
| 3 | +import stat |
3 | 4 | import sys |
4 | 5 | from unittest import mock |
5 | 6 |
|
6 | 7 | import pytest |
7 | 8 |
|
8 | 9 | from pre_commit import error_handler |
| 10 | +from pre_commit.store import Store |
9 | 11 | from pre_commit.util import CalledProcessError |
10 | 12 | from testing.util import cmd_output_mocked_pre_commit_home |
| 13 | +from testing.util import xfailif_windows |
11 | 14 |
|
12 | 15 |
|
13 | 16 | @pytest.fixture |
@@ -168,3 +171,29 @@ def test_error_handler_no_tty(tempdir_factory): |
168 | 171 | out_lines = out.splitlines() |
169 | 172 | assert out_lines[-2] == 'An unexpected error has occurred: ValueError: ☃' |
170 | 173 | assert out_lines[-1] == f'Check the log at {log_file}' |
| 174 | + |
| 175 | + |
| 176 | +@xfailif_windows # pragma: win32 no cover |
| 177 | +def test_error_handler_read_only_filesystem(mock_store_dir, cap_out, capsys): |
| 178 | + # a better scenario would be if even the Store crash would be handled |
| 179 | + # but realistically we're only targetting systems where the Store has |
| 180 | + # already been set up |
| 181 | + Store() |
| 182 | + |
| 183 | + write = (stat.S_IWGRP | stat.S_IWOTH | stat.S_IWUSR) |
| 184 | + os.chmod(mock_store_dir, os.stat(mock_store_dir).st_mode & ~write) |
| 185 | + |
| 186 | + with pytest.raises(SystemExit): |
| 187 | + with error_handler.error_handler(): |
| 188 | + raise ValueError('ohai') |
| 189 | + |
| 190 | + output = cap_out.get() |
| 191 | + assert output.startswith( |
| 192 | + 'An unexpected error has occurred: ValueError: ohai\n' |
| 193 | + 'Failed to write to log at ', |
| 194 | + ) |
| 195 | + |
| 196 | + # our cap_out mock is imperfect so the rest of the output goes to capsys |
| 197 | + out, _ = capsys.readouterr() |
| 198 | + # the things that normally go to the log file will end up here |
| 199 | + assert '### version information' in out |
0 commit comments