diff options
| author | Robert | 2017-11-22 16:14:47 +0100 | 
|---|---|---|
| committer | Robert | 2017-11-22 16:14:47 +0100 | 
| commit | caca3804a508481601d327ac3cd7f3f8b88e2680 (patch) | |
| tree | a7496d3c8d7b7c9d20052b0376b48374cfa1b935 | |
| parent | f01e81604053e9cbdaf4b84534214e37fbfcae5e (diff) | |
| download | chouette-core-0000-specs-easy-zip-setup.tar.bz2 | |
Refs: #5006@2h; Zip Support for specs0000-specs-easy-zip-setup
| -rw-r--r-- | spec/fixtures/zip/.gitkeep | 0 | ||||
| -rw-r--r-- | spec/services/meta_zip_data_spec.rb | 24 | ||||
| -rw-r--r-- | spec/support/zip_support.rb | 32 | ||||
| -rw-r--r-- | spec/support/zip_support/create_zip_data.rb | 50 | 
4 files changed, 106 insertions, 0 deletions
| diff --git a/spec/fixtures/zip/.gitkeep b/spec/fixtures/zip/.gitkeep new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/spec/fixtures/zip/.gitkeep diff --git a/spec/services/meta_zip_data_spec.rb b/spec/services/meta_zip_data_spec.rb new file mode 100644 index 000000000..88e33514d --- /dev/null +++ b/spec/services/meta_zip_data_spec.rb @@ -0,0 +1,24 @@ +# Convenience meta spec to debug potential bugs in zip support helpers +# uncomment run and check files in `zip_fixtures_path` +# RSpec.describe 'ZipData', type: [:zip, :meta] do + +#   let( :zip_file ){ zip_fixtures_path('xxx.zip') } + +#   before do +#     clear_all_zip_fixtures! +#   end + +#   context 'a simple file' do +#     let( :zip_data ){  +#       make_zip "xxx.zip", +#         'hello.txt' => 'hello', +#         'subdir/too.txt' => 'in a subdir' +#     } + + +#     it 'check' do +#       zip_data.write_to(zip_file) +#     end +#   end +   +# end diff --git a/spec/support/zip_support.rb b/spec/support/zip_support.rb new file mode 100644 index 000000000..2fa7755af --- /dev/null +++ b/spec/support/zip_support.rb @@ -0,0 +1,32 @@ +module ZipSupport + +  module Helper extend self +    MAX_LEVEL = 5 +    def remove_dir_tree path, level: 0 +      raise RuntimeError, "too many levels in tree, > #{MAX_LEVEL}" if level > MAX_LEVEL + +      Dir.glob(path) do | file | +        if File.directory? file +          remove_dir_tree(File.join(file, '*'), level: level.succ) +          Dir.unlink(file) +        else +          File.unlink(file) +        end +      end +    end +  end + +  def zip_fixtures_path(file_name) +    fixtures_path(File.join('zip', file_name)) +  end + +  def clear_all_zip_fixtures! relpath = '*' +    raise ArgumentError, 'up dir not allowed (..)' if %r{\.\.} === relpath +    Helper.remove_dir_tree zip_fixtures_path(relpath) +  end +end + +RSpec.configure do |conf| +  conf.include ZipSupport, type: :zip +end + diff --git a/spec/support/zip_support/create_zip_data.rb b/spec/support/zip_support/create_zip_data.rb new file mode 100644 index 000000000..bce3cf52d --- /dev/null +++ b/spec/support/zip_support/create_zip_data.rb @@ -0,0 +1,50 @@ +module ZipSupport +  module CreateZipData + +    class ZipData < Struct.new(:name, :data)  +      def write_to file +        File.write(file, data) +      end +    end + +    class Implementation + +      attr_reader :zip + +      def initialize name +        @zip = ZipData.new(name, '')  +      end + +      def make_from names_to_content_map +        os = Zip::OutputStream.write_buffer do | zio | +          names_to_content_map.each do |name_content_pair| +            p name_content_pair +            zio.put_next_entry(name_content_pair.first) +            zio.write(name_content_pair.last) +          end +        end +        zip.data = os.string +        zip +      end + + +      private + +      def add_entries for_output_stream +        -> *args do +          file_output_stream.put_next_entry(args.first) +          file_output_stream.write args.second +        end +      end +    end + + +    def make_zip(name, names_to_content_map = {}) +      Implementation.new(name).make_from(names_to_content_map) +    end +  end +end + +RSpec.configure do |conf| +  conf.include ZipSupport::CreateZipData, type: :zip +end | 
