generator_spec 0.0.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.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ pkg/*
4
+ spec/tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,92 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ generator_spec (0.0.1)
5
+ rails (~> 3.0)
6
+ rspec-rails
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ abstract (1.0.0)
12
+ actionmailer (3.0.5)
13
+ actionpack (= 3.0.5)
14
+ mail (~> 2.2.15)
15
+ actionpack (3.0.5)
16
+ activemodel (= 3.0.5)
17
+ activesupport (= 3.0.5)
18
+ builder (~> 2.1.2)
19
+ erubis (~> 2.6.6)
20
+ i18n (~> 0.4)
21
+ rack (~> 1.2.1)
22
+ rack-mount (~> 0.6.13)
23
+ rack-test (~> 0.5.7)
24
+ tzinfo (~> 0.3.23)
25
+ activemodel (3.0.5)
26
+ activesupport (= 3.0.5)
27
+ builder (~> 2.1.2)
28
+ i18n (~> 0.4)
29
+ activerecord (3.0.5)
30
+ activemodel (= 3.0.5)
31
+ activesupport (= 3.0.5)
32
+ arel (~> 2.0.2)
33
+ tzinfo (~> 0.3.23)
34
+ activeresource (3.0.5)
35
+ activemodel (= 3.0.5)
36
+ activesupport (= 3.0.5)
37
+ activesupport (3.0.5)
38
+ arel (2.0.9)
39
+ builder (2.1.2)
40
+ diff-lcs (1.1.2)
41
+ erubis (2.6.6)
42
+ abstract (>= 1.0.0)
43
+ i18n (0.5.0)
44
+ mail (2.2.15)
45
+ activesupport (>= 2.3.6)
46
+ i18n (>= 0.4.0)
47
+ mime-types (~> 1.16)
48
+ treetop (~> 1.4.8)
49
+ mime-types (1.16)
50
+ polyglot (0.3.1)
51
+ rack (1.2.1)
52
+ rack-mount (0.6.13)
53
+ rack (>= 1.0.0)
54
+ rack-test (0.5.7)
55
+ rack (>= 1.0)
56
+ rails (3.0.5)
57
+ actionmailer (= 3.0.5)
58
+ actionpack (= 3.0.5)
59
+ activerecord (= 3.0.5)
60
+ activeresource (= 3.0.5)
61
+ activesupport (= 3.0.5)
62
+ bundler (~> 1.0)
63
+ railties (= 3.0.5)
64
+ railties (3.0.5)
65
+ actionpack (= 3.0.5)
66
+ activesupport (= 3.0.5)
67
+ rake (>= 0.8.7)
68
+ thor (~> 0.14.4)
69
+ rake (0.8.7)
70
+ rspec (2.5.0)
71
+ rspec-core (~> 2.5.0)
72
+ rspec-expectations (~> 2.5.0)
73
+ rspec-mocks (~> 2.5.0)
74
+ rspec-core (2.5.1)
75
+ rspec-expectations (2.5.0)
76
+ diff-lcs (~> 1.1.2)
77
+ rspec-mocks (2.5.0)
78
+ rspec-rails (2.5.0)
79
+ actionpack (~> 3.0)
80
+ activesupport (~> 3.0)
81
+ railties (~> 3.0)
82
+ rspec (~> 2.5.0)
83
+ thor (0.14.6)
84
+ treetop (1.4.9)
85
+ polyglot (>= 0.3.1)
86
+ tzinfo (0.3.24)
87
+
88
+ PLATFORMS
89
+ ruby
90
+
91
+ DEPENDENCIES
92
+ generator_spec!
@@ -0,0 +1,26 @@
1
+ # Generator Spec
2
+
3
+ Allows testing of Rails generators inside RSpec using standard Rails::Generators::TestCase assertion methods.
4
+
5
+ # Usage
6
+
7
+ Add 'generator_spec' to Gemfile and use just like you would test generators in test unit:
8
+
9
+ # spec/lib/generators/test/test_generator_spec.rb
10
+
11
+ require "generator_spec/generator_example_group"
12
+
13
+ describe TestGenerator do
14
+ include GeneratorSpec::GeneratorExampleGroup
15
+
16
+ destination File.expand_path("../../tmp", __FILE__)
17
+
18
+ before(:all) do
19
+ prepare_destination
20
+ run_generator
21
+ end
22
+
23
+ it "creates a test initializer" do
24
+ assert_file "config/initializers/test.rb", "# Initializer"
25
+ end
26
+ end
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "generator_spec/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "generator_spec"
7
+ s.version = GeneratorSpec::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Steve Hodgkiss"]
10
+ s.email = ["steve@hodgkiss.me.uk"]
11
+ s.homepage = "https://github.com/stevehodgkiss/generator_spec"
12
+ s.summary = %q{Test Rails generators with RSpec}
13
+ s.description = %q{Test Rails generators with RSpec}
14
+
15
+ s.rubyforge_project = "generator_spec"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ s.add_dependency "rails", "~> 3.0"
22
+ s.add_dependency "rspec-rails"
23
+ end
File without changes
@@ -0,0 +1,252 @@
1
+ require "rspec/rails"
2
+ require "rails/generators/test_case"
3
+ require "fileutils"
4
+
5
+ # Adapted from Rails::Generators::TestCase
6
+ # https://github.com/rails/rails/blob/master/railties/lib/rails/generators/test_case.rb
7
+
8
+ module GeneratorSpec
9
+ module GeneratorExampleGroup
10
+ extend ActiveSupport::Concern
11
+ include RSpec::Rails::RailsExampleGroup
12
+ include FileUtils
13
+
14
+ included do
15
+ class_attribute :destination_root, :current_path, :generator_class, :default_arguments
16
+ delegate :destination_root, :current_path, :generator_class, :default_arguments, :to => :'self.class'
17
+
18
+ self.current_path = ::File.expand_path(Dir.pwd)
19
+ self.default_arguments = []
20
+
21
+ setup :destination_root_is_set?, :ensure_current_path
22
+ teardown :ensure_current_path
23
+
24
+ tests subject.call.class
25
+ end
26
+
27
+ module ClassMethods
28
+ # Sets which generator should be tested:
29
+ #
30
+ # tests AppGenerator
31
+ #
32
+ def tests(klass)
33
+ self.generator_class = klass
34
+ end
35
+
36
+ # Sets default arguments on generator invocation. This can be overwritten when
37
+ # invoking it.
38
+ #
39
+ # arguments %w(app_name --skip-active-record)
40
+ #
41
+ def arguments(array)
42
+ self.default_arguments = array
43
+ end
44
+
45
+ # Sets the destination of generator files:
46
+ #
47
+ # destination File.expand_path("../tmp", File.dirname(__FILE__))
48
+ #
49
+ def destination(path)
50
+ self.destination_root = path
51
+ end
52
+ end
53
+
54
+ module InstanceMethods
55
+ # Captures the given stream and returns it:
56
+ #
57
+ # stream = capture(:stdout){ puts "Cool" }
58
+ # stream # => "Cool\n"
59
+ #
60
+ def capture(stream)
61
+ begin
62
+ stream = stream.to_s
63
+ eval "$#{stream} = StringIO.new"
64
+ yield
65
+ result = eval("$#{stream}").string
66
+ ensure
67
+ eval("$#{stream} = #{stream.upcase}")
68
+ end
69
+
70
+ result
71
+ end
72
+ alias :silence :capture
73
+
74
+ # Asserts a given file exists. You need to supply an absolute path or a path relative
75
+ # to the configured destination:
76
+ #
77
+ # assert_file "config/environment.rb"
78
+ #
79
+ # You can also give extra arguments. If the argument is a regexp, it will check if the
80
+ # regular expression matches the given file content. If it's a string, it compares the
81
+ # file with the given string:
82
+ #
83
+ # assert_file "config/environment.rb", /initialize/
84
+ #
85
+ # Finally, when a block is given, it yields the file content:
86
+ #
87
+ # assert_file "app/controller/products_controller.rb" do |controller|
88
+ # assert_instance_method :index, content do |index|
89
+ # assert_match /Product\.all/, index
90
+ # end
91
+ # end
92
+ #
93
+ def assert_file(relative, *contents)
94
+ absolute = File.expand_path(relative, destination_root)
95
+ assert File.exists?(absolute), "Expected file #{relative.inspect} to exist, but does not"
96
+
97
+ read = File.read(absolute) if block_given? || !contents.empty?
98
+ yield read if block_given?
99
+
100
+ contents.each do |content|
101
+ case content
102
+ when String
103
+ assert_equal content, read
104
+ when Regexp
105
+ assert_match content, read
106
+ end
107
+ end
108
+ end
109
+ alias :assert_directory :assert_file
110
+
111
+ # Asserts a given file does not exist. You need to supply an absolute path or a
112
+ # path relative to the configured destination:
113
+ #
114
+ # assert_no_file "config/random.rb"
115
+ #
116
+ def assert_no_file(relative)
117
+ absolute = File.expand_path(relative, destination_root)
118
+ assert !File.exists?(absolute), "Expected file #{relative.inspect} to not exist, but does"
119
+ end
120
+ alias :assert_no_directory :assert_no_file
121
+
122
+ # Asserts a given file does not exist. You need to supply an absolute path or a
123
+ # path relative to the configured destination:
124
+ #
125
+ # assert_migration "db/migrate/create_products.rb"
126
+ #
127
+ # This method manipulates the given path and tries to find any migration which
128
+ # matches the migration name. For example, the call above is converted to:
129
+ #
130
+ # assert_file "db/migrate/003_create_products.rb"
131
+ #
132
+ # Consequently, assert_migration accepts the same arguments has assert_file.
133
+ #
134
+ def assert_migration(relative, *contents, &block)
135
+ file_name = migration_file_name(relative)
136
+ assert file_name, "Expected migration #{relative} to exist, but was not found"
137
+ assert_file file_name, *contents, &block
138
+ end
139
+
140
+ # Asserts a given migration does not exist. You need to supply an absolute path or a
141
+ # path relative to the configured destination:
142
+ #
143
+ # assert_no_file "config/random.rb"
144
+ #
145
+ def assert_no_migration(relative)
146
+ file_name = migration_file_name(relative)
147
+ assert_nil file_name, "Expected migration #{relative} to not exist, but found #{file_name}"
148
+ end
149
+
150
+ # Asserts the given class method exists in the given content. This method does not detect
151
+ # class methods inside (class << self), only class methods which starts with "self.".
152
+ # When a block is given, it yields the content of the method.
153
+ #
154
+ # assert_migration "db/migrate/create_products.rb" do |migration|
155
+ # assert_class_method :up, migration do |up|
156
+ # assert_match /create_table/, up
157
+ # end
158
+ # end
159
+ #
160
+ def assert_class_method(method, content, &block)
161
+ assert_instance_method "self.#{method}", content, &block
162
+ end
163
+
164
+ # Asserts the given method exists in the given content. When a block is given,
165
+ # it yields the content of the method.
166
+ #
167
+ # assert_file "app/controller/products_controller.rb" do |controller|
168
+ # assert_instance_method :index, content do |index|
169
+ # assert_match /Product\.all/, index
170
+ # end
171
+ # end
172
+ #
173
+ def assert_instance_method(method, content)
174
+ assert content =~ /def #{method}(\(.+\))?(.*?)\n end/m, "Expected to have method #{method}"
175
+ yield $2.strip if block_given?
176
+ end
177
+ alias :assert_method :assert_instance_method
178
+
179
+ # Asserts the given attribute type gets translated to a field type
180
+ # properly:
181
+ #
182
+ # assert_field_type :date, :date_select
183
+ #
184
+ def assert_field_type(attribute_type, field_type)
185
+ assert_equal(field_type, create_generated_attribute(attribute_type).field_type)
186
+ end
187
+
188
+ # Asserts the given attribute type gets a proper default value:
189
+ #
190
+ # assert_field_type :string, "MyString"
191
+ #
192
+ def assert_field_default_value(attribute_type, value)
193
+ assert_equal(value, create_generated_attribute(attribute_type).default)
194
+ end
195
+
196
+ # Runs the generator configured for this class. The first argument is an array like
197
+ # command line arguments:
198
+ #
199
+ # class AppGeneratorTest < Rails::Generators::TestCase
200
+ # tests AppGenerator
201
+ # destination File.expand_path("../tmp", File.dirname(__FILE__))
202
+ # teardown :cleanup_destination_root
203
+ #
204
+ # test "database.yml is not created when skipping Active Record" do
205
+ # run_generator %w(myapp --skip-active-record)
206
+ # assert_no_file "config/database.yml"
207
+ # end
208
+ # end
209
+ #
210
+ # You can provide a configuration hash as second argument. This method returns the output
211
+ # printed by the generator.
212
+ def run_generator(args=self.default_arguments, config={})
213
+ capture(:stdout) { self.generator_class.start(args, config.reverse_merge(:destination_root => destination_root)) }
214
+ end
215
+
216
+ # Instantiate the generator.
217
+ def generator(args=self.default_arguments, options={}, config={})
218
+ @generator ||= self.generator_class.new(args, options, config.reverse_merge(:destination_root => destination_root))
219
+ end
220
+
221
+ # Create a Rails::Generators::GeneratedAttribute by supplying the
222
+ # attribute type and, optionally, the attribute name:
223
+ #
224
+ # create_generated_attribute(:string, 'name')
225
+ #
226
+ def create_generated_attribute(attribute_type, name = 'test')
227
+ Rails::Generators::GeneratedAttribute.new(name, attribute_type.to_s)
228
+ end
229
+
230
+ protected
231
+
232
+ def destination_root_is_set? #:nodoc:
233
+ raise "You need to configure your Rails::Generators::TestCase destination root." unless destination_root
234
+ end
235
+
236
+ def ensure_current_path #:nodoc:
237
+ cd current_path
238
+ end
239
+
240
+ def prepare_destination
241
+ rm_rf(destination_root)
242
+ mkdir_p(destination_root)
243
+ end
244
+
245
+ def migration_file_name(relative) #:nodoc:
246
+ absolute = File.expand_path(relative, destination_root)
247
+ dirname, file_name = File.dirname(absolute), File.basename(absolute).sub(/\.rb$/, '')
248
+ Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first
249
+ end
250
+ end
251
+ end
252
+ end
@@ -0,0 +1,3 @@
1
+ module GeneratorSpec
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+
3
+ describe TestGenerator, "using normal assert methods" do
4
+ include GeneratorSpec::GeneratorExampleGroup
5
+
6
+ destination File.expand_path("../../tmp", __FILE__)
7
+
8
+ before(:all) do
9
+ prepare_destination
10
+ run_generator
11
+ end
12
+
13
+ it "creates a test initializer" do
14
+ assert_file "config/initializers/test.rb", "# Initializer"
15
+ end
16
+
17
+ it "creates a migration" do
18
+ assert_migration "db/migrate/create_tests.rb"
19
+ end
20
+
21
+ it "removes files" do
22
+ assert_no_file ".gitignore"
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ require 'bundler/setup'
2
+ require 'rails/all'
3
+ require 'rspec/rails'
4
+ require 'generator_spec/generator_example_group'
5
+
6
+ Dir[Pathname.new(File.expand_path("../", __FILE__)).join("support/*/*.rb")].each {|f| require f}
7
+
8
+ RSpec.configure do |config|
9
+
10
+ end
@@ -0,0 +1,3 @@
1
+ class TestMigration < ActiveRecord::Migration
2
+
3
+ end
@@ -0,0 +1,11 @@
1
+ class TestGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def copy_initializer
5
+ template "initializer.rb", "config/initializers/test.rb"
6
+ end
7
+
8
+ def create_migration
9
+ template "migration.rb", "db/migrate/123_create_tests.rb"
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: generator_spec
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Steve Hodgkiss
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-01 00:00:00 +00:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: "3.0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-rails
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: :runtime
37
+ version_requirements: *id002
38
+ description: Test Rails generators with RSpec
39
+ email:
40
+ - steve@hodgkiss.me.uk
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - .gitignore
49
+ - .rspec
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - README.md
53
+ - Rakefile
54
+ - generator_spec.gemspec
55
+ - lib/generator_spec.rb
56
+ - lib/generator_spec/generator_example_group.rb
57
+ - lib/generator_spec/version.rb
58
+ - spec/generator_spec/generator_example_group_spec.rb
59
+ - spec/spec_helper.rb
60
+ - spec/support/test_generator/templates/initializer.rb
61
+ - spec/support/test_generator/templates/migration.rb
62
+ - spec/support/test_generator/test_generator.rb
63
+ has_rdoc: true
64
+ homepage: https://github.com/stevehodgkiss/generator_spec
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options: []
69
+
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project: generator_spec
87
+ rubygems_version: 1.5.2
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Test Rails generators with RSpec
91
+ test_files:
92
+ - spec/generator_spec/generator_example_group_spec.rb
93
+ - spec/spec_helper.rb
94
+ - spec/support/test_generator/templates/initializer.rb
95
+ - spec/support/test_generator/templates/migration.rb
96
+ - spec/support/test_generator/test_generator.rb