Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

emp: use TCPServer instead of WEBrick for test #162041

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions Formula/e/emp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ def install
(buildpath/"src/github.com/remind101/").mkpath
ln_s buildpath, buildpath/"src/github.com/remind101/empire"

system "go", "build", "-o", bin/"emp", "./src/github.com/remind101/empire/cmd/emp"
system "go", "build", *std_go_args, "./src/github.com/remind101/empire/cmd/emp"
end

test do
require "webrick"
port = free_port

server = WEBrick::HTTPServer.new Port: 8035
server.mount_proc "/apps/foo/releases" do |_req, res|
# Mock an API server response to test the CLI
fork do
server = TCPServer.new(port)
resp = {
"created_at" => "2015-10-12T0:00:00.00000000-00:00",
"description" => "my awesome release",
Expand All @@ -49,17 +50,23 @@ def install
},
"version" => 1,
}
res.body = JSON.generate([resp])
body = JSON.generate([resp])

loop do
socket = server.accept
socket.write "HTTP/1.1 200 OK\r\n" \
"Content-Type: application/json; charset=utf-8\r\n" \
"Content-Length: #{body.bytesize}\r\n" \
"\r\n"
socket.write body
socket.close
end
end

Thread.new { server.start }
sleep 1

begin
ENV["EMPIRE_API_URL"] = "http://127.0.0.1:8035"
assert_match(/v1 zab Oct 1(1|2|3) 2015 my awesome release/,
shell_output("#{bin}/emp releases -a foo").strip)
ensure
server.shutdown
end
ENV["EMPIRE_API_URL"] = "http://127.0.0.1:#{port}"
assert_match(/v1 zab Oct 1(1|2|3) 2015 my awesome release/,
shell_output("#{bin}/emp releases -a foo").strip)
end
end
Loading