22
33require_relative "../spec_helper"
44require "react_on_rails/dev/server_manager"
5+ require "open3"
56
67RSpec . describe ReactOnRails ::Dev ::ServerManager do
78 # Suppress stdout/stderr during tests
@@ -73,16 +74,14 @@ def mock_system_calls
7374 end
7475
7576 it "attempts to kill development processes" do
76- # Mock all the pgrep patterns used in kill_processes
77- allow_any_instance_of ( Kernel ) . to receive ( :` ) . with ( "pgrep -f \" rails\" 2>/dev/null" ) . and_return ( "1234\n 5678" )
78- pgrep_cmd = "pgrep -f \" node.*react[-_]on[-_]rails\" 2>/dev/null"
79- allow_any_instance_of ( Kernel ) . to receive ( :` ) . with ( pgrep_cmd ) . and_return ( "2345" )
80- allow_any_instance_of ( Kernel ) . to receive ( :` ) . with ( "pgrep -f \" overmind\" 2>/dev/null" ) . and_return ( "" )
81- allow_any_instance_of ( Kernel ) . to receive ( :` ) . with ( "pgrep -f \" foreman\" 2>/dev/null" ) . and_return ( "" )
82- allow_any_instance_of ( Kernel ) . to receive ( :` ) . with ( "pgrep -f \" ruby.*puma\" 2>/dev/null" ) . and_return ( "" )
83- allow_any_instance_of ( Kernel ) . to receive ( :` ) . with ( "pgrep -f \" webpack-dev-server\" 2>/dev/null" ) . and_return ( "" )
84- shakapacker_cmd = "pgrep -f \" bin/shakapacker-dev-server\" 2>/dev/null"
85- allow_any_instance_of ( Kernel ) . to receive ( :` ) . with ( shakapacker_cmd ) . and_return ( "" )
77+ # Mock Open3.capture2 calls that find_process_pids uses
78+ allow ( Open3 ) . to receive ( :capture2 ) . with ( "pgrep" , "-f" , "rails" , err : File ::NULL ) . and_return ( [ "1234\n 5678" , nil ] )
79+ allow ( Open3 ) . to receive ( :capture2 ) . with ( "pgrep" , "-f" , "node.*react[-_]on[-_]rails" , err : File ::NULL ) . and_return ( [ "2345" , nil ] )
80+ allow ( Open3 ) . to receive ( :capture2 ) . with ( "pgrep" , "-f" , "overmind" , err : File ::NULL ) . and_return ( [ "" , nil ] )
81+ allow ( Open3 ) . to receive ( :capture2 ) . with ( "pgrep" , "-f" , "foreman" , err : File ::NULL ) . and_return ( [ "" , nil ] )
82+ allow ( Open3 ) . to receive ( :capture2 ) . with ( "pgrep" , "-f" , "ruby.*puma" , err : File ::NULL ) . and_return ( [ "" , nil ] )
83+ allow ( Open3 ) . to receive ( :capture2 ) . with ( "pgrep" , "-f" , "webpack-dev-server" , err : File ::NULL ) . and_return ( [ "" , nil ] )
84+ allow ( Open3 ) . to receive ( :capture2 ) . with ( "pgrep" , "-f" , "bin/shakapacker-dev-server" , err : File ::NULL ) . and_return ( [ "" , nil ] )
8685
8786 allow ( Process ) . to receive ( :pid ) . and_return ( 9999 ) # Current process PID
8887 expect ( Process ) . to receive ( :kill ) . with ( "TERM" , 1234 )
@@ -93,7 +92,12 @@ def mock_system_calls
9392 end
9493
9594 it "cleans up socket files when they exist" do
95+ # Make sure no processes are found so cleanup_socket_files gets called
96+ allow ( Open3 ) . to receive ( :capture2 ) . and_return ( [ "" , nil ] )
97+
9698 allow ( File ) . to receive ( :exist? ) . with ( ".overmind.sock" ) . and_return ( true )
99+ allow ( File ) . to receive ( :exist? ) . with ( "tmp/sockets/overmind.sock" ) . and_return ( false )
100+ allow ( File ) . to receive ( :exist? ) . with ( "tmp/pids/server.pid" ) . and_return ( false )
97101 expect ( File ) . to receive ( :delete ) . with ( ".overmind.sock" )
98102
99103 described_class . kill_processes
0 commit comments