1 require 'test/test_helper'
4 class TestUtil < Test::Unit::TestCase
6 EXPECT_FLAGS = File::WRONLY | File::APPEND
7 def test_reopen_logs_noop
8 tmp = Tempfile.new(nil)
9 tmp.reopen(tmp.path, 'a')
11 ext = tmp.external_encoding rescue nil
12 int = tmp.internal_encoding rescue nil
13 before = tmp.stat.inspect
14 Unicorn::Util.reopen_logs
15 assert_equal before, File.stat(tmp.path).inspect
16 assert_equal ext, (tmp.external_encoding rescue nil)
17 assert_equal int, (tmp.internal_encoding rescue nil)
20 def test_reopen_logs_renamed
21 tmp = Tempfile.new(nil)
22 tmp_path = tmp.path.freeze
23 tmp.reopen(tmp_path, 'a')
25 ext = tmp.external_encoding rescue nil
26 int = tmp.internal_encoding rescue nil
27 before = tmp.stat.inspect
28 to = Tempfile.new(nil)
29 File.rename(tmp_path, to.path)
30 assert ! File.exist?(tmp_path)
31 Unicorn::Util.reopen_logs
32 assert_equal tmp_path, tmp.path
33 assert File.exist?(tmp_path)
34 assert before != File.stat(tmp_path).inspect
35 assert_equal tmp.stat.inspect, File.stat(tmp_path).inspect
36 assert_equal ext, (tmp.external_encoding rescue nil)
37 assert_equal int, (tmp.internal_encoding rescue nil)
38 assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & tmp.fcntl(Fcntl::F_GETFL))
42 def test_reopen_logs_renamed_with_encoding
43 tmp = Tempfile.new(nil)
44 tmp_path = tmp.path.dup.freeze
45 Encoding.list.each { |encoding|
46 File.open(tmp_path, "a:#{encoding.to_s}") { |fp|
48 assert_equal encoding, fp.external_encoding
49 assert_nil fp.internal_encoding
51 assert ! File.exist?(tmp_path)
52 Unicorn::Util.reopen_logs
53 assert_equal tmp_path, fp.path
54 assert File.exist?(tmp_path)
55 assert_equal fp.stat.inspect, File.stat(tmp_path).inspect
56 assert_equal encoding, fp.external_encoding
57 assert_nil fp.internal_encoding
58 assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL))
62 end if STDIN.respond_to?(:external_encoding)
64 def test_reopen_logs_renamed_with_internal_encoding
65 tmp = Tempfile.new(nil)
66 tmp_path = tmp.path.dup.freeze
67 Encoding.list.each { |ext|
68 Encoding.list.each { |int|
70 File.open(tmp_path, "a:#{ext.to_s}:#{int.to_s}") { |fp|
72 assert_equal ext, fp.external_encoding
73 assert_equal int, fp.internal_encoding
75 assert ! File.exist?(tmp_path)
76 Unicorn::Util.reopen_logs
77 assert_equal tmp_path, fp.path
78 assert File.exist?(tmp_path)
79 assert_equal fp.stat.inspect, File.stat(tmp_path).inspect
80 assert_equal ext, fp.external_encoding
81 assert_equal int, fp.internal_encoding
82 assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL))
87 end if STDIN.respond_to?(:external_encoding)