unicorn 0.93.0
[unicorn.git] / test / unit / test_util.rb
blob4a1e21f6545be15efac72ded50a96770e4f56571
1 # -*- encoding: binary -*-
3 require 'test/test_helper'
4 require 'tempfile'
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')
12     tmp.sync = true
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! }
21   end
23   def test_reopen_logs_renamed
24     tmp = Tempfile.new(nil)
25     tmp_path = tmp.path.freeze
26     tmp.reopen(tmp_path, 'a')
27     tmp.sync = true
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))
42     assert tmp.sync
43     assert_nothing_raised { tmp.close! }
44     assert_nothing_raised { to.close! }
45   end
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|
52         fp.sync = true
53         assert_equal encoding, fp.external_encoding
54         assert_nil fp.internal_encoding
55         File.unlink(tmp_path)
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))
64         assert fp.sync
65       }
66     }
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|
75         next if ext == int
76         File.open(tmp_path, "a:#{ext.to_s}:#{int.to_s}") { |fp|
77           fp.sync = true
78           assert_equal ext, fp.external_encoding
79           assert_equal int, fp.internal_encoding
80           File.unlink(tmp_path)
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))
89           assert fp.sync
90         }
91       }
92     }
93     assert_nothing_raised { tmp.close! }
94   end if STDIN.respond_to?(:external_encoding)
96 end