blob: 98cb9026d2d7abf739b96ff3e20ee0a60c6c17cd (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 | RSpec.describe ZipService do
  let( :zip_service ){ described_class }
  let( :unzipper ){ zip_service.new(zip_data) }
  let( :zip_data ){ File.read zip_file }
  context 'correct test data' do
    before do
      subdir_names.each do | subdir_name |
        File.unlink( subdir_file subdir_name, suffix: '.zip' ) rescue nil
        Dir.unlink( subdir_file subdir_name ) rescue nil
      end
    end
    let( :subdir_names ){ %w<OFFRE_TRANSDEV_20170301122517 OFFRE_TRANSDEV_20170301122519>  }
    let( :expected_chksums ){
      checksum_trees( subdir_names.map{ |sn| subdir_file(sn, prefix: 'source_') } )
    }
    let( :zip_file ){ fixtures_path 'OFFRE_TRANSDEV_2017030112251.zip' }
    #
    # Remove potential test artefacts
    it 'yields the correct content' do
      # Write ZipService Streams to files and inflate them to file system
      unzipper.subdirs.each do | subdir |
        expect( subdir.spurious ).to be_empty
        File.open(subdir_file( subdir.name, suffix: '.zip' ), 'wb'){ |f| f.write subdir.stream.string }
        unzip_subdir subdir
      end
      # Represent the inflated file_system as a checksum tree
      actual_checksums = 
        checksum_trees( subdir_names.map{ |sn| subdir_file(sn, prefix: 'target/') } )
      expect( actual_checksums ).to eq( expected_chksums )
    end
  end
  context 'test data with spurious directories' do 
    let( :zip_file ){ fixtures_path 'OFFRE_WITH_EXTRA.zip' }
    it 'returns the extra dir in the spurious field of the entry' do
      expect( unzipper.subdirs.first.spurious ).to eq(%w{EXTRA})
    end
  end
  def checksum_trees *dirs
    dirs.flatten.inject({},&method(:checksum_tree))
  end
  def checksum_tree repr, dir
    Dir.glob("#{dir}/**/*").each do |file|
      if !File.directory?(file)
        repr.merge!( File.basename(file) => %x{cksum #{file}}.split.first ){ |_, ov, nv| Array(ov) << nv }
      end
    end
    repr
  end
  def subdir_file( subdir, prefix: 'target_', suffix: '' )
    fixtures_path("#{prefix}#{subdir}#{suffix}")
  end
  def unzip_subdir subdir
    %x{unzip -oqq #{subdir_file subdir.name, suffix: '.zip'} -d #{fixture_path}/target}
  end
end
 |