filetesthelper 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +27 -0
- data/Rakefile.rb +78 -0
- data/lib/filetesthelper.rb +55 -0
- data/spec/FileTestHelper_spec.rb +62 -0
- data/spec/spec.opts +6 -0
- data/spec/spec_helper.rb +3 -0
- metadata +58 -0
data/README.txt
ADDED
@@ -0,0 +1,27 @@
|
|
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/Rakefile.rb
ADDED
@@ -0,0 +1,78 @@
|
|
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
|
+
NAME = 'filetesthelper'
|
10
|
+
PKGVERSION = '0.9.0'
|
11
|
+
is_windows = (PLATFORM =~ /win32|cygwin/)
|
12
|
+
SUDO = is_windows ? '' : 'sudo'
|
13
|
+
README = "README.txt"
|
14
|
+
|
15
|
+
|
16
|
+
##############################################################################
|
17
|
+
# Packaging & Installation
|
18
|
+
##############################################################################
|
19
|
+
|
20
|
+
CLEAN.include ["pkg", "coverage", "doc"]
|
21
|
+
|
22
|
+
spec = Gem::Specification.new do |s|
|
23
|
+
s.name = NAME
|
24
|
+
s.version = PKGVERSION
|
25
|
+
s.author = "Daniel Cadenas Ni�n"
|
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']
|
37
|
+
end
|
38
|
+
|
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
|
+
|
58
|
+
Spec::Rake::SpecTask.new('specs_with_rcov') do |t|
|
59
|
+
t.ruby_opts << '-rubygems'
|
60
|
+
t.spec_files = Dir['spec/**/*_spec.rb']
|
61
|
+
t.spec_opts = ['--options', 'spec/spec.opts']
|
62
|
+
t.rcov = true
|
63
|
+
t.rcov_opts = ['--text-summary']
|
64
|
+
end
|
65
|
+
|
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]
|
@@ -0,0 +1,55 @@
|
|
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
|
@@ -0,0 +1,62 @@
|
|
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
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: filetesthelper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Daniel Cadenas Ni\xF3n"
|
8
|
+
autorequire: filetesthelper
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-03-08 00:00:00 -02: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: filetesthelper-devel@rubyforge.org
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.txt
|
24
|
+
files:
|
25
|
+
- spec/FileTestHelper_spec.rb
|
26
|
+
- spec/spec.opts
|
27
|
+
- spec/spec_helper.rb
|
28
|
+
- lib/filetesthelper.rb
|
29
|
+
- README.txt
|
30
|
+
- Rakefile.rb
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://rubyforge.org/projects/filetesthelper/
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "0"
|
43
|
+
version:
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.0.1
|
54
|
+
signing_key:
|
55
|
+
specification_version: 2
|
56
|
+
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
|
+
|