vfs 0.3.10 → 0.3.11

Sign up to get free protection for your applications and to get access to all the features.
data/lib/vfs.rb CHANGED
@@ -12,7 +12,6 @@
12
12
 
13
13
  entry_proxy
14
14
 
15
- storages/hash_fs
16
15
  storages/local
17
16
 
18
17
  integration/string
@@ -177,7 +177,8 @@ module Vfs
177
177
  raise "can't copy to unknown Entry!"
178
178
  end
179
179
 
180
- efficient_dir_copy(target, options) || unefficient_dir_copy(target, options)
180
+ # efficient_dir_copy(target, options) || unefficient_dir_copy(target, options)
181
+ unefficient_dir_copy(target, options)
181
182
 
182
183
  target
183
184
  end
@@ -196,8 +197,12 @@ module Vfs
196
197
  move_to to, options
197
198
  end
198
199
 
200
+ # class << self
201
+ # attr_accessor :dont_use_efficient_dir_copy
202
+ # end
203
+
199
204
  protected
200
- def unefficient_dir_copy to, options
205
+ def unefficient_dir_copy to, options
201
206
  to.create options
202
207
  entries options do |e|
203
208
  if e.is_a? Dir
@@ -210,64 +215,67 @@ module Vfs
210
215
  end
211
216
  end
212
217
 
213
- def efficient_dir_copy to, options
214
- storage.open_fs do |fs|
215
- try = 0
216
- begin
217
- try += 1
218
- self.class.efficient_dir_copy(self, to, options[:override])
219
- rescue StandardError => error
220
- unknown_errors = 0
221
-
222
- attrs = get
223
- if attrs[:file]
224
- raise Error, "can't copy File as a Dir ('#{self}')!"
225
- elsif attrs[:dir]
226
- # some unknown error (but it also maybe caused by to be fixed error in 'to')
227
- unknown_errors += 1
228
- else
229
- raise Error, "'#{self}' not exist!" if options[:bang]
230
- return true
231
- end
232
-
233
- attrs = to.get
234
- if attrs[:file]
235
- if options[:override]
236
- to.destroy
237
- else
238
- raise Vfs::Error, "entry #{to} already exist!"
239
- end
240
- elsif attrs[:dir]
241
- unknown_errors += 1
242
- # if options[:override]
243
- # to.destroy
244
- # else
245
- # dir_already_exist = true
246
- # # raise Vfs::Error, "entry #{to} already exist!"
247
- # end
248
- else # parent not exist
249
- parent = to.parent
250
- if parent.exist?
251
- # some unknown error (but it also maybe caused by already fixed error in 'from')
252
- unknown_errors += 1
253
- else
254
- parent.create(options)
255
- end
256
- end
257
-
258
- raise error if unknown_errors > 1
259
- try < 2 ? retry : raise(error)
260
- end
261
- end
262
- end
263
218
 
