|
1 | 1 | import unittest |
2 | 2 | import tempfile |
3 | | -import zipfile |
4 | 3 | import os |
| 4 | +import io |
| 5 | +from unittest.mock import patch |
5 | 6 | import compare50.__main__ as main |
6 | 7 | import compare50._api as api |
7 | 8 |
|
@@ -110,6 +111,26 @@ def test_ignore_non_utf8(self): |
110 | 111 | subs = {sub for sub in subs if sub.files} |
111 | 112 | self.assertEqual(subs, set()) |
112 | 113 |
|
| 114 | + def test_permission_error(self): |
| 115 | + os.mkdir("foo") |
| 116 | + file_path = "foo/bar.py" |
| 117 | + with open(file_path, "w") as f: |
| 118 | + f.write("test content") |
| 119 | + os.chmod(file_path, 0o000) |
| 120 | + with self.assertRaises(PermissionError): |
| 121 | + main.SubmissionFactory._is_valid_utf8(file_path) |
| 122 | + os.chmod(file_path, 0o644) |
| 123 | + |
| 124 | + def test_excepthook_permission_error(self): |
| 125 | + error = PermissionError("Permission denied") |
| 126 | + error.filename = "/path/to/restricted/file.py" |
| 127 | + with patch('sys.stderr', new_callable=io.StringIO) as mock_stderr: |
| 128 | + with patch('sys.exit') as mock_exit: |
| 129 | + main.excepthook(PermissionError, error, None) |
| 130 | + error_output = mock_stderr.getvalue() |
| 131 | + self.assertIn("Permission denied: /path/to/restricted/file.py", error_output) |
| 132 | + mock_exit.assert_called_once_with(1) |
| 133 | + |
113 | 134 |
|
114 | 135 | if __name__ == "__main__": |
115 | 136 | unittest.main() |
0 commit comments