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('')
11 fp = File.open(tmp.path, 'ab')
13 ext = fp.external_encoding rescue nil
14 int = fp.internal_encoding rescue nil
15 before = fp.stat.inspect
16 Unicorn::Util.reopen_logs
17 assert_equal before, File.stat(fp.path).inspect
18 assert_equal ext, (fp.external_encoding rescue nil)
19 assert_equal int, (fp.internal_encoding rescue nil)
20 assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL))
25 def test_reopen_logs_renamed
26 tmp = Tempfile.new('')
27 tmp_path = tmp.path.freeze
28 fp = File.open(tmp_path, 'ab')
31 ext = fp.external_encoding rescue nil
32 int = fp.internal_encoding rescue nil
33 before = fp.stat.inspect
35 File.rename(tmp_path, to.path)
36 assert ! File.exist?(tmp_path)
37 Unicorn::Util.reopen_logs
38 assert_equal tmp_path, tmp.path
39 assert File.exist?(tmp_path)
40 assert before != File.stat(tmp_path).inspect
41 assert_equal fp.stat.inspect, File.stat(tmp_path).inspect
42 assert_equal ext, (fp.external_encoding rescue nil)
43 assert_equal int, (fp.internal_encoding rescue nil)
44 assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL))
51 def test_reopen_logs_renamed_with_encoding
52 tmp = Tempfile.new('')
53 tmp_path = tmp.path.dup.freeze
54 Encoding.list.each { |encoding|
55 File.open(tmp_path, "a:#{encoding.to_s}") { |fp|
57 assert_equal encoding, fp.external_encoding
58 assert_nil fp.internal_encoding
60 assert ! File.exist?(tmp_path)
61 Unicorn::Util.reopen_logs
62 assert_equal tmp_path, fp.path
63 assert File.exist?(tmp_path)
64 assert_equal fp.stat.inspect, File.stat(tmp_path).inspect
65 assert_equal encoding, fp.external_encoding
66 assert_nil fp.internal_encoding
67 assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL))
72 end if STDIN.respond_to?(:external_encoding)
74 def test_reopen_logs_renamed_with_internal_encoding
75 tmp = Tempfile.new('')
76 tmp_path = tmp.path.dup.freeze
77 Encoding.list.each { |ext|
78 Encoding.list.each { |int|
80 File.open(tmp_path, "a:#{ext.to_s}:#{int.to_s}") { |fp|
82 assert_equal ext, fp.external_encoding
83 assert_equal int, fp.internal_encoding
85 assert ! File.exist?(tmp_path)
86 Unicorn::Util.reopen_logs
87 assert_equal tmp_path, fp.path
88 assert File.exist?(tmp_path)
89 assert_equal fp.stat.inspect, File.stat(tmp_path).inspect
90 assert_equal ext, fp.external_encoding
91 assert_equal int, fp.internal_encoding
92 assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL))
98 end if STDIN.respond_to?(:external_encoding)