fstest 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in fstest.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Jerry Cheung
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,52 @@
1
+ # FSTest
2
+
3
+ Blatantly stolen file and directory assertion methods from
4
+ Rails::Generators::TestCase. Include the FSTest module for testing file
5
+ existence, and file contents.
6
+
7
+ ## Installation
8
+
9
+ gem install fstest
10
+
11
+ ## Example
12
+
13
+ class MyClassTest < MiniTest::Unit::TestCase
14
+ include FSTest
15
+
16
+ def test_writes_file
17
+ assert_no_file '/tmp/foo'
18
+ File.open('/tmp/foo', 'w') {|fh| fh.puts "nom nom"}
19
+ assert_file '/tmp/foo', /^nom nom$/
20
+ end
21
+ end
22
+
23
+ ## Overview
24
+
25
+ I couldn't find any good examples of tests for [Rails
26
+ generators](http://guides.rubyonrails.org/generators.html), so I looked at the
27
+ Rails tests. Rails uses a custom test class called
28
+ Rails::Generators::TestCase. This class has methods for testing the output of
29
+ generators. I thought the methods would be useful outside of a Rails context,
30
+ so I wrapped it up in the FSTest gem. Enjoy!
31
+
32
+ ## Extras
33
+
34
+ If you want to work with relative paths instead of absolute paths, you can
35
+ set the 'base\_directory' in a setup block.
36
+
37
+ class MyClassTest < MiniTest::Unit::TestCase
38
+ include FSTest
39
+
40
+ def setup
41
+ # all assert_file assertions will be relative to your homedir
42
+ self.base_directory = File.expand_path("~")
43
+ end
44
+
45
+ def test_dot_emacs
46
+ # this will look for ~/.emacs
47
+ assert_file '.emacs'
48
+ end
49
+ end
50
+
51
+ I've also found it useful to use [FakeFS](https://github.com/defunkt/fakefs)
52
+ alongside this.
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new do |t|
6
+ t.libs << ["lib", "test"]
7
+ t.verbose = true
8
+ t.test_files = FileList["test/**/*_test.rb"]
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "fstest/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "fstest"
7
+ s.version = FSTest::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jerry Cheung"]
10
+ s.email = ["jch@whatcodecraves.com"]
11
+ s.homepage = "https://github.com/jch/fstest"
12
+ s.summary = %q{Extracted file and directory assertion methods from Rails::Generators::TestCase.}
13
+ s.description = %q{Include the FSTest module for testing file existence, and file contents.}
14
+
15
+ s.add_development_dependency "minitest"
16
+ s.add_development_dependency "fakefs"
17
+
18
+ s.rubyforge_project = "fstest"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+ end
@@ -0,0 +1,69 @@
1
+ module FSTest
2
+ def self.included(base)
3
+ base.class_eval {
4
+ include InstanceMethods
5
+ include Assertions
6
+ include FileUtils
7
+ }
8
+ end
9
+
10
+ module InstanceMethods
11
+ def base_directory
12
+ @base_directory
13
+ end
14
+
15
+ def base_directory=(path)
16
+ @base_directory = File.expand_path(path)
17
+ end
18
+ end
19
+
20
+ module Assertions
21
+ # Asserts a given file exists. You need to supply an absolute path or a path relative
22
+ # to the configured destination:
23
+ #
24
+ # assert_file "config/environment.rb"
25
+ #
26
+ # You can also give extra arguments. If the argument is a regexp, it will check if the
27
+ # regular expression matches the given file content. If it's a string, it compares the
28
+ # file with the given string:
29
+ #
30
+ # assert_file "config/environment.rb", /initialize/
31
+ #
32
+ # Finally, when a block is given, it yields the file content:
33
+ #
34
+ # assert_file "app/controller/products_controller.rb" do |controller|
35
+ # assert_instance_method :index, content do |index|
36
+ # assert_match /Product\.all/, index
37
+ # end
38
+ # end
39
+ #
40
+ def assert_file(relative, *contents)
41
+ absolute = File.expand_path(relative, base_directory)
42
+ assert File.exists?(absolute), "Expected file #{relative.inspect} to exist, but does not"
43
+
44
+ read = File.read(absolute) if block_given? || !contents.empty?
45
+ yield read if block_given?
46
+
47
+ contents.each do |content|
48
+ case content
49
+ when String
50
+ assert_equal content, read
51
+ when Regexp
52
+ assert_match content, read
53
+ end
54
+ end
55
+ end
56
+ alias :assert_directory :assert_file
57
+
58
+ # Asserts a given file does not exist. You need to supply an absolute path or a
59
+ # path relative to the configured destination:
60
+ #
61
+ # assert_no_file "config/random.rb"
62
+ #
63
+ def assert_no_file(relative)
64
+ absolute = File.expand_path(relative, base_directory)
65
+ assert !File.exists?(absolute), "Expected file #{relative.inspect} to not exist, but does"
66
+ end
67
+ alias :assert_no_directory :assert_no_file
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module FSTest
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ class FSTestTest < MiniTest::Unit::TestCase
4
+ include FSTest
5
+
6
+ def setup
7
+ @klass = Class.new { include FSTest }
8
+ @instance = @klass.new
9
+ end
10
+
11
+ def test_include_adds_base_directory_attribute
12
+ assert @instance.respond_to? :base_directory
13
+ assert @instance.respond_to? :base_directory=
14
+ assert @instance.base_directory = '/tmp'
15
+ assert_equal '/tmp', @instance.base_directory
16
+ end
17
+
18
+ def test_base_directory_is_expanded
19
+ @instance.base_directory = '~'
20
+ assert_equal File.expand_path('~'), @instance.base_directory
21
+ end
22
+
23
+ def test_includes_fstest_assertions
24
+ assert @klass.included_modules.include? FSTest::Assertions
25
+ end
26
+
27
+ def test_includes_file_utils
28
+ assert @klass.included_modules.include? FileUtils
29
+ end
30
+
31
+ def test_assert_file
32
+ File.open('/tmp/fstest', 'w') {|fh| fh.write("nom nom nom")}
33
+ assert_file "/tmp/fstest", /^nom nom nom$/
34
+ assert_file "/tmp/fstest", "nom nom nom"
35
+ end
36
+
37
+ def test_assert_not_file
38
+ assert_no_file('/tmp/unknown')
39
+ end
40
+
41
+ def test_assert_directory
42
+ FileUtils.mkdir_p('/tmp/fstest_dir')
43
+ assert_directory '/tmp/fstest_dir'
44
+ end
45
+
46
+ def test_assert_no_directory
47
+ assert_no_directory '/tmp/unknown_dir'
48
+ end
49
+ end
@@ -0,0 +1,4 @@
1
+ require 'bundler/setup'
2
+ require 'minitest/autorun'
3
+ require 'fakefs'
4
+ require 'fstest'
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fstest
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Jerry Cheung
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-20 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: minitest
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: fakefs
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ description: Include the FSTest module for testing file existence, and file contents.
39
+ email:
40
+ - jch@whatcodecraves.com
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - fstest.gemspec
54
+ - lib/fstest.rb
55
+ - lib/fstest/version.rb
56
+ - test/fstest_test.rb
57
+ - test/test_helper.rb
58
+ has_rdoc: true
59
+ homepage: https://github.com/jch/fstest
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project: fstest
82
+ rubygems_version: 1.6.1
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Extracted file and directory assertion methods from Rails::Generators::TestCase.
86
+ test_files:
87
+ - test/fstest_test.rb
88
+ - test/test_helper.rb