dcadenas-filetesthelper 0.10.0
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/README.rdoc +43 -0
- data/Rakefile.rb +37 -0
- data/VERSION.yml +4 -0
- data/lib/file_test_helper.rb +81 -0
- data/spec/file_test_helper_spec.rb +121 -0
- data/spec/spec.opts +7 -0
- data/spec/spec_helper.rb +3 -0
- metadata +60 -0
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 dcadenas-filetesthelper
|
7
|
+
|
8
|
+
== Usage
|
9
|
+
|
10
|
+
require 'rubygems'
|
11
|
+
require 'file_test_helper'
|
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
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rubygems/gem_runner'
|
3
|
+
require 'rake'
|
4
|
+
require "rake/clean"
|
5
|
+
require 'rake/rdoctask'
|
6
|
+
require 'rake/gempackagetask'
|
7
|
+
require 'spec/rake/spectask'
|
8
|
+
|
9
|
+
begin
|
10
|
+
require 'jeweler'
|
11
|
+
Jeweler::Tasks.new do |gemspec|
|
12
|
+
gemspec.name = "filetesthelper"
|
13
|
+
gemspec.summary = "A simple helper aimed at reducing the setup effort needed to create directories, files and file content in integration test cases."
|
14
|
+
gemspec.email = "dcadenas@gmail.com"
|
15
|
+
gemspec.homepage = "http://rubyforge.org/projects/filetesthelper/"
|
16
|
+
gemspec.description = gemspec.summary
|
17
|
+
gemspec.authors = ["Daniel Cadenas Nión"]
|
18
|
+
gemspec.version = '0.9.1'
|
19
|
+
gemspec.platform = Gem::Platform::RUBY
|
20
|
+
#gemspec.files = Dir["{spec,lib}/**/*"] + [README, "Rakefile.rb"]
|
21
|
+
gemspec.require_path = 'lib'
|
22
|
+
gemspec.autorequire = gemspec.name
|
23
|
+
end
|
24
|
+
rescue LoadError
|
25
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
Spec::Rake::SpecTask.new('specs_with_rcov') do |t|
|
30
|
+
t.ruby_opts << '-rubygems'
|
31
|
+
t.spec_files = Dir['spec/**/*_spec.rb']
|
32
|
+
t.spec_opts = ['--options', 'spec/spec.opts']
|
33
|
+
t.rcov = true
|
34
|
+
t.rcov_opts = ['--text-summary']
|
35
|
+
end
|
36
|
+
|
37
|
+
task :default => [:specs_with_rcov]
|
data/VERSION.yml
ADDED
@@ -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 ArguemntError, '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)
|
76
|
+
end
|
77
|
+
ensure
|
78
|
+
cd initial_dir
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,121 @@
|
|
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, 'somefile.txt' => '') {}
|
107
|
+
|
108
|
+
File.exist?('somefile.txt').should be_false
|
109
|
+
File.exist?(test_file).should be_true
|
110
|
+
ensure
|
111
|
+
File.delete(test_file)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should throw an exception if the specified base dir doesn't exist" do
|
116
|
+
lambda{
|
117
|
+
with_files_in_directory('this_dir_does_not_exist', 'somefile.txt' => '') {}
|
118
|
+
}.should raise_error(ArgumentError)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dcadenas-filetesthelper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.10.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Daniel Cadenas Ni\xC3\xB3n"
|
8
|
+
autorequire: filetesthelper
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-12 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A simple helper aimed at reducing the setup effort needed to create directories, files and file content in integration test cases.
|
17
|
+
email: dcadenas@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- Rakefile.rb
|
26
|
+
- README.rdoc
|
27
|
+
- VERSION.yml
|
28
|
+
- lib/file_test_helper.rb
|
29
|
+
- spec/file_test_helper_spec.rb
|
30
|
+
- spec/spec.opts
|
31
|
+
- spec/spec_helper.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://rubyforge.org/projects/filetesthelper/
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options:
|
36
|
+
- --inline-source
|
37
|
+
- --charset=UTF-8
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.2.0
|
56
|
+
signing_key:
|
57
|
+
specification_version: 2
|
58
|
+
summary: A simple helper aimed at reducing the setup effort needed to create directories, files and file content in integration test cases.
|
59
|
+
test_files: []
|
60
|
+
|