given_filesystem 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/MIT-LICENSE +19 -0
- data/README.md +188 -0
- data/lib/given_filesystem/spec_helpers.rb +77 -0
- data/lib/given_filesystem.rb +110 -0
- data/spec/data/testcontent +1 -0
- data/spec/data/welcome/universe +1 -0
- data/spec/given_filesystem_spec.rb +158 -0
- data/spec/given_filesystem_spec_helpers_spec.rb +108 -0
- data/spec/spec_helper.rb +1 -0
- metadata +74 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.rspec
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2014 Cornelius Schumacher <schumacher@kde.org>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
# GivenFilesystem
|
2
|
+
|
3
|
+
GivenFilesystem is a set of helpers for testing code which operates on file
|
4
|
+
systems. It lets you create temporary directories and files with given content as
|
5
|
+
test data. You can write to these directories and GivenFilesystem takes care of
|
6
|
+
cleaning up after the test. It only assumes that you can set the path to your
|
7
|
+
file system data in your tests.
|
8
|
+
|
9
|
+
GivenFilesystem provides helpers for RSpec and a standalone class.
|
10
|
+
|
11
|
+
## Usage with RSpec
|
12
|
+
|
13
|
+
### Setup
|
14
|
+
|
15
|
+
To use the GivenFilesystem helpers in a RSpec test you have to include the
|
16
|
+
`GivenFileSystemSpecHelpers` module to get access to the helper methods, and
|
17
|
+
set up the temporary test directory by calling `use_given_filesystem`:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
include GivenFileSystemSpecHelpers
|
21
|
+
|
22
|
+
describe "some test" do
|
23
|
+
use_given_filesystem
|
24
|
+
|
25
|
+
it "tests something" do
|
26
|
+
...
|
27
|
+
end
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
The activation with `use_given_filesystem` gives you fine-grained control
|
32
|
+
about in which scope the test directories are created. It takes a parameter to
|
33
|
+
optionally keep the created test files around:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
use_given_filesystem( :keep_files => true )
|
37
|
+
```
|
38
|
+
|
39
|
+
This can be useful when debugging tests.
|
40
|
+
|
41
|
+
|
42
|
+
### File fixtures
|
43
|
+
|
44
|
+
With GivenFilesystem you can create directories and files with pre-defined
|
45
|
+
content. These can be used as test fixtures for code reading from a file
|
46
|
+
system.
|
47
|
+
|
48
|
+
File fixtures, i.e. files containing test content, are stored in the
|
49
|
+
`spec/data/` directory. GivenFilesystem provides helper functions to create
|
50
|
+
temporary directories with defined structure and content using the file
|
51
|
+
fixtures. The file names can be taken directly from the name of the file
|
52
|
+
fixtures or be defined when creating the test directory structure.
|
53
|
+
|
54
|
+
|
55
|
+
### Creating directories
|
56
|
+
|
57
|
+
Create a temporary directory for writing test data:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
path = given_directory
|
61
|
+
```
|
62
|
+
|
63
|
+
Use the returned path to access the directory.
|
64
|
+
|
65
|
+
Create an empty temporary directory with a given name for writing test data:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
path = given_directory "myname"
|
69
|
+
```
|
70
|
+
|
71
|
+
Create a temporary directory with a given from data you provide in the
|
72
|
+
directory `spec/data` of your project:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
path = given_directory_from_data "myname"
|
76
|
+
```
|
77
|
+
|
78
|
+
This copies the directory `spec/data/myname` to a temporary test directory
|
79
|
+
and returns the path to the `myname` directory.
|
80
|
+
|
81
|
+
You can also create the directory under a different name:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
path = given_directory_from_data "myothername", :from => "myname"
|
85
|
+
```
|
86
|
+
|
87
|
+
This will copy the same data, but put it into a directory named `myothername`.
|
88
|
+
|
89
|
+
|
90
|
+
### Creating files
|
91
|
+
|
92
|
+
Create a temporary file with arbitrary content:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
path = given_dummy_file
|
96
|
+
```
|
97
|
+
|
98
|
+
Use the returned path to access the file.
|
99
|
+
|
100
|
+
Create a temporary file with given content taken from a file fixture:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
path = given_file "myfixture"
|
104
|
+
```
|
105
|
+
|
106
|
+
The content of the file is taken from a file fixture stored in your `spec/data`
|
107
|
+
directory. The name of the created file is the same as the name of the file
|
108
|
+
containing the test content.
|
109
|
+
|
110
|
+
Create a temporary file with given content under a different name:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
path = given_file "myspecialfile", :from => "myfixture"
|
114
|
+
```
|
115
|
+
|
116
|
+
The content of the file is taken from the file `spec/data/myfixture` and stored
|
117
|
+
under the name `myspecialfile` in a temporary directory. You can access it under
|
118
|
+
the returned path.
|
119
|
+
|
120
|
+
|
121
|
+
### Creating structures of directories and files
|
122
|
+
|
123
|
+
You can combine `given_directory` and `given_file` to create file system
|
124
|
+
structures as input for you tests. Here is an example:
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
path = given_directory "mydir" do
|
128
|
+
given_directory "one" do
|
129
|
+
given_file "myfile"
|
130
|
+
given_file "myotherfile"
|
131
|
+
end
|
132
|
+
given_directory "two" do
|
133
|
+
given_file "myfile2", :from => "myfile"
|
134
|
+
end
|
135
|
+
end
|
136
|
+
```
|
137
|
+
|
138
|
+
This will create the following file system structure and return the path to the
|
139
|
+
temporary directory `mydir`, which you can then use in your tests to access the
|
140
|
+
data:
|
141
|
+
|
142
|
+
```
|
143
|
+
/tmp/
|
144
|
+
given_filesystem/
|
145
|
+
3845-20140216-74592/
|
146
|
+
mydir/
|
147
|
+
one/
|
148
|
+
myfile
|
149
|
+
myotherfile
|
150
|
+
two/
|
151
|
+
myfile2
|
152
|
+
```
|
153
|
+
|
154
|
+
### Using directory structures in name parameters
|
155
|
+
|
156
|
+
All directory or file name parameters can contain additional directory
|
157
|
+
structure. The required nested directories are automatically created. For
|
158
|
+
example the call
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
given_directory( "a/b/c" )
|
162
|
+
```
|
163
|
+
|
164
|
+
will create the following structure:
|
165
|
+
|
166
|
+
```
|
167
|
+
/tmp/
|
168
|
+
given_filesystem/
|
169
|
+
7373-2014-03-15-98223/
|
170
|
+
a/
|
171
|
+
b/
|
172
|
+
c/
|
173
|
+
```
|
174
|
+
|
175
|
+
and return the path to the directory `a`.
|
176
|
+
|
177
|
+
## License
|
178
|
+
|
179
|
+
You may use GivenFilesystem under the terms of the MIT license.
|
180
|
+
|
181
|
+
## Contact
|
182
|
+
|
183
|
+
If you have questions or comments about GivenFilesystem don't hesitate to get in
|
184
|
+
touch with [Cornelius Schumacher](mailto:schumacher@kde.org).
|
185
|
+
|
186
|
+
## Thanks
|
187
|
+
|
188
|
+
Thanks to David Majda for reviewing code and documentation.
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# Copyright (c) 2014 Cornelius Schumacher <schumacher@kde.org>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require_relative "../given_filesystem"
|
22
|
+
|
23
|
+
module GivenFilesystemSpecHelpers
|
24
|
+
def self.included(example_group)
|
25
|
+
example_group.extend(self)
|
26
|
+
end
|
27
|
+
|
28
|
+
def use_given_filesystem options = {}
|
29
|
+
before do
|
30
|
+
@__given_filesystem = GivenFilesystem.new
|
31
|
+
end
|
32
|
+
|
33
|
+
if !options[:keep_files]
|
34
|
+
after do
|
35
|
+
@__given_filesystem.cleanup
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def given_directory directory_name = nil
|
41
|
+
check_initialization
|
42
|
+
if block_given?
|
43
|
+
path = @__given_filesystem.directory directory_name do
|
44
|
+
yield
|
45
|
+
end
|
46
|
+
else
|
47
|
+
path = @__given_filesystem.directory directory_name
|
48
|
+
end
|
49
|
+
path
|
50
|
+
end
|
51
|
+
|
52
|
+
def given_directory_from_data directory_name, options = {}
|
53
|
+
check_initialization
|
54
|
+
path = @__given_filesystem.directory_from_data directory_name, options[:from]
|
55
|
+
end
|
56
|
+
|
57
|
+
def given_file file_name, options = {}
|
58
|
+
check_initialization
|
59
|
+
if !options[:from]
|
60
|
+
options[:from] = file_name
|
61
|
+
end
|
62
|
+
@__given_filesystem.file file_name, options
|
63
|
+
end
|
64
|
+
|
65
|
+
def given_dummy_file file_name = nil
|
66
|
+
check_initialization
|
67
|
+
@__given_filesystem.file file_name
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def check_initialization
|
73
|
+
if !@__given_filesystem
|
74
|
+
raise "Call use_given_filesystem before calling other methods"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# Copyright (c) 2014 Cornelius Schumacher <schumacher@kde.org>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require "tmpdir"
|
22
|
+
|
23
|
+
class GivenFilesystem
|
24
|
+
def initialize
|
25
|
+
@path_elements = [ Dir.tmpdir, "given_filesystem" ]
|
26
|
+
@base_paths = Array.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def cleanup
|
30
|
+
@base_paths.each do |base_path|
|
31
|
+
# Better safe than sorry, so do sanity check on path before removing it
|
32
|
+
if base_path =~ /given_filesystem/
|
33
|
+
FileUtils.rm_r base_path
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def directory dir_name = nil
|
39
|
+
create_random_base_path unless path_has_base?
|
40
|
+
|
41
|
+
@path_elements.push dir_name || random_name
|
42
|
+
|
43
|
+
created_path = path
|
44
|
+
FileUtils.mkdir_p created_path
|
45
|
+
yield if block_given?
|
46
|
+
@path_elements.pop
|
47
|
+
created_path
|
48
|
+
end
|
49
|
+
|
50
|
+
def directory_from_data to, from = nil
|
51
|
+
from ||= to
|
52
|
+
|
53
|
+
create_random_base_path unless path_has_base?
|
54
|
+
|
55
|
+
FileUtils.mkdir_p path
|
56
|
+
@path_elements.push to
|
57
|
+
FileUtils.cp_r test_data_path(from), path
|
58
|
+
path
|
59
|
+
end
|
60
|
+
|
61
|
+
def file file_name = nil, options = {}
|
62
|
+
create_random_base_path unless path_has_base?
|
63
|
+
|
64
|
+
if file_name
|
65
|
+
@path_elements.push file_name
|
66
|
+
else
|
67
|
+
@path_elements.push random_name
|
68
|
+
end
|
69
|
+
|
70
|
+
FileUtils.mkdir_p File.dirname(path)
|
71
|
+
|
72
|
+
created_path = path
|
73
|
+
File.open(created_path,"w") do |file|
|
74
|
+
if options[:from]
|
75
|
+
test_data = test_data_path(options[:from])
|
76
|
+
if !File.exists? test_data
|
77
|
+
raise "Test data file '#{test_data}' doesn't exist"
|
78
|
+
end
|
79
|
+
file.write File.read(test_data)
|
80
|
+
else
|
81
|
+
file.puts "GivenFilesystem was here"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
@path_elements.pop
|
85
|
+
created_path
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def create_random_base_path
|
91
|
+
@path_elements.push random_name
|
92
|
+
@base_paths.push path
|
93
|
+
end
|
94
|
+
|
95
|
+
def random_name
|
96
|
+
"#{Process.pid}-#{Time.now.strftime("%Y%m%d")}-#{rand(99999).to_s}"
|
97
|
+
end
|
98
|
+
|
99
|
+
def path
|
100
|
+
@path_elements.join("/")
|
101
|
+
end
|
102
|
+
|
103
|
+
def path_has_base?
|
104
|
+
@path_elements.count > 2
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_data_path name
|
108
|
+
File.expand_path('spec/data/' + name)
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
This is my test content.
|
@@ -0,0 +1 @@
|
|
1
|
+
I was here
|
@@ -0,0 +1,158 @@
|
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe GivenFilesystem do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@given = GivenFilesystem.new
|
7
|
+
end
|
8
|
+
|
9
|
+
after(:each) do
|
10
|
+
@given.cleanup
|
11
|
+
end
|
12
|
+
|
13
|
+
it "creates directory" do
|
14
|
+
path = @given.directory
|
15
|
+
expect( File.exists? path ).to be_true
|
16
|
+
expect( File.directory? path ).to be_true
|
17
|
+
expect( path ).to match /tmp/
|
18
|
+
expect( path.split("/").length).to be > 3
|
19
|
+
end
|
20
|
+
|
21
|
+
it "creates nested unnamed directories" do
|
22
|
+
nested_path = nil
|
23
|
+
path = @given.directory do
|
24
|
+
nested_path = @given.directory
|
25
|
+
end
|
26
|
+
expect( File.exists? nested_path ).to be_true
|
27
|
+
expect( nested_path.split("/").count ).to eq path.split("/").count + 1
|
28
|
+
end
|
29
|
+
|
30
|
+
it "creates named directory" do
|
31
|
+
path = @given.directory "abc"
|
32
|
+
expect( File.exists? path ).to be_true
|
33
|
+
expect( path ).to match /tmp/
|
34
|
+
expect( path.split("/").length).to be > 4
|
35
|
+
expect( path ).to match /abc$/
|
36
|
+
end
|
37
|
+
|
38
|
+
it "creates named directory including path" do
|
39
|
+
path = @given.directory "abc"
|
40
|
+
deep_path = @given.directory "x/y/z"
|
41
|
+
expect( File.exists? deep_path ).to be_true
|
42
|
+
expect( File.directory? deep_path ).to be_true
|
43
|
+
expect( deep_path.split("/").count ).to eq path.split("/").count + 2
|
44
|
+
end
|
45
|
+
|
46
|
+
it "creates file" do
|
47
|
+
path = @given.file
|
48
|
+
expect( path ).to match /tmp/
|
49
|
+
expect( path.split("/").length).to be > 3
|
50
|
+
expect( File.exists? path ).to be_true
|
51
|
+
expect( File.directory? path ).to be_false
|
52
|
+
end
|
53
|
+
|
54
|
+
it "creates named file" do
|
55
|
+
path = @given.file "def"
|
56
|
+
expect( path ).to match /tmp/
|
57
|
+
expect( path.split("/").length).to be > 4
|
58
|
+
expect( path ).to match /def$/
|
59
|
+
end
|
60
|
+
|
61
|
+
it "creates named file including path" do
|
62
|
+
path = @given.file "def"
|
63
|
+
deep_path = @given.file "x/y/z"
|
64
|
+
expect( File.exists? deep_path ).to be_true
|
65
|
+
expect( File.directory? deep_path ).to be_false
|
66
|
+
expect( deep_path.split("/").count ).to eq path.split("/").count + 2
|
67
|
+
end
|
68
|
+
|
69
|
+
it "throws error on invalid test data file name" do
|
70
|
+
expect{@given.file "def", :from => "invalidname"}.to raise_error
|
71
|
+
end
|
72
|
+
|
73
|
+
it "creates file with content" do
|
74
|
+
path = @given.file "def", :from => "testcontent"
|
75
|
+
expect( path ).to match /tmp/
|
76
|
+
expect( path.split("/").length).to be > 4
|
77
|
+
expect( path ).to match /def$/
|
78
|
+
expect( File.read(path) ).to eq "This is my test content.\n"
|
79
|
+
end
|
80
|
+
|
81
|
+
it "creates directory tree" do
|
82
|
+
path = @given.directory do
|
83
|
+
@given.directory "one" do
|
84
|
+
@given.file "first"
|
85
|
+
end
|
86
|
+
@given.directory "two" do
|
87
|
+
@given.file "second"
|
88
|
+
@given.file "third"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
expect( File.exists? path).to be_true
|
93
|
+
expect( File.directory? path).to be_true
|
94
|
+
expect( File.exists? File.join(path,"one")).to be_true
|
95
|
+
expect( File.exists? File.join(path,"one")).to be_true
|
96
|
+
expect( File.directory? File.join(path,"one")).to be_true
|
97
|
+
expect( File.directory? File.join(path,"two")).to be_true
|
98
|
+
expect( File.exists? File.join(path,"one","first")).to be_true
|
99
|
+
expect( File.exists? File.join(path,"two","second")).to be_true
|
100
|
+
expect( File.exists? File.join(path,"two","third")).to be_true
|
101
|
+
end
|
102
|
+
|
103
|
+
it "creates directory from data" do
|
104
|
+
path = @given.directory_from_data( "welcome" )
|
105
|
+
expect( path ).to match /\/welcome$/
|
106
|
+
expect( File.exists? path ).to be_true
|
107
|
+
expect( File.directory? path ).to be_true
|
108
|
+
|
109
|
+
expect( File.exist? File.join( path, "universe" ) ).to be_true
|
110
|
+
expect( File.directory? File.join( path, "universe" ) ).to be_false
|
111
|
+
expect( File.read( File.join( path, "universe" ) ) ).to eq "I was here\n"
|
112
|
+
|
113
|
+
expect( File.exist? File.join( path, "space" ) ).to be_true
|
114
|
+
expect( File.directory? File.join( path, "space" ) ).to be_true
|
115
|
+
end
|
116
|
+
|
117
|
+
it "creates directory from data under different name" do
|
118
|
+
path = @given.directory_from_data( "hi", "welcome" )
|
119
|
+
expect( path ).to match /\/hi$/
|
120
|
+
expect( File.exists? path ).to be_true
|
121
|
+
expect( File.directory? path ).to be_true
|
122
|
+
|
123
|
+
expect( File.exist? File.join( path, "universe" ) ).to be_true
|
124
|
+
expect( File.directory? File.join( path, "universe" ) ).to be_false
|
125
|
+
expect( File.read( File.join( path, "universe" ) ) ).to eq "I was here\n"
|
126
|
+
|
127
|
+
expect( File.exist? File.join( path, "space" ) ).to be_true
|
128
|
+
expect( File.directory? File.join( path, "space" ) ).to be_true
|
129
|
+
end
|
130
|
+
|
131
|
+
it "returns paths" do
|
132
|
+
path1 = @given.directory "one"
|
133
|
+
expect( path1 ).to match /^\/tmp\/given_filesystem\/[\d-]+\/one$/
|
134
|
+
|
135
|
+
path2 = @given.directory "two"
|
136
|
+
expect( path2 ).to match /^\/tmp\/given_filesystem\/[\d-]+\/two$/
|
137
|
+
|
138
|
+
path3 = @given.directory "three" do
|
139
|
+
@given.file "first"
|
140
|
+
end
|
141
|
+
expect( path3 ).to match /^\/tmp\/given_filesystem\/[\d-]+\/three$/
|
142
|
+
end
|
143
|
+
|
144
|
+
it "cleans up directory tree" do
|
145
|
+
given = GivenFilesystem.new
|
146
|
+
path1 = given.directory
|
147
|
+
path2 = given.directory
|
148
|
+
|
149
|
+
expect( File.exists? path1).to be_true
|
150
|
+
expect( File.exists? path2).to be_true
|
151
|
+
|
152
|
+
given.cleanup
|
153
|
+
|
154
|
+
expect( File.exists? path1).to be_false
|
155
|
+
expect( File.exists? path2).to be_false
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe GivenFilesystem do
|
4
|
+
|
5
|
+
include GivenFilesystemSpecHelpers
|
6
|
+
|
7
|
+
context "not initialized" do
|
8
|
+
describe "#given_directory" do
|
9
|
+
it "raises error" do
|
10
|
+
expect{ given_directory }.to raise_error /given_filesystem/
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#given_file" do
|
15
|
+
it "raises error" do
|
16
|
+
expect{ given_directory }.to raise_error /given_filesystem/
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "with keeping of files enabled" do
|
22
|
+
use_given_filesystem :keep_files => true
|
23
|
+
|
24
|
+
it "creates directories" do
|
25
|
+
path = given_directory "hello"
|
26
|
+
expect( File.exists? path ).to be_true
|
27
|
+
|
28
|
+
# Manually clean up, so test doesn't leave files around
|
29
|
+
@__given_filesystem.cleanup
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "using the module" do
|
34
|
+
use_given_filesystem
|
35
|
+
|
36
|
+
describe "#given_directory" do
|
37
|
+
it "creates unnamed directory" do
|
38
|
+
path = given_directory
|
39
|
+
expect( File.exists? path ).to be_true
|
40
|
+
expect( File.directory? path ).to be_true
|
41
|
+
end
|
42
|
+
|
43
|
+
it "creates directory" do
|
44
|
+
path = given_directory "hello"
|
45
|
+
expect( path ).to match /\/hello$/
|
46
|
+
end
|
47
|
+
|
48
|
+
it "creates nested directory" do
|
49
|
+
path = nil
|
50
|
+
given_directory "hello" do
|
51
|
+
path = given_directory "world"
|
52
|
+
end
|
53
|
+
expect( path ).to match /\/hello\/world$/
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "#give_directory_from_data" do
|
58
|
+
it "creates directory with content from data" do
|
59
|
+
path = given_directory_from_data "welcome"
|
60
|
+
expect( path ).to match /\/welcome$/
|
61
|
+
end
|
62
|
+
|
63
|
+
it "creates directory with content from named data" do
|
64
|
+
path = given_directory_from_data "hi", :from => "welcome"
|
65
|
+
expect( path ).to match /\/hi$/
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "#given_dummy_file" do
|
70
|
+
it "creates unnamed dummy file" do
|
71
|
+
path = given_dummy_file
|
72
|
+
expect( File.exists? path ).to be_true
|
73
|
+
expect( File.directory? path ).to be_false
|
74
|
+
end
|
75
|
+
|
76
|
+
it "creates named dummy file" do
|
77
|
+
path = given_dummy_file "welcome"
|
78
|
+
expect( path ).to match /\/welcome$/
|
79
|
+
expect( File.exists? path ).to be_true
|
80
|
+
expect( File.directory? path ).to be_false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "#given_file" do
|
85
|
+
it "creates file with content" do
|
86
|
+
path = given_file "testcontent"
|
87
|
+
expect( path ).to match /\/testcontent$/
|
88
|
+
expect( File.read( path ) ).to eq "This is my test content.\n"
|
89
|
+
end
|
90
|
+
|
91
|
+
it "creates file with content and given filename" do
|
92
|
+
path = given_file "welcome", :from => "testcontent"
|
93
|
+
expect( path ).to match /\/welcome$/
|
94
|
+
expect( File.read( path ) ).to eq "This is my test content.\n"
|
95
|
+
end
|
96
|
+
|
97
|
+
it "creates file in directory" do
|
98
|
+
path = nil
|
99
|
+
given_directory "hello" do
|
100
|
+
path = given_file "world", :from => "testcontent"
|
101
|
+
end
|
102
|
+
expect( File.exists? path ).to be_true
|
103
|
+
expect( File.read( path ) ).to eq "This is my test content.\n"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative('../lib/given_filesystem/spec_helpers')
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: given_filesystem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Cornelius Schumacher
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-04-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: GivenFilesystem is a set of helpers for testing code which operates on
|
31
|
+
file systems.
|
32
|
+
email:
|
33
|
+
- schumacher@kde.org
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- .rspec
|
40
|
+
- MIT-LICENSE
|
41
|
+
- README.md
|
42
|
+
- lib/given_filesystem.rb
|
43
|
+
- lib/given_filesystem/spec_helpers.rb
|
44
|
+
- spec/data/testcontent
|
45
|
+
- spec/data/welcome/universe
|
46
|
+
- spec/given_filesystem_spec.rb
|
47
|
+
- spec/given_filesystem_spec_helpers_spec.rb
|
48
|
+
- spec/spec_helper.rb
|
49
|
+
homepage: http://github.com/cornelius/given_filesystem
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 1.3.6
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project: given_filesystem
|
70
|
+
rubygems_version: 1.8.23
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: A library for setting up files as test data
|
74
|
+
test_files: []
|