From c2ac23536dd73ae631f65a45145be170a1186e70 Mon Sep 17 00:00:00 2001 From: Ryan Winograd Date: Fri, 28 Aug 2020 21:14:44 -0500 Subject: Assert how contents are zipped Previously we tested that either rubyzip or shelling out to zip produced the expected xlsx file, but we never explicitly checked whether rubyzip or shell zip was used. I noticed that rubyzip always sets a far future date, whereas `zip` uses today's date. I'm using this as a heuristic to determine which zip method was used. --- test/tc_package.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/tc_package.rb b/test/tc_package.rb index 628d20ae..fc48a9eb 100644 --- a/test/tc_package.rb +++ b/test/tc_package.rb @@ -129,12 +129,14 @@ class TestPackage < Test::Unit::TestCase def test_serialization @package.serialize(@fname) assert_zip_file_matches_package(@fname, @package) + assert_created_with_rubyzip(@fname, @package) File.delete(@fname) end def test_serialization_with_zip_command @package.serialize(@fname, zip_command: "zip") assert_zip_file_matches_package(@fname, @package) + assert_created_with_zip_command(@fname, @package) File.delete(@fname) end @@ -142,6 +144,7 @@ class TestPackage < Test::Unit::TestCase fname = "#{Dir.tmpdir}/#{@fname}" @package.serialize(fname, zip_command: "zip") assert_zip_file_matches_package(fname, @package) + assert_created_with_zip_command(fname, @package) File.delete(fname) end @@ -156,6 +159,21 @@ class TestPackage < Test::Unit::TestCase package.send(:parts).each{ |part| zf.get_entry(part[:entry]) } end + def assert_created_with_rubyzip(fname, package) + assert_equal 2098, get_mtime(fname, package).year, "XLSX files created with RubyZip have 2098 as the file mtime" + end + + def assert_created_with_zip_command(fname, package) + assert_equal Time.now.utc.year, get_mtime(fname, package).year, "XLSX files created with a zip command have the current year as the file mtime" + end + + def get_mtime(fname, package) + zf = Zip::File.open(fname) + part = package.send(:parts).first + entry = zf.get_entry(part[:entry]) + entry.mtime.utc + end + def test_serialization_with_deprecated_argument warnings = capture_warnings do @package.serialize(@fname, false) -- cgit v1.2.3