264
- def self.efficient_dir_copy from, to, override
265
- from.storage.open_fs{|fs|
266
- fs.respond_to?(:efficient_dir_copy) and fs.efficient_dir_copy(from, to, override)
267
- } or
268
- to.storage.open_fs{|fs|
269
- fs.respond_to?(:efficient_dir_copy) and fs.efficient_dir_copy(from, to, override)
270
- }
271
- end
219
+ # def efficient_dir_copy to, options
220
+ # return false if self.class.dont_use_efficient_dir_copy
221
+ #
222
+ # storage.open_fs do |fs|
223
+ # try = 0
224
+ # begin
225
+ # try += 1
226
+ # self.class.efficient_dir_copy(self, to, options[:override])
227
+ # rescue StandardError => error
228
+ # unknown_errors = 0
229
+ #
230
+ # attrs = get
231
+ # if attrs[:file]
232
+ # raise Error, "can't copy File as a Dir ('#{self}')!"
233
+ # elsif attrs[:dir]
234
+ # # some unknown error (but it also maybe caused by to be fixed error in 'to')
235
+ # unknown_errors += 1
236
+ # else
237
+ # raise Error, "'#{self}' not exist!" if options[:bang]
238
+ # return true
239
+ # end
240
+ #
241
+ # attrs = to.get
242
+ # if attrs[:file]
243
+ # if options[:override]
244
+ # to.destroy
245
+ # else
246
+ # raise Vfs::Error, "entry #{to} already exist!"
247
+ # end
248
+ # elsif attrs[:dir]
249
+ # unknown_errors += 1
250
+ # # if options[:override]
251
+ # # to.destroy
252
+ # # else
253
+ # # dir_already_exist = true
254
+ # # # raise Vfs::Error, "entry #{to} already exist!"
255
+ # # end
256
+ # else # parent not exist
257
+ # parent = to.parent
258
+ # if parent.exist?
259
+ # # some unknown error (but it also maybe caused by already fixed error in 'from')
260
+ # unknown_errors += 1
261
+ # else
262
+ # parent.create(options)
263
+ # end
264
+ # end
265
+ #
266
+ # raise error if unknown_errors > 1
267
+ # try < 2 ? retry : raise(error)
268
+ # end
269
+ # end
270
+ # end
271
+ #
272
+ # def self.efficient_dir_copy from, to, override
273
+ # from.storage.open_fs{|fs|
274
+ # fs.respond_to?(:efficient_dir_copy) and fs.efficient_dir_copy(from, to, override)
275
+ # } or
276
+ # to.storage.open_fs{|fs|
277
+ # fs.respond_to?(:efficient_dir_copy) and fs.efficient_dir_copy(from, to, override)
278
+ # }
279
+ # end
272
280
  end
273
281
  end
@@ -69,7 +69,7 @@ module Vfs
69
69
  else
70
70
  fs.write_file(path, options[:append]){|writer| writer.call data}
71
71
  end
72
- rescue StandardError => error
72
+ rescue StandardError => error
73
73
  entry = self.entry
74
74
  if entry.exist?
75
75
  if options[:override]
@@ -86,7 +86,7 @@ module Vfs
86
86
  parent.create(options)
87
87
  end
88
88
  end
89
-
89
+
90
90
  retry if try < 2
91
91
  end
92
92
  end
@@ -159,7 +159,7 @@ module Vfs
159
159
  to.file
160
160
  else
161
161
  raise "can't copy to unknown Entry!"
162
- end
162
+ end
163
163
 
164
164
  target.write options do |writer|
165
165
  read(options){|buff| writer.call buff}
@@ -191,7 +191,7 @@ module Vfs
191
191
 
192
192
  args.unshift Object.new if args.size == 1 and args.first.is_a?(Hash)
193
193
 
194
- template = Tilt.new path
194
+ template = Tilt.new(path){read}
195
195
  template.render *args
196
196
  end
197
197
 
@@ -1,10 +1,11 @@
1
+ warn 'remove trailing spaces'
1
2
  require 'tempfile'
2
3
 
3
4
  module Vfs
4
5
  module Storages
5
6
  class Local
6
7
  module LocalVfsHelper
7
- DEFAULT_BUFFER = 1024*128
8
+ DEFAULT_BUFFER = 1000 * 1024
8
9
 
9
10
  attr_writer :buffer
10
11
  def buffer
@@ -23,7 +24,7 @@ module Vfs
23
24
  # attributes special for file system
24
25
  attrs[:created_at] = stat.ctime
25
26
  attrs[:updated_at] = stat.mtime
26
- attrs[:size] = stat.size
27
+ attrs[:size] = stat.size if stat.file?
27
28
  attrs
28
29
  rescue Errno::ENOENT
29
30
  {}
@@ -45,15 +46,18 @@ module Vfs
45
46
  end
46
47
  end
47
48
 
48
- def write_file path, append, &block
49
- option = append ? 'a' : 'w'
49
+ def write_file path, append, &block
50
+ # TODO2 Performance lost, extra call to check file existence
51
+ raise "can't write, entry #{path} already exist!" if !append and ::File.exist?(path)
52
+
53
+ option = append ? 'a' : 'w'
50
54
  ::File.open path, option do |os|
51
55
  writer = -> buff {os.write buff}
52
56
  block.call writer
53
57
  end
54
58
  end
55
59
 
