test-fs 0.1.1

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 ADDED
@@ -0,0 +1,98 @@
1
+ = TestFs
2
+
3
+ == Description
4
+
5
+ Test FS is a gem that's used in your testing environment to create a temporary directory and file structure so that you don't have to muck around with mocking out filesystem calls or have a specific directory structure as part of your project. It's easy to set up and easy to destroy.
6
+
7
+ == Installation
8
+
9
+ sudo gem install reagent-test-fs --source=http://gems.github.com
10
+
11
+ == Usage
12
+
13
+ TestFS is designed to be used from within your Unit Tests to set up a directory and then tear it down immediately after your test runs:
14
+
15
+ require 'rubygems'
16
+ require 'test/unit'
17
+ require 'test_fs'
18
+
19
+ class LameExample
20
+
21
+ def initialize(root)
22
+ @root = root
23
+ end
24
+
25
+ def magic?
26
+ File.exist?("#{@root}/path/file.txt")
27
+ end
28
+
29
+ end
30
+
31
+ class LameExampleTest < Test::Unit::TestCase
32
+
33
+ def setup
34
+ @fs = setup_filesystem do |root|
35
+ root.dir 'path' do |p|
36
+ p.file 'file.txt'
37
+ end
38
+ end
39
+ end
40
+
41
+ def teardown
42
+ @fs.destroy!
43
+ end
44
+
45
+ def test_magic
46
+ lame_example = LameExample.new(@fs.path)
47
+ assert_equal true, lame_example.magic?
48
+ end
49
+
50
+ end
51
+
52
+ That's just a simple example, you can have parallel directories and nest as deep as you need:
53
+
54
+ setup_filesystem do |root|
55
+ root.dir 'app' do |app|
56
+ app.dir 'controllers'
57
+ app.dir 'helpers
58
+ app.dir 'models'
59
+ end
60
+ root.dir 'config' do |config|
61
+ config.file 'database.yml'
62
+ config.file 'environment.rb'
63
+ config.dir 'environments' do |environments|
64
+ environments.file 'test.rb'
65
+ environments.file 'development.rb'
66
+ end
67
+ end
68
+
69
+ == TODO
70
+
71
+ * Teardown of filesystem is manual (e.g. @fs.destroy!) - this should be hooked into standard Test::Unit teardown if there is a filesystem that was created as part of the test
72
+ * This doesn't handle the creation of multiple filesytems in a single test run - new filesystems will clobber existing ones
73
+ * The <tt>file</tt> method just uses FileUtils.touch to create a file, it needs a way to easily get content into the file
74
+
75
+ == License
76
+
77
+ Copyright (c) 2008 Patrick Reagan of Viget Labs (patrick.reagan@viget.com)
78
+
79
+ Permission is hereby granted, free of charge, to any person
80
+ obtaining a copy of this software and associated documentation
81
+ files (the "Software"), to deal in the Software without
82
+ restriction, including without limitation the rights to use,
83
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
84
+ copies of the Software, and to permit persons to whom the
85
+ Software is furnished to do so, subject to the following
86
+ conditions:
87
+
88
+ The above copyright notice and this permission notice shall be
89
+ included in all copies or substantial portions of the Software.
90
+
91
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
92
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
93
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
94
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
95
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
96
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
97
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
98
+ OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+
5
+ require 'lib/test_fs/version'
6
+
7
+ task :default => :test
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.name = 'test-fs'
11
+ s.version = TestFs::Version.to_s
12
+ s.has_rdoc = true
13
+ s.extra_rdoc_files = %w(README.rdoc)
14
+ s.rdoc_options = %w(--main README.rdoc)
15
+ s.summary = "This gem provides an easy way to generate and destroy filesystems from within Test::Unit tests"
16
+ s.author = 'Patrick Reagan'
17
+ s.email = 'patrick.reagan@viget.com'
18
+ s.homepage = 'http://www.viget.com/extend/'
19
+ s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib,test}/**/*")
20
+ # s.executables = ['test-fs']
21
+
22
+ # s.add_dependency('gem_name', '~> 0.0.1')
23
+ end
24
+
25
+ Rake::GemPackageTask.new(spec) do |pkg|
26
+ pkg.gem_spec = spec
27
+ end
28
+
29
+ Rake::TestTask.new do |t|
30
+ t.libs << 'test'
31
+ t.test_files = FileList["test/**/*_test.rb"]
32
+ t.verbose = true
33
+ end
34
+
35
+ desc 'Generate the gemspec to serve this Gem from Github'
36
+ task :github do
37
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
38
+ File.open(file, 'w') {|f| f << spec.to_ruby }
39
+ puts "Created gemspec: #{file}"
40
+ end
data/lib/test_fs.rb ADDED
@@ -0,0 +1,19 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'tmpdir'
4
+ require 'fileutils'
5
+ require 'test/unit'
6
+
7
+ require 'test_fs/node'
8
+ require 'test_fs/directory'
9
+ require 'test_fs/root_directory'
10
+ require 'test_fs/file'
11
+ require 'test_fs/helper'
12
+
13
+ module Test # :nodoc:
14
+ module Unit # :nodoc:
15
+ class TestCase # :nodoc:
16
+ include TestFs::Helper
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,43 @@
1
+ module TestFs # :nodoc:
2
+
3
+ # = Directory
4
+ #
5
+ # Represents a single directory node.
6
+ #
7
+ class Directory < Node
8
+
9
+ # A collection of sub-directories or files contained in this directory
10
+ #
11
+ attr_reader :nodes
12
+
13
+ # Create a new directory at the given path with the supplied name
14
+ #
15
+ def initialize(root_path, name)
16
+ @nodes = []
17
+ super(root_path, name)
18
+ end
19
+
20
+ # Register a sub-directory under the current directory
21
+ #
22
+ def dir(name)
23
+ directory = Directory.new(self.path, name)
24
+ self.nodes << directory
25
+
26
+ yield directory if block_given?
27
+ end
28
+
29
+ # Register a file under the current directory
30
+ #
31
+ def file(name)
32
+ self.nodes << File.new(self.path, name)
33
+ end
34
+
35
+ # Create the current directory and any associated nodes (files / directories)
36
+ #
37
+ def create!
38
+ FileUtils.mkdir(self.path)
39
+ self.nodes.each {|node| node.create! }
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,16 @@
1
+ module TestFs # :nodoc:
2
+
3
+ # = File
4
+ #
5
+ # Represents a single file node.
6
+ #
7
+ class File < Node
8
+
9
+ # Create the current file
10
+ #
11
+ def create!
12
+ FileUtils.touch(self.path)
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ module TestFs # :nodoc:
2
+ module Helper
3
+
4
+ # Register and create a filesystem for use in tests. Yields an instance of the
5
+ # root of the filesystem and returns that instance as well. This method is automatically
6
+ # mixed in to Test::Unit::TestCase
7
+ #
8
+ def setup_filesystem(&block)
9
+ root_directory = TestFs::RootDirectory.new
10
+
11
+ block.call(root_directory)
12
+ root_directory.create!
13
+
14
+ root_directory
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ module TestFs # :nodoc:
2
+ class Node # :nodoc:
3
+
4
+ def initialize(root_path, name)
5
+ @root_path = root_path
6
+ @name = name
7
+ end
8
+
9
+ def path
10
+ "#{@root_path}/#{@name}"
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,32 @@
1
+ module TestFs # :nodoc:
2
+
3
+ # = RootDirectory
4
+ #
5
+ # Represents the root of the filesystem
6
+ #
7
+ class RootDirectory < Directory
8
+
9
+ def initialize
10
+ @nodes = []
11
+ end
12
+
13
+ # Retrieve the path for the generated root directory
14
+ #
15
+ def path
16
+ "#{Dir.tmpdir}/#{name}"
17
+ end
18
+
19
+ # TODO: clear lists & value for root?
20
+ # Recursively destroy the root filesystem
21
+ #
22
+ def destroy!
23
+ FileUtils.rm_rf(self.path)
24
+ end
25
+
26
+ def name
27
+ sprintf('%s.%d.%d', self.class.name, $$, 1)
28
+ end
29
+ private :name
30
+
31
+ end
32
+ end
@@ -0,0 +1,13 @@
1
+ module TestFs
2
+ module Version
3
+
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ TINY = 1
7
+
8
+ def self.to_s # :nodoc:
9
+ [MAJOR, MINOR, TINY].join('.')
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ # http://sneaq.net/textmate-wtf
2
+ $:.reject! { |e| e.include? 'TextMate' }
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'matchy'
7
+ require 'context'
8
+ require 'mocha'
9
+
10
+ require File.dirname(__FILE__) + '/../lib/test_fs'
@@ -0,0 +1,78 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ module TestFs
4
+ class DirectoryTest < Test::Unit::TestCase
5
+
6
+ describe "An instance of the TestFs::Directory class" do
7
+
8
+ it "should know the full path to the directory" do
9
+ dir = Directory.new('/path', 'dir')
10
+ dir.path.should == '/path/dir'
11
+ end
12
+
13
+ context "After initialization" do
14
+ before do
15
+ @root_path = '/root'
16
+ @name = 'dir'
17
+
18
+ @dir = Directory.new(@root_path, @name)
19
+ end
20
+ it "should be able to create the underlying directory" do
21
+ FileUtils.expects(:mkdir).with("#{@root_path}/#{@name}")
22
+ @dir.create!
23
+ end
24
+
25
+ it "should have an empty node list by default" do
26
+ @dir.nodes.should == []
27
+ end
28
+
29
+ it "should be able to add a directory" do
30
+ dir = stub()
31
+
32
+ @dir.stubs(:path).with().returns('/root')
33
+ Directory.expects(:new).with('/root', 'dir').returns(dir)
34
+
35
+ @dir.dir 'dir'
36
+ @dir.nodes.should == [dir]
37
+ end
38
+
39
+ it "should be able to create the root directory" do
40
+ @dir.stubs(:path).with().returns('/root')
41
+ FileUtils.expects(:mkdir).with('/root')
42
+
43
+ @dir.create!
44
+ end
45
+
46
+ it "should be able to create the underlying directories" do
47
+ FileUtils.stubs(:mkdir)
48
+
49
+ dir = mock {|m| m.expects(:create!).with() }
50
+
51
+ @dir.stubs(:nodes).with().returns([dir])
52
+ @dir.create!
53
+ end
54
+
55
+ it "should be able to add a file" do
56
+ file = stub()
57
+ @dir.stubs(:path).with().returns('/root')
58
+
59
+ File.expects(:new).with('/root', 'file').returns(file)
60
+
61
+ @dir.file 'file'
62
+ @dir.nodes.should == [file]
63
+ end
64
+
65
+ it "should be able to create the underlying files" do
66
+ FileUtils.stubs(:mkdir)
67
+
68
+ file = mock {|m| m.expects(:create!).with() }
69
+ @dir.stubs(:nodes).with().returns([file])
70
+
71
+ @dir.create!
72
+ end
73
+
74
+ end
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,23 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ module TestFs
4
+ class FileTest < Test::Unit::TestCase
5
+
6
+ describe "An instance of the TestFs::File class" do
7
+
8
+ it "should know its path" do
9
+ file = File.new('/path', 'name')
10
+ file.path.should == '/path/name'
11
+ end
12
+
13
+ it "should be able to create the underlying file" do
14
+ file = File.new('/path', 'name')
15
+ FileUtils.expects(:touch).with('/path/name')
16
+
17
+ file.create!
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,38 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ class HelperImplementation
4
+ include TestFs::Helper
5
+ end
6
+
7
+ module TestFs
8
+ class HelperTest < Test::Unit::TestCase
9
+
10
+ describe "An instance of HelperImplementation" do
11
+
12
+ it "should have a setup_filesystem method" do
13
+ hi = HelperImplementation.new
14
+ hi.respond_to?(:setup_filesystem).should be(true)
15
+ end
16
+
17
+ it "should create a test filesystem when using the setup_filesystem helper" do
18
+ hi = HelperImplementation.new
19
+
20
+ root_directory = mock do |m|
21
+ m.expects(:dir).with('dir')
22
+ m.expects(:create!).with()
23
+ end
24
+
25
+ TestFs::RootDirectory.expects(:new).with().returns(root_directory)
26
+
27
+ fs = hi.setup_filesystem do |root|
28
+ root.dir 'dir'
29
+ end
30
+
31
+ fs.should == root_directory
32
+ end
33
+
34
+
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,35 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ module TestFs
4
+ class RootDirectoryTest < Test::Unit::TestCase
5
+
6
+ describe "An instance of the TestFs class" do
7
+ before { @root_dir = RootDirectory.new }
8
+
9
+ it "should know the :root_name" do
10
+ @root_dir.expects(:sprintf).with('%s.%d.%d', 'TestFs::RootDirectory', kind_of(Fixnum), 1).returns('blip')
11
+ @root_dir.send(:name).should == 'blip'
12
+ end
13
+
14
+ it "should know the root of the filesystem" do
15
+ Dir.expects(:tmpdir).with().returns('/tmp')
16
+ @root_dir.expects(:name).with().returns('name')
17
+
18
+ @root_dir.path.should == '/tmp/name'
19
+ end
20
+
21
+ it "should have an empty node list by default" do
22
+ @root_dir.nodes.should == []
23
+ end
24
+
25
+ it "should be able to destroy the filesystem created" do
26
+ @root_dir.expects(:path).with().returns('/root')
27
+ FileUtils.expects(:rm_rf).with('/root')
28
+
29
+ @root_dir.destroy!
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: test-fs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Reagan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-08 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: patrick.reagan@viget.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - README.rdoc
26
+ - Rakefile
27
+ - lib/test_fs/directory.rb
28
+ - lib/test_fs/file.rb
29
+ - lib/test_fs/helper.rb
30
+ - lib/test_fs/node.rb
31
+ - lib/test_fs/root_directory.rb
32
+ - lib/test_fs/version.rb
33
+ - lib/test_fs.rb
34
+ - test/test_helper.rb
35
+ - test/unit/test_fs/directory_test.rb
36
+ - test/unit/test_fs/file_test.rb
37
+ - test/unit/test_fs/helper_test.rb
38
+ - test/unit/test_fs/root_directory_test.rb
39
+ has_rdoc: true
40
+ homepage: http://www.viget.com/extend/
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --main
46
+ - README.rdoc
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.5
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: This gem provides an easy way to generate and destroy filesystems from within Test::Unit tests
68
+ test_files: []
69
+