Skip to content

Commit 69e47ec

Browse files
committed
Add more tests for Sentry.Sources
1 parent e10df68 commit 69e47ec

File tree

4 files changed

+82
-7
lines changed

4 files changed

+82
-7
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ erl_crash.dump
77
/plts/*.plt.hash
88
/cover
99

10+
# Generated by ExUnit's tmp_dir
11+
/tmp
12+
1013
# Generated by "mix sentry.package_source_code"
1114
/priv/sentry.map

lib/sentry/sources.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ defmodule Sentry.Sources do
1111

1212
@source_code_map_key {:sentry, :source_code_map}
1313

14-
@spec load_source_code_map_if_present() :: {:loaded, source_map()} | {:error, term()}
15-
def load_source_code_map_if_present do
16-
path = Path.relative_to_cwd(path_of_packaged_source_code())
14+
# Default argument is here for testing.
15+
@spec load_source_code_map_if_present(Path.t()) :: {:loaded, source_map()} | {:error, term()}
16+
def load_source_code_map_if_present(path \\ path_of_packaged_source_code()) do
17+
path = Path.relative_to_cwd(path)
1718

1819
with {:ok, contents} <- File.read(path),
1920
{:ok, source_map} <- decode_source_code_map(contents) do

test/sentry/config_test.exs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,15 @@ defmodule Sentry.ConfigTest do
118118
assert Config.validate!([])[:json_library] == Jason
119119

120120
assert_raise ArgumentError, ~r/invalid value for :json_library option/, fn ->
121-
Config.validate!(json_library: URI)
121+
Config.validate!(json_library: Atom)
122+
end
123+
124+
assert_raise ArgumentError, ~r/invalid value for :json_library option/, fn ->
125+
Config.validate!(json_library: nil)
126+
end
127+
128+
assert_raise ArgumentError, ~r/invalid value for :json_library option/, fn ->
129+
Config.validate!(json_library: "not a module")
122130
end
123131
end
124132

test/sources_test.exs

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
defmodule Sentry.SourcesTest do
2-
use Sentry.Case, async: true
2+
# Not async because we're capturing IO.
3+
use Sentry.Case, async: false
4+
5+
import ExUnit.CaptureIO
6+
7+
alias Sentry.Sources
38

49
describe "load_files/1" do
510
test "loads files" do
@@ -25,7 +30,7 @@ defmodule Sentry.SourcesTest do
2530
5 => "end"
2631
}
2732
}} =
28-
Sentry.Sources.load_files(
33+
Sources.load_files(
2934
root_source_code_paths: paths,
3035
source_code_exclude_patterns: []
3136
)
@@ -38,7 +43,7 @@ defmodule Sentry.SourcesTest do
3843
]
3944

4045
assert {:error, message} =
41-
Sentry.Sources.load_files(
46+
Sources.load_files(
4247
root_source_code_paths: paths,
4348
source_code_exclude_patterns: []
4449
)
@@ -60,4 +65,62 @@ defmodule Sentry.SourcesTest do
6065
"""
6166
end
6267
end
68+
69+
describe "load_source_code_map_if_present/0" do
70+
@tag :tmp_dir
71+
test "fails if the source code map at the destination is malformed", %{tmp_dir: tmp_dir} do
72+
path = Path.join(tmp_dir, "malformed.map")
73+
File.write!(path, "hello!")
74+
75+
output =
76+
capture_io(:stderr, fn ->
77+
assert {:error, :decoding_error} = Sources.load_source_code_map_if_present(path)
78+
end)
79+
80+
assert output =~ "Sentry found a source code map file"
81+
assert output =~ "but it was unable to decode"
82+
end
83+
84+
@tag :tmp_dir
85+
test "stores the source code map in :persistent_term if valid", %{tmp_dir: tmp_dir} do
86+
encoded_map = Sources.encode_source_code_map(%{"foo.ex" => %{}})
87+
path = Path.join(tmp_dir, "valid.map")
88+
File.write!(path, encoded_map)
89+
assert {:loaded, map} = Sources.load_source_code_map_if_present(path)
90+
assert map == %{"foo.ex" => %{}}
91+
assert :persistent_term.get({:sentry, :source_code_map}) == %{"foo.ex" => %{}}
92+
after
93+
:persistent_term.erase({:sentry, :source_code_map})
94+
end
95+
end
96+
97+
describe "get_source_context/3" do
98+
test "returns the correct context" do
99+
map = %{
100+
"foo.ex" => %{
101+
1 => "defmodule Foo do",
102+
2 => " def bar do",
103+
3 => " \"bar\"",
104+
4 => " end",
105+
5 => "end"
106+
},
107+
"bar.ex" => %{
108+
1 => "defmodule Bar do",
109+
2 => " def baz do",
110+
3 => " \"baz\"",
111+
4 => " end",
112+
5 => "end"
113+
}
114+
}
115+
116+
assert {pre, context, post} = Sources.get_source_context(map, "foo.ex", 4)
117+
assert pre == ["defmodule Foo do", " def bar do", " \"bar\""]
118+
assert context == " end"
119+
assert post == ["end"]
120+
end
121+
122+
test "works if the file doesn't exist" do
123+
assert {[], nil, []} = Sources.get_source_context(%{}, "foo.ex", 4)
124+
end
125+
end
63126
end

0 commit comments

Comments
 (0)