filetesthelper 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +43 -0
- data/Rakefile.rb +20 -60
- data/VERSION.yml +4 -0
- data/filetesthelper.gemspec +49 -0
- data/lib/file_test_helper.rb +81 -0
- data/spec/file_test_helper_spec.rb +123 -0
- data/spec/spec.opts +4 -3
- metadata +34 -17
- data/README.txt +0 -27
- data/lib/filetesthelper.rb +0 -55
- data/spec/FileTestHelper_spec.rb +0 -62
data/README.rdoc
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
= FileTestHelper
|
2
|
+
|
3
|
+
A simple helper aimed at reducing the setup effort needed to create directories, files and file content in the scope of an integration test case.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
sudo gem install filetesthelper
|
7
|
+
|
8
|
+
== Usage
|
9
|
+
|
10
|
+
require 'rubygems'
|
11
|
+
require 'filetesthelper'
|
12
|
+
include FileTestHelper
|
13
|
+
|
14
|
+
...
|
15
|
+
|
16
|
+
def test_some_code_that_needs_to_manipulate_some_files
|
17
|
+
#Let's say that the current directory here is X
|
18
|
+
|
19
|
+
with_files('dir1/file1' => 'this is file1 content', 'dir1/file2' => 'this is file2 content') do
|
20
|
+
#Now the current directory changed to Y which is a new directory
|
21
|
+
#created under Dir.tmpdir containing only 'dir1/file1' and 'dir1/file2'.
|
22
|
+
|
23
|
+
#Put some test code here.
|
24
|
+
end
|
25
|
+
|
26
|
+
#When we finish we are back at directory X and the Y directory is deleted with all its contents
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_some_code_that_uses_a_base_directory_other_than_the_temp_dir
|
30
|
+
#Let's say that the current directory here is X
|
31
|
+
|
32
|
+
with_files_in_directory('existent_dir', 'dir1/file1' => 'this is file1 content', 'dir1/file2' => 'this is file2 content') do
|
33
|
+
#Now the current directory changed to existent_dir which is some existent directory under your system, absolute paths are allowed.
|
34
|
+
#The directory contents are 'dir1/file1' and 'dir1/file2' plus anything that existed previously.
|
35
|
+
|
36
|
+
#Put some test code here.
|
37
|
+
end
|
38
|
+
|
39
|
+
#When we finish we are back at directory X and the existent_dir directory has our two test files removed
|
40
|
+
end
|
41
|
+
|
42
|
+
See the {specs}[http://filetesthelper.rubyforge.org/specs.html] and the {Rubyforge project homepage}[http://rubyforge.org/projects/filetesthelper] for more details.
|
43
|
+
|
data/Rakefile.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# coding: utf-8
|
1
2
|
require 'rubygems'
|
2
3
|
require 'rubygems/gem_runner'
|
3
4
|
require 'rake'
|
@@ -5,55 +6,26 @@ require "rake/clean"
|
|
5
6
|
require 'rake/rdoctask'
|
6
7
|
require 'rake/gempackagetask'
|
7
8
|
require 'spec/rake/spectask'
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
s.email = "filetesthelper-devel@rubyforge.org"
|
27
|
-
s.homepage = "http://rubyforge.org/projects/filetesthelper/"
|
28
|
-
s.platform = Gem::Platform::RUBY
|
29
|
-
s.summary = "A simple helper aimed at reducing the setup effort needed to create directories, files and file content in integration test cases."
|
30
|
-
s.description = s.summary
|
31
|
-
s.files = Dir["{spec,lib}/**/*"] + [README, "Rakefile.rb"]
|
32
|
-
s.has_rdoc = true
|
33
|
-
s.extra_rdoc_files = [README]
|
34
|
-
s.require_path = 'lib'
|
35
|
-
s.autorequire = NAME
|
36
|
-
# s.require_paths << ['lib']
|
9
|
+
|
10
|
+
begin
|
11
|
+
require 'jeweler'
|
12
|
+
Jeweler::Tasks.new do |gemspec|
|
13
|
+
gemspec.name = "filetesthelper"
|
14
|
+
gemspec.summary = "A simple helper aimed at reducing the setup effort needed to create directories, files and file content in integration test cases."
|
15
|
+
gemspec.email = "dcadenas@gmail.com"
|
16
|
+
gemspec.homepage = "http://rubyforge.org/projects/filetesthelper/"
|
17
|
+
gemspec.description = gemspec.summary
|
18
|
+
gemspec.authors = ["Daniel Cadenas Nión"]
|
19
|
+
gemspec.version = '1.0.1'
|
20
|
+
gemspec.platform = Gem::Platform::RUBY
|
21
|
+
#gemspec.files = Dir["{spec,lib}/**/*"] + [README, "Rakefile.rb"]
|
22
|
+
gemspec.require_path = 'lib'
|
23
|
+
gemspec.autorequire = gemspec.name
|
24
|
+
end
|
25
|
+
rescue LoadError
|
26
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
37
27
|
end
|
38
28
|
|
39
|
-
Rake::GemPackageTask.new(spec) do |pkg|
|
40
|
-
pkg.gem_spec = spec
|
41
|
-
end
|
42
|
-
|
43
|
-
desc "Run :package and install the resulting .gem"
|
44
|
-
task :install => [:clean, :specs_with_rcov, :rdoc, :package] do
|
45
|
-
Gem::GemRunner.new.run(['install', "pkg/#{NAME}-#{PKGVERSION}.gem", '--no-rdoc', '--no-ri'])
|
46
|
-
end
|
47
|
-
|
48
|
-
desc "Run :clean and uninstall the .gem"
|
49
|
-
task :uninstall => :clean do
|
50
|
-
Gem::GemRunner.new.run(['uninstall', NAME])
|
51
|
-
end
|
52
|
-
|
53
|
-
|
54
|
-
##############################################################################
|
55
|
-
# rSpec & rcov
|
56
|
-
##############################################################################
|
57
29
|
|
58
30
|
Spec::Rake::SpecTask.new('specs_with_rcov') do |t|
|
59
31
|
t.ruby_opts << '-rubygems'
|
@@ -63,16 +35,4 @@ Spec::Rake::SpecTask.new('specs_with_rcov') do |t|
|
|
63
35
|
t.rcov_opts = ['--text-summary']
|
64
36
|
end
|
65
37
|
|
66
|
-
|
67
|
-
##############################################################################
|
68
|
-
# Documentation
|
69
|
-
##############################################################################
|
70
|
-
|
71
|
-
Rake::RDocTask.new(:rdoc) do |rd|
|
72
|
-
rd.rdoc_files = [README] + Dir['lib/**/*']
|
73
|
-
rd.rdoc_dir = 'doc'
|
74
|
-
rd.title = NAME
|
75
|
-
rd.options << "-SNm#{README}"
|
76
|
-
end
|
77
|
-
|
78
|
-
task :default => [:specs_with_rcov, :rdoc]
|
38
|
+
task :default => [:specs_with_rcov]
|
data/VERSION.yml
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile.rb, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{filetesthelper}
|
8
|
+
s.version = "1.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Daniel Cadenas Ni\303\263n"]
|
12
|
+
s.autorequire = %q{filetesthelper}
|
13
|
+
s.date = %q{2010-08-24}
|
14
|
+
s.description = %q{A simple helper aimed at reducing the setup effort needed to create directories, files and file content in integration test cases.}
|
15
|
+
s.email = %q{dcadenas@gmail.com}
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"README.rdoc",
|
21
|
+
"Rakefile.rb",
|
22
|
+
"VERSION.yml",
|
23
|
+
"filetesthelper.gemspec",
|
24
|
+
"lib/file_test_helper.rb",
|
25
|
+
"spec/file_test_helper_spec.rb",
|
26
|
+
"spec/spec.opts",
|
27
|
+
"spec/spec_helper.rb"
|
28
|
+
]
|
29
|
+
s.homepage = %q{http://rubyforge.org/projects/filetesthelper/}
|
30
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
31
|
+
s.require_paths = ["lib"]
|
32
|
+
s.rubygems_version = %q{1.3.7}
|
33
|
+
s.summary = %q{A simple helper aimed at reducing the setup effort needed to create directories, files and file content in integration test cases.}
|
34
|
+
s.test_files = [
|
35
|
+
"spec/file_test_helper_spec.rb",
|
36
|
+
"spec/spec_helper.rb"
|
37
|
+
]
|
38
|
+
|
39
|
+
if s.respond_to? :specification_version then
|
40
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
44
|
+
else
|
45
|
+
end
|
46
|
+
else
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'tmpdir'
|
2
|
+
|
3
|
+
module FileTestHelper
|
4
|
+
include FileUtils
|
5
|
+
|
6
|
+
#Specify the files you need to create in a hash.
|
7
|
+
#The keys of the hash represent the file path and the value represents it's content.
|
8
|
+
#Examples:
|
9
|
+
#* {'a dir/a file' => 'content'} creates the 'a dir' directory containing an 'a file' file which content is 'content'.
|
10
|
+
#* {'another dir/' => ''} creates an empty 'another dir' directory. The value must be an empty string.
|
11
|
+
#Optionally you can specify a different base directory (other than the default tmp) to use as a base directory.
|
12
|
+
#Example:
|
13
|
+
#* 'stylesheets', 'file.txt' => "" will create the file.txt file inside the existent stylesheets directory. The stylesheets directory won't be deleted
|
14
|
+
def with_files(files_with_contents = {})
|
15
|
+
with_files_in_directory(nil, files_with_contents) do
|
16
|
+
yield
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def with_files_in_directory(base_dir, files_with_contents)
|
21
|
+
initial_directory = current_directory
|
22
|
+
raise ArgumentError, "The base directory '#{base_dir}' does not exist." if base_dir && !File.exist?(base_dir)
|
23
|
+
working_directory = base_dir || create_working_directory
|
24
|
+
|
25
|
+
begin
|
26
|
+
create_files_in_working_directory(working_directory, files_with_contents)
|
27
|
+
yield
|
28
|
+
ensure
|
29
|
+
cd initial_directory
|
30
|
+
if base_dir.nil?
|
31
|
+
remove_dir(working_directory) if File.exist?(working_directory)
|
32
|
+
else
|
33
|
+
remove_files(base_dir, files_with_contents)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
def current_directory
|
40
|
+
Dir.pwd
|
41
|
+
end
|
42
|
+
|
43
|
+
def create_working_directory
|
44
|
+
working_directory = File.join(Dir.tmpdir, "__test_dir__#{Process.pid}")
|
45
|
+
mkpath(working_directory)
|
46
|
+
return working_directory
|
47
|
+
end
|
48
|
+
|
49
|
+
def create_files_in_working_directory(working_directory, files_with_contents)
|
50
|
+
cd working_directory
|
51
|
+
|
52
|
+
return unless files_with_contents
|
53
|
+
files_with_contents.each do |path, file_contents|
|
54
|
+
raise ArgumentError, 'A path is not allowed to start with /' if path =~ /^\//
|
55
|
+
raise ArgumentError, 'A path is not allowed to contain ..' if path =~ /\.\./
|
56
|
+
|
57
|
+
dir, file = path.scan(/(.*[\/])?([^\/]*$)/)[0]
|
58
|
+
mkpath dir unless(dir.nil? or dir.empty?)
|
59
|
+
|
60
|
+
unless(file.nil? or file.empty?)
|
61
|
+
File.open(path, 'w') do |f|
|
62
|
+
f << file_contents unless file_contents == nil
|
63
|
+
end
|
64
|
+
else
|
65
|
+
raise ArgumentError, 'File content can only be set to files' unless file_contents.nil? or file_contents.empty?
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def remove_files(base_dir, files_with_contents)
|
71
|
+
initial_dir = Dir.pwd
|
72
|
+
cd base_dir
|
73
|
+
begin
|
74
|
+
files_with_contents.each do |path, file_contents|
|
75
|
+
remove_dir(path.split(File::SEPARATOR).first)
|
76
|
+
end
|
77
|
+
ensure
|
78
|
+
cd initial_dir
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/spec_helper"
|
2
|
+
require 'rubygems'
|
3
|
+
require 'spec'
|
4
|
+
require 'file_test_helper'
|
5
|
+
include FileTestHelper
|
6
|
+
|
7
|
+
describe FileTestHelper do
|
8
|
+
parameters = {
|
9
|
+
'nil' => nil,
|
10
|
+
'an empty hash' => {},
|
11
|
+
'a hash with only one file' => {'file.txt' => 'file content'}
|
12
|
+
}
|
13
|
+
|
14
|
+
parameters.each do |parameter_name, parameter|
|
15
|
+
describe "when the hash paramater is #{parameter_name}" do
|
16
|
+
it 'should execute the block parameter' do
|
17
|
+
block_runned = false
|
18
|
+
with_files(parameter) {block_runned = true}
|
19
|
+
block_runned.should be_true
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should not use the current directory' do
|
23
|
+
initial_directory = Dir.pwd
|
24
|
+
with_files(parameter) do
|
25
|
+
Dir.pwd.should_not == initial_directory
|
26
|
+
end
|
27
|
+
Dir.pwd.should == initial_directory
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should start in an empty working directory' do
|
31
|
+
with_files(parameter) do
|
32
|
+
Dir.glob('**/*').size == 0
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should delete the working directory and its contents after running the block' do
|
37
|
+
working_directory_path = ''
|
38
|
+
with_files(parameter) do
|
39
|
+
working_directory_path = Dir.pwd
|
40
|
+
end
|
41
|
+
working_directory_path.should_not be_empty
|
42
|
+
File.exist?(working_directory_path).should be_false
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should create the files and directories that were specified in a hash' do
|
48
|
+
files_to_create = {'a directory/a file.txt' => '', 'a directory/another file.rb' => '', 'an_empty_directory/' => '', 'a_file' => ''}
|
49
|
+
with_files(files_to_create) do
|
50
|
+
files_to_create.each_key{|created_file| File.exist?(created_file).should be_true}
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should be possible to define the content of created files' do
|
55
|
+
with_files('filea' => 'content of filea', 'fileb' => 'content of fileb') do
|
56
|
+
File.read('filea').should == 'content of filea'
|
57
|
+
File.read('fileb').should == 'content of fileb'
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should interpret a path with an ending / as a directory' do
|
62
|
+
with_files('this is a directory/another dir/' => '') do
|
63
|
+
File.directory?('this is a directory/another dir').should be_true
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should throw an error if trying to set file content to a directory' do
|
68
|
+
lambda { with_files('directory/' => 'imposible content, directories are not files') {}}.should raise_error
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should throw an error if a path which starts with the "/" character is specified' do
|
72
|
+
lambda { with_files('/directory/filea' => 'content of filea') {}}.should raise_error
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'should throw an error if a path uses ".."' do
|
76
|
+
lambda { with_files('../../dir/filea' => 'content of filea') {}}.should raise_error
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should not throw an exception when the hash parameter is nil' do
|
80
|
+
lambda { with_files(nil) {}}.should_not raise_error
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "with base_dir" do
|
84
|
+
it "should use the specified base dir instead of the default tmp dir" do
|
85
|
+
initial_directory = Dir.pwd
|
86
|
+
with_files_in_directory(initial_directory, 'somefile.txt' => '') do
|
87
|
+
Dir.pwd.should == initial_directory
|
88
|
+
end
|
89
|
+
Dir.pwd.should == initial_directory
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should not delete the base dir" do
|
93
|
+
base_dir = FileUtils.mkpath("delete_this_dir")
|
94
|
+
begin
|
95
|
+
with_files_in_directory(base_dir, 'somefile.txt' => '') {}
|
96
|
+
File.exist?(base_dir).should be_true
|
97
|
+
ensure
|
98
|
+
FileUtils.remove_dir(base_dir)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should delete only the created files" do
|
103
|
+
test_file = 'should_persist_after_test.txt'
|
104
|
+
begin
|
105
|
+
`touch #{test_file}`
|
106
|
+
with_files_in_directory(Dir.pwd, 'dir/somefile.txt' => '', 'anotherfile.txt' => '') {}
|
107
|
+
|
108
|
+
File.exist?('dir/somefile.txt').should be_false
|
109
|
+
File.exist?('anotherfile.txt').should be_false
|
110
|
+
File.exist?('dir').should be_false
|
111
|
+
File.exist?(test_file).should be_true
|
112
|
+
ensure
|
113
|
+
File.delete(test_file)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should throw an exception if the specified base dir doesn't exist" do
|
118
|
+
lambda{
|
119
|
+
with_files_in_directory('this_dir_does_not_exist', 'somefile.txt' => '') {}
|
120
|
+
}.should raise_error(ArgumentError)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
data/spec/spec.opts
CHANGED
metadata
CHANGED
@@ -1,58 +1,75 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filetesthelper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 21
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
|
-
- "Daniel Cadenas Ni\
|
13
|
+
- "Daniel Cadenas Ni\xC3\xB3n"
|
8
14
|
autorequire: filetesthelper
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2010-08-24 00:00:00 -03:00
|
13
19
|
default_executable:
|
14
20
|
dependencies: []
|
15
21
|
|
16
22
|
description: A simple helper aimed at reducing the setup effort needed to create directories, files and file content in integration test cases.
|
17
|
-
email:
|
23
|
+
email: dcadenas@gmail.com
|
18
24
|
executables: []
|
19
25
|
|
20
26
|
extensions: []
|
21
27
|
|
22
28
|
extra_rdoc_files:
|
23
|
-
- README.
|
29
|
+
- README.rdoc
|
24
30
|
files:
|
25
|
-
-
|
31
|
+
- README.rdoc
|
32
|
+
- Rakefile.rb
|
33
|
+
- VERSION.yml
|
34
|
+
- filetesthelper.gemspec
|
35
|
+
- lib/file_test_helper.rb
|
36
|
+
- spec/file_test_helper_spec.rb
|
26
37
|
- spec/spec.opts
|
27
38
|
- spec/spec_helper.rb
|
28
|
-
- lib/filetesthelper.rb
|
29
|
-
- README.txt
|
30
|
-
- Rakefile.rb
|
31
39
|
has_rdoc: true
|
32
40
|
homepage: http://rubyforge.org/projects/filetesthelper/
|
33
|
-
|
34
|
-
rdoc_options: []
|
41
|
+
licenses: []
|
35
42
|
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --charset=UTF-8
|
36
46
|
require_paths:
|
37
47
|
- lib
|
38
48
|
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
39
50
|
requirements:
|
40
51
|
- - ">="
|
41
52
|
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
42
56
|
version: "0"
|
43
|
-
version:
|
44
57
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
45
59
|
requirements:
|
46
60
|
- - ">="
|
47
61
|
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
48
65
|
version: "0"
|
49
|
-
version:
|
50
66
|
requirements: []
|
51
67
|
|
52
68
|
rubyforge_project:
|
53
|
-
rubygems_version: 1.
|
69
|
+
rubygems_version: 1.3.7
|
54
70
|
signing_key:
|
55
|
-
specification_version:
|
71
|
+
specification_version: 3
|
56
72
|
summary: A simple helper aimed at reducing the setup effort needed to create directories, files and file content in integration test cases.
|
57
|
-
test_files:
|
58
|
-
|
73
|
+
test_files:
|
74
|
+
- spec/file_test_helper_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
data/README.txt
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
= FileTestHelper
|
2
|
-
|
3
|
-
A simple helper aimed at reducing the setup effort needed to create directories, files and file content in the scope of an integration test case.
|
4
|
-
|
5
|
-
== Example
|
6
|
-
|
7
|
-
require 'rubygems'
|
8
|
-
require 'filetesthelper'
|
9
|
-
include FileTestHelper
|
10
|
-
|
11
|
-
...
|
12
|
-
|
13
|
-
def test_some_code_that_uses_the_file_system
|
14
|
-
#Let's say that the current directory here is X
|
15
|
-
|
16
|
-
with_files('dir1/file1' => 'this is file1 content', 'dir1/file2' => 'this is file2 content') do
|
17
|
-
#Now the current directory changed to Y which is a new directory
|
18
|
-
#created under Dir.tmpdir containing only 'dir1/file1' and 'dir1/file2'.
|
19
|
-
|
20
|
-
#Put some test code here.
|
21
|
-
end
|
22
|
-
|
23
|
-
#When we finish we are back at directory X and the Y directory is deleted with all its contents
|
24
|
-
end
|
25
|
-
|
26
|
-
See the specs for more details.
|
27
|
-
|
data/lib/filetesthelper.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
require 'tmpdir'
|
2
|
-
|
3
|
-
module FileTestHelper
|
4
|
-
include FileUtils
|
5
|
-
|
6
|
-
#Specify the files you need to create in a hash.
|
7
|
-
#The keys of the hash represent the file path and the value represents it's content.
|
8
|
-
#Examples:
|
9
|
-
#* {'a dir/a file' => 'content'} creates the 'a dir' directory containing an 'a file' file which content is 'content'.
|
10
|
-
#* {'another dir/' => ''} creates an empty 'another dir' directory. The value must be an empty string.
|
11
|
-
def with_files(files_with_contents = {})
|
12
|
-
begin
|
13
|
-
initial_directory = current_directory()
|
14
|
-
working_directory = create_working_directory()
|
15
|
-
create_files_in_working_directory(working_directory, files_with_contents)
|
16
|
-
yield
|
17
|
-
ensure
|
18
|
-
cd initial_directory
|
19
|
-
remove_dir(working_directory) if File.exist?(working_directory)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
def current_directory
|
25
|
-
Dir.pwd
|
26
|
-
end
|
27
|
-
|
28
|
-
def create_working_directory
|
29
|
-
process_id = $$
|
30
|
-
working_directory = File.join(Dir.tmpdir, "__test_dir__#{process_id}")
|
31
|
-
mkpath working_directory
|
32
|
-
return working_directory
|
33
|
-
end
|
34
|
-
|
35
|
-
def create_files_in_working_directory(working_directory, files_with_contents)
|
36
|
-
cd working_directory
|
37
|
-
files_with_contents.each do |path, file_contents|
|
38
|
-
fail 'A path is not allowed to start with /' if path =~ /^\//
|
39
|
-
fail 'A path is not allowed to contain ..' if path =~ /\.\./
|
40
|
-
|
41
|
-
dir, file = path.scan(/(.*[\/])?([^\/]*$)/)[0]
|
42
|
-
unless(dir.nil? or dir.empty?)
|
43
|
-
mkpath dir
|
44
|
-
end
|
45
|
-
|
46
|
-
unless(file.nil? or file.empty?)
|
47
|
-
File.open(path, 'w') do |f|
|
48
|
-
f << file_contents unless file_contents == nil
|
49
|
-
end
|
50
|
-
else
|
51
|
-
fail 'File content can only be set to files' unless file_contents.nil? or file_contents.empty?
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
data/spec/FileTestHelper_spec.rb
DELETED
@@ -1,62 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + "/spec_helper"
|
2
|
-
require 'rubygems'
|
3
|
-
require 'spec'
|
4
|
-
require 'filetesthelper'
|
5
|
-
include FileTestHelper
|
6
|
-
|
7
|
-
describe FileTestHelper do
|
8
|
-
it 'should start in an empty working directory' do
|
9
|
-
with_files() do
|
10
|
-
Dir.glob('**/*').size == 0
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'should not use the current directory as default' do
|
15
|
-
initial_directory = Dir.pwd
|
16
|
-
with_files() do
|
17
|
-
Dir.pwd.should_not == initial_directory
|
18
|
-
end
|
19
|
-
Dir.pwd.should == initial_directory
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'should delete the working directory and its contents after use' do
|
23
|
-
working_directory_path = ''
|
24
|
-
with_files() do
|
25
|
-
working_directory_path = Dir.pwd
|
26
|
-
end
|
27
|
-
working_directory_path.should_not be_empty
|
28
|
-
File.exist?(working_directory_path).should be_false
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'should create the files and directories that were specified in a hash' do
|
32
|
-
files_to_create = {'a directory/a file.txt' => '', 'a directory/another file.rb' => '', 'an_empty_directory/' => '', 'a_file' => ''}
|
33
|
-
with_files(files_to_create) do
|
34
|
-
files_to_create.each_key{|created_file| File.exist?(created_file).should be_true}
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'should be possible to define the content of created files' do
|
39
|
-
with_files('filea' => 'content of filea', 'fileb' => 'content of fileb') do
|
40
|
-
File.read('filea').should == 'content of filea'
|
41
|
-
File.read('fileb').should == 'content of fileb'
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'should interpret a path with an ending / as a directory' do
|
46
|
-
with_files('this is a directory/another dir/' => '') do
|
47
|
-
File.directory?('this is a directory/another dir').should be_true
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'should throw an error if trying to set file content to a directory' do
|
52
|
-
lambda { with_files('directory/' => 'imposible content, directories are not files') {}}.should raise_error
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'should throw an error if a path which starts with the "/" character is specified' do
|
56
|
-
lambda { with_files('/directory/filea' => 'content of filea') {}}.should raise_error
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'should throw an error if a path uses ".."' do
|
60
|
-
lambda { with_files('../../dir/filea' => 'content of filea') {}}.should raise_error
|
61
|
-
end
|
62
|
-
end
|