Skip to content

Commit

Permalink
Add a test for restoring file permissions on extract.
Browse files Browse the repository at this point in the history
  • Loading branch information
hainesr committed Oct 7, 2019
1 parent dbbb6cf commit d8b6f6e
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/file_options_test.rb
Expand Up @@ -3,15 +3,46 @@
class FileOptionsTest < MiniTest::Test
ZIPPATH = ::File.join(Dir.tmpdir, 'options.zip').freeze
TXTPATH = ::File.expand_path(::File.join('data', 'file1.txt'), __dir__).freeze
TXTPATH_600 = ::File.join(Dir.tmpdir, 'file1.600.txt').freeze
TXTPATH_755 = ::File.join(Dir.tmpdir, 'file1.755.txt').freeze
EXTPATH_1 = ::File.join(Dir.tmpdir, 'extracted_1.txt').freeze
EXTPATH_2 = ::File.join(Dir.tmpdir, 'extracted_2.txt').freeze
EXTPATH_3 = ::File.join(Dir.tmpdir, 'extracted_3.txt').freeze
ENTRY_1 = 'entry_1.txt'.freeze
ENTRY_2 = 'entry_2.txt'.freeze
ENTRY_3 = 'entry_3.txt'.freeze

def teardown
::File.unlink(ZIPPATH) if ::File.exist?(ZIPPATH)
::File.unlink(EXTPATH_1) if ::File.exist?(EXTPATH_1)
::File.unlink(EXTPATH_2) if ::File.exist?(EXTPATH_2)
::File.unlink(EXTPATH_3) if ::File.exist?(EXTPATH_3)
::File.unlink(TXTPATH_600) if ::File.exist?(TXTPATH_600)
::File.unlink(TXTPATH_755) if ::File.exist?(TXTPATH_755)
end

def test_restore_permissions
# Copy and set up files with different permissions.
::FileUtils.cp(TXTPATH, TXTPATH_600)
::File.chmod(0600, TXTPATH_600)
::FileUtils.cp(TXTPATH, TXTPATH_755)
::File.chmod(0755, TXTPATH_755)

::Zip::File.open(ZIPPATH, true) do |zip|
zip.add(ENTRY_1, TXTPATH)
zip.add(ENTRY_2, TXTPATH_600)
zip.add(ENTRY_3, TXTPATH_755)
end

zip = ::Zip::File.new(ZIPPATH, false, false, restore_permissions: true)
zip.extract(ENTRY_1, EXTPATH_1)
zip.extract(ENTRY_2, EXTPATH_2)
zip.extract(ENTRY_3, EXTPATH_3)
zip.close

assert_equal(::File.stat(TXTPATH).mode, ::File.stat(EXTPATH_1).mode)
assert_equal(::File.stat(TXTPATH_600).mode, ::File.stat(EXTPATH_2).mode)
assert_equal(::File.stat(TXTPATH_755).mode, ::File.stat(EXTPATH_3).mode)
end

def test_restore_times_true
Expand Down

0 comments on commit d8b6f6e

Please sign in to comment.