From a7455dff86dbe77b5ba5a1700d49eaac8a13194b Mon Sep 17 00:00:00 2001 From: Vyacheslav Alexeev Date: Sun, 12 Jul 2020 20:00:29 +0300 Subject: [PATCH] Add tmp_path manager module --- test/helper.rb | 7 ------ test/helpers/integration.rb | 2 ++ test/helpers/tmp_path.rb | 37 ++++++++++++++++++++++++++++++++ test/test_binder.rb | 2 ++ test/test_cli.rb | 2 ++ test/test_integration_pumactl.rb | 1 + test/test_launcher.rb | 3 +++ test/test_unix_socket.rb | 9 ++++---- 8 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 test/helpers/tmp_path.rb diff --git a/test/helper.rb b/test/helper.rb index 28ca392e..47240707 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -44,13 +44,6 @@ def hit(uris) end end -def tmp_path(extension=nil) - sock_file = Tempfile.new(['', extension]) - path = sock_file.path - sock_file.close! - path -end - module UniquePort def self.call TCPServer.open('127.0.0.1', 0) do |server| diff --git a/test/helpers/integration.rb b/test/helpers/integration.rb index c458bac5..afa2985e 100644 --- a/test/helpers/integration.rb +++ b/test/helpers/integration.rb @@ -2,10 +2,12 @@ require "puma/control_cli" require "open3" +require_relative 'tmp_path' # Only single mode tests go here. Cluster and pumactl tests # have their own files, use those instead class TestIntegration < Minitest::Test + include TmpPath HOST = "127.0.0.1" TOKEN = "xxyyzz" WORKERS = 2 diff --git a/test/helpers/tmp_path.rb b/test/helpers/tmp_path.rb new file mode 100644 index 00000000..02b129d0 --- /dev/null +++ b/test/helpers/tmp_path.rb @@ -0,0 +1,37 @@ +module TmpPath + def run(*args) + begin + result = super(*args) + rescue Interrupt + clean_tmp_paths + raise + end + + clean_tmp_paths + result + end + + private + + def tmp_path(extension=nil) + sock_file = Tempfile.new(['', extension]) + path = sock_file.path + sock_file.close! + tmp_paths << path + path + end + + def tmp_paths + @tmp_paths ||= [] + end + + def clean_tmp_paths + tmp_paths.each { |path| delete_tmp_path(path) } + @tmp_paths = [] + end + + def delete_tmp_path(path) + File.unlink(path) + rescue Errno::ENOENT + end +end diff --git a/test/test_binder.rb b/test/test_binder.rb index a21051c0..42c50069 100644 --- a/test/test_binder.rb +++ b/test/test_binder.rb @@ -2,6 +2,7 @@ require_relative "helper" require_relative "helpers/ssl" +require_relative "helpers/tmp_path" require "puma/binder" require "puma/puma_http11" @@ -10,6 +11,7 @@ require "puma/configuration" class TestBinderBase < Minitest::Test include SSLHelper + include TmpPath def setup @events = Puma::Events.strings diff --git a/test/test_cli.rb b/test/test_cli.rb index bdb43f9d..2a474668 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -1,11 +1,13 @@ require_relative "helper" require_relative "helpers/ssl" +require_relative "helpers/tmp_path" require "puma/cli" require "json" class TestCLI < Minitest::Test include SSLHelper + include TmpPath def setup @environment = 'production' diff --git a/test/test_integration_pumactl.rb b/test/test_integration_pumactl.rb index 1fe5fa9d..9f39f314 100644 --- a/test/test_integration_pumactl.rb +++ b/test/test_integration_pumactl.rb @@ -2,6 +2,7 @@ require_relative "helper" require_relative "helpers/integration" class TestIntegrationPumactl < TestIntegration + include TmpPath parallelize_me! def setup diff --git a/test/test_launcher.rb b/test/test_launcher.rb index 1a4de12f..2a69ac25 100644 --- a/test/test_launcher.rb +++ b/test/test_launcher.rb @@ -1,9 +1,12 @@ require_relative "helper" +require_relative "helpers/tmp_path" require "puma/configuration" require 'puma/events' class TestLauncher < Minitest::Test + include TmpPath + def test_dependencies_and_files_to_require_after_prune_is_correctly_built_for_no_extra_deps skip_on :no_bundler diff --git a/test/test_unix_socket.rb b/test/test_unix_socket.rb index 8f35ae5b..74c158c2 100644 --- a/test/test_unix_socket.rb +++ b/test/test_unix_socket.rb @@ -1,17 +1,18 @@ # frozen_string_literal: true require_relative "helper" +require_relative "helpers/tmp_path" class TestPumaUnixSocket < Minitest::Test + include TmpPath App = lambda { |env| [200, {}, ["Works"]] } - PATH = tmp_path('.sock') - def setup return unless UNIX_SKT_EXIST + @tmp_socket_path = tmp_path('.sock') @server = Puma::Server.new App - @server.add_unix_listener PATH + @server.add_unix_listener @tmp_socket_path @server.run end @@ -22,7 +23,7 @@ class TestPumaUnixSocket < Minitest::Test def test_server skip UNIX_SKT_MSG unless UNIX_SKT_EXIST - sock = UNIXSocket.new PATH + sock = UNIXSocket.new @tmp_socket_path sock << "GET / HTTP/1.0\r\nHost: blah.com\r\n\r\n"