file-visitor 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +30 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/file/visitor.rb +128 -0
- data/lib/file/visitor/filter.rb +10 -0
- data/lib/file/visitor/filter/ext.rb +27 -0
- data/lib/file/visitor/filter/mtime.rb +59 -0
- data/lib/file/visitor/filter/name.rb +25 -0
- data/lib/file/visitor/filter/proc.rb +23 -0
- data/lib/file/visitor/filter_dispatcher.rb +22 -0
- data/lib/file/visitor/time_utils.rb +94 -0
- data/spec/integration/use_case_spec.rb +62 -0
- data/spec/lib/file/vistor/filter/ext_spec.rb +23 -0
- data/spec/lib/file/vistor/filter/mtime_spec.rb +116 -0
- data/spec/lib/file/vistor/filter/name_spec.rb +40 -0
- data/spec/lib/file/vistor/filter/proc_spec.rb +36 -0
- data/spec/lib/file/vistor/filter_dispatcher_spec.rb +26 -0
- data/spec/lib/file/vistor/filter_spec.rb +12 -0
- data/spec/lib/file/vistor/time_utils_spec.rb +156 -0
- data/spec/lib/file/vistor_spec.rb +197 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/spec_utils.rb +33 -0
- metadata +139 -0
@@ -0,0 +1,197 @@
|
|
1
|
+
|
2
|
+
require 'file/visitor'
|
3
|
+
require 'spec_utils'
|
4
|
+
|
5
|
+
describe File::Visitor do
|
6
|
+
include SpecUtils
|
7
|
+
|
8
|
+
before(:each) do
|
9
|
+
setup_test_dir
|
10
|
+
@visitor = File::Visitor.new
|
11
|
+
end
|
12
|
+
|
13
|
+
after(:all) do
|
14
|
+
clear_test_dir
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'can be created with new()' do
|
18
|
+
visitor = File::Visitor.new
|
19
|
+
visitor.should be_a File::Visitor
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'file/directory traversal' do
|
23
|
+
|
24
|
+
it 'raises error on non-dir argument' do
|
25
|
+
expect { @visitor.visit(nil) }.to raise_error(ArgumentError)
|
26
|
+
expect { @visitor.visit(1) }.to raise_error(ArgumentError)
|
27
|
+
expect { @visitor.visit_dir(nil) }.to raise_error(ArgumentError)
|
28
|
+
expect { @visitor.visit_dir(1) }.to raise_error(ArgumentError)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'can visit given directory' do
|
32
|
+
create_file(['dir1'], 'file1', 'a')
|
33
|
+
create_file(['dir1'], 'file2', 'b')
|
34
|
+
create_file(['dir1'], 'file3', 'c')
|
35
|
+
|
36
|
+
files = []
|
37
|
+
@visitor.visit(test_dir) do |path|
|
38
|
+
files.push File.basename(path)
|
39
|
+
end
|
40
|
+
|
41
|
+
files.size.should == 3
|
42
|
+
files[0].should == "file1"
|
43
|
+
files[1].should == "file2"
|
44
|
+
files[2].should == "file3"
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'can visit sibling directories' do
|
48
|
+
create_file(['dir1'], 'file10', 'a')
|
49
|
+
create_file(['dir2'], 'file11', 'b')
|
50
|
+
create_file(['dir3'], 'file12', 'c')
|
51
|
+
|
52
|
+
files = []
|
53
|
+
@visitor.visit(test_dir) do |path|
|
54
|
+
files.push File.basename(path)
|
55
|
+
end
|
56
|
+
|
57
|
+
files.size.should == 3
|
58
|
+
files[0].should == "file10"
|
59
|
+
files[1].should == "file11"
|
60
|
+
files[2].should == "file12"
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'can visit deep directories' do
|
64
|
+
create_file(['sub'], 'file20', 'a')
|
65
|
+
create_file(['sub', 'subsub'], 'file21', 'b')
|
66
|
+
create_file(['sub', 'subsub', 'subsubsub'], 'file22', 'c')
|
67
|
+
|
68
|
+
files = []
|
69
|
+
@visitor.visit(test_dir) do |path|
|
70
|
+
files.push File.basename(path)
|
71
|
+
end
|
72
|
+
|
73
|
+
files.size.should == 3
|
74
|
+
files[0].should == "file20"
|
75
|
+
files[1].should == "file21"
|
76
|
+
files[2].should == "file22"
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'return empty array when no target files' do
|
80
|
+
files = []
|
81
|
+
@visitor.visit(test_dir) do |path|
|
82
|
+
files.push File.basename(path)
|
83
|
+
end
|
84
|
+
files.should be_empty
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'can do dir-only traversal' do
|
88
|
+
create_file(['dir1'], 'file', 'a')
|
89
|
+
create_file(['dir2-1'], 'file', 'a')
|
90
|
+
create_file(['dir2-1', 'dir2-2'], 'file', 'a')
|
91
|
+
|
92
|
+
dirs = []
|
93
|
+
@visitor.visit_dir(test_dir) do |path|
|
94
|
+
File.directory?(path).should be_true
|
95
|
+
dirs.push File.basename(path)
|
96
|
+
end
|
97
|
+
|
98
|
+
dirs.size.should == 3
|
99
|
+
dirs[0].should == "dir1"
|
100
|
+
dirs[1].should == "dir2-1"
|
101
|
+
dirs[2].should == "dir2-2"
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'it can get files list' do
|
107
|
+
create_file(['dir1'], 'file1', 'a')
|
108
|
+
create_file(['dir2'], 'file2', 'b')
|
109
|
+
create_file(['dir2'], 'file3', 'c')
|
110
|
+
|
111
|
+
files = @visitor.file_list(test_dir)
|
112
|
+
files.size.should == 3
|
113
|
+
|
114
|
+
File.exist?(files[0]).should be_true
|
115
|
+
File.exist?(files[1]).should be_true
|
116
|
+
File.exist?(files[2]).should be_true
|
117
|
+
|
118
|
+
File.basename(files[0]).should == "file1"
|
119
|
+
File.basename(files[1]).should == "file2"
|
120
|
+
File.basename(files[2]).should == "file3"
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'can get dir list' do
|
124
|
+
create_file(['dir1'], 'file1', 'a')
|
125
|
+
create_file(['dir2'], 'file2', 'b')
|
126
|
+
create_file(['dir2'], 'file3', 'c')
|
127
|
+
|
128
|
+
dirs = @visitor.dir_list(test_dir)
|
129
|
+
dirs.size.should == 2
|
130
|
+
|
131
|
+
File.directory?(dirs[0]).should be_true
|
132
|
+
File.directory?(dirs[1]).should be_true
|
133
|
+
|
134
|
+
File.basename(dirs[0]).should == "dir1"
|
135
|
+
File.basename(dirs[1]).should == "dir2"
|
136
|
+
end
|
137
|
+
|
138
|
+
context "filters registration" do
|
139
|
+
|
140
|
+
before(:each) do
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'built-in filter' do
|
144
|
+
@visitor.add_filter(:name, '2013-01-01.log')
|
145
|
+
@visitor.filters.size.should == 1
|
146
|
+
@visitor.filters[0].should be_a File::Visitor::Filter::Name
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'custom filter' do
|
150
|
+
class ValidCustomFilter
|
151
|
+
def match?
|
152
|
+
true
|
153
|
+
end
|
154
|
+
end
|
155
|
+
@visitor.add_filter(ValidCustomFilter.new)
|
156
|
+
@visitor.filters.size.should == 1
|
157
|
+
@visitor.filters[0].should be_a ValidCustomFilter
|
158
|
+
end
|
159
|
+
|
160
|
+
it "add custom filter fails on invalid class" do
|
161
|
+
class BrokenCustomFilter
|
162
|
+
# match? not implemented
|
163
|
+
end
|
164
|
+
expect {
|
165
|
+
@visitor.add_filter(BrokenCustomFilter.new)
|
166
|
+
}.to raise_error(ArgumentError, /must implement match\?\(\)/)
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'block filter' do
|
170
|
+
@visitor.add_filter do |path|
|
171
|
+
path =~ /\./
|
172
|
+
end
|
173
|
+
@visitor.filters.size.should == 1
|
174
|
+
@visitor.filters[0].should be_a File::Visitor::Filter::Proc
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
describe "target?" do
|
180
|
+
|
181
|
+
it "all the paths are target, when no filters" do
|
182
|
+
@visitor.target?("/tmp").should be_true
|
183
|
+
end
|
184
|
+
|
185
|
+
it "filter AND combination" do
|
186
|
+
@visitor.add_filter(:ext, :txt)
|
187
|
+
@visitor.add_filter { |path| path =~ /feb/ }
|
188
|
+
|
189
|
+
@visitor.target?("/tmp/2013-jan.txt").should be_false
|
190
|
+
@visitor.target?("/tmp/2013-feb.txt").should be_true
|
191
|
+
@visitor.target?("/tmp/2013-mar.txt").should be_false
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
end
|
197
|
+
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
|
2
|
+
def spec_data_dir
|
3
|
+
data_dir = File.join(
|
4
|
+
File.dirname(__FILE__), "data_for_spec")
|
5
|
+
unless File.directory?(data_dir)
|
6
|
+
FileUtils.mkdir(data_dir)
|
7
|
+
end
|
8
|
+
data_dir
|
9
|
+
end
|
10
|
+
|
11
|
+
def cleanup_spec_data_dir
|
12
|
+
return unless File.directory?(spec_data_dir)
|
13
|
+
FileUtils.rm_rf(spec_data_dir)
|
14
|
+
end
|
15
|
+
|
16
|
+
def spec_data_create(filename)
|
17
|
+
path = File.join(spec_data_dir, filename)
|
18
|
+
file = File.open(path, "w") do |file|
|
19
|
+
file.puts "test"
|
20
|
+
end
|
21
|
+
path
|
22
|
+
end
|
23
|
+
|
data/spec/spec_utils.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
module SpecUtils
|
5
|
+
|
6
|
+
def test_dir
|
7
|
+
File.join(File.dirname(__FILE__), 'data_for_test')
|
8
|
+
end
|
9
|
+
|
10
|
+
def clear_test_dir
|
11
|
+
FileUtils.rm_rf(test_dir)
|
12
|
+
end
|
13
|
+
|
14
|
+
def setup_test_dir
|
15
|
+
clear_test_dir
|
16
|
+
FileUtils.mkdir_p(test_dir)
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_dir(dirs)
|
20
|
+
dirs.unshift test_dir
|
21
|
+
FileUtils.mkdir_p(File.join(dirs))
|
22
|
+
end
|
23
|
+
|
24
|
+
def create_file(dirs, filename, content)
|
25
|
+
dir = create_dir(dirs)
|
26
|
+
path = File.join(dir, filename)
|
27
|
+
File.open(path, 'w') do |file|
|
28
|
+
file.puts content
|
29
|
+
end
|
30
|
+
path
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: file-visitor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- bonar
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2013-02-04 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
type: :development
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 47
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 8
|
31
|
+
- 0
|
32
|
+
version: 2.8.0
|
33
|
+
version_requirements: *id001
|
34
|
+
name: rspec
|
35
|
+
prerelease: false
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
type: :development
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 31
|
44
|
+
segments:
|
45
|
+
- 3
|
46
|
+
- 12
|
47
|
+
version: "3.12"
|
48
|
+
version_requirements: *id002
|
49
|
+
name: rdoc
|
50
|
+
prerelease: false
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
type: :development
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ~>
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 63
|
59
|
+
segments:
|
60
|
+
- 1
|
61
|
+
- 8
|
62
|
+
- 4
|
63
|
+
version: 1.8.4
|
64
|
+
version_requirements: *id003
|
65
|
+
name: jeweler
|
66
|
+
prerelease: false
|
67
|
+
description: you can collect file paths by mtime/name/ext/block filters.
|
68
|
+
email: bonar@me.com
|
69
|
+
executables: []
|
70
|
+
|
71
|
+
extensions: []
|
72
|
+
|
73
|
+
extra_rdoc_files:
|
74
|
+
- LICENSE.txt
|
75
|
+
- README.rdoc
|
76
|
+
files:
|
77
|
+
- .document
|
78
|
+
- .rspec
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.rdoc
|
83
|
+
- Rakefile
|
84
|
+
- VERSION
|
85
|
+
- lib/file/visitor.rb
|
86
|
+
- lib/file/visitor/filter.rb
|
87
|
+
- lib/file/visitor/filter/ext.rb
|
88
|
+
- lib/file/visitor/filter/mtime.rb
|
89
|
+
- lib/file/visitor/filter/name.rb
|
90
|
+
- lib/file/visitor/filter/proc.rb
|
91
|
+
- lib/file/visitor/filter_dispatcher.rb
|
92
|
+
- lib/file/visitor/time_utils.rb
|
93
|
+
- spec/integration/use_case_spec.rb
|
94
|
+
- spec/lib/file/vistor/filter/ext_spec.rb
|
95
|
+
- spec/lib/file/vistor/filter/mtime_spec.rb
|
96
|
+
- spec/lib/file/vistor/filter/name_spec.rb
|
97
|
+
- spec/lib/file/vistor/filter/proc_spec.rb
|
98
|
+
- spec/lib/file/vistor/filter_dispatcher_spec.rb
|
99
|
+
- spec/lib/file/vistor/filter_spec.rb
|
100
|
+
- spec/lib/file/vistor/time_utils_spec.rb
|
101
|
+
- spec/lib/file/vistor_spec.rb
|
102
|
+
- spec/spec.opts
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
- spec/spec_utils.rb
|
105
|
+
homepage: http://github.com/bonar/file-visitor
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
hash: 3
|
119
|
+
segments:
|
120
|
+
- 0
|
121
|
+
version: "0"
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
hash: 3
|
128
|
+
segments:
|
129
|
+
- 0
|
130
|
+
version: "0"
|
131
|
+
requirements: []
|
132
|
+
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 1.8.24
|
135
|
+
signing_key:
|
136
|
+
specification_version: 3
|
137
|
+
summary: file path collecting utility
|
138
|
+
test_files: []
|
139
|
+
|