rails_generators_test_case_modules 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+ Gemfile.lock
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails_generators_test_case_modules.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Pablo Herrero
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ rails_generators_test_case_modules
2
+ ==================================
3
+ # RailsGeneratorsTestCaseModules
4
+
5
+ Backports for Rails::Generator::TestCase helper modules for railties 3.2
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'rails_generators_test_case_modules'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rails_generators_test_case_modules
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,121 @@
1
+ module Rails
2
+ module Generators
3
+ module Testing
4
+ module Assertions
5
+ # Asserts a given file exists. You need to supply an absolute path or a path relative
6
+ # to the configured destination:
7
+ #
8
+ # assert_file "config/environment.rb"
9
+ #
10
+ # You can also give extra arguments. If the argument is a regexp, it will check if the
11
+ # regular expression matches the given file content. If it's a string, it compares the
12
+ # file with the given string:
13
+ #
14
+ # assert_file "config/environment.rb", /initialize/
15
+ #
16
+ # Finally, when a block is given, it yields the file content:
17
+ #
18
+ # assert_file "app/controllers/products_controller.rb" do |controller|
19
+ # assert_instance_method :index, controller do |index|
20
+ # assert_match(/Product\.all/, index)
21
+ # end
22
+ # end
23
+ def assert_file(relative, *contents)
24
+ absolute = File.expand_path(relative, destination_root)
25
+ assert File.exists?(absolute), "Expected file #{relative.inspect} to exist, but does not"
26
+
27
+ read = File.read(absolute) if block_given? || !contents.empty?
28
+ yield read if block_given?
29
+
30
+ contents.each do |content|
31
+ case content
32
+ when String
33
+ assert_equal content, read
34
+ when Regexp
35
+ assert_match content, read
36
+ end
37
+ end
38
+ end
39
+ alias :assert_directory :assert_file
40
+
41
+ # Asserts a given file does not exist. You need to supply an absolute path or a
42
+ # path relative to the configured destination:
43
+ #
44
+ # assert_no_file "config/random.rb"
45
+ def assert_no_file(relative)
46
+ absolute = File.expand_path(relative, destination_root)
47
+ assert !File.exists?(absolute), "Expected file #{relative.inspect} to not exist, but does"
48
+ end
49
+ alias :assert_no_directory :assert_no_file
50
+
51
+ # Asserts a given migration exists. You need to supply an absolute path or a
52
+ # path relative to the configured destination:
53
+ #
54
+ # assert_migration "db/migrate/create_products.rb"
55
+ #
56
+ # This method manipulates the given path and tries to find any migration which
57
+ # matches the migration name. For example, the call above is converted to:
58
+ #
59
+ # assert_file "db/migrate/003_create_products.rb"
60
+ #
61
+ # Consequently, assert_migration accepts the same arguments has assert_file.
62
+ def assert_migration(relative, *contents, &block)
63
+ file_name = migration_file_name(relative)
64
+ assert file_name, "Expected migration #{relative} to exist, but was not found"
65
+ assert_file file_name, *contents, &block
66
+ end
67
+
68
+ # Asserts a given migration does not exist. You need to supply an absolute path or a
69
+ # path relative to the configured destination:
70
+ #
71
+ # assert_no_migration "db/migrate/create_products.rb"
72
+ def assert_no_migration(relative)
73
+ file_name = migration_file_name(relative)
74
+ assert_nil file_name, "Expected migration #{relative} to not exist, but found #{file_name}"
75
+ end
76
+
77
+ # Asserts the given class method exists in the given content. This method does not detect
78
+ # class methods inside (class << self), only class methods which starts with "self.".
79
+ # When a block is given, it yields the content of the method.
80
+ #
81
+ # assert_migration "db/migrate/create_products.rb" do |migration|
82
+ # assert_class_method :up, migration do |up|
83
+ # assert_match(/create_table/, up)
84
+ # end
85
+ # end
86
+ def assert_class_method(method, content, &block)
87
+ assert_instance_method "self.#{method}", content, &block
88
+ end
89
+
90
+ # Asserts the given method exists in the given content. When a block is given,
91
+ # it yields the content of the method.
92
+ #
93
+ # assert_file "app/controllers/products_controller.rb" do |controller|
94
+ # assert_instance_method :index, controller do |index|
95
+ # assert_match(/Product\.all/, index)
96
+ # end
97
+ # end
98
+ def assert_instance_method(method, content)
99
+ assert content =~ /(\s+)def #{method}(\(.+\))?(.*?)\n\1end/m, "Expected to have method #{method}"
100
+ yield $3.strip if block_given?
101
+ end
102
+ alias :assert_method :assert_instance_method
103
+
104
+ # Asserts the given attribute type gets translated to a field type
105
+ # properly:
106
+ #
107
+ # assert_field_type :date, :date_select
108
+ def assert_field_type(attribute_type, field_type)
109
+ assert_equal(field_type, create_generated_attribute(attribute_type).field_type)
110
+ end
111
+
112
+ # Asserts the given attribute type gets a proper default value:
113
+ #
114
+ # assert_field_default_value :string, "MyString"
115
+ def assert_field_default_value(attribute_type, value)
116
+ assert_equal(value, create_generated_attribute(attribute_type).default)
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,103 @@
1
+ require 'active_support/core_ext/class/attribute'
2
+ require 'active_support/core_ext/module/delegation'
3
+ require 'active_support/core_ext/hash/reverse_merge'
4
+ require 'active_support/core_ext/kernel/reporting'
5
+ require 'active_support/concern'
6
+ require 'rails/generators'
7
+
8
+ module Rails
9
+ module Generators
10
+ module Testing
11
+ module Behaviour
12
+ extend ActiveSupport::Concern
13
+
14
+ included do
15
+ class_attribute :destination_root, :current_path, :generator_class, :default_arguments
16
+
17
+ # Generators frequently change the current path using +FileUtils.cd+.
18
+ # So we need to store the path at file load and revert back to it after each test.
19
+ self.current_path = File.expand_path(Dir.pwd)
20
+ self.default_arguments = []
21
+ end
22
+
23
+ module ClassMethods
24
+ # Sets which generator should be tested:
25
+ #
26
+ # tests AppGenerator
27
+ def tests(klass)
28
+ self.generator_class = klass
29
+ end
30
+
31
+ # Sets default arguments on generator invocation. This can be overwritten when
32
+ # invoking it.
33
+ #
34
+ # arguments %w(app_name --skip-active-record)
35
+ def arguments(array)
36
+ self.default_arguments = array
37
+ end
38
+
39
+ # Sets the destination of generator files:
40
+ #
41
+ # destination File.expand_path("../tmp", File.dirname(__FILE__))
42
+ def destination(path)
43
+ self.destination_root = path
44
+ end
45
+ end
46
+
47
+ # Runs the generator configured for this class. The first argument is an array like
48
+ # command line arguments:
49
+ #
50
+ # class AppGeneratorTest < Rails::Generators::TestCase
51
+ # tests AppGenerator
52
+ # destination File.expand_path("../tmp", File.dirname(__FILE__))
53
+ # teardown :cleanup_destination_root
54
+ #
55
+ # test "database.yml is not created when skipping Active Record" do
56
+ # run_generator %w(myapp --skip-active-record)
57
+ # assert_no_file "config/database.yml"
58
+ # end
59
+ # end
60
+ #
61
+ # You can provide a configuration hash as second argument. This method returns the output
62
+ # printed by the generator.
63
+ def run_generator(args=self.default_arguments, config={})
64
+ capture(:stdout) { self.generator_class.start(args, config.reverse_merge(destination_root: destination_root)) }
65
+ end
66
+
67
+ # Instantiate the generator.
68
+ def generator(args=self.default_arguments, options={}, config={})
69
+ @generator ||= self.generator_class.new(args, options, config.reverse_merge(destination_root: destination_root))
70
+ end
71
+
72
+ # Create a Rails::Generators::GeneratedAttribute by supplying the
73
+ # attribute type and, optionally, the attribute name:
74
+ #
75
+ # create_generated_attribute(:string, 'name')
76
+ def create_generated_attribute(attribute_type, name = 'test', index = nil)
77
+ Rails::Generators::GeneratedAttribute.parse([name, attribute_type, index].compact.join(':'))
78
+ end
79
+
80
+ protected
81
+
82
+ def destination_root_is_set? # :nodoc:
83
+ raise "You need to configure your Rails::Generators::TestCase destination root." unless destination_root
84
+ end
85
+
86
+ def ensure_current_path # :nodoc:
87
+ cd current_path
88
+ end
89
+
90
+ def prepare_destination # :nodoc:
91
+ rm_rf(destination_root)
92
+ mkdir_p(destination_root)
93
+ end
94
+
95
+ def migration_file_name(relative) # :nodoc:
96
+ absolute = File.expand_path(relative, destination_root)
97
+ dirname, file_name = File.dirname(absolute), File.basename(absolute).sub(/\.rb$/, '')
98
+ Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,18 @@
1
+ module Rails
2
+ module Generators
3
+ module Testing
4
+ module SetupAndTeardown
5
+ def setup # :nodoc:
6
+ destination_root_is_set?
7
+ ensure_current_path
8
+ super
9
+ end
10
+
11
+ def teardown # :nodoc:
12
+ ensure_current_path
13
+ super
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module RailsGeneratorsTestCaseModules
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "rails_generators_test_case_modules/version"
2
+
3
+ require "rails/generators/testing/behaviour"
4
+ require "rails/generators/testing/setup_and_teardown"
5
+ require "rails/generators/testing/assertions"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_generators_test_case_modules/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_generators_test_case_modules"
8
+ spec.version = RailsGeneratorsTestCaseModules::VERSION
9
+ spec.authors = ["Pablo Herrero"]
10
+ spec.email = ["pablodherrero@gmail.com"]
11
+ spec.description = %q{Backports for Rails::Generator::TestCase helper modules for Rails 3.2}
12
+ spec.summary = %q{Backports for Rails::Generator::TestCase helper modules for Rails 3.2}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'railties', ['>= 3.2', '< 4.0']
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_generators_test_case_modules
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Pablo Herrero
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ - - <
21
+ - !ruby/object:Gem::Version
22
+ version: '4.0'
23
+ none: false
24
+ prerelease: false
25
+ name: railties
26
+ type: :runtime
27
+ requirement: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ! '>='
30
+ - !ruby/object:Gem::Version
31
+ version: '3.2'
32
+ - - <
33
+ - !ruby/object:Gem::Version
34
+ version: '4.0'
35
+ none: false
36
+ - !ruby/object:Gem::Dependency
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '1.3'
42
+ none: false
43
+ prerelease: false
44
+ name: bundler
45
+ type: :development
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ~>
49
+ - !ruby/object:Gem::Version
50
+ version: '1.3'
51
+ none: false
52
+ - !ruby/object:Gem::Dependency
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ none: false
59
+ prerelease: false
60
+ name: rake
61
+ type: :development
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ none: false
68
+ description: Backports for Rails::Generator::TestCase helper modules for Rails 3.2
69
+ email:
70
+ - pablodherrero@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - .gitignore
76
+ - Gemfile
77
+ - LICENSE.txt
78
+ - README.md
79
+ - Rakefile
80
+ - lib/rails/generators/testing/assertions.rb
81
+ - lib/rails/generators/testing/behaviour.rb
82
+ - lib/rails/generators/testing/setup_and_teardown.rb
83
+ - lib/rails_generators_test_case_modules.rb
84
+ - lib/rails_generators_test_case_modules/version.rb
85
+ - rails_generators_test_case_modules.gemspec
86
+ homepage: ''
87
+ licenses:
88
+ - MIT
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ segments:
98
+ - 0
99
+ hash: 538841381395110015
100
+ version: '0'
101
+ none: false
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ segments:
107
+ - 0
108
+ hash: 538841381395110015
109
+ version: '0'
110
+ none: false
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 1.8.25
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Backports for Rails::Generator::TestCase helper modules for Rails 3.2
117
+ test_files: []