56
- def delete_file path
60
+ def delete_file path
57
61
  ::File.delete path
58
62
  end
59
63
 
@@ -70,6 +74,9 @@ module Vfs
70
74
  end
71
75
 
72
76
  def delete_dir path
77
+ # TODO2 Performance lost, extra call to check file existence
78
+ raise "can't delete file (#{path})!" if ::File.file?(path)
79
+
73
80
  FileUtils.rm_r path
74
81
  end
75
82
 
@@ -96,33 +103,20 @@ module Vfs
96
103
  end
97
104
  end
98
105
 
99
- def efficient_dir_copy from, to, override
100
- return false if override # FileUtils.cp_r doesn't support this behaviour
101
-
102
- from.storage.open_fs do |from_fs|
103
- to.storage.open_fs do |to_fs|
104
- if from_fs.local? and to_fs.local?
105
- FileUtils.cp_r from.path, to.path
106
- true
107
- else
108
- false
109
- end
110
- end
111
- end
112
- end
113
-
114
- # def move_dir path
115
- # raise 'not supported'
116
- # end
117
-
118
- # def upload_directory from_local_path, to_remote_path
119
- # FileUtils.cp_r from_local_path, to_remote_path
106
+ # def efficient_dir_copy from, to, override
107
+ # return false if override # FileUtils.cp_r doesn't support this behaviour
108
+ #
109
+ # from.storage.open_fs do |from_fs|
110
+ # to.storage.open_fs do |to_fs|
111
+ # if from_fs.local? and to_fs.local?
112
+ # FileUtils.cp_r from.path, to.path
113
+ # true
114
+ # else
115
+ # false
116
+ # end
117
+ # end
118
+ # end
120
119
  # end
121
- #
122
- # def download_directory from_remote_path, to_local_path
123
- # FileUtils.cp_r from_remote_path, to_local_path
124
- # end
125
-
126
120
 
127
121
  #
128
122
  # Other
@@ -1,30 +1,29 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'Container' do
4
- before do
5
- @fs = '/'.to_entry_on(Vfs::Storages::HashFs.new)
6
- end
4
+ with_test_fs
7
5
 
8
6
  it "should threat paths as UniversalEntry except it ends with '/'" do
9
- @fs.should_receive(:entry).with('/a/b')
10
- @fs['/a/b']
7
+ test_fs.should_receive(:entry).with('tmp/a/b')
8
+ test_fs['tmp/a/b']
11
9
 
12
- @fs.should_receive(:dir).with('/a/b')
13
- @fs['/a/b/']
10
+ test_fs.should_receive(:dir).with('tmp/a/b')
11
+ test_fs['tmp/a/b/']
14
12
  end
15
13
 
16
14
  it '/' do
17
- @fs[:some_path].should == @fs / :some_path
15
+ test_fs[:some_path].should == test_fs / :some_path
16
+ test_fs[:some_path][:another_path].should == test_fs / :some_path / :another_path
18
17
  end
19
18
 
20
19
  it "UniversalEntry should be wrapped inside of proxy, Dir and File should not" do
21
- -> {@fs.dir.proxy?}.should raise_error(NoMethodError)
22
- -> {@fs.file.proxy?}.should raise_error(NoMethodError)
23
- @fs.entry.proxy?.should be_true
20
+ -> {test_fs.dir.proxy?}.should raise_error(NoMethodError)
21
+ -> {test_fs.file.proxy?}.should raise_error(NoMethodError)
22
+ test_fs.entry.proxy?.should be_true
24
23
  end
25
24
 
26
25
  it "sometimes it also should inexplicitly guess that path is a Dir instead of UniversalEntry (but still wrap it inside of Proxy)" do
27
- dir = @fs['/a/..']
26
+ dir = test_fs['tmp/a/..']
28
27
  dir.proxy?.should be_true
29
28
  dir.should be_a(Vfs::Dir)
30
29
  end
@@ -1,9 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'Dir' do
4
+ with_test_fs
5
+
4
6
  before do
5
- @fs = '/'.to_entry_on(Vfs::Storages::HashFs.new)
6
- @path = @fs['/a/b/c']
7
+ @path = test_fs['a/b/c']
7
8
  end
