generator_spec 0.0.3 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -1
- data/Gemfile.lock +3 -1
- data/README.md +11 -5
- data/lib/generator_spec/matcher.rb +1 -2
- data/lib/generator_spec/test_case.rb +48 -0
- data/lib/generator_spec/version.rb +1 -1
- data/spec/generator_spec/matcher_spec.rb +238 -1
- data/spec/generator_spec/test_case_spec.rb +48 -0
- data/spec/spec_helper.rb +4 -3
- data/spec/support/file_system.rb +11 -0
- metadata +7 -5
- data/lib/generator_spec/generator_example_group.rb +0 -254
- data/spec/generator_spec/generator_example_group_spec.rb +0 -24
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
generator_spec (0.0.
|
4
|
+
generator_spec (0.0.3)
|
5
5
|
rails (~> 3.0)
|
6
6
|
rspec-rails
|
7
7
|
|
@@ -40,6 +40,7 @@ GEM
|
|
40
40
|
diff-lcs (1.1.2)
|
41
41
|
erubis (2.6.6)
|
42
42
|
abstract (>= 1.0.0)
|
43
|
+
fakefs (0.3.1)
|
43
44
|
i18n (0.5.0)
|
44
45
|
mail (2.2.15)
|
45
46
|
activesupport (>= 2.3.6)
|
@@ -89,4 +90,5 @@ PLATFORMS
|
|
89
90
|
ruby
|
90
91
|
|
91
92
|
DEPENDENCIES
|
93
|
+
fakefs
|
92
94
|
generator_spec!
|
data/README.md
CHANGED
@@ -1,17 +1,23 @@
|
|
1
1
|
# Generator Spec
|
2
2
|
|
3
|
-
|
3
|
+
Test Rails generators with RSpec using the standard Rails::Generators::TestCase assertion methods.
|
4
4
|
|
5
5
|
# Usage
|
6
6
|
|
7
|
-
|
7
|
+
Gemfile:
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem "generator_spec"
|
11
|
+
end
|
12
|
+
|
13
|
+
Spec:
|
8
14
|
|
9
15
|
# spec/lib/generators/test/test_generator_spec.rb
|
10
16
|
|
11
|
-
require "generator_spec/
|
17
|
+
require "generator_spec/test_case"
|
12
18
|
|
13
19
|
describe TestGenerator do
|
14
|
-
include GeneratorSpec::
|
20
|
+
include GeneratorSpec::TestCase
|
15
21
|
|
16
22
|
destination File.expand_path("../../tmp", __FILE__)
|
17
23
|
|
@@ -28,7 +34,7 @@ Add 'generator_spec' to Gemfile and use just like you would test generators in t
|
|
28
34
|
An RSpec file matching DSL is also provided, taken with permission from [beard](https://github.com/carlhuda/beard/blob/master/spec/support/matcher.rb) by [carlhuda](https://github.com/carlhuda).
|
29
35
|
|
30
36
|
describe TestGenerator, "using custom matcher" do
|
31
|
-
include GeneratorSpec::
|
37
|
+
include GeneratorSpec::TestCase
|
32
38
|
|
33
39
|
destination File.expand_path("../../tmp", __FILE__)
|
34
40
|
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require "rspec/rails"
|
2
|
+
require "rails/generators/test_case"
|
3
|
+
require "fileutils"
|
4
|
+
require "generator_spec/matcher"
|
5
|
+
|
6
|
+
module GeneratorSpec
|
7
|
+
module TestCase
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
include RSpec::Rails::RailsExampleGroup
|
10
|
+
include Matcher
|
11
|
+
include FileUtils
|
12
|
+
|
13
|
+
included do
|
14
|
+
cattr_accessor :test_case, :test_case_instance
|
15
|
+
self.test_case = Class.new(Rails::Generators::TestCase)
|
16
|
+
self.test_case.tests subject.call.class
|
17
|
+
self.test_case_instance = self.test_case.new(:test)
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
def tests(klass)
|
22
|
+
self.test_case.generator_class = klass
|
23
|
+
end
|
24
|
+
|
25
|
+
def arguments(array)
|
26
|
+
self.test_case.default_arguments = array
|
27
|
+
end
|
28
|
+
|
29
|
+
def destination(path)
|
30
|
+
self.test_case.destination_root = path
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module InstanceMethods
|
35
|
+
def method_missing(method_sym, *arguments, &block)
|
36
|
+
self.test_case_instance.send(method_sym, *arguments, &block)
|
37
|
+
end
|
38
|
+
|
39
|
+
def respond_to?(method_sym, include_private = false)
|
40
|
+
if self.test_case_instance.respond_to?(method_sym)
|
41
|
+
true
|
42
|
+
else
|
43
|
+
super
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe TestGenerator, "using custom matcher" do
|
4
|
-
include GeneratorSpec::
|
4
|
+
include GeneratorSpec::TestCase
|
5
5
|
|
6
6
|
destination File.expand_path("../../tmp", __FILE__)
|
7
7
|
|
@@ -30,4 +30,241 @@ describe TestGenerator, "using custom matcher" do
|
|
30
30
|
end
|
31
31
|
}
|
32
32
|
end
|
33
|
+
end
|
34
|
+
|
35
|
+
TMP_ROOT = Pathname.new(File.expand_path("../../tmp"))
|
36
|
+
|
37
|
+
module GeneratorSpec::Matcher
|
38
|
+
describe File do
|
39
|
+
describe "#matches?" do
|
40
|
+
include FakeFS::SpecHelpers
|
41
|
+
|
42
|
+
before do
|
43
|
+
@file = File.new("test_file")
|
44
|
+
@location = TMP_ROOT.join("test_file")
|
45
|
+
end
|
46
|
+
|
47
|
+
context "with no contains" do
|
48
|
+
it "doesn't throw if the file exists" do
|
49
|
+
write_file(@location, "")
|
50
|
+
expect {
|
51
|
+
@file.matches?(TMP_ROOT)
|
52
|
+
}.to_not throw_symbol
|
53
|
+
end
|
54
|
+
|
55
|
+
it "throws :failure if it doesn't exist" do
|
56
|
+
expect {
|
57
|
+
@file.matches?(TMP_ROOT)
|
58
|
+
}.to throw_symbol(:failure)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "with contains" do
|
63
|
+
before do
|
64
|
+
write_file(@location, "class CreatePosts")
|
65
|
+
end
|
66
|
+
|
67
|
+
it "doesn't throw if the content includes the string" do
|
68
|
+
@file.contains "CreatePosts"
|
69
|
+
expect {
|
70
|
+
@file.matches?(TMP_ROOT)
|
71
|
+
}.to_not throw_symbol
|
72
|
+
end
|
73
|
+
|
74
|
+
it "throws :failure if the contents don't include the string" do
|
75
|
+
@file.contains "PostsMigration"
|
76
|
+
expect {
|
77
|
+
@file.matches?(TMP_ROOT)
|
78
|
+
}.to throw_symbol(:failure)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe Migration do
|
85
|
+
include FakeFS::SpecHelpers
|
86
|
+
|
87
|
+
describe "#matches?" do
|
88
|
+
before do
|
89
|
+
@migration = Migration.new("create_posts")
|
90
|
+
@location = TMP_ROOT.join("123456_create_posts.rb")
|
91
|
+
end
|
92
|
+
|
93
|
+
context "with no contains" do
|
94
|
+
it "doesn't throw if the migration exists" do
|
95
|
+
write_file(@location, "class CreatePosts")
|
96
|
+
expect {
|
97
|
+
@migration.matches?(TMP_ROOT)
|
98
|
+
}.to_not throw_symbol
|
99
|
+
end
|
100
|
+
|
101
|
+
it "throws :failure if it doesn't exist" do
|
102
|
+
expect {
|
103
|
+
@migration.matches?(TMP_ROOT)
|
104
|
+
}.to throw_symbol(:failure)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "with contains" do
|
109
|
+
before do
|
110
|
+
write_file(@location, "class CreatePosts")
|
111
|
+
end
|
112
|
+
|
113
|
+
it "doesn't throw if the migration includes the given content" do
|
114
|
+
@migration.contains("CreatePosts")
|
115
|
+
expect {
|
116
|
+
@migration.matches?(TMP_ROOT)
|
117
|
+
}.to_not throw_symbol
|
118
|
+
end
|
119
|
+
|
120
|
+
it "throws failure if the migration doesn't include the given content" do
|
121
|
+
@migration.contains("CreateNotes")
|
122
|
+
expect {
|
123
|
+
@migration.matches?(TMP_ROOT)
|
124
|
+
}.to throw_symbol(:failure)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe Directory do
|
131
|
+
include FakeFS::SpecHelpers
|
132
|
+
|
133
|
+
describe "#location" do
|
134
|
+
it "equals the full path" do
|
135
|
+
Directory.new("test").location("test_2").should eq("test/test_2")
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe "#directory" do
|
140
|
+
context "without a block" do
|
141
|
+
it "adds a directory name to the tree" do
|
142
|
+
dir = Directory.new "test" do
|
143
|
+
directory "dir"
|
144
|
+
end
|
145
|
+
dir.tree["dir"].should eq(false)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context "with a block" do
|
150
|
+
it "add a directory object to the tree" do
|
151
|
+
dir = Directory.new "test" do
|
152
|
+
directory "dir" do
|
153
|
+
directory "test_2"
|
154
|
+
end
|
155
|
+
end
|
156
|
+
dir.tree["dir"].should be_an_instance_of(Directory)
|
157
|
+
dir.tree["dir"].tree["test_2"].should eq(false)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe "#file" do
|
163
|
+
it "adds it to the tree" do
|
164
|
+
dir = Directory.new "test" do
|
165
|
+
file "test_file"
|
166
|
+
end
|
167
|
+
dir.tree["test_file"].should be_an_instance_of(File)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
describe "#file" do
|
172
|
+
it "adds it to the tree" do
|
173
|
+
dir = Directory.new "test" do
|
174
|
+
migration "test_file"
|
175
|
+
end
|
176
|
+
dir.tree["test_file"].should be_an_instance_of(Migration)
|
177
|
+
dir.tree["test_file"].instance_variable_get("@name").should eq("test/test_file")
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
describe "#matches?" do
|
182
|
+
context "with a directory name" do
|
183
|
+
before do
|
184
|
+
@dir = Directory.new "test" do
|
185
|
+
directory "test_2"
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
it "doesn't throw if the directory exists" do
|
190
|
+
write_directory(TMP_ROOT.join("test/test_2"))
|
191
|
+
expect {
|
192
|
+
@dir.matches?(TMP_ROOT)
|
193
|
+
}.to_not throw_symbol
|
194
|
+
end
|
195
|
+
|
196
|
+
it "throws :failure if it doesn't exist" do
|
197
|
+
expect {
|
198
|
+
@dir.matches?(TMP_ROOT)
|
199
|
+
}.to throw_symbol(:failure)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
context "with a directory object" do
|
204
|
+
before do
|
205
|
+
@dir = Directory.new "test" do
|
206
|
+
directory "test_2" do
|
207
|
+
file "test_file"
|
208
|
+
end
|
209
|
+
end
|
210
|
+
write_directory(TMP_ROOT.join("test/test_2"))
|
211
|
+
end
|
212
|
+
|
213
|
+
it "doesn't throw if the file exists" do
|
214
|
+
write_file(TMP_ROOT.join("test/test_2/test_file"), "")
|
215
|
+
expect {
|
216
|
+
@dir.matches?(TMP_ROOT)
|
217
|
+
}.to_not throw_symbol
|
218
|
+
end
|
219
|
+
|
220
|
+
it "throws :failure if it doesn't exist" do
|
221
|
+
expect {
|
222
|
+
@dir.matches?(TMP_ROOT)
|
223
|
+
}.to throw_symbol(:failure)
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
context "#no_file" do
|
228
|
+
before do
|
229
|
+
@dir = Directory.new "test" do
|
230
|
+
no_file "test_file"
|
231
|
+
end
|
232
|
+
write_directory(TMP_ROOT.join("test"))
|
233
|
+
end
|
234
|
+
|
235
|
+
it "doesn't throw if the file exist" do
|
236
|
+
expect {
|
237
|
+
@dir.matches?(TMP_ROOT)
|
238
|
+
}.to_not throw_symbol
|
239
|
+
end
|
240
|
+
|
241
|
+
it "throws if the file exists" do
|
242
|
+
write_file(TMP_ROOT.join("test/test_file"), "")
|
243
|
+
expect {
|
244
|
+
@dir.matches?(TMP_ROOT)
|
245
|
+
}.to throw_symbol(:failure)
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
end
|
251
|
+
|
252
|
+
describe Root do
|
253
|
+
describe "#matches?" do
|
254
|
+
before do
|
255
|
+
@root = Root.new "test" do
|
256
|
+
directory "test_dir"
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
it "returns false on no failures" do
|
261
|
+
write_directory(TMP_ROOT.join("test/test_dir"))
|
262
|
+
@root.matches?(TMP_ROOT).should be_true
|
263
|
+
end
|
264
|
+
|
265
|
+
it "returns true on failures" do
|
266
|
+
@root.matches?(TMP_ROOT).should be_false
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
33
270
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
class TestClass
|
4
|
+
|
5
|
+
end
|
6
|
+
|
7
|
+
describe GeneratorSpec::TestCase do
|
8
|
+
before do
|
9
|
+
@klass = Class.new do
|
10
|
+
self.should_receive(:subject).and_return(Proc.new {TestClass.new})
|
11
|
+
include GeneratorSpec::TestCase
|
12
|
+
end
|
13
|
+
@klass.test_case_instance = mock
|
14
|
+
end
|
15
|
+
|
16
|
+
it "passes unknown messages on to test_case_instance" do
|
17
|
+
@klass.test_case_instance.should_receive(:assert_file).with("test")
|
18
|
+
@klass.new.assert_file("test")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "handles respond_to accordingly" do
|
22
|
+
@klass.test_case_instance.should_receive(:respond_to?).with(:assert_no_file).and_return(true)
|
23
|
+
@klass.new.respond_to?(:assert_no_file).should be_true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe TestGenerator, "using normal assert methods" do
|
28
|
+
include GeneratorSpec::TestCase
|
29
|
+
|
30
|
+
destination File.expand_path("../../tmp", __FILE__)
|
31
|
+
|
32
|
+
before(:all) do
|
33
|
+
prepare_destination
|
34
|
+
run_generator
|
35
|
+
end
|
36
|
+
|
37
|
+
it "creates a test initializer" do
|
38
|
+
assert_file "config/initializers/test.rb", "# Initializer"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "creates a migration" do
|
42
|
+
assert_migration "db/migrate/create_tests.rb"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "removes files" do
|
46
|
+
assert_no_file ".gitignore"
|
47
|
+
end
|
48
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require 'bundler/setup'
|
2
2
|
require 'rails/all'
|
3
3
|
require 'rspec/rails'
|
4
|
-
require 'generator_spec/
|
4
|
+
require 'generator_spec/test_case'
|
5
|
+
require 'fakefs/spec_helpers'
|
5
6
|
|
6
|
-
Dir[Pathname.new(File.expand_path("../", __FILE__)).join("support
|
7
|
+
Dir[Pathname.new(File.expand_path("../", __FILE__)).join("support/**/*.rb")].each {|f| require f}
|
7
8
|
|
8
9
|
RSpec.configure do |config|
|
9
|
-
|
10
|
+
config.include Helpers::FileSystem
|
10
11
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: generator_spec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.8.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Steve Hodgkiss
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-02 00:00:00 +00:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -53,12 +53,13 @@ files:
|
|
53
53
|
- Rakefile
|
54
54
|
- generator_spec.gemspec
|
55
55
|
- lib/generator_spec.rb
|
56
|
-
- lib/generator_spec/generator_example_group.rb
|
57
56
|
- lib/generator_spec/matcher.rb
|
57
|
+
- lib/generator_spec/test_case.rb
|
58
58
|
- lib/generator_spec/version.rb
|
59
|
-
- spec/generator_spec/generator_example_group_spec.rb
|
60
59
|
- spec/generator_spec/matcher_spec.rb
|
60
|
+
- spec/generator_spec/test_case_spec.rb
|
61
61
|
- spec/spec_helper.rb
|
62
|
+
- spec/support/file_system.rb
|
62
63
|
- spec/support/test_generator/templates/initializer.rb
|
63
64
|
- spec/support/test_generator/templates/migration.rb
|
64
65
|
- spec/support/test_generator/test_generator.rb
|
@@ -91,9 +92,10 @@ signing_key:
|
|
91
92
|
specification_version: 3
|
92
93
|
summary: Test Rails generators with RSpec
|
93
94
|
test_files:
|
94
|
-
- spec/generator_spec/generator_example_group_spec.rb
|
95
95
|
- spec/generator_spec/matcher_spec.rb
|
96
|
+
- spec/generator_spec/test_case_spec.rb
|
96
97
|
- spec/spec_helper.rb
|
98
|
+
- spec/support/file_system.rb
|
97
99
|
- spec/support/test_generator/templates/initializer.rb
|
98
100
|
- spec/support/test_generator/templates/migration.rb
|
99
101
|
- spec/support/test_generator/test_generator.rb
|
@@ -1,254 +0,0 @@
|
|
1
|
-
require "rspec/rails"
|
2
|
-
require "rails/generators/test_case"
|
3
|
-
require "fileutils"
|
4
|
-
require "generator_spec/matcher"
|
5
|
-
|
6
|
-
# Adapted from Rails::Generators::TestCase
|
7
|
-
# https://github.com/rails/rails/blob/master/railties/lib/rails/generators/test_case.rb
|
8
|
-
|
9
|
-
module GeneratorSpec
|
10
|
-
module GeneratorExampleGroup
|
11
|
-
extend ActiveSupport::Concern
|
12
|
-
include RSpec::Rails::RailsExampleGroup
|
13
|
-
include Matcher
|
14
|
-
include FileUtils
|
15
|
-
|
16
|
-
included do
|
17
|
-
class_attribute :destination_root, :current_path, :generator_class, :default_arguments
|
18
|
-
delegate :destination_root, :current_path, :generator_class, :default_arguments, :to => :'self.class'
|
19
|
-
|
20
|
-
self.current_path = ::File.expand_path(Dir.pwd)
|
21
|
-
self.default_arguments = []
|
22
|
-
|
23
|
-
setup :destination_root_is_set?, :ensure_current_path
|
24
|
-
teardown :ensure_current_path
|
25
|
-
|
26
|
-
tests subject.call.class
|
27
|
-
end
|
28
|
-
|
29
|
-
module ClassMethods
|
30
|
-
# Sets which generator should be tested:
|
31
|
-
#
|
32
|
-
# tests AppGenerator
|
33
|
-
#
|
34
|
-
def tests(klass)
|
35
|
-
self.generator_class = klass
|
36
|
-
end
|
37
|
-
|
38
|
-
# Sets default arguments on generator invocation. This can be overwritten when
|
39
|
-
# invoking it.
|
40
|
-
#
|
41
|
-
# arguments %w(app_name --skip-active-record)
|
42
|
-
#
|
43
|
-
def arguments(array)
|
44
|
-
self.default_arguments = array
|
45
|
-
end
|
46
|
-
|
47
|
-
# Sets the destination of generator files:
|
48
|
-
#
|
49
|
-
# destination File.expand_path("../tmp", File.dirname(__FILE__))
|
50
|
-
#
|
51
|
-
def destination(path)
|
52
|
-
self.destination_root = path
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
module InstanceMethods
|
57
|
-
# Captures the given stream and returns it:
|
58
|
-
#
|
59
|
-
# stream = capture(:stdout){ puts "Cool" }
|
60
|
-
# stream # => "Cool\n"
|
61
|
-
#
|
62
|
-
def capture(stream)
|
63
|
-
begin
|
64
|
-
stream = stream.to_s
|
65
|
-
eval "$#{stream} = StringIO.new"
|
66
|
-
yield
|
67
|
-
result = eval("$#{stream}").string
|
68
|
-
ensure
|
69
|
-
eval("$#{stream} = #{stream.upcase}")
|
70
|
-
end
|
71
|
-
|
72
|
-
result
|
73
|
-
end
|
74
|
-
alias :silence :capture
|
75
|
-
|
76
|
-
# Asserts a given file exists. You need to supply an absolute path or a path relative
|
77
|
-
# to the configured destination:
|
78
|
-
#
|
79
|
-
# assert_file "config/environment.rb"
|
80
|
-
#
|
81
|
-
# You can also give extra arguments. If the argument is a regexp, it will check if the
|
82
|
-
# regular expression matches the given file content. If it's a string, it compares the
|
83
|
-
# file with the given string:
|
84
|
-
#
|
85
|
-
# assert_file "config/environment.rb", /initialize/
|
86
|
-
#
|
87
|
-
# Finally, when a block is given, it yields the file content:
|
88
|
-
#
|
89
|
-
# assert_file "app/controller/products_controller.rb" do |controller|
|
90
|
-
# assert_instance_method :index, content do |index|
|
91
|
-
# assert_match /Product\.all/, index
|
92
|
-
# end
|
93
|
-
# end
|
94
|
-
#
|
95
|
-
def assert_file(relative, *contents)
|
96
|
-
absolute = File.expand_path(relative, destination_root)
|
97
|
-
assert File.exists?(absolute), "Expected file #{relative.inspect} to exist, but does not"
|
98
|
-
|
99
|
-
read = File.read(absolute) if block_given? || !contents.empty?
|
100
|
-
yield read if block_given?
|
101
|
-
|
102
|
-
contents.each do |content|
|
103
|
-
case content
|
104
|
-
when String
|
105
|
-
assert_equal content, read
|
106
|
-
when Regexp
|
107
|
-
assert_match content, read
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
alias :assert_directory :assert_file
|
112
|
-
|
113
|
-
# Asserts a given file does not exist. You need to supply an absolute path or a
|
114
|
-
# path relative to the configured destination:
|
115
|
-
#
|
116
|
-
# assert_no_file "config/random.rb"
|
117
|
-
#
|
118
|
-
def assert_no_file(relative)
|
119
|
-
absolute = File.expand_path(relative, destination_root)
|
120
|
-
assert !File.exists?(absolute), "Expected file #{relative.inspect} to not exist, but does"
|
121
|
-
end
|
122
|
-
alias :assert_no_directory :assert_no_file
|
123
|
-
|
124
|
-
# Asserts a given file does not exist. You need to supply an absolute path or a
|
125
|
-
# path relative to the configured destination:
|
126
|
-
#
|
127
|
-
# assert_migration "db/migrate/create_products.rb"
|
128
|
-
#
|
129
|
-
# This method manipulates the given path and tries to find any migration which
|
130
|
-
# matches the migration name. For example, the call above is converted to:
|
131
|
-
#
|
132
|
-
# assert_file "db/migrate/003_create_products.rb"
|
133
|
-
#
|
134
|
-
# Consequently, assert_migration accepts the same arguments has assert_file.
|
135
|
-
#
|
136
|
-
def assert_migration(relative, *contents, &block)
|
137
|
-
file_name = migration_file_name(relative)
|
138
|
-
assert file_name, "Expected migration #{relative} to exist, but was not found"
|
139
|
-
assert_file file_name, *contents, &block
|
140
|
-
end
|
141
|
-
|
142
|
-
# Asserts a given migration does not exist. You need to supply an absolute path or a
|
143
|
-
# path relative to the configured destination:
|
144
|
-
#
|
145
|
-
# assert_no_file "config/random.rb"
|
146
|
-
#
|
147
|
-
def assert_no_migration(relative)
|
148
|
-
file_name = migration_file_name(relative)
|
149
|
-
assert_nil file_name, "Expected migration #{relative} to not exist, but found #{file_name}"
|
150
|
-
end
|
151
|
-
|
152
|
-
# Asserts the given class method exists in the given content. This method does not detect
|
153
|
-
# class methods inside (class << self), only class methods which starts with "self.".
|
154
|
-
# When a block is given, it yields the content of the method.
|
155
|
-
#
|
156
|
-
# assert_migration "db/migrate/create_products.rb" do |migration|
|
157
|
-
# assert_class_method :up, migration do |up|
|
158
|
-
# assert_match /create_table/, up
|
159
|
-
# end
|
160
|
-
# end
|
161
|
-
#
|
162
|
-
def assert_class_method(method, content, &block)
|
163
|
-
assert_instance_method "self.#{method}", content, &block
|
164
|
-
end
|
165
|
-
|
166
|
-
# Asserts the given method exists in the given content. When a block is given,
|
167
|
-
# it yields the content of the method.
|
168
|
-
#
|
169
|
-
# assert_file "app/controller/products_controller.rb" do |controller|
|
170
|
-
# assert_instance_method :index, content do |index|
|
171
|
-
# assert_match /Product\.all/, index
|
172
|
-
# end
|
173
|
-
# end
|
174
|
-
#
|
175
|
-
def assert_instance_method(method, content)
|
176
|
-
assert content =~ /def #{method}(\(.+\))?(.*?)\n end/m, "Expected to have method #{method}"
|
177
|
-
yield $2.strip if block_given?
|
178
|
-
end
|
179
|
-
alias :assert_method :assert_instance_method
|
180
|
-
|
181
|
-
# Asserts the given attribute type gets translated to a field type
|
182
|
-
# properly:
|
183
|
-
#
|
184
|
-
# assert_field_type :date, :date_select
|
185
|
-
#
|
186
|
-
def assert_field_type(attribute_type, field_type)
|
187
|
-
assert_equal(field_type, create_generated_attribute(attribute_type).field_type)
|
188
|
-
end
|
189
|
-
|
190
|
-
# Asserts the given attribute type gets a proper default value:
|
191
|
-
#
|
192
|
-
# assert_field_type :string, "MyString"
|
193
|
-
#
|
194
|
-
def assert_field_default_value(attribute_type, value)
|
195
|
-
assert_equal(value, create_generated_attribute(attribute_type).default)
|
196
|
-
end
|
197
|
-
|
198
|
-
# Runs the generator configured for this class. The first argument is an array like
|
199
|
-
# command line arguments:
|
200
|
-
#
|
201
|
-
# class AppGeneratorTest < Rails::Generators::TestCase
|
202
|
-
# tests AppGenerator
|
203
|
-
# destination File.expand_path("../tmp", File.dirname(__FILE__))
|
204
|
-
# teardown :cleanup_destination_root
|
205
|
-
#
|
206
|
-
# test "database.yml is not created when skipping Active Record" do
|
207
|
-
# run_generator %w(myapp --skip-active-record)
|
208
|
-
# assert_no_file "config/database.yml"
|
209
|
-
# end
|
210
|
-
# end
|
211
|
-
#
|
212
|
-
# You can provide a configuration hash as second argument. This method returns the output
|
213
|
-
# printed by the generator.
|
214
|
-
def run_generator(args=self.default_arguments, config={})
|
215
|
-
capture(:stdout) { self.generator_class.start(args, config.reverse_merge(:destination_root => destination_root)) }
|
216
|
-
end
|
217
|
-
|
218
|
-
# Instantiate the generator.
|
219
|
-
def generator(args=self.default_arguments, options={}, config={})
|
220
|
-
@generator ||= self.generator_class.new(args, options, config.reverse_merge(:destination_root => destination_root))
|
221
|
-
end
|
222
|
-
|
223
|
-
# Create a Rails::Generators::GeneratedAttribute by supplying the
|
224
|
-
# attribute type and, optionally, the attribute name:
|
225
|
-
#
|
226
|
-
# create_generated_attribute(:string, 'name')
|
227
|
-
#
|
228
|
-
def create_generated_attribute(attribute_type, name = 'test')
|
229
|
-
Rails::Generators::GeneratedAttribute.new(name, attribute_type.to_s)
|
230
|
-
end
|
231
|
-
|
232
|
-
protected
|
233
|
-
|
234
|
-
def destination_root_is_set? #:nodoc:
|
235
|
-
raise "You need to configure your Rails::Generators::TestCase destination root." unless destination_root
|
236
|
-
end
|
237
|
-
|
238
|
-
def ensure_current_path #:nodoc:
|
239
|
-
cd current_path
|
240
|
-
end
|
241
|
-
|
242
|
-
def prepare_destination
|
243
|
-
rm_rf(destination_root)
|
244
|
-
mkdir_p(destination_root)
|
245
|
-
end
|
246
|
-
|
247
|
-
def migration_file_name(relative) #:nodoc:
|
248
|
-
absolute = File.expand_path(relative, destination_root)
|
249
|
-
dirname, file_name = File.dirname(absolute), File.basename(absolute).sub(/\.rb$/, '')
|
250
|
-
Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first
|
251
|
-
end
|
252
|
-
end
|
253
|
-
end
|
254
|
-
end
|
@@ -1,24 +0,0 @@
|
|
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
|