file_test_helper 1.0.2
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 +18 -0
- data/file_test_helper.gemspec +48 -0
- data/lib/file_test_helper.rb +88 -0
- data/spec/file_test_helper_spec.rb +123 -0
- data/spec/spec.opts +7 -0
- data/spec/spec_helper.rb +3 -0
- metadata +74 -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 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
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rubygems/gem_runner'
|
4
|
+
require 'rake'
|
5
|
+
require "rake/clean"
|
6
|
+
require 'rake/rdoctask'
|
7
|
+
require 'rake/gempackagetask'
|
8
|
+
require 'spec/rake/spectask'
|
9
|
+
|
10
|
+
Spec::Rake::SpecTask.new('specs_with_rcov') do |t|
|
11
|
+
t.ruby_opts << '-rubygems'
|
12
|
+
t.spec_files = Dir['spec/**/*_spec.rb']
|
13
|
+
t.spec_opts = ['--options', 'spec/spec.opts']
|
14
|
+
t.rcov = true
|
15
|
+
t.rcov_opts = ['--text-summary']
|
16
|
+
end
|
17
|
+
|
18
|
+
task :default => [:specs_with_rcov]
|
@@ -0,0 +1,48 @@
|
|
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{file_test_helper}
|
8
|
+
s.version = "1.0.2"
|
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{file_test_helper}
|
13
|
+
s.date = %q{2010-11-16}
|
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
|
+
"file_test_helper.gemspec",
|
23
|
+
"lib/file_test_helper.rb",
|
24
|
+
"spec/file_test_helper_spec.rb",
|
25
|
+
"spec/spec.opts",
|
26
|
+
"spec/spec_helper.rb"
|
27
|
+
]
|
28
|
+
s.homepage = %q{http://github.com/dcadenas/filetesthelper}
|
29
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
30
|
+
s.require_paths = ["lib"]
|
31
|
+
s.rubygems_version = %q{1.3.7}
|
32
|
+
s.summary = %q{A simple helper aimed at reducing the setup effort needed to create directories, files and file content in integration test cases.}
|
33
|
+
s.test_files = [
|
34
|
+
"spec/file_test_helper_spec.rb",
|
35
|
+
"spec/spec_helper.rb"
|
36
|
+
]
|
37
|
+
|
38
|
+
if s.respond_to? :specification_version then
|
39
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
40
|
+
s.specification_version = 3
|
41
|
+
|
42
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
43
|
+
else
|
44
|
+
end
|
45
|
+
else
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
@@ -0,0 +1,88 @@
|
|
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
|
+
stdout = $stdout
|
22
|
+
$stdout = File.new('/dev/null', 'w')
|
23
|
+
|
24
|
+
initial_directory = current_directory
|
25
|
+
raise ArgumentError, "The base directory '#{base_dir}' does not exist." if base_dir && !File.exist?(base_dir)
|
26
|
+
working_directory = base_dir || create_working_directory
|
27
|
+
|
28
|
+
begin
|
29
|
+
create_files_in_working_directory(working_directory, files_with_contents)
|
30
|
+
$stdout = stdout
|
31
|
+
yield
|
32
|
+
ensure
|
33
|
+
$stdout = File.new('/dev/null', 'w')
|
34
|
+
cd initial_directory
|
35
|
+
if base_dir.nil?
|
36
|
+
remove_dir(working_directory) if File.exist?(working_directory)
|
37
|
+
else
|
38
|
+
remove_files(base_dir, files_with_contents)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
ensure
|
42
|
+
$stdout = stdout
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
def current_directory
|
47
|
+
Dir.pwd
|
48
|
+
end
|
49
|
+
|
50
|
+
def create_working_directory
|
51
|
+
working_directory = File.join(Dir.tmpdir, "__test_dir__#{Process.pid}")
|
52
|
+
mkpath(working_directory)
|
53
|
+
return working_directory
|
54
|
+
end
|
55
|
+
|
56
|
+
def create_files_in_working_directory(working_directory, files_with_contents)
|
57
|
+
cd working_directory
|
58
|
+
|
59
|
+
return unless files_with_contents
|
60
|
+
files_with_contents.each do |path, file_contents|
|
61
|
+
raise ArgumentError, 'A path is not allowed to start with /' if path =~ /^\//
|
62
|
+
raise ArgumentError, 'A path is not allowed to contain ..' if path =~ /\.\./
|
63
|
+
|
64
|
+
dir, file = path.scan(/(.*[\/])?([^\/]*$)/)[0]
|
65
|
+
mkpath dir unless(dir.nil? or dir.empty?)
|
66
|
+
|
67
|
+
unless(file.nil? or file.empty?)
|
68
|
+
File.open(path, 'w') do |f|
|
69
|
+
f << file_contents unless file_contents == nil
|
70
|
+
end
|
71
|
+
else
|
72
|
+
raise ArgumentError, 'File content can only be set to files' unless file_contents.nil? or file_contents.empty?
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def remove_files(base_dir, files_with_contents)
|
78
|
+
initial_dir = Dir.pwd
|
79
|
+
cd base_dir
|
80
|
+
begin
|
81
|
+
files_with_contents.each do |path, file_contents|
|
82
|
+
remove_dir(path.split(File::SEPARATOR).first)
|
83
|
+
end
|
84
|
+
ensure
|
85
|
+
cd initial_dir
|
86
|
+
end
|
87
|
+
end
|
88
|
+
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
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: file_test_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 1.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "Daniel Cadenas Ni\xC3\xB3n"
|
14
|
+
autorequire: file_test_helper
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-16 00:00:00 -02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: A simple helper aimed at reducing the setup effort needed to create directories, files and file content in integration test cases.
|
23
|
+
email: dcadenas@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- README.rdoc
|
32
|
+
- Rakefile.rb
|
33
|
+
- file_test_helper.gemspec
|
34
|
+
- lib/file_test_helper.rb
|
35
|
+
- spec/file_test_helper_spec.rb
|
36
|
+
- spec/spec.opts
|
37
|
+
- spec/spec_helper.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/dcadenas/filetesthelper
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --charset=UTF-8
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.7
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: A simple helper aimed at reducing the setup effort needed to create directories, files and file content in integration test cases.
|
72
|
+
test_files:
|
73
|
+
- spec/file_test_helper_spec.rb
|
74
|
+
- spec/spec_helper.rb
|