8
9
 
9
10
  describe 'existence' do
@@ -195,28 +196,28 @@ describe 'Dir' do
195
196
  @to.file('other_file').read.should == 'other'
196
197
  end
197
198
  end
198
-
199
+
199
200
  describe 'general copy' do
200
201
  it_should_behave_like 'copy_to behavior'
201
202
 
202
203
  before do
203
- # we using here another HashFs storage, to prevent :effective_dir_copy to be used
204
- @to = '/'.to_entry_on(Vfs::Storages::HashFs.new)['to']
205
-
206
- @from.storage.should_not_receive(:for_spec_helper_effective_copy_used)
207
- end
204
+ # prevenging usage of :efficient_dir_copy
205
+ # Vfs::Dir.dont_use_efficient_dir_copy = true
206
+
207
+ @to = test_fs['to']
208
+ end
209
+ # after do
210
+ # Vfs::Dir.dont_use_efficient_dir_copy = false
211
+ # end
208
212
  end
209
213
 
210
- describe 'effective copy' do
211
- it_should_behave_like 'copy_to behavior'
212
-
213
- before do
214
- # we using the same HashFs storage, so :effective_dir_copy will be used
215
- @to = @fs['to']
216
-
217
- # @from.storage.should_receive(:for_spec_helper_effective_copy_used).at_least(1).times
218
- end
219
- end
214
+ # describe 'effective copy' do
215
+ # it_should_behave_like 'copy_to behavior'
216
+ #
217
+ # before do
218
+ # @to = test_fs['to']
219
+ # end
220
+ # end
220
221
  end
221
222
 
222
223
  describe 'moving' do
@@ -1,9 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'Entry' do
4
+ with_test_fs
5
+
4
6
  before do
5
- @fs = '/'.to_entry_on(Vfs::Storages::HashFs.new)
6
- @path = @fs['/a/b/c']
7
+ @path = test_fs['a/b/c']
7
8
  end
8
9
 
9
10
  it "name" do
@@ -11,11 +12,11 @@ describe 'Entry' do
11
12
  end
12
13
 
13
14
  it 'tmp' do
14
- tmp = @fs.tmp
15
+ tmp = test_fs.tmp
15
16
  tmp.should be_dir
16
17
 
17
18
  tmp = nil
18
- @fs.tmp do |path|
19
+ test_fs.tmp do |path|
19
20
  tmp = path
20
21
  tmp.should be_dir
21
22
  end
@@ -1,9 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'File' do
4
+ with_test_fs
5
+
4
6
  before do
5
- @fs = '/'.to_entry_on(Vfs::Storages::HashFs.new)
6
- @path = @fs['/a/b/c']
7
+ @path = test_fs['a/b/c']
7
8
  end
8
9
 
9
10
  describe 'existence' do
@@ -67,15 +68,16 @@ describe 'File' do
67
68
  @path.read.should == 'something'
68
69
  end
69
70
 
70
- it 'should override existing file if override specified' do
71
+ it 'should not override existing file, only if :override specified' do
71
72
  @path.write 'something'
72
73
  @path.should be_file
73
74
  -> {@path.write 'another'}.should raise_error(Vfs::Error, /exist/)
74
- @path.write! 'another'
75
- @path.read.should == 'another'
75
+
76
+ @path.write! 'other'
77
+ @path.read.should == 'other'
76
78
  end
77
79
 
78
- it 'should override existing dir if override specified' do
80
+ it 'should override existing dir if :override specified' do
79
81
  @path.dir.create
80
82
  @path.should be_dir
81
83
  -> {@path.write 'another'}.should raise_error(Vfs::Error, /exist/)
@@ -132,22 +134,28 @@ describe 'File' do
132
134
  end
133
135
 
134
136
  it 'should copy to file (and overwrite if forced)' do
135
- check_copy_for @fs.file('to')
137
+ check_copy_for test_fs.file('to')
136
138
  end
137
139
 
138
140
  it 'should copy to dir (and overwrite if forced)' do
139
- check_copy_for @fs.dir("to")
141
+ check_copy_for test_fs.dir("to")
140
142
  end
