fix tests to run correctly under 1.9.2preview1
[unicorn.git] / test / unit / test_util.rb
blob032f0be664da64dd352c1f830557c5ffcc3e03f7
1 require 'test/test_helper'
2 require 'tempfile'
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')
10     tmp.sync = true
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)
18   end
20   def test_reopen_logs_renamed
21     tmp = Tempfile.new(nil)
22     tmp_path = tmp.path.freeze
23     tmp.reopen(tmp_path, 'a')
24     tmp.sync = true
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))
39     assert tmp.sync
40   end
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|
47         fp.sync = true
48         assert_equal encoding, fp.external_encoding
49         assert_nil fp.internal_encoding
50         File.unlink(tmp_path)
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))
59         assert fp.sync
60       }
61     }
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|
69         next if ext == int
70         File.open(tmp_path, "a:#{ext.to_s}:#{int.to_s}") { |fp|
71           fp.sync = true
72           assert_equal ext, fp.external_encoding
73           assert_equal int, fp.internal_encoding
74           File.unlink(tmp_path)
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))
83           assert fp.sync
84         }
85       }
86     }
87   end if STDIN.respond_to?(:external_encoding)
89 end