fix uninstalled testing and reduce require paths
[unicorn.git] / test / unit / test_util.rb
blob4d17a166edf6c12b6a51965687859a400b00f562
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('')
11     fp = File.open(tmp.path, 'ab')
12     fp.sync = true
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))
21     tmp.close!
22     fp.close
23   end
25   def test_reopen_logs_renamed
26     tmp = Tempfile.new('')
27     tmp_path = tmp.path.freeze
28     fp = File.open(tmp_path, 'ab')
29     fp.sync = true
31     ext = fp.external_encoding rescue nil
32     int = fp.internal_encoding rescue nil
33     before = fp.stat.inspect
34     to = Tempfile.new('')
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))
45     assert fp.sync
46     tmp.close!
47     to.close!
48     fp.close
49   end
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|
56         fp.sync = true
57         assert_equal encoding, fp.external_encoding
58         assert_nil fp.internal_encoding
59         File.unlink(tmp_path)
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))
68         assert fp.sync
69       }
70     }
71     tmp.close!
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|
79         next if ext == int
80         File.open(tmp_path, "a:#{ext.to_s}:#{int.to_s}") { |fp|
81           fp.sync = true
82           assert_equal ext, fp.external_encoding
84           if ext != Encoding::BINARY
85             assert_equal int, fp.internal_encoding
86           end
88           File.unlink(tmp_path)
89           assert ! File.exist?(tmp_path)
90           Unicorn::Util.reopen_logs
91           assert_equal tmp_path, fp.path
92           assert File.exist?(tmp_path)
93           assert_equal fp.stat.inspect, File.stat(tmp_path).inspect
94           assert_equal ext, fp.external_encoding
95           if ext != Encoding::BINARY
96             assert_equal int, fp.internal_encoding
97           end
98           assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL))
99           assert fp.sync
100         }
101       }
102     }
103     tmp.close!
104   end if STDIN.respond_to?(:external_encoding)