141
143
 
142
144
  it 'should copy to UniversalEntry (and overwrite if forced)' do
143
- check_copy_for @fs.entry('to')
145
+ check_copy_for test_fs.entry('to')
144
146
  end
145
147
 
146
148
  it 'should be chainable' do
147
- to = @fs['to']
149
+ to = test_fs['to']
148
150
  @from.copy_to(to).should == to
149
151
  @from.copy_to!(to).should == to
150
152
  end
153
+
154
+ it "should autocreate parent's path if not exist (from error)" do
155
+ to = test_fs['parent_path/to']
156
+ @from.copy_to(to)
157
+ to.read.should == 'something'
158
+ end
151
159
  end
152
160
 
153
161
  describe 'moving' do
@@ -188,7 +196,23 @@ describe 'File' do
188
196
  end
189
197
 
190
198
  describe "extra stuff" do
191
- it 'render'
199
+ it 'render' do
200
+ template = test_fs / 'letter.erb'
201
+ template.write "Hello dear <%= name %>"
202
+ template.render(name: 'Mary').should == "Hello dear Mary"
203
+ end
204
+
205
+ begin
206
+ require 'haml'
207
+
208
+ it 'render using other template engines' do
209
+ template = test_fs / 'letter.haml'
210
+ template.write "Hello dear \#{name}"
211
+ template.render(name: 'Mary').should =~ /Hello dear Mary/
212
+ end
213
+ rescue LoadError
214
+ warn "no :haml template engine, skipping rendering with haml specs"
215
+ end
192
216
 
193
217
  it 'size'
194
218
  end
@@ -1,4 +1,50 @@
1
1
  require 'rspec_ext'
2
2
  require 'ruby_ext'
3
3
 
4
- require 'vfs'
4
+ require 'tilt'
5
+ require 'vfs'
6
+
7
+ rspec do
8
+ def self.with_test_fs
9
+ before do
10
+ @test_fs = "/tmp/test_fs".to_dir
11
+
12
+ FileUtils.rm_r test_fs.path if File.exist? test_fs.path
13
+ FileUtils.mkdir_p test_fs.path
14
+ end
15
+
16
+ after do
17
+ FileUtils.rm_r test_fs.path if File.exist? test_fs.path
18
+ @test_fs = nil
19
+ end
20
+ end
21
+
22
+ def test_fs
23
+ @test_fs
24
+ end
25
+ end
26
+
27
+ # require 'fakefs/spec_helpers'
28
+ #
29
+ # include FakeFS::SpecHelpers
30
+ # use_fakefs self
31
+ #
32
+ #
33
+ # #
34
+ # # FakeFS fixes
35
+ # #
36
+ # FakeFS::File::Stat.class_eval do
37
+ # # there's also file? method defined on File::Stat
38
+ # def file?; !directory? end
39
+ # end
40
+ #
41
+ # FakeFS::File.class_eval do
42
+ # class << self
43
+ # # File.delete should raise error if it's directory
44
+ # alias_method :delete_without_bang, :delete
45
+ # def delete path
46
+ # raise Errno::EPERM, "Operation not permitted - #{path}" if directory?(path)
47
+ # delete_without_bang path
48
+ # end
49
+ # end
50
+ # end
@@ -1,9 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'UniversalEntry' do
4
+ with_test_fs
5
+
4
6
  before do
5
- @fs = '/'.to_entry_on(Vfs::Storages::HashFs.new)
6
- @path = @fs['/a/b/c']
7
+ @path = test_fs['a/b/c']
7
8
  end
8
9
 
9
10
  describe 'existence' do
metadata CHANGED
@@ -1,28 +1,22 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: vfs
3
- version: !ruby/object:Gem::Version
4
- version: 0.3.10
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.11
5
5
  prerelease:
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Alexey Petrushin
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-08-08 00:00:00 +04:00
14
- default_executable:
12
+ date: 2011-08-10 00:00:00.000000000Z
15
13
  dependencies: []
16
-
17
14
  description:
18
15
  email:
19
16
  executables: []
20
-
21
17
  extensions: []
