rubypath 0.3.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,27 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'README examples' do
4
- describe 'Mock FS' do
5
- around {|example| Path::Backend.mock(&example) }
6
-
7
- before do
8
- Path.mock do |root, backend|
9
- backend.cwd = '/root'
10
- backend.current_user = 'test'
11
- backend.homes = {'test' => '/home/test'}
12
-
13
- home = root.mkpath('/home/test')
14
- home.mkfile('src/test.txt').write 'CONTENT'
15
- home.mkfile('src/test.html').write '<html><head><title></title>...'
16
- end
17
- end
18
-
19
- it 'should mock all FS' do
20
- base = Path('~test').expand
21
- expect(base.join(%w(src test.txt)).read).to eq 'CONTENT'
22
-
23
- files = base.glob('**/*').select{|p| p.file? }
24
- expect(files.size).to eq 2
25
- end
26
- end
27
- end
@@ -1,77 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Path do
4
- describe 'Comparison' do
5
- let(:path) { Path.new '/path/to/file' }
6
-
7
- describe_method :eql?, aliases: [:==] do
8
- context 'with Path object' do
9
- it 'should compare paths (1)' do
10
- res = path.send described_method, Path('/path/to/file')
11
- expect(res).to be true
12
- end
13
-
14
- it 'should compare paths (2)' do
15
- res = path.send described_method, Path('/path/to/another/file')
16
- expect(res).to be false
17
- end
18
-
19
- it 'should compare clean paths (1)' do
20
- res = path.send described_method, Path('/path/to/./file')
21
- expect(res).to be true
22
- end
23
-
24
- it 'should compare clean paths (2)' do
25
- res = path.send described_method, Path('/path/to/another/../file')
26
- expect(res).to be true
27
- end
28
- end
29
-
30
- context 'with String object' do
31
- it 'should compare paths (1)' do
32
- res = path.send described_method, '/path/to/file'
33
- expect(res).to be true
34
- end
35
-
36
- it 'should compare paths (1)' do
37
- res = path.send described_method, '/path/to/another/file'
38
- expect(res).to be false
39
- end
40
-
41
- it 'should compare clean paths (1)' do
42
- res = path.send described_method, '/path/to/./file'
43
- expect(res).to be true
44
- end
45
-
46
- it 'should compare clean paths (2)' do
47
- res = path.send described_method, '/path/to/another/../file'
48
- expect(res).to be true
49
- end
50
- end
51
-
52
- context 'with Pathname object' do
53
- it 'should compare paths (1)' do
54
- res = path.send described_method, Pathname.new('/path/to/file')
55
- expect(res).to be true
56
- end
57
-
58
- it 'should compare paths (1)' do
59
- res = path.send described_method,
60
- Pathname.new('/path/to/another/file')
61
- expect(res).to be false
62
- end
63
-
64
- it 'should compare clean paths (1)' do
65
- res = path.send described_method, Pathname.new('/path/to/./file')
66
- expect(res).to be true
67
- end
68
-
69
- it 'should compare clean paths (2)' do
70
- res = path.send described_method,
71
- Pathname.new('/path/to/another/../file')
72
- expect(res).to be true
73
- end
74
- end
75
- end
76
- end
77
- end
@@ -1,101 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Path do
4
- describe 'Construction' do
5
- let(:str) { '/path/to/file' }
6
- let(:args) { [str] }
7
- let(:path) { described_class.new(*args) }
8
- subject { path }
9
-
10
- describe_method :path, aliases: [:to_path, :to_s] do
11
- subject { path.send described_method }
12
-
13
- it { should eq str }
14
-
15
- # Should not return same object as internal variable
16
- # to avoid in-place modifications like
17
- # `Path.new('/abc').path.delete!('abc')`
18
- it { should_not equal path.send(:instance_variable_get, :@path) }
19
- end
20
-
21
- describe '#initialize' do
22
- context 'w/o args' do
23
- let(:args) { %w() }
24
- it { expect(subject.path).to eq '' }
25
- it { should be_a Path }
26
- end
27
-
28
- context 'with multiple strings' do
29
- let(:args) { %w(path to a file.txt) }
30
- it { expect(subject.path).to eq 'path/to/a/file.txt' }
31
- it { should be_a Path }
32
- end
33
-
34
- context 'with Pathname' do
35
- let(:args) { [Pathname.new('path/to/dir'), 'file.txt'] }
36
- it { expect(subject.path).to eq 'path/to/dir/file.txt' }
37
- it { should be_a Path }
38
- end
39
-
40
- context 'with Numerals' do
41
- let(:args) { ['path', 5, 'to', 4.5, 'file.txt'] }
42
- it { expect(subject.path).to eq 'path/5/to/4.5/file.txt' }
43
- it { should be_a Path }
44
- end
45
- end
46
-
47
- describe 'class' do
48
- describe '#new' do
49
- context 'with Path as argument' do
50
- let(:args) { [Path.new('/abc')] }
51
- it('should return same object') { should equal args.first }
52
- end
53
-
54
- context 'w/o args' do
55
- let(:args) { Array.new }
56
- it('should return Path::EMPTY') { should equal Path::EMPTY }
57
- end
58
- end
59
-
60
- describe '#like?' do
61
- subject { Path.like? obj }
62
-
63
- context 'positive list' do
64
- {
65
- 'Path' => Path.new('/path/to/file.ext'),
66
- 'Pathname' => Pathname.new('/path/to/file.ext'),
67
- 'String' => '/path/to/file.ext',
68
- '#to_path' => Class.new{ def to_path; '/path/to/file.ext' end }.new,
69
- '#path' => Class.new{ def path; '/path/to/file.ext' end }.new
70
- }.each do |name, example|
71
- let(:obj) { example.dup }
72
- it("should accept #{name}") { should be true }
73
- end
74
- end
75
- end
76
-
77
- describe '#like_path' do
78
- subject { Path.like_path obj }
79
-
80
- context 'positive list' do
81
- {
82
- 'Path' => Path.new('/path/to/file.ext'),
83
- 'Pathname' => Pathname.new('/path/to/file.ext'),
84
- 'String' => '/path/to/file.ext',
85
- '#to_path' => Class.new{ def to_path; '/path/to/file.ext' end }.new,
86
- '#path' => Class.new{ def path; '/path/to/file.ext' end }.new
87
- }.each do |name, example|
88
- let(:obj) { example.dup }
89
- it("should get path from #{name}") { should eq '/path/to/file.ext' }
90
- end
91
- end
92
- end
93
-
94
- describe '#to_proc' do
95
- it 'should allow to use Path as block' do
96
- expect(%w(path1 path2).map(&Path)).to eq [Path('path1'), Path('path2')]
97
- end
98
- end
99
- end
100
- end
101
- end
@@ -1,225 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Path do
4
- describe 'Directory Operations' do
5
- with_backends :mock, :sys do
6
- describe 'class' do
7
- describe_method :glob do
8
- before do
9
- Path.mock do |root|
10
- root.mkfile '/file.txt'
11
- root.mkfile '/lib/path.rb'
12
- root.mkfile '/lib/path/dir.rb'
13
- root.mkfile '/lib/path/file.rb'
14
- root.mkfile '/lib/path/ext.rb'
15
- end
16
- end
17
- subject { ->(*args){ Path.glob(*args) } }
18
-
19
- it 'should return matching files (I)' do
20
- expect(subject.call('/*')).to match_array %w(/file.txt /lib)
21
- end
22
-
23
- it 'should return matching files (II)' do
24
- expect(subject.call('/**/*.rb')).to match_array \
25
- %w(/lib/path.rb /lib/path/dir.rb
26
- /lib/path/file.rb /lib/path/ext.rb)
27
- end
28
-
29
- if defined?(::File::FNM_EXTGLOB)
30
- it 'should return matching files (III)' do
31
- expect(subject.call('/**/{dir,ext}.rb')).to match_array \
32
- %w(/lib/path/dir.rb /lib/path/ext.rb)
33
- end
34
- end
35
-
36
- it 'should return matching files (IV)' do
37
- expect(subject.call('/lib/*.rb')).to match_array %w(/lib/path.rb)
38
- end
39
- end
40
- end
41
-
42
- shared_examples '#remove_recursive' do
43
- context 'on existent file' do
44
- before { path.mkfile }
45
- it{ expect{ subject }.to change(path, :exist?).from(true).to(false) }
46
- end
47
-
48
- context 'on existent directory' do
49
- before { path.mkpath }
50
- it{ expect{ subject }.to change(path, :exist?).from(true).to(false) }
51
- end
52
-
53
- context 'on existent directory with children' do
54
- before { path.mkfile('subdir/file') }
55
- it{ expect{ subject }.to change(path, :exist?).from(true).to(false) }
56
- end
57
- end
58
-
59
- describe_method :rmtree, aliases: [:rm_rf] do
60
- let(:path) { Path '/path' }
61
- subject { path.send(described_method) }
62
-
63
- context 'on non-existent file' do
64
- it { expect{ subject }.to_not raise_error }
65
- end
66
-
67
- it_behaves_like '#remove_recursive'
68
- end
69
-
70
- describe_method :safe_rmtree do
71
- let(:path) { Path '/path' }
72
- subject { path.send(described_method) }
73
-
74
- it 'should use #remove_entry_secure' do
75
- if backend_type == :sys
76
- path.mkfile
77
- expect(FileUtils).to receive(:remove_entry_secure).and_call_original
78
- subject
79
- end
80
- end
81
-
82
- context 'on non-existent file' do
83
- it { expect{ subject }.to_not raise_error }
84
- end
85
-
86
- it_behaves_like '#remove_recursive'
87
- end
88
-
89
- describe_method :rmtree!, aliases: [:rm_r] do
90
- let(:path) { Path '/path' }
91
- subject { path.send(described_method) }
92
-
93
- context 'on non-existent file' do
94
- it { expect{ subject }.to raise_error Errno::ENOENT }
95
- end
96
-
97
- it_behaves_like '#remove_recursive'
98
- end
99
-
100
- describe_method :safe_rmtree! do
101
- let(:path) { Path '/path' }
102
- subject { path.send(described_method) }
103
-
104
- it 'should use #remove_entry_secure' do
105
- if backend_type == :sys
106
- path.mkfile
107
- expect(FileUtils).to receive(:remove_entry_secure).and_call_original
108
- subject
109
- end
110
- end
111
-
112
- context 'on non-existent file' do
113
- it { expect{ subject }.to raise_error Errno::ENOENT }
114
- end
115
-
116
- it_behaves_like '#remove_recursive'
117
- end
118
-
119
- describe '#glob' do
120
- it 'should delegate to class#glob' do
121
- expect(Path).to receive(:glob)
122
- .with('/abc\[\]/.\*\{\}/file/**/{a,b}.rb', 10).and_return([])
123
-
124
- Path('/abc[]/.*{}/file').glob('**/{a,b}.rb', 10)
125
- end
126
- end
127
-
128
- describe '#mkdir' do
129
- context 'w/o arg' do
130
- let(:dir) { Path '/dir' }
131
- before { expect(dir).to_not be_existent }
132
- subject { dir.mkdir }
133
-
134
- it 'should create directory' do
135
- expect(subject).to be_directory
136
- end
137
-
138
- it 'should return path to directory' do
139
- expect(subject).to eq '/dir'
140
- end
141
-
142
- it { should be_a Path }
143
-
144
- context 'in non-existent parent directory' do
145
- let(:dir) { Path '/non-ext/dir' }
146
- before { expect(dir).to_not be_existent }
147
- before { expect(dir.parent).to_not be_existent }
148
- subject { dir.mkdir }
149
-
150
- it 'should raise some error' do
151
- expect{ subject }.to raise_error(
152
- Errno::ENOENT, 'No such file or directory - /non-ext/dir')
153
- end
154
- end
155
- end
156
-
157
- context 'with arg' do
158
- let(:dir) { Path '/' }
159
- let(:args) { ['fuu'] }
160
- before { expect(dir.join(*args)).to_not be_existent }
161
- subject { dir.mkdir(*args) }
162
-
163
- it 'should create directory' do
164
- expect(subject).to be_directory
165
- end
166
-
167
- it 'should return path to directory' do
168
- expect(subject).to eq '/fuu'
169
- end
170
-
171
- it { should be_a Path }
172
- end
173
- end
174
-
175
- describe_method :mkpath, aliases: [:mkdir_p] do
176
- let(:dir) { Path '/path/to/dir' }
177
- before { expect(dir).to_not be_existent }
178
- before { expect(dir.parent).to_not be_existent }
179
- subject { dir.send(described_method) }
180
-
181
- it 'should create directories' do
182
- expect(subject).to be_directory
183
- end
184
-
185
- it 'should return path to directory' do
186
- expect(subject).to eq '/path/to/dir'
187
- end
188
-
189
- it { should be_a Path }
190
- end
191
-
192
- describe_method :entries do
193
- let(:path) { Path '/' }
194
- let(:args) { Array.new }
195
- subject { path.send described_method, *args }
196
-
197
- context 'with directory with children' do
198
- before do
199
- path.touch 'file.a'
200
- path.touch 'file.b'
201
- path.mkdir 'dir.a'
202
- path.mkdir 'dir.b'
203
- end
204
-
205
- it 'should list of entries' do
206
- expect(subject).to match_array %w(.. . file.a file.b dir.a dir.b)
207
- end
208
-
209
- it 'should return list of Path objects' do
210
- subject.each{ |e| expect(e).to be_a Path }
211
- end
212
- end
213
-
214
- context 'with non-existent directory' do
215
- let(:path) { Path '/non-existent-dir' }
216
-
217
- it 'should raise error' do
218
- expect{ subject }.to raise_error(
219
- Errno::ENOENT, 'No such file or directory - /non-existent-dir')
220
- end
221
- end
222
- end
223
- end
224
- end
225
- end
@@ -1,270 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Path do
4
- describe 'Extensions' do
5
- let(:path) { Path '/path/to/template.de.html.slim' }
6
- let(:dotfile) { Path '/path/to/.dotfile' }
7
- let(:dotfile_ext) { Path '/path/to/.dotfile.en.sh' }
8
-
9
- describe_method :extensions, aliases: [:exts] do
10
- subject { path.send described_method }
11
-
12
- it 'should return all file extensions' do
13
- should eq %w(de html slim)
14
- end
15
-
16
- context 'dotfile w/o ext' do
17
- let(:path) { dotfile }
18
-
19
- it 'should not return dotfile name as extension' do
20
- should eq Array.new
21
- end
22
- end
23
-
24
- context 'dotfile with ext' do
25
- let(:path) { dotfile_ext }
26
-
27
- it 'should only return dotfile extension' do
28
- should eq %w(en sh)
29
- end
30
- end
31
- end
32
-
33
- describe '#extname' do
34
- subject { path.extname }
35
-
36
- it 'should return file extensions including dot' do
37
- should eq '.slim'
38
- end
39
-
40
- context 'dotfile w/o ext' do
41
- let(:path) { dotfile }
42
-
43
- it 'should not return dotfile name as extension' do
44
- should eq ''
45
- end
46
- end
47
-
48
- context 'dotfile with ext' do
49
- let(:path) { dotfile_ext }
50
-
51
- it 'should only return dotfile extension' do
52
- should eq '.sh'
53
- end
54
- end
55
- end
56
-
57
- describe '#pure_name' do
58
- subject { path.pure_name }
59
-
60
- it 'should return file name without extensions' do
61
- should eq 'template'
62
- end
63
-
64
- context 'dotfile w/o ext' do
65
- let(:path) { dotfile }
66
-
67
- it 'should return dotfile name' do
68
- should eq '.dotfile'
69
- end
70
- end
71
-
72
- context 'dotfile with ext' do
73
- let(:path) { dotfile_ext }
74
-
75
- it 'should return dotfile name w/o exts' do
76
- should eq '.dotfile'
77
- end
78
- end
79
- end
80
-
81
- describe_method :extension, aliases: [:ext] do
82
- subject { path.send described_method }
83
-
84
- it 'should return last file extensions' do
85
- should eq 'slim'
86
- end
87
- end
88
-
89
- describe_method :replace_extensions do
90
- let(:path) { Path "#{base}file#{exts}" }
91
-
92
- shared_examples 'extensions replacement' do
93
- context 'with array' do
94
- subject { path.send described_method, %w(en txt) }
95
-
96
- it 'should replace all file extensions' do
97
- should eq "#{base}file.en.txt"
98
- end
99
-
100
- it { should be_a Path }
101
- end
102
-
103
- context 'with multiple arguments' do
104
- subject { path.send described_method, *%w(en txt) }
105
-
106
- it 'should replace all file extensions' do
107
- should eq "#{base}file.en.txt"
108
- end
109
-
110
- it { should be_a Path }
111
- end
112
- end
113
-
114
- shared_examples 'w/o ext' do
115
- let(:exts) { '' }
116
- it_behaves_like 'extensions replacement'
117
-
118
- context 'with replacement hash' do
119
- subject{ path.send(described_method, 'txt' => 'html') }
120
-
121
- it 'should replace all file extensions' do
122
- should eq "#{base}file"
123
- end
124
-
125
- it { should be_a Path }
126
- end
127
- end
128
-
129
- shared_examples 'with single ext' do
130
- let(:exts) { '.txt' }
131
- it_behaves_like 'extensions replacement'
132
-
133
- context 'with replacement hash' do
134
- subject { path.send(described_method, 'txt' => 'html') }
135
-
136
- it 'should replace all file extensions' do
137
- should eq "#{base}file.html"
138
- end
139
-
140
- it { should be_a Path }
141
- end
142
- end
143
-
144
- shared_examples 'with multiple ext' do
145
- let(:exts) { '.en.html.slim' }
146
- it_behaves_like 'extensions replacement'
147
-
148
- context 'with replacement hash' do
149
- subject { path.send(described_method, 'en' => 'de') }
150
-
151
- it 'should replace all file extensions' do
152
- should eq "#{base}file.de.html.slim"
153
- end
154
-
155
- it { should be_a Path }
156
- end
157
- end
158
-
159
- context 'with path' do
160
- let(:base) { '/path/to/' }
161
- it_behaves_like 'w/o ext'
162
- it_behaves_like 'with single ext'
163
- it_behaves_like 'with multiple ext'
164
- end
165
-
166
- context 'with filename only' do
167
- let(:base) { '' }
168
- it_behaves_like 'w/o ext'
169
- it_behaves_like 'with single ext'
170
- it_behaves_like 'with multiple ext'
171
- end
172
-
173
- context 'with relative file path (I)' do
174
- let(:base) { './' }
175
- it_behaves_like 'w/o ext'
176
- it_behaves_like 'with single ext'
177
- it_behaves_like 'with multiple ext'
178
- end
179
-
180
- context 'with relative file path (II)' do
181
- let(:base) { 'path/' }
182
- it_behaves_like 'w/o ext'
183
- it_behaves_like 'with single ext'
184
- it_behaves_like 'with multiple ext'
185
- end
186
- end
187
-
188
- describe_method :replace_extension do
189
- let(:path) { Path "#{base}#{file}#{ext}" }
190
-
191
- shared_examples 'extension replacement' do
192
- context 'with array' do
193
- subject { path.send described_method, %w(mobile txt) }
194
-
195
- it 'should replace last file extensions' do
196
- should eq "#{base}#{file}.mobile.txt"
197
- end
198
-
199
- it { should be_a Path }
200
- end
201
-
202
- context 'with multiple arguments' do
203
- subject { path.send described_method, *%w(mobile txt) }
204
-
205
- it 'should replace last file extensions' do
206
- should eq "#{base}#{file}.mobile.txt"
207
- end
208
-
209
- it { should be_a Path }
210
- end
211
-
212
- context 'with single string' do
213
- subject { path.send described_method, 'haml' }
214
-
215
- it 'should replace last file extensions' do
216
- should eq "#{base}#{file}.haml"
217
- end
218
-
219
- it { should be_a Path }
220
- end
221
- end
222
-
223
- shared_examples 'w/o ext' do
224
- let(:file) { 'file' }
225
- let(:ext) { '' }
226
- it_behaves_like 'extension replacement'
227
- end
228
-
229
- shared_examples 'with single ext' do
230
- let(:file) { 'file' }
231
- let(:ext) { '.txt' }
232
- it_behaves_like 'extension replacement'
233
- end
234
-
235
- shared_examples 'with multiple ext' do
236
- let(:file) { 'file.de' }
237
- let(:ext) { '.txt' }
238
- it_behaves_like 'extension replacement'
239
- end
240
-
241
- context 'on path file' do
242
- let(:base) { '/path/to/file/' }
243
- it_behaves_like 'w/o ext'
244
- it_behaves_like 'with single ext'
245
- it_behaves_like 'with multiple ext'
246
- end
247
-
248
- context 'on relative path file' do
249
- let(:base) { 'to/file/' }
250
- it_behaves_like 'w/o ext'
251
- it_behaves_like 'with single ext'
252
- it_behaves_like 'with multiple ext'
253
- end
254
-
255
- context 'on relative root path file' do
256
- let(:base) { './' }
257
- it_behaves_like 'w/o ext'
258
- it_behaves_like 'with single ext'
259
- it_behaves_like 'with multiple ext'
260
- end
261
-
262
- context 'on filename only' do
263
- let(:base) { '' }
264
- it_behaves_like 'w/o ext'
265
- it_behaves_like 'with single ext'
266
- it_behaves_like 'with multiple ext'
267
- end
268
- end
269
- end
270
- end