fake_zip 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +5 -0
- data/fake_zip.gemspec +27 -0
- data/lib/fake_zip/version.rb +3 -0
- data/lib/fake_zip.rb +105 -0
- data/spec/examples_spec.rb +30 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/support/meta_module.rb +69 -0
- data/spec/support/my_temp.rb +49 -0
- data/spec/support/require.rb +1 -0
- data/spec/support/zip_entries.rb.rb +11 -0
- metadata +137 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Alexander K
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# FakeZip
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'fake_zip'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install fake_zip
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/fake_zip.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fake_zip/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fake_zip"
|
8
|
+
spec.version = FakeZip::VERSION
|
9
|
+
spec.authors = ["Alexander K"]
|
10
|
+
spec.email = ["xpyro@ya.ru"]
|
11
|
+
spec.description = %q{ build zip files with given file structure for testing }.strip
|
12
|
+
spec.summary = %q{ build zip files with given file structure for testing }.strip
|
13
|
+
spec.homepage = "https://github.com/sowcow/fake_zip"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
# deps:
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_runtime_dependency "rubyzip"
|
27
|
+
end
|
data/lib/fake_zip.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
require "fake_zip/version"
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
require 'forwardable'
|
5
|
+
require 'zip/zipfilesystem'
|
6
|
+
|
7
|
+
|
8
|
+
module FakeZip
|
9
|
+
|
10
|
+
####################
|
11
|
+
|
12
|
+
module Helpers
|
13
|
+
extend self
|
14
|
+
|
15
|
+
def traverse array_or_hash, path=[], &block
|
16
|
+
given = array_or_hash
|
17
|
+
case given
|
18
|
+
when Hash
|
19
|
+
given.each_pair do |k,v|
|
20
|
+
block.call path+[k], :dir # entering directory
|
21
|
+
traverse v,path+[k],&block
|
22
|
+
end
|
23
|
+
when Array then given.each { |x| traverse x,path,&block }
|
24
|
+
else
|
25
|
+
block.call path+[given], :file
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def collect_all(given)
|
30
|
+
found = []
|
31
|
+
traverse given do |x,y| found << [x,y] end
|
32
|
+
found
|
33
|
+
end
|
34
|
+
def collect_only(given, kind)
|
35
|
+
collect_all(given).map { |(name,type)| name if type == kind }.compact
|
36
|
+
end
|
37
|
+
def files(given)
|
38
|
+
collect_only(given, :file).map { |x| x.join ?/ }
|
39
|
+
end
|
40
|
+
def dirs(given)
|
41
|
+
collect_only(given, :dir).map { |x| x.join ?/ }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
####################
|
46
|
+
|
47
|
+
class FakeZip < Struct.new :file_structure
|
48
|
+
def save file
|
49
|
+
Zip::ZipFile.open file, Zip::ZipFile::CREATE do |z|
|
50
|
+
dirs(struct).each do |dir|
|
51
|
+
z.dir.mkdir(dir) unless z.file.exist? dir
|
52
|
+
end
|
53
|
+
files(struct).each do |file|
|
54
|
+
z.file.open(file, "w") { |f| f.write file }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def struct given=file_structure
|
60
|
+
given.is_a?(String) ? YAML.load(given) : given
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
extend Forwardable
|
66
|
+
def_delegators Helpers, :files, :dirs
|
67
|
+
end
|
68
|
+
|
69
|
+
def new file, structure
|
70
|
+
FakeZip.new(structure).save file
|
71
|
+
end
|
72
|
+
|
73
|
+
module_function :new
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
__END__
|
78
|
+
# Other tests...
|
79
|
+
|
80
|
+
found = []
|
81
|
+
FakeZip::Helpers.traverse( {a: [{a: 1, b: 2}, 3]}, &proc{ |x| found << x } )
|
82
|
+
found.map { |x| x.join ?/ } == ["a", "a/a", "a/a/1", "a/b", "a/b/2", "a/3"] or raise
|
83
|
+
|
84
|
+
FakeZip::Helpers.files({a: [{a: 1, b: 2}, 3]}) == %w[ a/a/1 a/b/2 a/3 ] or raise
|
85
|
+
|
86
|
+
|
87
|
+
require 'yaml'
|
88
|
+
|
89
|
+
raise unless FakeZip('empty_dir: []').contents == {'empty_dir' => []}
|
90
|
+
raise unless FakeZip('dir: [file1, file2]').contents == {'dir' => ['file1','file2']}
|
91
|
+
|
92
|
+
tree = 'root_dir: [file1, file2, nested_dir: []]'
|
93
|
+
raise unless FakeZip(tree).contents == {'root_dir' => ['file1','file2',{'nested_dir' => [] }]}
|
94
|
+
|
95
|
+
file_name = 'temp-test-fake.zip'
|
96
|
+
tree = 'root_dir: [file1, file2, nested_dir: [nested_file.any], empty_dir: []]'
|
97
|
+
FakeZip(tree).save file_name
|
98
|
+
|
99
|
+
Zip::ZipFile.open file_name do |z|
|
100
|
+
z.file.exist? 'root_dir/file1' or raise
|
101
|
+
z.file.exist? 'root_dir/file2' or raise
|
102
|
+
z.file.exist? 'root_dir/nested_dir/nested_file.any' or raise
|
103
|
+
z.file.exist? 'root_dir/empty_dir' or raise # no empty dirs
|
104
|
+
end
|
105
|
+
File.delete file_name
|
@@ -0,0 +1,30 @@
|
|
1
|
+
describe FakeZip do
|
2
|
+
extend MyTemp.new(:temp, 'tmp/testing.zip')
|
3
|
+
|
4
|
+
describe '.new' do
|
5
|
+
it 'takes file name and yaml string' do
|
6
|
+
expect { FakeZip.new temp, '{}' }.to change { File.exist? temp }.from(false).to(true)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'takes file name and hash' do
|
10
|
+
expect { FakeZip.new temp, {} }.to change { File.exist? temp }.from(false).to(true)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'examples (given structure --- files in archive)' do
|
15
|
+
examples = <<-END.lines.map(&:strip).map { |x| x.split(' --- ',2).map { |x| eval x } }
|
16
|
+
"[]" --- []
|
17
|
+
{} --- []
|
18
|
+
{root: []} --- ['root/']
|
19
|
+
"root: []" --- ['root/']
|
20
|
+
'root_dir: [file1, file2, nested_dir: [nested_file.any], empty_dir: []]' --- ["root_dir/", "root_dir/nested_dir/", "root_dir/empty_dir/", "root_dir/file1", "root_dir/file2", "root_dir/nested_dir/nested_file.any"]
|
21
|
+
END
|
22
|
+
|
23
|
+
examples.each do |input,output|
|
24
|
+
specify "#{input.inspect} --- #{output.inspect}" do
|
25
|
+
FakeZip.new(temp, input)
|
26
|
+
zip_entries(temp).should == output
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
Dir["./spec/support/**/*.rb"].each {|f| require f} # no comment
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
config.order = 'random'
|
15
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# this file gona be gem!
|
2
|
+
#
|
3
|
+
# TODO:
|
4
|
+
# use Modules.call as a default method, instead of using used do
|
5
|
+
|
6
|
+
|
7
|
+
class MetaModule < Module
|
8
|
+
# use .new so I don't bother user to use super in #initialize
|
9
|
+
def self.new(*)
|
10
|
+
super.tap { |x| x.send :include, self::Methods }
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.used &block
|
14
|
+
define_method :included, &block
|
15
|
+
define_method :extended, &block
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
class MetaModule2 #< Class
|
22
|
+
def self.new *params
|
23
|
+
_params = params.join ?,
|
24
|
+
a_params = params.map{|x|"@#{x}"}.join ?,
|
25
|
+
|
26
|
+
Class.new(MetaModule) do
|
27
|
+
eval "def initialize(#{_params}); #{a_params} = #{_params} end"
|
28
|
+
private; attr_reader *params
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
class My2 < MetaModule2.new :x, :y
|
35
|
+
used do |target|
|
36
|
+
target.check x
|
37
|
+
target.check y
|
38
|
+
target.check [x,y]
|
39
|
+
end
|
40
|
+
|
41
|
+
module Methods
|
42
|
+
def check given
|
43
|
+
p given
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# module Context
|
49
|
+
# extend My2.new 1, 2
|
50
|
+
# end
|
51
|
+
|
52
|
+
# class My < MetaModule
|
53
|
+
# def initialize name
|
54
|
+
# @name = name
|
55
|
+
# end
|
56
|
+
|
57
|
+
# used do |at|
|
58
|
+
# at.abc @name
|
59
|
+
# end
|
60
|
+
|
61
|
+
# module Methods
|
62
|
+
# def abc given; p given end
|
63
|
+
# end
|
64
|
+
# end
|
65
|
+
|
66
|
+
# module A
|
67
|
+
# extend My.new 123
|
68
|
+
# end
|
69
|
+
# include My.new 234
|
@@ -0,0 +1,49 @@
|
|
1
|
+
class MyTemp < MetaModule2.new :getter, :file
|
2
|
+
used do |at|
|
3
|
+
at.def_temp_file getter, file
|
4
|
+
end
|
5
|
+
|
6
|
+
module Methods
|
7
|
+
def def_temp_file getter, file
|
8
|
+
let(getter) { file }
|
9
|
+
after(:each) { File.delete file if File.exist? file }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
__END__
|
32
|
+
# def initialize getter, file
|
33
|
+
# @getter, @file = getter, file
|
34
|
+
# end
|
35
|
+
|
36
|
+
|
37
|
+
# def extended target
|
38
|
+
# target.def_temp_file @getter, @file
|
39
|
+
# end
|
40
|
+
|
41
|
+
# def temp_file getter, file
|
42
|
+
# let(getter) { file }
|
43
|
+
# after(:each) { File.delete file if File.exist? file }
|
44
|
+
# end
|
45
|
+
|
46
|
+
# def self.extended target
|
47
|
+
# target.instance_eval do
|
48
|
+
# end
|
49
|
+
# end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'fake_zip'
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fake_zip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alexander K
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rubyzip
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: build zip files with given file structure for testing
|
79
|
+
email:
|
80
|
+
- xpyro@ya.ru
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- fake_zip.gemspec
|
92
|
+
- lib/fake_zip.rb
|
93
|
+
- lib/fake_zip/version.rb
|
94
|
+
- spec/examples_spec.rb
|
95
|
+
- spec/spec_helper.rb
|
96
|
+
- spec/support/meta_module.rb
|
97
|
+
- spec/support/my_temp.rb
|
98
|
+
- spec/support/require.rb
|
99
|
+
- spec/support/zip_entries.rb.rb
|
100
|
+
homepage: https://github.com/sowcow/fake_zip
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
segments:
|
114
|
+
- 0
|
115
|
+
hash: -767813113576811598
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
118
|
+
requirements:
|
119
|
+
- - '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
segments:
|
123
|
+
- 0
|
124
|
+
hash: -767813113576811598
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 1.8.25
|
128
|
+
signing_key:
|
129
|
+
specification_version: 3
|
130
|
+
summary: build zip files with given file structure for testing
|
131
|
+
test_files:
|
132
|
+
- spec/examples_spec.rb
|
133
|
+
- spec/spec_helper.rb
|
134
|
+
- spec/support/meta_module.rb
|
135
|
+
- spec/support/my_temp.rb
|
136
|
+
- spec/support/require.rb
|
137
|
+
- spec/support/zip_entries.rb.rb
|