22
-
23
18
  extra_rdoc_files: []
24
-
25
- files:
19
+ files:
26
20
  - Rakefile
27
21
  - readme.md
28
22
  - lib/vfs/entries/dir.rb
@@ -34,7 +28,6 @@ files:
34
28
  - lib/vfs/error.rb
35
29
  - lib/vfs/integration/string.rb
36
30
  - lib/vfs/path.rb
37
- - lib/vfs/storages/hash_fs.rb
38
31
  - lib/vfs/storages/local.rb
39
32
  - lib/vfs/storages/specification.rb
40
33
  - lib/vfs/support.rb
@@ -46,36 +39,30 @@ files:
46
39
  - spec/file_spec.rb
47
40
  - spec/path_spec.rb
48
41
  - spec/spec_helper.rb
49
- - spec/storages/hash_fs_spec.rb
50
42
  - spec/storages/local_spec.rb
51
43
  - spec/universal_entry_spec.rb
52
- has_rdoc: true
53
44
  homepage: http://github.com/alexeypetrushin/vfs
54
45
  licenses: []
55
-
56
46
  post_install_message:
57
47
  rdoc_options: []
58
-
59
- require_paths:
48
+ require_paths:
60
49
  - lib
61
- required_ruby_version: !ruby/object:Gem::Requirement
50
+ required_ruby_version: !ruby/object:Gem::Requirement
62
51
  none: false
63
- requirements:
64
- - - ">="
65
- - !ruby/object:Gem::Version
66
- version: "0"
67
- required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
57
  none: false
69
- requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- version: "0"
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
73
62
  requirements: []
74
-
75
63
  rubyforge_project:
76
- rubygems_version: 1.5.1
64
+ rubygems_version: 1.8.6
77
65
  signing_key:
78
66
  specification_version: 3
79
67
  summary: Virtual File System
80
68
  test_files: []
