about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-11-03 23:34:45 -0800
committerEric Wong <normalperson@yhbt.net>2009-11-03 23:37:42 -0800
commit334e37421ef4fcd9d70538c926c52b871897d419 (patch)
treec1f8f1512c84d1eb1636d712798ba9300e56d4b9
parentd69f794d53a352d0cfdd500ca58589cc8babf167 (diff)
downloadunicorn-334e37421ef4fcd9d70538c926c52b871897d419.tar.gz
It makes more sense this way since users usually expect config
file directives to be order-independent.
-rw-r--r--lib/unicorn/configurator.rb17
-rw-r--r--test/exec/test_exec.rb45
2 files changed, 56 insertions, 6 deletions
diff --git a/lib/unicorn/configurator.rb b/lib/unicorn/configurator.rb
index 93b7bf4..6559e7c 100644
--- a/lib/unicorn/configurator.rb
+++ b/lib/unicorn/configurator.rb
@@ -94,6 +94,15 @@ module Unicorn
 
     def reload #:nodoc:
       instance_eval(File.read(config_file), config_file) if config_file
+
+      # working_directory binds immediately (easier error checking that way),
+      # now ensure any paths we changed are correctly set.
+      [ :pid, :stderr_path, :stdout_path ].each do |var|
+        String === (path = set[var]) or next
+        path = File.expand_path(path)
+        test(?w, path) || test(?w, File.dirname(path)) or \
+              raise ArgumentError, "directory for #{var}=#{path} not writable"
+      end
     end
 
     def commit!(server, options = {}) #:nodoc:
@@ -385,15 +394,11 @@ module Unicorn
 
     def set_path(var, path) #:nodoc:
       case path
-      when NilClass
-      when String
-        path = File.expand_path(path)
-        File.writable?(File.dirname(path)) or \
-               raise ArgumentError, "directory for #{var}=#{path} not writable"
+      when NilClass, String
+        set[var] = path
       else
         raise ArgumentError
       end
-      set[var] = path
     end
 
     def set_hook(var, my_proc, req_arity = 2) #:nodoc:
diff --git a/test/exec/test_exec.rb b/test/exec/test_exec.rb
index 7edfa71..4625b45 100644
--- a/test/exec/test_exec.rb
+++ b/test/exec/test_exec.rb
@@ -114,6 +114,51 @@ EOF
       FileUtils.rmtree(other.path)
   end
 
+  def test_working_directory_controls_relative_paths
+    other = Tempfile.new('unicorn.wd')
+    File.unlink(other.path)
+    Dir.mkdir(other.path)
+    File.open("config.ru", "wb") do |fp|
+      fp.syswrite <<EOF
+use Rack::ContentLength
+run proc { |env| [ 200, { 'Content-Type' => 'text/plain' }, [ Dir.pwd ] ] }
+EOF
+    end
+    FileUtils.cp("config.ru", other.path + "/config.ru")
+    system('mkfifo', "#{other.path}/fifo")
+    tmp = Tempfile.new('unicorn.config')
+    tmp.syswrite <<EOF
+pid "pid_file_here"
+stderr_path "stderr_log_here"
+stdout_path "stdout_log_here"
+working_directory '#{other.path}'
+listen '#@addr:#@port'
+after_fork do |server, worker|
+  File.open("fifo", "wb").close
+end
+EOF
+    pid = xfork { redirect_test_io { exec($unicorn_bin, "-c#{tmp.path}") } }
+    File.open("#{other.path}/fifo", "rb").close
+
+    assert ! File.exist?("stderr_log_here")
+    assert ! File.exist?("stdout_log_here")
+    assert ! File.exist?("pid_file_here")
+
+    assert ! File.exist?("#@tmpdir/stderr_log_here")
+    assert ! File.exist?("#@tmpdir/stdout_log_here")
+    assert ! File.exist?("#@tmpdir/pid_file_here")
+
+    assert File.exist?("#{other.path}/pid_file_here")
+    assert_equal "#{pid}\n", File.read("#{other.path}/pid_file_here")
+    assert File.exist?("#{other.path}/stderr_log_here")
+    assert File.exist?("#{other.path}/stdout_log_here")
+
+    Process.kill(:QUIT, pid)
+    ensure
+      FileUtils.rmtree(other.path)
+  end
+
+
   def test_exit_signals
     %w(INT TERM QUIT).each do |sig|
       File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }