@@ -39,21 +39,38 @@ defmodule Bypass do
3939 ```
4040
4141 """
42- @ spec open ( Keyword . t ( ) ) :: Bypass . t ( ) | DynamicSupervisor . on_start_child ( )
42+ @ spec open ( Keyword . t ( ) ) :: Bypass . t ( )
4343 def open ( opts \\ [ ] ) do
44+ pid = start_instance ( opts )
45+ port = Bypass.Instance . call ( pid , :port )
46+ debug_log ( "Did open connection #{ inspect ( pid ) } on port #{ inspect ( port ) } " )
47+ bypass = % Bypass { pid: pid , port: port }
48+ setup_framework_integration ( test_framework ( ) , bypass )
49+ bypass
50+ end
51+
52+ defp start_instance ( opts ) do
4453 case DynamicSupervisor . start_child ( Bypass.Supervisor , Bypass.Instance . child_spec ( opts ) ) do
4554 { :ok , pid } ->
46- port = Bypass.Instance . call ( pid , :port )
47- debug_log ( "Did open connection #{ inspect ( pid ) } on port #{ inspect ( port ) } " )
48- bypass = % Bypass { pid: pid , port: port }
49- setup_framework_integration ( test_framework ( ) , bypass )
50- bypass
51-
52- other - >
53- other
55+ pid
56+
57+ { :ok , pid , _info } ->
58+ pid
59+
60+ { :error , reason } ->
61+ raise "Failed to start bypass instance. \n " < >
62+ "Reason: #{ start_supervised_error ( reason ) } "
5463 end
5564 end
5665
66+ defp start_supervised_error ( { { :EXIT , reason } , info } ) when is_tuple ( info ) ,
67+ do: Exception . format_exit ( reason )
68+
69+ defp start_supervised_error ( { reason , info } ) when is_tuple ( info ) ,
70+ do: Exception . format_exit ( reason )
71+
72+ defp start_supervised_error ( reason ) , do: Exception . format_exit ( { :start_spec , reason } )
73+
5774 defp setup_framework_integration ( :ex_unit , bypass = % { pid: pid } ) do
5875 ExUnit.Callbacks . on_exit ( { Bypass , pid } , fn ->
5976 do_verify_expectations ( bypass . pid , ExUnit.AssertionError )
0 commit comments