Skip to content

Commit 8d7bfab

Browse files
committed
test(utils): Add test for get_lines_from_file
Fixes: GH-3515
1 parent 1b868c8 commit 8d7bfab

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tests/test_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
ensure_integration_enabled,
3535
to_string,
3636
exc_info_from_error,
37+
get_lines_from_file,
3738
)
3839

3940

@@ -1037,3 +1038,31 @@ class NotAnException:
10371038
exc_info_from_error(NotAnException())
10381039

10391040
assert "Expected Exception object to report, got <class" in str(exc.value)
1041+
1042+
1043+
def test_get_lines_from_file_handle_linecache_errors():
1044+
expected_result = ([], None, [])
1045+
1046+
class Loader:
1047+
@staticmethod
1048+
def get_source(module):
1049+
raise IOError("something went wrong")
1050+
1051+
result = get_lines_from_file("filename", 10, loader=Loader())
1052+
assert result == expected_result
1053+
1054+
with mock.patch(
1055+
"sentry_sdk.utils.linecache.getlines",
1056+
side_effect=OSError("something went wrong"),
1057+
):
1058+
result = get_lines_from_file("filename", 10)
1059+
assert result == expected_result
1060+
1061+
lines = ["line1", "line2", "line3"]
1062+
1063+
def fake_getlines(filename):
1064+
return lines
1065+
1066+
with mock.patch("sentry_sdk.utils.linecache.getlines", fake_getlines):
1067+
result = get_lines_from_file("filename", 10)
1068+
assert result == expected_result

0 commit comments

Comments
 (0)