This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
/
copy_api_version_generator_spec.rb
352 lines (296 loc) · 19.9 KB
/
copy_api_version_generator_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
require 'spec_helper'
require 'generator_spec/test_case'
describe Versionist::CopyApiVersionGenerator do
include GeneratorSpec::TestCase
include Versionist::InflectorFixes
destination File.expand_path("../../tmp", __FILE__)
before :each do
prepare_destination
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers", __FILE__))
::Dir.mkdir(::File.expand_path("../../tmp/config", __FILE__))
::FileUtils.touch(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
end
{"v1" => "V1", "v2" => "V2", "v2.1" => "V2_1", "v20120119" => "Api::V20120119"}.each do |ver, mod|
context "#{ver} => #{mod}" do
context "api version doesn't exist" do
before :each do
::FileUtils.rm(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\nend"}
end
after :each do
::FileUtils.rm(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
::FileUtils.touch(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
::FileUtils.rm_rf(::File.expand_path("../../tmp/app/controllers/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.rm_rf(::File.expand_path("../../tmp/app/presenters/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.rm_rf(::File.expand_path("../../tmp/test/functional/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.rm_rf(::File.expand_path("../../tmp/spec/controllers/#{module_name_for_path(mod)}", __FILE__))
Versionist.configuration.configured_test_framework = nil
end
it "should raise an error if old version not found config/routes.rb" do
lambda {
run_generator [ver, mod, "x", "X", {}]
}.should raise_error(RuntimeError, /old API version #{mod} not found in config\/routes.rb/)
end
it "should raise an error if old version module not found in app/controllers" do
::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-#{ver}\") do\n end\nend"}
lambda {
run_generator [ver, mod, "x", "X", {}]
}.should raise_error(RuntimeError, /old API version module namespace #{mod} not found in app\/controllers/)
end
it "should raise an error if old version module not found in test/functional when Test::Unit is the test framework" do
::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-#{ver}\") do\n end\nend"}
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/presenters/#{module_name_for_path(mod)}", __FILE__))
Versionist.configuration.configured_test_framework = :test_unit
lambda {
run_generator [ver, mod, "x", "X", {}]
}.should raise_error(RuntimeError, /old API version module namespace #{mod} not found in test\/functional/)
end
it "should raise an error if old version module not found in spec/controllers when rspec is the test framework" do
::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-#{ver}\") do\n end\nend"}
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/presenters/#{module_name_for_path(mod)}", __FILE__))
Versionist.configuration.configured_test_framework = :rspec
lambda {
run_generator [ver, mod, "x", "X", {}]
}.should raise_error(RuntimeError, /old API version module namespace #{mod} not found in spec\/controllers/)
end
it "should raise an error if old version module not found in app/presenters" do
::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-#{ver}\") do\n end\nend"}
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/spec/controllers/#{module_name_for_path(mod)}", __FILE__))
Versionist.configuration.configured_test_framework = :rspec
lambda {
run_generator [ver, mod, "x", "X", {}]
}.should raise_error(RuntimeError, /old API version module namespace #{mod} not found in app\/presenters/)
end
it "should raise an error if old version module not found in test/presenters when Test::Unit is the test framework" do
::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-#{ver}\") do\n end\nend"}
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/test/functional/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/presenters/#{module_name_for_path(mod)}", __FILE__))
Versionist.configuration.configured_test_framework = :test_unit
lambda {
run_generator [ver, mod, "x", "X", {}]
}.should raise_error(RuntimeError, /old API version module namespace #{mod} not found in test\/presenters/)
end
it "should raise an error if old version module not found in spec/presenters when rspec is the test framework" do
::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-#{ver}\") do\n end\nend"}
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/spec/controllers/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/presenters/#{module_name_for_path(mod)}", __FILE__))
Versionist.configuration.configured_test_framework = :rspec
lambda {
run_generator [ver, mod, "x", "X", {}]
}.should raise_error(RuntimeError, /old API version module namespace #{mod} not found in spec\/presenters/)
end
it "should raise an error if old version not found in public/docs" do
::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-#{ver}\") do\n end\nend"}
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/spec/controllers/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/presenters/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/spec/presenters/#{module_name_for_path(mod)}", __FILE__))
Versionist.configuration.configured_test_framework = :rspec
lambda {
run_generator [ver, mod, "x", "X", {}]
}.should raise_error(RuntimeError, /old API version #{ver} not found in public\/docs/)
end
end
context "api version exists" do
before :each do
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/spec/controllers/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/presenters/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/spec/presenters/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/public/docs/#{ver}", __FILE__))
::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-#{ver}\") do\n end\nend"}
::File.open(::File.expand_path("../../tmp/app/controllers/#{module_name_for_path(mod)}/base_controller.rb", __FILE__), "w") {|f| f.write "class #{mod}::BaseController < ApplicationController\nend"}
::File.open(::File.expand_path("../../tmp/app/controllers/#{module_name_for_path(mod)}/foos_controller.rb", __FILE__), "w") {|f| f.write "class #{mod}::FoosController < #{mod}::BaseController\nend"}
::File.open(::File.expand_path("../../tmp/app/presenters/#{module_name_for_path(mod)}/base_presenter.rb", __FILE__), "w") {|f| f.write "class #{mod}::BasePresenter\n\n def initialize(#{ver})\n @#{ver} = #{ver}\n end\n\n def as_json(options={})\n # fill me in...\n end\n\n def to_xml(options={}, &block)\n xml = options[:builder] ||= Builder::XmlMarkup.new\n # fill me in...\n end\nend"}
::File.open(::File.expand_path("../../tmp/app/presenters/#{module_name_for_path(mod)}/foo_presenter.rb", __FILE__), "w") {|f| f.write "class #{mod}::FooPresenter < #{mod}::BasePresenter\n\n def initialize(#{ver})\n @#{ver} = #{ver}\n end\n\n def as_json(options={})\n # fill me in...\n end\n\n def to_xml(options={}, &block)\n xml = options[:builder] ||= Builder::XmlMarkup.new\n # fill me in...\n end\nend"}
::File.open(::File.expand_path("../../tmp/public/docs/#{ver}/style.css", __FILE__), "w") {|f| f.write "body {margin: 0; background-color: #fff; color: #000; font-family: Arial,sans-serif;}\ncontent {margin-left: 200px;}\ncontent h1 {text-align: center;}\noperations {float: left; width: 200px; border-right: 1px solid #ccc;}\noperations h3 {text-align: center;}"}
::File.open(::File.expand_path("../../tmp/public/docs/#{ver}/index.html", __FILE__), "w") {|f| f.write "<!DOCTYPE html>\n<html lang=\"en-US\">\n <head>\n <title>Documentation for #{ver}</title>\n <link href=\"#{ver}/style.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\">\n </head>\n <body>\n <div id=\"container\">\n <div id=\"operations\">\n <h3>API Operations</h3>\n </div>\n <div id=\"content\">\n <h1>Documentation for #{ver}</h1>\n </div>\n </div>\n </body>\n</html>"}
Versionist.configuration.configured_test_framework = nil
run_generator [ver, mod, "x", "X"]
end
it "should copy correct api_version to config/routes.rb" do
if RUBY_VERSION =~ /1.9/ && defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby"
assert_file "config/routes.rb"
expected = <<-CONTENTS
Test::Application.routes.draw do
api_version(:module => \"X\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-x\") do
end
api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-#{ver}\") do
end
end
CONTENTS
assert_file "config/routes.rb", expected.chop
end
end
it "should copy old controllers to new controllers" do
expected_base_controller = <<-BASE
class X::BaseController < ApplicationController
end
BASE
expected_foos_controller = <<-FOOS
class X::FoosController < X::BaseController
end
FOOS
assert_file "app/controllers/#{module_name_for_path("X")}/base_controller.rb", expected_base_controller.chop
assert_file "app/controllers/#{module_name_for_path("X")}/foos_controller.rb", expected_foos_controller.chop
end
it "should copy old presenters to new presenters" do
expected_base_presenter = <<-BASE
class X::BasePresenter
def initialize(#{ver})
@#{ver} = #{ver}
end
def as_json(options={})
# fill me in...
end
def to_xml(options={}, &block)
xml = options[:builder] ||= Builder::XmlMarkup.new
# fill me in...
end
end
BASE
expected_foo_presenter = <<-FOOS
class X::FooPresenter < X::BasePresenter
def initialize(#{ver})
@#{ver} = #{ver}
end
def as_json(options={})
# fill me in...
end
def to_xml(options={}, &block)
xml = options[:builder] ||= Builder::XmlMarkup.new
# fill me in...
end
end
FOOS
assert_file "app/presenters/#{module_name_for_path("X")}/base_presenter.rb", expected_base_presenter.chop
assert_file "app/presenters/#{module_name_for_path("X")}/foo_presenter.rb", expected_foo_presenter.chop
end
it "should copy documentation" do
expected_index_html = <<-HTML
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Documentation for x</title>
<link href="x/style.css" media="screen" rel="stylesheet" type="text/css">
</head>
<body>
<div id="container">
<div id="operations">
<h3>API Operations</h3>
</div>
<div id="content">
<h1>Documentation for x</h1>
</div>
</div>
</body>
</html>
HTML
assert_file "public/docs/x/style.css"
assert_file "public/docs/x/index.html", expected_index_html.chop
end
context "test_framework: test_unit" do
before :each do
::FileUtils.mkdir_p(::File.expand_path("../../tmp/test/functional/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/test/presenters/#{module_name_for_path(mod)}", __FILE__))
::File.open(::File.expand_path("../../tmp/test/functional/#{module_name_for_path(mod)}/base_controller_test.rb", __FILE__), "w") {|f| f.write "require 'test_helper'\n\nclass #{mod}::BaseControllerTest < ActionController::TestCase\n # Replace this with your real tests.\n test \"the truth\" do\n assert true\n end\nend"}
::File.open(::File.expand_path("../../tmp/test/functional/#{module_name_for_path(mod)}/foos_controller_test.rb", __FILE__), "w") {|f| f.write "require 'test_helper'\n\nclass #{mod}::FoosControllerTest < ActionController::TestCase\n # Replace this with your real tests.\n test \"the truth\" do\n assert true\n end\nend"}
::File.open(::File.expand_path("../../tmp/test/presenters/#{module_name_for_path(mod)}/base_presenter_test.rb", __FILE__), "w") {|f| f.write "require 'test_helper'\n\nclass #{mod}::BasePresenterTest < Test::Unit::TestCase\n # Replace this with your real tests.\n test \"the truth\" do\n assert true\n end\nend"}
::File.open(::File.expand_path("../../tmp/test/presenters/#{module_name_for_path(mod)}/foo_presenter_test.rb", __FILE__), "w") {|f| f.write "require 'test_helper'\n\nclass #{mod}::FooPresenterTest < Test::Unit::TestCase\n # Replace this with your real tests.\n test \"the truth\" do\n assert true\n end\nend"}
Versionist.configuration.configured_test_framework = :test_unit
run_generator [ver, mod, "x", "X"]
end
it "should copy old controller tests to new controller tests" do
expected_base_controller_test = <<-BASE
require 'test_helper'
class X::BaseControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
BASE
expected_foos_controller_test = <<-FOOS
require 'test_helper'
class X::FoosControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
FOOS
assert_file "test/functional/#{module_name_for_path("X")}/base_controller_test.rb", expected_base_controller_test.chop
assert_file "test/functional/#{module_name_for_path("X")}/foos_controller_test.rb", expected_foos_controller_test.chop
end
it "should copy old presenter tests to new presenter tests" do
expected_base_presenter_test = <<-BASE
require 'test_helper'
class X::BasePresenterTest < Test::Unit::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
BASE
expected_foo_presenter_test = <<-FOOS
require 'test_helper'
class X::FooPresenterTest < Test::Unit::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
FOOS
assert_file "test/presenters/#{module_name_for_path("X")}/base_presenter_test.rb", expected_base_presenter_test.chop
assert_file "test/presenters/#{module_name_for_path("X")}/foo_presenter_test.rb", expected_foo_presenter_test.chop
end
end
context "test_framework: rspec" do
before :each do
::FileUtils.mkdir_p(::File.expand_path("../../tmp/spec/controllers/#{module_name_for_path(mod)}", __FILE__))
::FileUtils.mkdir_p(::File.expand_path("../../tmp/spec/presenters/#{module_name_for_path(mod)}", __FILE__))
::File.open(::File.expand_path("../../tmp/spec/controllers/#{module_name_for_path(mod)}/base_controller_spec.rb", __FILE__), "w") {|f| f.write "require 'spec_helper'\n\ndescribe #{mod}::BaseController do\n\nend"}
::File.open(::File.expand_path("../../tmp/spec/controllers/#{module_name_for_path(mod)}/foos_controller_spec.rb", __FILE__), "w") {|f| f.write "require 'spec_helper'\n\ndescribe #{mod}::FoosController do\n\nend"}
::File.open(::File.expand_path("../../tmp/spec/presenters/#{module_name_for_path(mod)}/base_presenter_spec.rb", __FILE__), "w") {|f| f.write "require 'spec_helper'\n\ndescribe #{mod}::BasePresenter do\n\nend"}
::File.open(::File.expand_path("../../tmp/spec/presenters/#{module_name_for_path(mod)}/foo_presenter_spec.rb", __FILE__), "w") {|f| f.write "require 'spec_helper'\n\ndescribe #{mod}::FooPresenter do\n\nend"}
Versionist.configuration.configured_test_framework = :rspec
run_generator [ver, mod, "x", "X"]
end
it "should copy old controller specs to new controller specs" do
expected_base_controller_spec = <<-BASE
require 'spec_helper'
describe X::BaseController do
end
BASE
expected_foos_controller_spec = <<-FOOS
require 'spec_helper'
describe X::FoosController do
end
FOOS
assert_file "spec/controllers/#{module_name_for_path("X")}/base_controller_spec.rb", expected_base_controller_spec.chop
assert_file "spec/controllers/#{module_name_for_path("X")}/foos_controller_spec.rb", expected_foos_controller_spec.chop
end
it "should copy old presenter specs to new presenter specs" do
expected_base_presenter_spec = <<-BASE
require 'spec_helper'
describe X::BasePresenter do
end
BASE
expected_foo_presenter_spec = <<-FOOS
require 'spec_helper'
describe X::FooPresenter do
end
FOOS
assert_file "spec/presenters/#{module_name_for_path("X")}/base_presenter_spec.rb", expected_base_presenter_spec.chop
assert_file "spec/presenters/#{module_name_for_path("X")}/foo_presenter_spec.rb", expected_foo_presenter_spec.chop
end
end
end
end
end
end