diff options
author | Eric Wong <normalperson@yhbt.net> | 2009-05-04 02:01:32 -0700 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2009-05-04 02:22:21 -0700 |
commit | 79d4a02e343e9fdf92535e2689f10d4a311cc88d (patch) | |
tree | a4d45692eae2bef032c85b25e1bd4745a9cd0beb /test | |
parent | 17dc99cc5c3a3fc59145724059f36c5c907f6c3f (diff) | |
download | unicorn-79d4a02e343e9fdf92535e2689f10d4a311cc88d.tar.gz |
Ensure we preserve both internal and external encodings when reopening logs.
Diffstat (limited to 'test')
-rw-r--r-- | test/unit/test_util.rb | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/test/unit/test_util.rb b/test/unit/test_util.rb new file mode 100644 index 0000000..1616eac --- /dev/null +++ b/test/unit/test_util.rb @@ -0,0 +1,87 @@ +require 'test/test_helper' +require 'tempfile' + +class TestUtil < Test::Unit::TestCase + + EXPECT_FLAGS = File::WRONLY | File::APPEND + def test_reopen_logs_noop + tmp = Tempfile.new(nil) + tmp.reopen(tmp.path, 'a') + tmp.sync = true + ext = tmp.external_encoding rescue nil + int = tmp.internal_encoding rescue nil + before = tmp.stat.inspect + Unicorn::Util.reopen_logs + assert_equal before, File.stat(tmp.path).inspect + assert_equal ext, (tmp.external_encoding rescue nil) + assert_equal int, (tmp.internal_encoding rescue nil) + end + + def test_reopen_logs_renamed + tmp = Tempfile.new(nil) + tmp_path = tmp.path.freeze + tmp.reopen(tmp_path, 'a') + tmp.sync = true + ext = tmp.external_encoding rescue nil + int = tmp.internal_encoding rescue nil + before = tmp.stat.inspect + to = Tempfile.new(nil) + File.rename(tmp_path, to.path) + assert ! File.exist?(tmp_path) + Unicorn::Util.reopen_logs + assert_equal tmp_path, tmp.path + assert File.exist?(tmp_path) + assert before != File.stat(tmp_path).inspect + assert_equal tmp.stat.inspect, File.stat(tmp_path).inspect + assert_equal ext, (tmp.external_encoding rescue nil) + assert_equal int, (tmp.internal_encoding rescue nil) + assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & tmp.fcntl(Fcntl::F_GETFL)) + assert tmp.sync + end + + def test_reopen_logs_renamed_with_encoding + tmp = Tempfile.new(nil) + tmp_path = tmp.path.dup.freeze + Encoding.list.each { |encoding| + tmp.reopen(tmp_path, "a:#{encoding.to_s}") + tmp.sync = true + assert_equal encoding, tmp.external_encoding + assert_nil tmp.internal_encoding + File.unlink(tmp_path) + assert ! File.exist?(tmp_path) + Unicorn::Util.reopen_logs + assert_equal tmp_path, tmp.path + assert File.exist?(tmp_path) + assert_equal tmp.stat.inspect, File.stat(tmp_path).inspect + assert_equal encoding, tmp.external_encoding + assert_nil tmp.internal_encoding + assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & tmp.fcntl(Fcntl::F_GETFL)) + assert tmp.sync + } + end if STDIN.respond_to?(:external_encoding) + + def test_reopen_logs_renamed_with_internal_encoding + tmp = Tempfile.new(nil) + tmp_path = tmp.path.dup.freeze + Encoding.list.each { |ext| + Encoding.list.each { |int| + next if ext == int + tmp.reopen(tmp_path, "a:#{ext.to_s}:#{int.to_s}") + tmp.sync = true + assert_equal ext, tmp.external_encoding + assert_equal int, tmp.internal_encoding + File.unlink(tmp_path) + assert ! File.exist?(tmp_path) + Unicorn::Util.reopen_logs + assert_equal tmp_path, tmp.path + assert File.exist?(tmp_path) + assert_equal tmp.stat.inspect, File.stat(tmp_path).inspect + assert_equal ext, tmp.external_encoding + assert_equal int, tmp.internal_encoding + assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & tmp.fcntl(Fcntl::F_GETFL)) + assert tmp.sync + } + } + end if STDIN.respond_to?(:external_encoding) + +end |