fs 0.0.1 → 0.0.2
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.
- data/.gitignore +2 -0
- data/Rakefile +7 -1
- data/fs.gemspec +2 -1
- data/lib/fs.rb +5 -75
- data/lib/fs/base.rb +83 -0
- data/lib/fs/version.rb +1 -1
- data/spec/base_spec.rb +221 -0
- data/spec/spec_helper.rb +2 -2
- data/spec/support/test_dir_support.rb +33 -0
- data/spec/test_dir_spec.rb +31 -0
- metadata +22 -8
- data/spec/fs_spec.rb +0 -196
- data/spec/support/fakefs_support.rb +0 -32
data/Rakefile
CHANGED
@@ -3,4 +3,10 @@ Bundler::GemHelper.install_tasks
|
|
3
3
|
|
4
4
|
require 'rspec/core/rake_task'
|
5
5
|
RSpec::Core::RakeTask.new
|
6
|
-
task :default => :spec
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
require 'yard'
|
9
|
+
YARD::Rake::YardocTask.new do |t|
|
10
|
+
t.files = ['lib/**/*.rb']
|
11
|
+
t.options = ["--title", "FS #{FS::VERSION}"]
|
12
|
+
end
|
data/fs.gemspec
CHANGED
@@ -13,7 +13,8 @@ Gem::Specification.new do |s|
|
|
13
13
|
s.description = %q{FS gathers the cluttered methods for working with files and dirs. Internally using the good old standard library, but providing simple methods in a single place.}
|
14
14
|
|
15
15
|
s.add_development_dependency 'rspec', '2.5.0'
|
16
|
-
s.add_development_dependency '
|
16
|
+
s.add_development_dependency 'yard', '0.6.5'
|
17
|
+
s.add_development_dependency 'bluecloth', '2.1.0'
|
17
18
|
|
18
19
|
s.files = `git ls-files`.split("\n")
|
19
20
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
data/lib/fs.rb
CHANGED
@@ -1,80 +1,10 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__)) unless $LOAD_PATH.include?(File.dirname(__FILE__))
|
2
|
+
|
1
3
|
require 'fileutils'
|
4
|
+
require 'etc'
|
5
|
+
require 'fs/base'
|
2
6
|
|
3
7
|
module FS
|
4
8
|
extend self
|
5
|
-
|
6
|
-
# FileUtils.touch
|
7
|
-
def touch(files)
|
8
|
-
FileUtils.touch(files)
|
9
|
-
end
|
10
|
-
|
11
|
-
# FileUtils#mkdir
|
12
|
-
def makedir(dirs)
|
13
|
-
FileUtils.mkdir(dirs)
|
14
|
-
end
|
15
|
-
|
16
|
-
# FileUtils#mkdir_p
|
17
|
-
def makedirs(dirs)
|
18
|
-
FileUtils.mkdir_p(dirs)
|
19
|
-
end
|
20
|
-
|
21
|
-
# Dir#entries
|
22
|
-
def list(dir, pattern='*')
|
23
|
-
glob(dir, pattern)
|
24
|
-
end
|
25
|
-
|
26
|
-
# Find#find
|
27
|
-
def find(dir, pattern='*')
|
28
|
-
glob(dir, '**', pattern)
|
29
|
-
end
|
30
|
-
|
31
|
-
# FileUtils#mv
|
32
|
-
def move(*froms, to)
|
33
|
-
froms.each do |from|
|
34
|
-
FileUtils.mv(from, to)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
# FileUtils#rm
|
39
|
-
def remove(*pathes)
|
40
|
-
FileUtils.rm(pathes, :verbose => true)
|
41
|
-
end
|
42
|
-
|
43
|
-
# File#open(file, 'w')
|
44
|
-
def write(file, content=nil, &block)
|
45
|
-
if block_given?
|
46
|
-
File.open(file, 'w', &block)
|
47
|
-
else
|
48
|
-
File.open(file, 'w') {|f| f.write(content) }
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
# File#open(file, 'r')
|
53
|
-
def read(file, &block)
|
54
|
-
if block_given?
|
55
|
-
File.open(file, 'r', &block)
|
56
|
-
else
|
57
|
-
content = nil
|
58
|
-
File.open(file, 'r') {|f| content = f.read }
|
59
|
-
content
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
|
64
|
-
def root
|
65
|
-
'/'
|
66
|
-
end
|
67
|
-
|
68
|
-
def home(user=nil)
|
69
|
-
Dir.home(user)
|
70
|
-
end
|
71
|
-
|
72
|
-
private
|
73
|
-
|
74
|
-
def glob(dir, *patterns)
|
75
|
-
Dir.glob(File.join(dir, patterns)).map do |path|
|
76
|
-
path.gsub(/^\.?#{dir}\/?/, '')
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
9
|
+
extend FS::Base
|
80
10
|
end
|
data/lib/fs/base.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
module FS
|
2
|
+
module Base
|
3
|
+
# FileUtils.touch
|
4
|
+
def touch(files)
|
5
|
+
FileUtils.touch(files)
|
6
|
+
end
|
7
|
+
|
8
|
+
# FileUtils#mkdir
|
9
|
+
def makedir(dirs)
|
10
|
+
FileUtils.mkdir(dirs)
|
11
|
+
end
|
12
|
+
|
13
|
+
# FileUtils#mkdir_p
|
14
|
+
def makedirs(dirs)
|
15
|
+
FileUtils.mkdir_p(dirs)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Dir#glob
|
19
|
+
def list(dir='.', pattern='*')
|
20
|
+
glob(dir, pattern)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Dir#glob
|
24
|
+
# TODO: use Find#find
|
25
|
+
def find(dir='.', pattern='*')
|
26
|
+
glob(dir, '**', pattern)
|
27
|
+
end
|
28
|
+
|
29
|
+
# FileUtils#mv
|
30
|
+
def move(*froms, to)
|
31
|
+
froms.each do |from|
|
32
|
+
FileUtils.mv(from, to)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# FileUtils#rm
|
37
|
+
def remove(*pathes)
|
38
|
+
FileUtils.rm(pathes)
|
39
|
+
end
|
40
|
+
|
41
|
+
# File#open(file, 'w')
|
42
|
+
def write(file, content=nil, &block)
|
43
|
+
if block_given?
|
44
|
+
File.open(file, 'w', &block)
|
45
|
+
else
|
46
|
+
File.open(file, 'w') {|f| f.write(content) }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# File#open(file, 'r')
|
51
|
+
def read(file, &block)
|
52
|
+
if block_given?
|
53
|
+
File.open(file, 'r', &block)
|
54
|
+
else
|
55
|
+
content = nil
|
56
|
+
File.open(file, 'r') {|f| content = f.read }
|
57
|
+
content
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Dir#home
|
62
|
+
def home(user=nil)
|
63
|
+
Dir.home(user)
|
64
|
+
end
|
65
|
+
|
66
|
+
# always returns '/'
|
67
|
+
def root
|
68
|
+
'/'
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def assert_dir(path)
|
74
|
+
raise "not a directory: #{path}" unless File.directory?(path)
|
75
|
+
end
|
76
|
+
|
77
|
+
def glob(dir, *patterns)
|
78
|
+
Dir.glob(File.join(dir, patterns)).map do |path|
|
79
|
+
path.gsub(/^\.?#{dir}\/?/, '')
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
data/lib/fs/version.rb
CHANGED
data/spec/base_spec.rb
ADDED
@@ -0,0 +1,221 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FS::Base do
|
4
|
+
|
5
|
+
describe 'touch' do
|
6
|
+
it 'touches a single file' do
|
7
|
+
file = 'foobar.txt'
|
8
|
+
FS.touch(file)
|
9
|
+
File.exist?(file).should be_true
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'touches a list of files' do
|
13
|
+
files = ['foo.txt', 'bar.txt']
|
14
|
+
FS.touch(files)
|
15
|
+
files.each do |file|
|
16
|
+
File.exist?(file).should be_true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'makedir' do
|
22
|
+
it 'creates a dir' do
|
23
|
+
FS.makedir('foo')
|
24
|
+
File.directory?('foo').should be_true
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'fails if a parent dir is missing' do
|
28
|
+
lambda {FS.makedir('foo/bar')}.should raise_error
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'makedirs' do
|
33
|
+
it 'creates all missing parent dirs' do
|
34
|
+
FS.makedirs 'foo/bar/baz'
|
35
|
+
File.directory?('foo').should be_true
|
36
|
+
File.directory?('foo/bar').should be_true
|
37
|
+
File.directory?('foo/bar/baz').should be_true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'list' do
|
42
|
+
it 'returns an empty list if there are no files' do
|
43
|
+
FS.list.should be_empty
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'lists all files and dirs (without . and ..)' do
|
47
|
+
FS.touch('file')
|
48
|
+
FS.makedir('dir')
|
49
|
+
FS.list.should eql(['dir', 'file'])
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'lists files and dirs in the current dir' do
|
53
|
+
FS.makedir('foo')
|
54
|
+
FS.makedir('foo/dir')
|
55
|
+
FS.touch('foo/file')
|
56
|
+
Dir.chdir('foo')
|
57
|
+
FS.list.should eql(['dir', 'file'])
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'globs files and dirs' do
|
61
|
+
FS.touch('file.txt')
|
62
|
+
FS.touch('file.rb')
|
63
|
+
FS.makedir('dir.txt')
|
64
|
+
FS.makedir('dir.rb')
|
65
|
+
FS.list('.', '*.txt').should eql(['dir.txt', 'file.txt'])
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'globs files and dirs' do
|
69
|
+
FS.touch('file.txt')
|
70
|
+
FS.touch('file.rb')
|
71
|
+
FS.makedir('dir.txt')
|
72
|
+
FS.makedir('dir.rb')
|
73
|
+
FS.list('.', '*.txt').should eql(['dir.txt', 'file.txt'])
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'lists files and dirs in a subdir' do
|
77
|
+
FS.makedir('foo')
|
78
|
+
FS.makedir('foo/dir')
|
79
|
+
FS.touch('foo/file')
|
80
|
+
FS.list('foo').should eql(['dir', 'file'])
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'globs files and dirs in a subdir' do
|
84
|
+
FS.makedir('foo')
|
85
|
+
FS.touch('foo/file.txt')
|
86
|
+
FS.touch('foo/file.rb')
|
87
|
+
FS.makedir('foo/dir.txt')
|
88
|
+
FS.makedir('foo/dir.rb')
|
89
|
+
FS.list('foo', '*.txt').should eql(['dir.txt', 'file.txt'])
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe 'find' do
|
94
|
+
it 'returns an empty list if there are no files' do
|
95
|
+
FS.find('.').should be_empty
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'finds files in all subdirs' do
|
99
|
+
FS.makedirs('one/two/three')
|
100
|
+
FS.touch('one/file.one')
|
101
|
+
FS.touch('one/two/three/file.three')
|
102
|
+
FS.find.should eql([
|
103
|
+
'one',
|
104
|
+
'one/file.one',
|
105
|
+
'one/two',
|
106
|
+
'one/two/three',
|
107
|
+
'one/two/three/file.three'
|
108
|
+
])
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'globs files in all subdirs' do
|
112
|
+
FS.makedirs('one/two/three')
|
113
|
+
FS.touch('one/file.one')
|
114
|
+
FS.touch('one/two/three/file.three')
|
115
|
+
FS.find('.', 'file.*').should eql([
|
116
|
+
'one/file.one',
|
117
|
+
'one/two/three/file.three'
|
118
|
+
])
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe 'move' do
|
123
|
+
it 'rename a file' do
|
124
|
+
FS.touch('foo.txt')
|
125
|
+
FS.move('foo.txt', 'bar.txt')
|
126
|
+
FS.list.should eql(['bar.txt'])
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'moves a file' do
|
130
|
+
FS.touch('foo.txt')
|
131
|
+
FS.makedirs('tmp')
|
132
|
+
FS.move('foo.txt', 'tmp')
|
133
|
+
FS.list.should eql(['tmp'])
|
134
|
+
FS.list('tmp').should eql(['foo.txt'])
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'moves files and dirs' do
|
138
|
+
FS.touch('file')
|
139
|
+
FS.makedir('dir')
|
140
|
+
FS.makedir('tmp')
|
141
|
+
FS.move('file', 'dir', 'tmp')
|
142
|
+
FS.list.should eql(['tmp'])
|
143
|
+
FS.list('tmp').should eql(['dir', 'file'])
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe 'remove' do
|
148
|
+
it 'removes files' do
|
149
|
+
FS.touch('file')
|
150
|
+
FS.remove('file')
|
151
|
+
FS.list.should be_empty
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'removes multiple files' do
|
155
|
+
FS.touch('file.a')
|
156
|
+
FS.touch('file.b')
|
157
|
+
FS.remove('file.a', 'file.b')
|
158
|
+
FS.list.should be_empty
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'fails on dirs' do
|
162
|
+
FS.makedir('dir')
|
163
|
+
lambda {FS.remove('dir')}.should raise_error
|
164
|
+
end
|
165
|
+
|
166
|
+
# FIXME: fakefs
|
167
|
+
# it 'fails if the dir is not empty' do
|
168
|
+
# FS.makedir('/foo')
|
169
|
+
# FS.touch('/foo/bar')
|
170
|
+
# lambda {FS.remove('/foo')}.should raise_error
|
171
|
+
# end
|
172
|
+
end
|
173
|
+
|
174
|
+
describe 'write' do
|
175
|
+
it 'writes content from a string' do
|
176
|
+
FS.write('foo.txt', 'bar')
|
177
|
+
File.open('foo.txt').read.should eql('bar')
|
178
|
+
end
|
179
|
+
|
180
|
+
it 'writes content from a block' do
|
181
|
+
FS.write('foo.txt') {|f| f.write 'bar' }
|
182
|
+
File.open('foo.txt').read.should eql('bar')
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
describe 'read' do
|
187
|
+
it 'reads the content to a string' do
|
188
|
+
File.open('foo.txt', 'w') {|f| f.write 'bar' }
|
189
|
+
FS.read('foo.txt').should eql('bar')
|
190
|
+
end
|
191
|
+
|
192
|
+
it 'reads the content to a block' do
|
193
|
+
File.open('foo.txt', 'w') {|f| f.write 'bar' }
|
194
|
+
FS.read('foo.txt') {|f| f.read.should eql('bar')}
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
describe 'root' do
|
199
|
+
it 'always returns /' do
|
200
|
+
FS.root.should eql('/')
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
describe 'home' do
|
205
|
+
it 'returns the home of the current user' do
|
206
|
+
FS.home.should match(/\/#{Etc.getlogin}$/)
|
207
|
+
end
|
208
|
+
|
209
|
+
it 'returns the home of another user' do
|
210
|
+
FS.home('root').should match(/\/root$/)
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
# describe 'removedir'
|
215
|
+
# describe 'removedirs'
|
216
|
+
# describe 'currentdir'
|
217
|
+
# describe 'changedir'
|
218
|
+
# describe 'copy'
|
219
|
+
# describe 'link'
|
220
|
+
|
221
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,33 @@
|
|
1
|
+
module FS
|
2
|
+
module SpecHelpers
|
3
|
+
|
4
|
+
def self.extended(example_group)
|
5
|
+
example_group.use_helper(example_group)
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.included(example_group)
|
9
|
+
example_group.extend self
|
10
|
+
end
|
11
|
+
|
12
|
+
def use_helper(describe_block)
|
13
|
+
describe_block.before :each do
|
14
|
+
unless @test_dir
|
15
|
+
@test_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'test'))
|
16
|
+
FileUtils.mkdir_p(@test_dir)
|
17
|
+
Dir.chdir(@test_dir)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe_block.after :each do
|
22
|
+
if @test_dir
|
23
|
+
FileUtils.rm_r(@test_dir)
|
24
|
+
@test_dir = nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
RSpec.configure do |config|
|
32
|
+
config.include FS::SpecHelpers
|
33
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FS do
|
4
|
+
describe 'test dir' do
|
5
|
+
it 'is absolute' do
|
6
|
+
Pathname.new(@test_dir).should be_absolute
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'is created' do
|
10
|
+
File.exist?(@test_dir).should be_true
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'is a directory' do
|
14
|
+
File.directory?(@test_dir).should be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'is empty' do
|
18
|
+
Dir.entries(@test_dir).should eql([".", ".."])
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'is the current dir' do
|
22
|
+
File.expand_path(Dir.pwd).should eql(@test_dir)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'is deleted when not empty' do
|
26
|
+
filename = 'test.file'
|
27
|
+
FileUtils.touch(filename)
|
28
|
+
File.exist?(File.join(@test_dir, filename)).should be_true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: fs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "Bernd J\xC3\xBCnger"
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-29 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -25,16 +25,27 @@ dependencies:
|
|
25
25
|
type: :development
|
26
26
|
version_requirements: *id001
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: yard
|
29
29
|
prerelease: false
|
30
30
|
requirement: &id002 !ruby/object:Gem::Requirement
|
31
31
|
none: false
|
32
32
|
requirements:
|
33
33
|
- - "="
|
34
34
|
- !ruby/object:Gem::Version
|
35
|
-
version: 0.
|
35
|
+
version: 0.6.5
|
36
36
|
type: :development
|
37
37
|
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: bluecloth
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - "="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.1.0
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id003
|
38
49
|
description: FS gathers the cluttered methods for working with files and dirs. Internally using the good old standard library, but providing simple methods in a single place.
|
39
50
|
email:
|
40
51
|
- blindgaenger@gmail.com
|
@@ -51,10 +62,12 @@ files:
|
|
51
62
|
- Rakefile
|
52
63
|
- fs.gemspec
|
53
64
|
- lib/fs.rb
|
65
|
+
- lib/fs/base.rb
|
54
66
|
- lib/fs/version.rb
|
55
|
-
- spec/
|
67
|
+
- spec/base_spec.rb
|
56
68
|
- spec/spec_helper.rb
|
57
|
-
- spec/support/
|
69
|
+
- spec/support/test_dir_support.rb
|
70
|
+
- spec/test_dir_spec.rb
|
58
71
|
has_rdoc: true
|
59
72
|
homepage: http://github.com/blindgaenger/fs
|
60
73
|
licenses: []
|
@@ -84,6 +97,7 @@ signing_key:
|
|
84
97
|
specification_version: 3
|
85
98
|
summary: Work with your filesystem!
|
86
99
|
test_files:
|
87
|
-
- spec/
|
100
|
+
- spec/base_spec.rb
|
88
101
|
- spec/spec_helper.rb
|
89
|
-
- spec/support/
|
102
|
+
- spec/support/test_dir_support.rb
|
103
|
+
- spec/test_dir_spec.rb
|
data/spec/fs_spec.rb
DELETED
@@ -1,196 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe FS do
|
4
|
-
|
5
|
-
describe 'touch' do
|
6
|
-
it 'touches a single file' do
|
7
|
-
file = '/foobar.txt'
|
8
|
-
FS.touch(file)
|
9
|
-
File.exist?(file).should be_true
|
10
|
-
end
|
11
|
-
|
12
|
-
it 'touches a list of files' do
|
13
|
-
files = ['/foo.txt', '/bar.txt']
|
14
|
-
FS.touch(files)
|
15
|
-
files.each do |file|
|
16
|
-
File.exist?(file).should be_true
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
describe 'makedir' do
|
22
|
-
it 'creates a dir' do
|
23
|
-
FS.makedir('/foo')
|
24
|
-
File.directory?('/foo').should be_true
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'fails if a parent dir is missing' do
|
28
|
-
lambda {FS.makedir('/foo/bar')}.should raise_error
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe 'makedirs' do
|
33
|
-
it 'creates all missing parent dirs' do
|
34
|
-
FS.makedirs '/foo/bar/baz'
|
35
|
-
File.directory?('/foo').should be_true
|
36
|
-
File.directory?('/foo/bar').should be_true
|
37
|
-
File.directory?('/foo/bar/baz').should be_true
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
describe 'list' do
|
42
|
-
it 'returns an empty list if there are no files' do
|
43
|
-
FS.list('/').should be_empty
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'lists all files and dirs (without . and ..)' do
|
47
|
-
FS.touch('/foo')
|
48
|
-
FS.makedir('/bar')
|
49
|
-
FS.list('/').should eql(['foo', 'bar'])
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'globs files and dirs' do
|
53
|
-
FS.touch('/file.txt')
|
54
|
-
FS.touch('/file.rb')
|
55
|
-
FS.makedir('/dir.txt')
|
56
|
-
FS.makedir('/dir.rb')
|
57
|
-
FS.list('/', '*.txt').should eql(['file.txt', 'dir.txt'])
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'lists files and dirs in a subdir' do
|
61
|
-
FS.makedir('/foo')
|
62
|
-
FS.makedir('/foo/dir')
|
63
|
-
FS.touch('/foo/file')
|
64
|
-
FS.list('/foo').should eql(['dir', 'file'])
|
65
|
-
end
|
66
|
-
|
67
|
-
it 'globs files and dirs in a subdir' do
|
68
|
-
FS.makedir('/foo')
|
69
|
-
FS.touch('/foo/file.txt')
|
70
|
-
FS.touch('/foo/file.rb')
|
71
|
-
FS.makedir('/foo/dir.txt')
|
72
|
-
FS.makedir('/foo/dir.rb')
|
73
|
-
FS.list('/foo', '*.txt').should eql(['dir.txt', 'file.txt'])
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
describe 'find' do
|
78
|
-
it 'returns an empty list if there are no files' do
|
79
|
-
FS.list('/').should be_empty
|
80
|
-
end
|
81
|
-
|
82
|
-
it 'finds files in all subdirs' do
|
83
|
-
FS.makedirs('/one/two/three')
|
84
|
-
FS.touch('/one/file.one')
|
85
|
-
FS.touch('/one/two/three/file.three')
|
86
|
-
FS.find('/').should eql([
|
87
|
-
'one',
|
88
|
-
'one/file.one',
|
89
|
-
'one/two',
|
90
|
-
'one/two/three',
|
91
|
-
'one/two/three/file.three'
|
92
|
-
])
|
93
|
-
end
|
94
|
-
|
95
|
-
it 'globs files in all subdirs' do
|
96
|
-
FS.makedirs('/one/two/three')
|
97
|
-
FS.touch('/one/file.one')
|
98
|
-
FS.touch('/one/two/three/file.three')
|
99
|
-
FS.find('/', 'file.*').should eql([
|
100
|
-
'one/file.one',
|
101
|
-
'one/two/three/file.three'
|
102
|
-
])
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
describe 'move' do
|
107
|
-
it 'rename a file' do
|
108
|
-
FS.touch('/foo.txt')
|
109
|
-
FS.move('/foo.txt', '/bar.txt')
|
110
|
-
FS.list('/').should eql(['bar.txt'])
|
111
|
-
end
|
112
|
-
|
113
|
-
# FIXME: fakefs
|
114
|
-
# it 'moves a file' do
|
115
|
-
# FS.touch('/foo.txt')
|
116
|
-
# FS.makedirs('/tmp')
|
117
|
-
# FS.move('/foo.txt', '/tmp')
|
118
|
-
# FS.list('/').should eql(['tmp'])
|
119
|
-
# FS.list('/tmp').should eql(['foo.txt'])
|
120
|
-
# end
|
121
|
-
|
122
|
-
# FIXME: fakefs
|
123
|
-
# it 'moves files and dirs' do
|
124
|
-
# FS.touch('/file')
|
125
|
-
# FS.makedir('/dir')
|
126
|
-
# FS.makedir('/tmp')
|
127
|
-
# FS.move('/file', '/dir', '/tmp')
|
128
|
-
# FS.list('/').should eql(['tmp'])
|
129
|
-
# FS.list('/tmp').should eql(['file', 'dir'])
|
130
|
-
# end
|
131
|
-
end
|
132
|
-
|
133
|
-
describe 'remove' do
|
134
|
-
it 'removes files and dirs' do
|
135
|
-
FS.touch('/file')
|
136
|
-
FS.makedir('/dir')
|
137
|
-
FS.remove('/file', '/dir')
|
138
|
-
FS.list('/').should be_empty
|
139
|
-
end
|
140
|
-
|
141
|
-
# FIXME: fakefs
|
142
|
-
# it 'fails if the dir is not empty' do
|
143
|
-
# FS.makedir('/foo')
|
144
|
-
# FS.touch('/foo/bar')
|
145
|
-
# lambda {FS.remove('/foo')}.should raise_error
|
146
|
-
# end
|
147
|
-
end
|
148
|
-
|
149
|
-
describe 'write' do
|
150
|
-
it 'writes content from a string' do
|
151
|
-
FS.write('/foo.txt', 'bar')
|
152
|
-
File.open('/foo.txt').read.should eql('bar')
|
153
|
-
end
|
154
|
-
|
155
|
-
it 'writes content from a block' do
|
156
|
-
FS.write('/foo.txt') {|f| f.write 'bar' }
|
157
|
-
File.open('/foo.txt').read.should eql('bar')
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
describe 'read' do
|
162
|
-
it 'reads the content to a string' do
|
163
|
-
File.open('/foo.txt', 'w') {|f| f.write 'bar' }
|
164
|
-
FS.read('/foo.txt').should eql('bar')
|
165
|
-
end
|
166
|
-
|
167
|
-
it 'reads the content to a block' do
|
168
|
-
File.open('/foo.txt', 'w') {|f| f.write 'bar' }
|
169
|
-
FS.read('/foo.txt') {|f| f.read.should eql('bar')}
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
describe 'root' do
|
174
|
-
it 'always returns /' do
|
175
|
-
FS.root.should eql('/')
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
describe 'home' do
|
180
|
-
it 'returns the home of the current user' do
|
181
|
-
FS.home.should eql('/Users/me')
|
182
|
-
end
|
183
|
-
|
184
|
-
it 'returns the home of another user' do
|
185
|
-
FS.home('you').should eql('/Users/you')
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
# describe 'removedir'
|
190
|
-
# describe 'removedirs'
|
191
|
-
# describe 'currentdir'
|
192
|
-
# describe 'changedir'
|
193
|
-
# describe 'copy'
|
194
|
-
# describe 'link'
|
195
|
-
|
196
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'fakefs/spec_helpers'
|
2
|
-
|
3
|
-
RSpec.configure do |config|
|
4
|
-
config.include FakeFS::SpecHelpers
|
5
|
-
end
|
6
|
-
|
7
|
-
# FileUtils.mkdir is not handled
|
8
|
-
# https://github.com/defunkt/fakefs/issues/closed#issue/37
|
9
|
-
#
|
10
|
-
# Patch:
|
11
|
-
# https://github.com/flavio/fakefs/blob/ea22773bc293ea3bce97317086b8171669780eab/lib/fakefs/fileutils.rb
|
12
|
-
module FakeFS
|
13
|
-
module FileUtils
|
14
|
-
extend self
|
15
|
-
def mkdir(path)
|
16
|
-
parent = path.split('/')
|
17
|
-
parent.pop
|
18
|
-
raise Errno::ENOENT, "No such file or directory - #{path}" unless parent.join == "" || FileSystem.find(parent.join('/'))
|
19
|
-
raise Errno::EEXIST, "File exists - #{path}" if FileSystem.find(path)
|
20
|
-
FileSystem.add(path, FakeDir.new)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
|
26
|
-
module FakeFS
|
27
|
-
class Dir
|
28
|
-
def self.home(user=nil)
|
29
|
-
"/Users/#{user || 'me'}"
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|