mnoble-fakefs 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ Autotest.add_hook :initialize do |at|
2
+ at.add_mapping(%r%^test/.+_test.rb$%) do |filename, _|
3
+ filename
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ *.sw?
2
+ pkg
3
+
@@ -0,0 +1,27 @@
1
+ Scott Taylor <scott@railsnewbie.com>
2
+ Pat Nakajima <patnakajima@gmail.com>
3
+ Chris Wanstrath <chris@ozmm.org>
4
+ Myles Eftos <myles@madpilot.com.au>
5
+ Jeff Hodges <jeff@somethingsimilar.com>
6
+ Matt Freels <matt@freels.name>
7
+ Víctor Martínez <knoopx@gmail.com>
8
+ Nick Quaranto <nick@quaran.to>
9
+ Aaron Suggs <aaron@ktheory.com>
10
+ Victor Costan <costan@gmail.com>
11
+ Jon Yurek <jyurek@thoughtbot.com>
12
+ Greg Campbell <gtcampbell@gmail.com>
13
+ Ben Mabey <ben@benmabey.com>
14
+ Mark <mark@amerine.net>
15
+ Sam Goldstein <sam@aboutus.org>
16
+ dmathieu <42@dmathieu.com>
17
+ Lars Gierth <lars.gierth@gmail.com>
18
+ marano <thiagomarano@gmail.com>
19
+ jameswilding <james@jameswilding.net>
20
+ Scott Barron <scott@elitists.net>
21
+ Tymon Tobolski <i@teamon.eu>
22
+ Mariusz Pietrzyk <wijet@wijet.pl>
23
+ Keita Urashima <ursm@ursm.jp>
24
+ David Reese <david@whatcould.com>
25
+ msassak <msassak@gmail.com>
26
+ Mislav Marohnić <mislav.marohnic@gmail.com>
27
+ Rob Sanheim <rsanheim@gmail.com>
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Chris Wanstrath
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,123 @@
1
+ FakeFS
2
+ ======
3
+
4
+ Mocha is great. But when your library is all about manipulating the
5
+ filesystem, you really want to test the behavior and not the implementation.
6
+
7
+ If you're mocking and stubbing every call to FileUtils or File, you're
8
+ tightly coupling your tests with the implementation.
9
+
10
+ def test_creates_directory
11
+ FileUtils.expects(:mkdir).with("directory").once
12
+ Library.add "directory"
13
+ end
14
+
15
+ The above test will break if we decide to use `mkdir_p` in our code. Refactoring
16
+ code shouldn't necessitate refactoring tests.
17
+
18
+ With FakeFS:
19
+
20
+ def test_creates_directory
21
+ Library.add "directory"
22
+ assert File.directory?("directory")
23
+ end
24
+
25
+ Woot.
26
+
27
+
28
+ Usage
29
+ -----
30
+
31
+ require 'fakefs'
32
+
33
+ # That's it.
34
+
35
+
36
+ Don't Fake the FS Immediately
37
+ -----------------------------
38
+
39
+ require 'fakefs/safe'
40
+
41
+ FakeFS.activate!
42
+ # your code
43
+ FakeFS.deactivate!
44
+
45
+ # or
46
+ FakeFS do
47
+ # your code
48
+ end
49
+
50
+
51
+ RSpec
52
+ -----
53
+
54
+ The above approach works with RSpec as well. In addition you may include
55
+ FakeFS::SpecHelpers to turn FakeFS on and off in a given example group:
56
+
57
+ require 'fakefs/spec_helpers'
58
+
59
+ describe "my spec" do
60
+ include FakeFS::SpecHelpers
61
+ end
62
+
63
+ See `lib/fakefs/spec_helpers.rb` for more info.
64
+
65
+
66
+ How is this different than MockFS?
67
+ ----------------------------------
68
+
69
+ FakeFS provides a test suite and works with symlinks. It's also strictly a
70
+ test-time dependency: your actual library does not need to use or know about
71
+ FakeFS.
72
+
73
+
74
+ Caveats
75
+ -------
76
+
77
+ FakeFS internally uses the `Pathname` and `FileUtils` constants. If you use
78
+ these in your app, be certain you're properly requiring them and not counting
79
+ on FakeFS' own require.
80
+
81
+
82
+ Speed?
83
+ ------
84
+
85
+ <http://gist.github.com/156091>
86
+
87
+
88
+ Installation
89
+ ------------
90
+
91
+ ### [Gemcutter](http://gemcutter.org/)
92
+
93
+ $ gem install fakefs
94
+
95
+ ### [Rip](http://hellorip.com)
96
+
97
+ $ rip install git://github.com/defunkt/fakefs.git
98
+
99
+
100
+ Contributing
101
+ ------------
102
+
103
+ Once you've made your great commits:
104
+
105
+ 1. [Fork][0] FakeFS
106
+ 2. Create a topic branch - `git checkout -b my_branch`
107
+ 3. Push to your branch - `git push origin my_branch`
108
+ 4. Create an [Issue][1] with a link to your branch
109
+ 5. That's it!
110
+
111
+ Meta
112
+ ----
113
+
114
+ * Code: `git clone git://github.com/defunkt/fakefs.git`
115
+ * Home: <http://github.com/defunkt/fakefs>
116
+ * Docs: <http://defunkt.github.com/fakefs>
117
+ * Bugs: <http://github.com/defunkt/fakefs/issues>
118
+ * List: <http://groups.google.com/group/fakefs>
119
+ * Test: <http://runcoderun.com/defunkt/fakefs>
120
+ * Gems: <http://gemcutter.org/gems/fakefs>
121
+
122
+ [0]: http://help.github.com/forking/
123
+ [1]: http://github.com/defunkt/fakefs/issues
@@ -0,0 +1,66 @@
1
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'test')
3
+
4
+ desc "Run tests"
5
+ task :test do
6
+ Dir['test/**/*_test.rb'].each { |file| require file }
7
+ end
8
+
9
+ task :default => [:test, :spec]
10
+
11
+ begin
12
+ require 'spec/rake/spectask'
13
+
14
+ desc "Run specs"
15
+ Spec::Rake::SpecTask.new(:spec) do |t|
16
+ t.spec_files = FileList["spec/**/*.rb"]
17
+ end
18
+ rescue LoadError
19
+ puts "Spec task can't be loaded. `gem install rspec`"
20
+ end
21
+
22
+ begin
23
+ require 'jeweler'
24
+
25
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/lib'
26
+ require 'fakefs/version'
27
+
28
+ Jeweler::Tasks.new do |gemspec|
29
+ gemspec.name = "fakefs"
30
+ gemspec.summary = "A fake filesystem. Use it in your tests."
31
+ gemspec.email = "chris@ozmm.org"
32
+ gemspec.homepage = "http://github.com/defunkt/fakefs"
33
+ gemspec.description = "A fake filesystem. Use it in your tests."
34
+ gemspec.authors = ["Chris Wanstrath", "Scott Taylor", "Jeff Hodges", "Pat Nakajima"]
35
+ gemspec.has_rdoc = false
36
+ gemspec.version = FakeFS::Version.to_s
37
+ end
38
+ rescue LoadError
39
+ puts "Jeweler not available."
40
+ puts "Install it with: gem install jeweler"
41
+ end
42
+
43
+ begin
44
+ require 'sdoc_helpers'
45
+ rescue LoadError
46
+ puts "sdoc support not enabled. Please gem install sdoc-helpers."
47
+ end
48
+
49
+ desc "Build a gem"
50
+ task :gem => [ :gemspec, :build ]
51
+
52
+ desc "Push a new version to Gemcutter"
53
+ task :publish => [ :gemspec, :build ] do
54
+ abort("Tests failed!") unless system("rake test")
55
+ system "git tag v#{FakeFS::Version}"
56
+ system "git push origin v#{FakeFS::Version}"
57
+ system "git push origin master"
58
+ system "gem push pkg/fakefs-#{FakeFS::Version}.gem"
59
+ system "git clean -fd"
60
+ exec "rake pages"
61
+ end
62
+
63
+ desc "Update contributors"
64
+ task :update_contributors do
65
+ sh "git-rank-contributors > CONTRIBUTORS"
66
+ end
@@ -0,0 +1,86 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mnoble-fakefs}
8
+ s.version = "0.3.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Chris Wanstrath", "Scott Taylor", "Jeff Hodges", "Pat Nakajima"]
12
+ s.date = %q{2010-12-18}
13
+ s.description = %q{A fake filesystem. Use it in your tests. (Updates to support mkdir)}
14
+ s.email = %q{chris@ozmm.org}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".autotest",
21
+ ".gitignore",
22
+ "CONTRIBUTORS",
23
+ "LICENSE",
24
+ "README.markdown",
25
+ "Rakefile",
26
+ "fakefs.gemspec",
27
+ "lib/fakefs.rb",
28
+ "lib/fakefs/base.rb",
29
+ "lib/fakefs/dir.rb",
30
+ "lib/fakefs/fake/dir.rb",
31
+ "lib/fakefs/fake/file.rb",
32
+ "lib/fakefs/fake/symlink.rb",
33
+ "lib/fakefs/file.rb",
34
+ "lib/fakefs/file_system.rb",
35
+ "lib/fakefs/file_test.rb",
36
+ "lib/fakefs/fileutils.rb",
37
+ "lib/fakefs/safe.rb",
38
+ "lib/fakefs/spec_helpers.rb",
39
+ "lib/fakefs/version.rb",
40
+ "spec/fakefs/spec_helpers_spec.rb",
41
+ "spec/spec.opts",
42
+ "spec/spec_helper.rb",
43
+ "test/fake/file/lstat_test.rb",
44
+ "test/fake/file/stat_test.rb",
45
+ "test/fake/file/sysseek_test.rb",
46
+ "test/fake/file/syswrite_test.rb",
47
+ "test/fake/file_test.rb",
48
+ "test/fake/symlink_test.rb",
49
+ "test/fakefs_test.rb",
50
+ "test/file/stat_test.rb",
51
+ "test/safe_test.rb",
52
+ "test/test_helper.rb",
53
+ "test/verify.rb"
54
+ ]
55
+ s.homepage = %q{http://github.com/defunkt/fakefs}
56
+ s.rdoc_options = ["--charset=UTF-8"]
57
+ s.require_paths = ["lib"]
58
+ s.rubygems_version = %q{1.3.7}
59
+ s.summary = %q{A fake filesystem. Use it in your tests.}
60
+ s.test_files = [
61
+ "spec/fakefs/spec_helpers_spec.rb",
62
+ "spec/spec_helper.rb",
63
+ "test/fake/file/lstat_test.rb",
64
+ "test/fake/file/stat_test.rb",
65
+ "test/fake/file/sysseek_test.rb",
66
+ "test/fake/file/syswrite_test.rb",
67
+ "test/fake/file_test.rb",
68
+ "test/fake/symlink_test.rb",
69
+ "test/fakefs_test.rb",
70
+ "test/file/stat_test.rb",
71
+ "test/safe_test.rb",
72
+ "test/test_helper.rb",
73
+ "test/verify.rb"
74
+ ]
75
+
76
+ if s.respond_to? :specification_version then
77
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
78
+ s.specification_version = 3
79
+
80
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
81
+ else
82
+ end
83
+ else
84
+ end
85
+ end
86
+
@@ -0,0 +1,3 @@
1
+ require 'fakefs/safe'
2
+
3
+ FakeFS.activate!
@@ -0,0 +1,45 @@
1
+ RealFile = File
2
+ RealFileTest = FileTest
3
+ RealFileUtils = FileUtils
4
+ RealDir = Dir
5
+
6
+ module FakeFS
7
+ def self.activate!
8
+ Object.class_eval do
9
+ remove_const(:Dir)
10
+ remove_const(:File)
11
+ remove_const(:FileTest)
12
+ remove_const(:FileUtils)
13
+
14
+ const_set(:Dir, FakeFS::Dir)
15
+ const_set(:File, FakeFS::File)
16
+ const_set(:FileUtils, FakeFS::FileUtils)
17
+ const_set(:FileTest, FakeFS::FileTest)
18
+ end
19
+ true
20
+ end
21
+
22
+ def self.deactivate!
23
+ Object.class_eval do
24
+ remove_const(:Dir)
25
+ remove_const(:File)
26
+ remove_const(:FileTest)
27
+ remove_const(:FileUtils)
28
+
29
+ const_set(:Dir, RealDir)
30
+ const_set(:File, RealFile)
31
+ const_set(:FileTest, RealFileTest)
32
+ const_set(:FileUtils, RealFileUtils)
33
+ end
34
+ true
35
+ end
36
+ end
37
+
38
+ def FakeFS
39
+ return ::FakeFS unless block_given?
40
+ ::FakeFS.activate!
41
+ yield
42
+ ensure
43
+ ::FakeFS.deactivate!
44
+ end
45
+
@@ -0,0 +1,116 @@
1
+ module FakeFS
2
+ class Dir
3
+ include Enumerable
4
+
5
+ def initialize(string)
6
+ raise Errno::ENOENT, string unless FileSystem.find(string)
7
+ @path = string
8
+ @open = true
9
+ @pointer = 0
10
+ @contents = [ '.', '..', ] + FileSystem.find(@path).values
11
+ end
12
+
13
+ def close
14
+ @open = false
15
+ @pointer = nil
16
+ @contents = nil
17
+ nil
18
+ end
19
+
20
+ def each(&block)
21
+ while f = read
22
+ yield f
23
+ end
24
+ end
25
+
26
+ def path
27
+ @path
28
+ end
29
+
30
+ def pos
31
+ @pointer
32
+ end
33
+
34
+ def pos=(integer)
35
+ @pointer = integer
36
+ end
37
+
38
+ def read
39
+ raise IOError, "closed directory" if @pointer == nil
40
+ n = @contents[@pointer]
41
+ @pointer += 1
42
+ n.to_s.gsub(path + '/', '') if n
43
+ end
44
+
45
+ def rewind
46
+ @pointer = 0
47
+ end
48
+
49
+ def seek(integer)
50
+ raise IOError, "closed directory" if @pointer == nil
51
+ @pointer = integer
52
+ @contents[integer]
53
+ end
54
+
55
+ def self.[](pattern)
56
+ glob(pattern)
57
+ end
58
+
59
+ def self.chdir(dir, &blk)
60
+ FileSystem.chdir(dir, &blk)
61
+ end
62
+
63
+ def self.chroot(string)
64
+ # Not implemented yet
65
+ end
66
+
67
+ def self.delete(string)
68
+ raise SystemCallError, "No such file or directory - #{string}" unless FileSystem.find(string).values.empty?
69
+ FileSystem.delete(string)
70
+ end
71
+
72
+ def self.entries(dirname)
73
+ raise SystemCallError, dirname unless FileSystem.find(dirname)
74
+ Dir.new(dirname).map { |file| File.basename(file) }
75
+ end
76
+
77
+ def self.foreach(dirname, &block)
78
+ Dir.open(dirname) { |file| yield file }
79
+ end
80
+
81
+ def self.glob(pattern, &block)
82
+ files = [FileSystem.find(pattern) || []].flatten.map(&:to_s).sort
83
+ block_given? ? files.each { |file| block.call(file) } : files
84
+ end
85
+
86
+ def self.mkdir(string, integer = 0)
87
+ parent = string.split('/')
88
+ parent.pop
89
+ raise Errno::ENOENT, "No such file or directory - #{string}" unless parent.join == "" || FileSystem.find(parent.join('/'))
90
+ raise Errno::EEXIST, "File exists - #{string}" if File.exists?(string)
91
+ FileUtils.mkdir_p(string)
92
+ end
93
+
94
+ def self.open(string, &block)
95
+ if block_given?
96
+ Dir.new(string).each { |file| yield(file) }
97
+ else
98
+ Dir.new(string)
99
+ end
100
+ end
101
+
102
+ def self.tmpdir
103
+ '/tmp'
104
+ end
105
+
106
+ def self.pwd
107
+ FileSystem.current_dir.to_s
108
+ end
109
+
110
+ class << self
111
+ alias_method :getwd, :pwd
112
+ alias_method :rmdir, :delete
113
+ alias_method :unlink, :delete
114
+ end
115
+ end
116
+ end