Skip to content

Commit

Permalink
emp: use TCPServer instead of WEBrick for test
Browse files Browse the repository at this point in the history
Related:
* Homebrew#161976
  • Loading branch information
alebcay committed Feb 10, 2024
1 parent e150e53 commit 9bf26f8
Showing 1 changed file with 20 additions and 13 deletions.
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

0 comments on commit 9bf26f8

Please sign in to comment.