reagent-test-fs 0.1.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 ADDED
@@ -0,0 +1,40 @@
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.
6
+
7
+ == Installation
8
+
9
+ sudo gem install test-fs
10
+
11
+ == Usage
12
+
13
+ require 'test_fs'
14
+
15
+ TestFS is designed to be used from within your Unit Tests ...
16
+
17
+ == License
18
+
19
+ Copyright (c) 2008 Patrick Reagan of Viget Labs (patrick.reagan@viget.com)
20
+
21
+ Permission is hereby granted, free of charge, to any person
22
+ obtaining a copy of this software and associated documentation
23
+ files (the "Software"), to deal in the Software without
24
+ restriction, including without limitation the rights to use,
25
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
26
+ copies of the Software, and to permit persons to whom the
27
+ Software is furnished to do so, subject to the following
28
+ conditions:
29
+
30
+ The above copyright notice and this permission notice shall be
31
+ included in all copies or substantial portions of the Software.
32
+
33
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
34
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
35
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
36
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
37
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
38
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
39
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
40
+ 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 does ... "
16
+ s.author = 'First Last'
17
+ s.email = 'user@example.com'
18
+ s.homepage = 'http://my-site.net'
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
@@ -0,0 +1,28 @@
1
+ module TestFs
2
+ class Directory < Node
3
+
4
+ attr_reader :nodes
5
+
6
+ def initialize(root_path, name)
7
+ @nodes = []
8
+ super(root_path, name)
9
+ end
10
+
11
+ def dir(name)
12
+ directory = Directory.new(self.path, name)
13
+ self.nodes << directory
14
+
15
+ yield directory if block_given?
16
+ end
17
+
18
+ def file(name)
19
+ self.nodes << File.new(self.path, name)
20
+ end
21
+
22
+ def create!
23
+ FileUtils.mkdir(self.path)
24
+ self.nodes.each {|node| node.create! }
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ module TestFs
2
+ class File < Node
3
+
4
+ def create!
5
+ FileUtils.touch(self.path)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module TestFs
2
+ class Node
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,25 @@
1
+ module TestFs
2
+ class RootDirectory < Directory
3
+
4
+ attr_reader :nodes
5
+
6
+ def initialize
7
+ @nodes = []
8
+ end
9
+
10
+ def path
11
+ "#{Dir.tmpdir}/#{name}"
12
+ end
13
+
14
+ # TODO: clear lists & value for root?
15
+ def destroy!
16
+ FileUtils.rm_rf(self.path)
17
+ end
18
+
19
+ def name
20
+ sprintf('%s.%d.%d', self.class.name, $$, 1)
21
+ end
22
+ private :name
23
+
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ module TestFs
2
+ module Version
3
+
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ TINY = 0
7
+
8
+ def self.to_s # :nodoc:
9
+ [MAJOR, MINOR, TINY].join('.')
10
+ end
11
+
12
+ end
13
+ end
data/lib/test_fs.rb ADDED
@@ -0,0 +1,9 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'tmpdir'
4
+ require 'fileutils'
5
+
6
+ require 'test_fs/node'
7
+ require 'test_fs/directory'
8
+ require 'test_fs/root_directory'
9
+ require 'test_fs/file'
@@ -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,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,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: reagent-test-fs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - First Last
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-15 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: user@example.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
28
+ - lib/test_fs/directory.rb
29
+ - lib/test_fs/file.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
36
+ - test/unit/test_fs
37
+ - test/unit/test_fs/directory_test.rb
38
+ - test/unit/test_fs/file_test.rb
39
+ - test/unit/test_fs/root_directory_test.rb
40
+ has_rdoc: true
41
+ homepage: http://my-site.net
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --main
45
+ - README.rdoc
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: This gem does ...
67
+ test_files: []
68
+