Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compare50/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def excepthook(cls, exc, tb):
termcolor.cprint(str(exc), "red", file=sys.stderr)
elif cls is FileNotFoundError:
termcolor.cprint("{} not found".format(exc.filename), "red", file=sys.stderr)
elif cls is PermissionError:
termcolor.cprint("Permission denied: {}".format(exc.filename), "red", file=sys.stderr)
elif not issubclass(cls, Exception) and not isinstance(exc, KeyboardInterrupt):
# Class is some other BaseException, better just let it go
return
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
packages=find_packages(exclude=["tests"]),
scripts=["bin/compare50"],
url="https://github.com/cs50/compare50",
version="1.2.11",
version="1.2.12",
include_package_data=True,
)
23 changes: 22 additions & 1 deletion tests/main_tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import unittest
import tempfile
import zipfile
import os
import io
from unittest.mock import patch
import compare50.__main__ as main
import compare50._api as api

Expand Down Expand Up @@ -110,6 +111,26 @@ def test_ignore_non_utf8(self):
subs = {sub for sub in subs if sub.files}
self.assertEqual(subs, set())

def test_permission_error(self):
os.mkdir("foo")
file_path = "foo/bar.py"
with open(file_path, "w") as f:
f.write("test content")
os.chmod(file_path, 0o000)
with self.assertRaises(PermissionError):
main.SubmissionFactory._is_valid_utf8(file_path)
os.chmod(file_path, 0o644)

def test_excepthook_permission_error(self):
error = PermissionError("Permission denied")
error.filename = "/path/to/restricted/file.py"
with patch('sys.stderr', new_callable=io.StringIO) as mock_stderr:
with patch('sys.exit') as mock_exit:
main.excepthook(PermissionError, error, None)
error_output = mock_stderr.getvalue()
self.assertIn("Permission denied: /path/to/restricted/file.py", error_output)
mock_exit.assert_called_once_with(1)


if __name__ == "__main__":
unittest.main()
Loading