/
path_list_spec.rb
189 lines (152 loc) · 5.74 KB
/
path_list_spec.rb
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
require File.expand_path('../../../spec_helper', __FILE__)
module Pod
describe Sandbox::PathList do
before do
@path_list = Sandbox::PathList.new(fixture('banana-lib'))
end
describe "In general" do
it "creates the list of all the files" do
files = @path_list.files
files.reject! do |f|
f.include?('libPusher') || f.include?('.git') || f.include?('DS_Store')
end
expected = %w[
BananaLib.podspec
Classes/Banana.h
Classes/Banana.m
Classes/BananaLib.pch
Classes/BananaPrivate.h
Classes/BananaTrace.d
README
Resources/logo-sidebar.png
Resources/sub_dir/logo-sidebar.png
libBananalib.a
preserve_me.txt
sub-dir/sub-dir-2/somefile.txt
]
files.sort.should == expected
end
it "creates the list of the directories" do
dirs = @path_list.dirs
dirs.reject! do |f|
f.include?('libPusher') || f.include?('.git')
end
dirs.sort.should == %w| Bananalib.framework Classes Resources Resources/sub_dir sub-dir sub-dir/sub-dir-2 |
end
it "handles directories with glob metacharacters" do
root = temporary_directory + '[CP] Test'
root.mkpath
FileUtils.touch(root + 'Class.h')
@path_list = Sandbox::PathList.new(root)
@path_list.files.should == ["Class.h"]
end
end
#-------------------------------------------------------------------------#
describe "Globbing" do
it "can glob the root for a given pattern" do
paths = @path_list.relative_glob('Classes/*.{h,m}').map(&:to_s)
paths.sort.should == %w[
Classes/Banana.h
Classes/Banana.m
Classes/BananaPrivate.h
]
end
it "can return the absolute paths from glob" do
paths = @path_list.glob('Classes/*.{h,m}')
paths.all? { |p| p.absolute? }.should == true
end
it "can return the relative paths from glob" do
paths = @path_list.relative_glob('Classes/*.{h,m}')
paths.any? { |p| p.absolute? }.should == false
end
it "supports the `**` glob pattern" do
paths = @path_list.relative_glob('Classes/**/*.{h,m}').map(&:to_s)
paths.sort.should == %w[
Classes/Banana.h
Classes/Banana.m
Classes/BananaPrivate.h
]
end
it "supports an optional pattern for globbing directories" do
paths = @path_list.relative_glob('Classes', { :dir_pattern => '*.{h,m}'} ).map(&:to_s)
paths.sort.should == %w[
Classes/Banana.h
Classes/Banana.m
Classes/BananaPrivate.h
]
end
it "handles directories specified with a trailing slash" do
paths = @path_list.relative_glob('Classes/', { :dir_pattern => '*.{h,m}'} ).map(&:to_s)
paths.sort.should == %w[
Classes/Banana.h
Classes/Banana.m
Classes/BananaPrivate.h
]
end
it "supports an optional list of patterns to exclude" do
exclude_patterns = ['**/*.m', '**/*Private*.*']
paths = @path_list.relative_glob('Classes/*', { :exclude_patterns => exclude_patterns }).map(&:to_s)
paths.sort.should == %w[
Classes/Banana.h
Classes/BananaLib.pch
Classes/BananaTrace.d
]
end
it "allows to specify folders in the exclude patterns" do
paths = @path_list.relative_glob('Classes/*', { :exclude_patterns => 'Classes' }).map(&:to_s)
paths.sort.should.be.empty
end
it "can optionally include the directories in the results" do
paths = @path_list.relative_glob('Resources/*', { :include_dirs => true }).map(&:to_s)
paths.sort.should == %w[
Resources/logo-sidebar.png
Resources/sub_dir
]
end
end
#-------------------------------------------------------------------------#
describe "Private Helpers" do
describe "#directory?" do
it "detects a directory" do
@path_list.send(:directory?, 'classes').should == true
end
it "doesn't reports as a directory a file" do
@path_list.send(:directory?, 'Classes/Banana.m').should == false
end
end
#--------------------------------------#
describe "#directory?" do
it "expands a pattern into all the combinations of Dir#glob literals" do
patterns = @path_list.send(:dir_glob_equivalent_patterns, '{file1,file2}.{h,m}')
patterns.sort.should == %w[ file1.h file1.m file2.h file2.m ]
end
it "returns the original pattern if there are no Dir#glob expansions" do
patterns = @path_list.send(:dir_glob_equivalent_patterns, 'file*.*')
patterns.sort.should == %w[ file*.* ]
end
it "expands `**`" do
patterns = @path_list.send(:dir_glob_equivalent_patterns, 'Classes/**/file.m')
patterns.sort.should == %w[ Classes/**/file.m Classes/file.m ]
end
it "supports a combination of `**` and literals" do
patterns = @path_list.send(:dir_glob_equivalent_patterns, 'Classes/**/file.{h,m}')
patterns.sort.should == %w[
Classes/**/file.h
Classes/**/file.m
Classes/file.h
Classes/file.m
]
end
end
#--------------------------------------#
describe "#escape_path_for_glob" do
it "escapes metacharacters" do
escaped = @path_list.send(:escape_path_for_glob, '[]{}?**')
escaped.to_s.should == '\[\]\{\}\?\*\*'
end
end
#--------------------------------------#
end
#-------------------------------------------------------------------------#
end
end