11defmodule 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
63126end
0 commit comments