Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle os.path.devnull access issues #2579

Merged
merged 4 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@

def get_git_revision():
# type: () -> Optional[str]
with open(os.path.devnull, "w+") as null:
try:
try:
with open(os.path.devnull, "w+") as null:
revision = (
subprocess.Popen(
["git", "rev-parse", "HEAD"],
Expand All @@ -110,8 +110,8 @@
.strip()
.decode("utf-8")
)
except (OSError, IOError):
return None
except (OSError, IOError, FileNotFoundError):
return None

Check warning on line 114 in sentry_sdk/utils.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/utils.py#L113-L114

Added lines #L113 - L114 were not covered by tests

return revision

Expand Down
15 changes: 15 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Components,
Dsn,
get_error_message,
get_git_revision,
is_valid_sample_rate,
logger,
match_regex_list,
Expand Down Expand Up @@ -557,3 +558,17 @@ def test_installed_modules_caching():

_get_installed_modules()
mock_generate_installed_modules.assert_not_called()


def test_devnull_inaccessible():
with mock.patch("sentry_sdk.utils.open", side_effect=OSError("oh no")):
revision = get_git_revision()

assert revision is None


def test_devnull_not_found():
with mock.patch("sentry_sdk.utils.open", side_effect=FileNotFoundError("oh no")):
revision = get_git_revision()

assert revision is None