81
-
@@ -1,216 +0,0 @@
1
- #
2
- # Very dirty and inefficient In Memory File System, mainly for tests.
3
- #
4
- module Vfs
5
- module Storages
6
- class HashFs < Hash
7
- def initialize
8
- super
9
- self['/'] = {dir: true}
10
- end
11
-
12
-
13
- def open_fs &block
14
- block.call self
15
- end
16
-
17
- #
18
- # Attributes
19
- #
20
- def attributes path
21
- base, name = split_path path
22
-
23
- # if path == '/'
24
- # return {dir: true, file: false}
25
- # end
26
- #
27
- stat = cd(base)[name]
28
- attrs = {}
29
- attrs[:file] = !!stat[:file]
30
- attrs[:dir] = !!stat[:dir]
31
- attrs
32
- rescue Exception
33
- {}
34
- end
35
-
36
- def set_attributes path, attrs
37
- raise 'not supported'
38
- end
39
-
40
-
41
- #
42
- # File
43
- #
44
- def read_file path, &block
45
- base, name = split_path path
46
- assert cd(base)[name], :include?, :file
47
- block.call cd(base)[name][:content]
48
- end
49
-
50
- def write_file path, append, &block
51
- base, name = split_path path
52
-
53
- os = if append
54
- file = cd(base)[name]
55
- file ? file[:content] : ''
56
- else
57
- assert_not cd(base), :include?, name
58
- ''
59
- end
60
- writer = -> buff {os << buff}
61
- block.call writer
62
-
63
- cd(base)[name] = {file: true, content: os}
64
- end
65
-
66
- def delete_file path
67
- base, name = split_path path
68
- assert cd(base)[name], :include?, :file
69
- cd(base).delete name
70
- end
71
-
72
- # def move_file path
73
- # raise 'not supported'
74
- # end
75
-
76
-
77
- #
78
- # Dir
79
- #
80
- def create_dir path
81
- base, name = split_path path
82
- assert_not cd(base), :include?, name
83
- cd(base)[name] = {dir: true}
84
- end
85
-
86
- def delete_dir path
87
- base, name = split_path path
88
- assert cd(base)[name], :include?, :dir
89
- # empty = true
90
- # cd(base)[name].each do |key, value|
91
- # empty = false if key.is_a? String
92
- # end
93
- # raise 'you are trying to delete not empty dir!' unless empty
94
- cd(base).delete name
95
- end
96
-
97
- # def move_dir path
98
- # raise 'not supported'
99
- # end
100
-
101
- def each_entry path, query, &block
102
- raise "hash_fs not support :each_entry with query!" if query
103
-
104
- base, name = split_path path
105
- assert cd(base)[name], :include?, :dir
106
- cd(base)[name].each do |relative_name, content|
107
- next if relative_name.is_a? Symbol
108
- if content[:dir]
109
- block.call relative_name, :dir
110
- else
111
- block.call relative_name, :file
112
- end
113
- end
114
- end
115
-
116
- def efficient_dir_copy from, to, override
117
- from.storage.open_fs do |from_fs|
118
- to.storage.open_fs do |to_fs|
119
- if from_fs == to_fs
120
- for_spec_helper_effective_copy_used
121
-
122
- _efficient_dir_copy from.path, to.path, override
123
- true
124
- else
125
- false
126
- end
127
- end
128
- end
129
- end
130
-
131
- def _efficient_dir_copy from_path, to_path, override
132
- from_base, from_name = split_path from_path
133
- assert cd(from_base)[from_name], :include?, :dir
134
-
135
- to_base, to_name = split_path to_path
136
- # assert_not cd(to_base), :include?, to_name
137
-
138
- if cd(to_base).include? to_name
139
- if cd(to_base)[to_name][:dir]
140
- each_entry from_path, nil do |name, type|
141
- if type == :dir
142
- _efficient_dir_copy "#{from_path}/#{name}", "#{to_path}/#{name}", override
143
- else
144
- raise "file #{to_path}/#{name} already exist!" if cd(to_base)[to_name].include?(name) and !override
145
- cd(to_base)[to_name][name] = cd(from_base)[from_name][name].clone
146
- end
147
- end
148
- else
149
- raise "can't copy dir #{from_path} to file #{to_path}!"
150
- end
151
- else
152
- cd(to_base)[to_name] = {dir: true}
153
- _efficient_dir_copy from_path, to_path, override
154
- end
155
- end
156
- protected :_efficient_dir_copy
157
- def for_spec_helper_effective_copy_used; end
158
-
159
- # def upload_directory from_local_path, to_remote_path
160
- # FileUtils.cp_r from_local_path, to_remote_path
161
- # end
162
- #
163
- # def download_directory from_remote_path, to_local_path
164
- # FileUtils.cp_r from_remote_path, to_local_path
165
- # end
166
-
167
-
168
- #
169
- # Other
170
- #
171
- def local?; true end
172
-
173
- def to_s; 'hash_fs' end
174
-
175
- def tmp &block
176
- tmp_dir = "/tmp_#{rand(10**6)}"
177
- create_dir tmp_dir
178
- if block
179
- begin
180
- block.call tmp_dir
181
- ensure
182
- delete_dir tmp_dir
183
- end
184
- else
185
- tmp_dir
186
- end
187
- end
188
-
189
- protected
190
- def assert obj, method, arg
191
- raise "#{obj} should #{method} #{arg}" unless obj.send method, arg
192
- end
193
-
194
- def assert_not obj, method, arg
195
- raise "#{obj} should not #{method} #{arg}" if obj.send method, arg
196
- end
197
-
198
- def split_path path
199
- parts = path[1..-1].split('/')
200
- parts.unshift '/'
201
- name = parts.pop
202
- return parts, name
203
- end
204
-
205
- def cd parts
206
- current = self
207
- iterator = parts.clone
208
- while iterator.first
209
- current = current[iterator.first]
210
- iterator.shift
211
- end
212
- current
213
- end
214
- end
215
- end
216
- end
@@ -1,10 +0,0 @@
1
- require 'vfs/storages/hash_fs'
2
- require 'vfs/storages/specification'
3
-
4
- describe Vfs::Storages::HashFs do
5
- it_should_behave_like "vfs storage"
6
-
7
- before do
8
- @storage = Vfs::Storages::HashFs.new
9
- end
10
- end