reagent-test-fs 0.1.0 → 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.
@@ -2,17 +2,75 @@
2
2
 
3
3
  == Description
4
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.
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
6
 
7
7
  == Installation
8
8
 
9
- sudo gem install test-fs
9
+ sudo gem install reagent-test-fs --source=http://gems.github.com
10
10
 
11
11
  == Usage
12
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'
13
17
  require 'test_fs'
14
18
 
15
- TestFS is designed to be used from within your Unit Tests ...
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.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
16
74
 
17
75
  == License
18
76
 
@@ -2,8 +2,18 @@ $:.unshift File.dirname(__FILE__)
2
2
 
3
3
  require 'tmpdir'
4
4
  require 'fileutils'
5
+ require 'test/unit'
5
6
 
6
7
  require 'test_fs/node'
7
8
  require 'test_fs/directory'
8
9
  require 'test_fs/root_directory'
9
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
@@ -1,13 +1,24 @@
1
- module TestFs
1
+ module TestFs # :nodoc:
2
+
3
+ # = Directory
4
+ #
5
+ # Represents a single directory node.
6
+ #
2
7
  class Directory < Node
3
8
 
9
+ # A collection of sub-directories or files contained in this directory
10
+ #
4
11
  attr_reader :nodes
5
12
 
13
+ # Create a new directory at the given path with the supplied name
14
+ #
6
15
  def initialize(root_path, name)
7
16
  @nodes = []
8
17
  super(root_path, name)
9
18
  end
10
19
 
20
+ # Register a sub-directory under the current directory
21
+ #
11
22
  def dir(name)
12
23
  directory = Directory.new(self.path, name)
13
24
  self.nodes << directory
@@ -15,10 +26,14 @@ module TestFs
15
26
  yield directory if block_given?
16
27
  end
17
28
 
29
+ # Register a file under the current directory
30
+ #
18
31
  def file(name)
19
32
  self.nodes << File.new(self.path, name)
20
33
  end
21
34
 
35
+ # Create the current directory and any associated nodes (files / directories)
36
+ #
22
37
  def create!
23
38
  FileUtils.mkdir(self.path)
24
39
  self.nodes.each {|node| node.create! }
@@ -1,6 +1,13 @@
1
- module TestFs
1
+ module TestFs # :nodoc:
2
+
3
+ # = File
4
+ #
5
+ # Represents a single file node.
6
+ #
2
7
  class File < Node
3
8
 
9
+ # Create the current file
10
+ #
4
11
  def create!
5
12
  FileUtils.touch(self.path)
6
13
  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
@@ -1,5 +1,5 @@
1
- module TestFs
2
- class Node
1
+ module TestFs # :nodoc:
2
+ class Node # :nodoc:
3
3
 
4
4
  def initialize(root_path, name)
5
5
  @root_path = root_path
@@ -1,17 +1,24 @@
1
- module TestFs
1
+ module TestFs # :nodoc:
2
+
3
+ # = RootDirectory
4
+ #
5
+ # Represents the root of the filesystem
6
+ #
2
7
  class RootDirectory < Directory
3
8
 
4
- attr_reader :nodes
5
-
6
9
  def initialize
7
10
  @nodes = []
8
11
  end
9
-
12
+
13
+ # Retrieve the path for the generated root directory
14
+ #
10
15
  def path
11
16
  "#{Dir.tmpdir}/#{name}"
12
17
  end
13
18
 
14
19
  # TODO: clear lists & value for root?
20
+ # Recursively destroy the root filesystem
21
+ #
15
22
  def destroy!
16
23
  FileUtils.rm_rf(self.path)
17
24
  end
@@ -3,7 +3,7 @@ module TestFs
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- TINY = 0
6
+ TINY = 1
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reagent-test-fs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - First Last
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-15 00:00:00 -08:00
12
+ date: 2009-01-18 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -27,6 +27,7 @@ files:
27
27
  - lib/test_fs
28
28
  - lib/test_fs/directory.rb
29
29
  - lib/test_fs/file.rb
30
+ - lib/test_fs/helper.rb
30
31
  - lib/test_fs/node.rb
31
32
  - lib/test_fs/root_directory.rb
32
33
  - lib/test_fs/version.rb
@@ -36,6 +37,7 @@ files:
36
37
  - test/unit/test_fs
37
38
  - test/unit/test_fs/directory_test.rb
38
39
  - test/unit/test_fs/file_test.rb
40
+ - test/unit/test_fs/helper_test.rb
39
41
  - test/unit/test_fs/root_directory_test.rb
40
42
  has_rdoc: true
41
43
  homepage: http://my-site.net