1 # -*- encoding: binary -*-
3 require 'test/test_helper'
6 class TestUtil < Test::Unit::TestCase
8 EXPECT_FLAGS = File::WRONLY | File::APPEND
9 def test_reopen_logs_noop
10 tmp = Tempfile.new(nil)
11 tmp.reopen(tmp.path, 'a')
13 ext = tmp.external_encoding rescue nil
14 int = tmp.internal_encoding rescue nil
15 before = tmp.stat.inspect
16 Unicorn::Util.reopen_logs
17 assert_equal before, File.stat(tmp.path).inspect
18 assert_equal ext, (tmp.external_encoding rescue nil)
19 assert_equal int, (tmp.internal_encoding rescue nil)
20 assert_nothing_raised { tmp.close! }
23 def test_reopen_logs_renamed
24 tmp = Tempfile.new(nil)
25 tmp_path = tmp.path.freeze
26 tmp.reopen(tmp_path, 'a')
28 ext = tmp.external_encoding rescue nil
29 int = tmp.internal_encoding rescue nil
30 before = tmp.stat.inspect
31 to = Tempfile.new(nil)
32 File.rename(tmp_path, to.path)
33 assert ! File.exist?(tmp_path)
34 Unicorn::Util.reopen_logs
35 assert_equal tmp_path, tmp.path
36 assert File.exist?(tmp_path)
37 assert before != File.stat(tmp_path).inspect
38 assert_equal tmp.stat.inspect, File.stat(tmp_path).inspect
39 assert_equal ext, (tmp.external_encoding rescue nil)
40 assert_equal int, (tmp.internal_encoding rescue nil)
41 assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & tmp.fcntl(Fcntl::F_GETFL))
43 assert_nothing_raised { tmp.close! }
44 assert_nothing_raised { to.close! }
47 def test_reopen_logs_renamed_with_encoding
48 tmp = Tempfile.new(nil)
49 tmp_path = tmp.path.dup.freeze
50 Encoding.list.each { |encoding|
51 File.open(tmp_path, "a:#{encoding.to_s}") { |fp|
53 assert_equal encoding, fp.external_encoding
54 assert_nil fp.internal_encoding
56 assert ! File.exist?(tmp_path)
57 Unicorn::Util.reopen_logs
58 assert_equal tmp_path, fp.path
59 assert File.exist?(tmp_path)
60 assert_equal fp.stat.inspect, File.stat(tmp_path).inspect
61 assert_equal encoding, fp.external_encoding
62 assert_nil fp.internal_encoding
63 assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL))
67 assert_nothing_raised { tmp.close! }
68 end if STDIN.respond_to?(:external_encoding)
70 def test_reopen_logs_renamed_with_internal_encoding
71 tmp = Tempfile.new(nil)
72 tmp_path = tmp.path.dup.freeze
73 Encoding.list.each { |ext|
74 Encoding.list.each { |int|
76 File.open(tmp_path, "a:#{ext.to_s}:#{int.to_s}") { |fp|
78 assert_equal ext, fp.external_encoding
79 assert_equal int, fp.internal_encoding
81 assert ! File.exist?(tmp_path)
82 Unicorn::Util.reopen_logs
83 assert_equal tmp_path, fp.path
84 assert File.exist?(tmp_path)
85 assert_equal fp.stat.inspect, File.stat(tmp_path).inspect
86 assert_equal ext, fp.external_encoding
87 assert_equal int, fp.internal_encoding
88 assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL))
93 assert_nothing_raised { tmp.close! }
94 end if STDIN.respond_to?(:external_encoding)