file_tree 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3ff24fa1a91f9106b1650b273cded718c4f6ddc1
4
+ data.tar.gz: 088f764abfd51f0ec0969aaeae1a9e2eb788a7a7
5
+ SHA512:
6
+ metadata.gz: a9729cce5ef8687002ccbdeb4588e8135c749c2e135d4f6777abc4ca1b4c2af6ea4a98494ab02f187ce2efdf6d52445b225347a223b0cebd81554d1825296cf9
7
+ data.tar.gz: 07a14450b4a35f9ef8ec25b5a6087c447aebe7b0c0dfcdbf8320b9fe643ab95eb4e432d596fe8424369ba73cc46609ddea58672cbb4b06d1867e935086110e75
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .idea
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ *.bundle
20
+ *.so
21
+ *.o
22
+ *.a
23
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.1.1"
4
+ - "1.9.3"
5
+ - jruby-19mode
6
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in file_tree.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 aliakb
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,85 @@
1
+ # FileTree
2
+ FileTree is a ruby library for creating file/directory trees from your code, mostly for testing, where you need to generate file/directory input for your components. The tree gets created somewhere under system's temporary directory (Dir::mktmpdir is used to create a top-level directory).
3
+
4
+ ## Requirements
5
+ * Ruby 1.9 or greater
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'file_tree'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install file_tree
20
+
21
+ ## Usage
22
+ Start by adding _file_tree_ module to your project:
23
+ ```ruby
24
+ require "rubygems"
25
+ require "file_tree"
26
+ ```
27
+ Now add file tree DSL to your class/module. Depending on the context you may use _include_ or _extend_:
28
+ ```ruby
29
+ include FileTree
30
+ ```
31
+
32
+ Now you're ready to create directories and populate them with some files.
33
+ ### Directory with files and subdirectories
34
+ ```ruby
35
+ path = root_dir do
36
+ dir "dir1-1" do
37
+ file "file1"
38
+ file "file2"
39
+ end
40
+ dir "dir1-2"
41
+ dir "dir1-3" do
42
+ file "file3"
43
+ file "file4"
44
+ end
45
+ file "file5"
46
+ file "file6"
47
+ end
48
+ ```
49
+ Now path variable stores path to the directory that you have just created. Files will be of random size and content; zero-length files are possible, as well as binary and text files.
50
+ ### Content type
51
+ You can explicitly specify whether your files should have binary or text content:
52
+ ```ruby
53
+ root_dir do
54
+ file "data.bin", :binary
55
+ file "data.txt", :text
56
+ file "empty", :empty
57
+ file "random-1", :random
58
+ file "random-2"
59
+ end
60
+ ```
61
+ ("random-2" file will have random content because of the default content type specified in FileTree::Defaults.content_type variable)
62
+ ### Predefined content
63
+ There may be times where you want your files to store some predefined content. This is doable.
64
+ ```ruby
65
+ root_dir do
66
+ file "test1.txt", "This is a test!!!"
67
+ file "test2.txt", "This is only a test!"
68
+ end
69
+ ```
70
+ ### Configuration
71
+ The library has a bunch of parameters for you to play with. Messing with them is not necessary - the default values work just fine. All settings are gathered inside FileTree::Defaults module.
72
+
73
+ Setting | Default | Meaning
74
+ -------------------- | -------------------- | -------------------------------------------------
75
+ content_type | :random | Default content type for files
76
+ min_file_size | 1024 | minimal size for non-empty files
77
+ max_file_size | 3072 | maximal size for non-empty files
78
+
79
+ ## Contributing
80
+
81
+ 1. Fork it ( https://github.com/[my-github-username]/file_tree/fork )
82
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
83
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
84
+ 4. Push to the branch (`git push origin my-new-feature`)
85
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task :default => :spec
data/file_tree.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'file_tree/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "file_tree"
8
+ spec.version = FileTree::VERSION
9
+ spec.authors = ["aliakb"]
10
+ spec.email = ["abaturytski@gmail.com"]
11
+ spec.summary = "DSL for creating file trees."
12
+ spec.description = "DSL for creating file trees; mostly for tests which consume files and directories."
13
+ spec.homepage = "https://github.com/aliakb/file_tree"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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.6"
22
+ spec.add_development_dependency "rake", "~> 10.1"
23
+ spec.add_development_dependency "rspec", "~> 2.13"
24
+ spec.add_development_dependency "simplecov", "~> 0.8"
25
+ end
data/lib/file_tree.rb ADDED
@@ -0,0 +1,38 @@
1
+ require "file_tree/version"
2
+ require "file_tree/defaults"
3
+ require "file_tree/content_generator"
4
+ require "tmpdir"
5
+
6
+ module FileTree
7
+ extend self
8
+
9
+ def self.with_context_dir(dir, &block)
10
+ dir_stack.push(dir)
11
+ yield dir if block_given?
12
+ dir
13
+ ensure
14
+ dir_stack.pop
15
+ end
16
+
17
+ def self.dir_stack
18
+ Thread.current[:file_tree_dir_stack] ||= []
19
+ end
20
+
21
+ def root_dir(&block)
22
+ dir = Dir.mktmpdir
23
+ FileTree.with_context_dir(dir, &block)
24
+ end
25
+
26
+ def dir(name, &block)
27
+ dir = File.join(FileTree.dir_stack[-1], name)
28
+ Dir.mkdir(dir)
29
+ FileTree.with_context_dir(dir, &block)
30
+ end
31
+
32
+ def file(name, content = Defaults.content_type)
33
+ content = ContentGenerator.create(content) unless content.is_a?(String)
34
+ path = File.join(FileTree.dir_stack[-1], name)
35
+ File.open(path, "wb"){|stream| stream << content}
36
+ path
37
+ end
38
+ end
@@ -0,0 +1,33 @@
1
+ module FileTree
2
+ module ContentGenerator
3
+ LETTERS = ["a".."z", "A".."Z", "0".."9", " ", "\n", "_", "*"].map{|i| Array(i)}.flatten
4
+ CONTENT_TYPES = [:binary, :text, :empty]
5
+
6
+ def self.create(type)
7
+ type = CONTENT_TYPES.sample if type == :random
8
+
9
+ case type
10
+ when :binary
11
+ create_binary_content
12
+ when :text
13
+ create_text_content
14
+ when :empty
15
+ ""
16
+ else
17
+ raise(ArgumentError, "Unsupported content type: #{type}")
18
+ end
19
+ end
20
+
21
+ def self.create_binary_content
22
+ Random.new.bytes(random_file_size)
23
+ end
24
+
25
+ def self.create_text_content
26
+ (1..random_file_size).map{LETTERS.sample}.join
27
+ end
28
+
29
+ def self.random_file_size
30
+ rand(Defaults.min_file_size..Defaults.max_file_size)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,21 @@
1
+ module FileTree
2
+ module Defaults
3
+ class << self
4
+
5
+ attr_writer(:content_type)
6
+ def content_type
7
+ @content_type || :random
8
+ end
9
+
10
+ attr_writer(:min_file_size)
11
+ def min_file_size
12
+ @min_file_size || 1024
13
+ end
14
+
15
+ attr_writer(:max_file_size)
16
+ def max_file_size
17
+ @max_file_size || 3 * 1024
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module FileTree
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,200 @@
1
+ require "spec_helper"
2
+
3
+ describe FileTree do
4
+ include FileTree
5
+
6
+ let(:random_content) {FileTree::ContentGenerator.create(:random)}
7
+
8
+ def test_root(&block)
9
+ dir = root_dir(&block)
10
+ ensure
11
+ FileUtils.rm_rf(dir)
12
+ end
13
+
14
+ context "#root_dir" do
15
+ it "should yield" do
16
+ expect{|b| test_root(&b)}.to yield_control
17
+ end
18
+
19
+ it "should not raise with no code block" do
20
+ expect{test_root}.to_not raise_error
21
+ end
22
+
23
+ it "should create a temp directory" do
24
+ temp_dir = Dir.mktmpdir
25
+ Dir.should_receive(:mktmpdir).once.and_return(temp_dir)
26
+ test_root
27
+ end
28
+
29
+ it "should yield a directory name" do
30
+ temp_dir = Dir.mktmpdir
31
+ Dir.stub(:mktmpdir).and_return(temp_dir)
32
+
33
+ expect{|b| test_root(&b)}.to yield_with_args(temp_dir)
34
+ end
35
+
36
+ it "should return a directory name" do
37
+ temp_dir = Dir.mktmpdir
38
+ Dir.stub(:mktmpdir).and_return(temp_dir)
39
+
40
+ test_root.should eql temp_dir
41
+ end
42
+ end
43
+
44
+ context "dir" do
45
+ let(:root) {@root}
46
+ around(:each) do |example|
47
+ test_root do |dir|
48
+ @root = dir
49
+ example.run
50
+ end
51
+ end
52
+
53
+ it "should yield" do
54
+ expect{|b| dir("foo", &b)}.to yield_control
55
+ end
56
+
57
+ it "should yield a directory name" do
58
+ expect{|b| dir("foo", &b)}.to yield_with_args(File.join(root, "foo"))
59
+ end
60
+
61
+ it "should return a directory name" do
62
+ expected = File.join(root, "test")
63
+ dir("test").should eql expected
64
+ end
65
+
66
+ it "should return a directory name when called with a block" do
67
+ expected = File.join(root, "test")
68
+ dir("test"){}.should eql expected
69
+ end
70
+
71
+ it "should create directory under the root" do
72
+ expected = File.join(root, "test")
73
+ Dir.should_receive(:mkdir).once.with(expected)
74
+ dir "test"
75
+ end
76
+
77
+ it "should return an existing directory" do
78
+ File.directory?(dir("foo")).should be_true
79
+ end
80
+
81
+ context "nested" do
82
+ around(:each) do |example|
83
+ dir "top" do |dir|
84
+ @root = dir
85
+ example.run
86
+ end
87
+ end
88
+
89
+ it "should create a subdirectory" do
90
+ expected = File.join(root, "test")
91
+ dir("test").should eql expected
92
+ end
93
+ end
94
+ end
95
+
96
+ context "file" do
97
+ context "in root" do
98
+ let(:root) {@root}
99
+
100
+ around(:each) do |example|
101
+ test_root do |dir|
102
+ @root = dir
103
+ example.run
104
+ end
105
+ end
106
+
107
+ it "should create a file" do
108
+ expected = File.join(root, "test")
109
+ File.should_receive(:open).with(expected, "wb").and_call_original
110
+
111
+ file "test"
112
+ end
113
+
114
+ it "should return path" do
115
+ expected = File.join(root, "test")
116
+ file("test").should eql expected
117
+ end
118
+
119
+ it "should use explicity specified content" do
120
+ content = random_content
121
+ path = file("test", content)
122
+
123
+ IO::binread(path).should eql content
124
+ end
125
+ end
126
+
127
+ context "in subdirectory" do
128
+ let(:root) {@root}
129
+
130
+ around(:each) do |example|
131
+ test_root do
132
+ dir "subdir" do |dir|
133
+ @root = dir
134
+
135
+ example.run
136
+ end
137
+ end
138
+ end
139
+
140
+ it "should create a file" do
141
+ expected = File.join(root, "test")
142
+ file("test").should eql expected
143
+ end
144
+
145
+ it "should use text content by default" do
146
+ FileTree::Defaults.stub(:content_type).and_return(:text)
147
+ content = FileTree::ContentGenerator.create_text_content
148
+ FileTree::ContentGenerator.should_receive(:create_text_content).once.and_return(content)
149
+
150
+ path = file("test")
151
+ IO.binread(path).should eql content
152
+ end
153
+
154
+ it "should use binary content by default" do
155
+ FileTree::Defaults.stub(:content_type).and_return(:binary)
156
+ content = FileTree::ContentGenerator.create_binary_content
157
+ FileTree::ContentGenerator.should_receive(:create_binary_content).once.and_return(content)
158
+
159
+ path = file "test"
160
+ IO.binread(path).should eql content
161
+ end
162
+
163
+ it "should use randomized text content by default" do
164
+ FileTree::ContentGenerator::CONTENT_TYPES.should_receive(:sample).once.and_return(:text)
165
+ FileTree::ContentGenerator.should_receive(:create_text_content).once.and_call_original
166
+
167
+ file "test"
168
+ end
169
+
170
+ it "should use randomized binary content by default" do
171
+ FileTree::ContentGenerator::CONTENT_TYPES.should_receive(:sample).once.and_return(:binary)
172
+ FileTree::ContentGenerator.should_receive(:create_binary_content).once.and_call_original
173
+
174
+ file "test"
175
+ end
176
+
177
+ it "should use random text content" do
178
+ FileTree::ContentGenerator::CONTENT_TYPES.should_receive(:sample).once.and_return(:text)
179
+ FileTree::ContentGenerator.should_receive(:create_text_content).once.and_call_original
180
+ file "foo", :random
181
+ end
182
+
183
+ it "should use random binary content" do
184
+ FileTree::ContentGenerator::CONTENT_TYPES.should_receive(:sample).once.and_return(:binary)
185
+ FileTree::ContentGenerator.should_receive(:create_binary_content).once.and_call_original
186
+ file "foo", :random
187
+ end
188
+
189
+ it "should create an empty file" do
190
+ path = file "empty", :empty
191
+ File.file?(path).should be_true
192
+ File.size(path).should be_zero
193
+ end
194
+
195
+ it "should raise for unsupported type" do
196
+ expect{file("test", :unsupported)}.to raise_error(ArgumentError)
197
+ end
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,4 @@
1
+ require "simplecov"
2
+ SimpleCov.start
3
+
4
+ require "file_tree"
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: file_tree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - aliakb
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.8'
69
+ description: DSL for creating file trees; mostly for tests which consume files and
70
+ directories.
71
+ email:
72
+ - abaturytski@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - file_tree.gemspec
84
+ - lib/file_tree.rb
85
+ - lib/file_tree/content_generator.rb
86
+ - lib/file_tree/defaults.rb
87
+ - lib/file_tree/version.rb
88
+ - spec/file_tree_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: https://github.com/aliakb/file_tree
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.2.2
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: DSL for creating file trees.
114
+ test_files:
115
+ - spec/file_tree_spec.rb
116
+ - spec/spec_helper.rb