Skip to content
Browse files

Issue #6: Copy API Generator

  • Loading branch information...
1 parent 1439b70 commit c952cf62bec7b4e045aaf5544be8cf880c8863d7 Brian Ploetz committed
View
31 README.markdown
@@ -191,6 +191,8 @@ To see the available generators, simply run `rails generate`, and you will see t
The following generators are available:
+### `versionist:new_api_version`
+
`versionist:new_api_version` - creates the infrastructure for a new API version. This will create:
- A new controller namespace, base controller and test
@@ -219,6 +221,8 @@ Example:
create public/docs/v2.0.0/style.css
+### `versionist:new_controller`
+
`versionist:new_controller` - creates a new controller class with the given name under the given version module.
Usage
@@ -232,6 +236,8 @@ Example:
create spec/controllers/v2_0_0/foos_controller_spec.rb
+### `versionist:new_presenter`
+
`versionist:new_presenter` - creates a new presenter class with the given name under the given version module.
Usage
@@ -243,3 +249,28 @@ Example:
rails generate versionist:new_presenter foos V2_0_0
create app/presenters/v2_0_0/foos_presenter.rb
create spec/presenters/v2_0_0/foos_presenter_spec.rb
+
+
+### `versionist:copy_api_version`
+
+`versionist:copy_api_version` - copies an existing API version to a new API version. This will do the following:
+
+- Copy all existing routes in config/routes.rb from the old API version to routes for the new API version in config/routes.rb
+- Copy all existing controllers and tests from the old API version to the new API version
+- Copy all existing presenters and tests from the old API version to the new API version
+- Copy all documentation from the old API version to the new API version
+
+Usage
+
+ rails generate versionist:copy_api_version <old version> <old module namespace> <new version> <new module namespace>
+
+Example:
+
+ rails generate versionist:copy_api_version v2.0.0 V2_0_0 v3.0.0 V3_0_0
+ route api_version(:module => "V3__0__0", :header=>"Accept", :value=>"application/vnd.mycompany.com-v3.0.0") do
+ end
+ Copying all files from app/controllers/v2_0_0 to app/controllers/v3_0_0
+ Copying all files from spec/controllers/v2_0_0 to spec/controllers/v3_0_0
+ Copying all files from app/presenters/v2_0_0 to app/presenters/v3_0_0
+ Copying all files from spec/presenters/v2_0_0 to spec/presenters/v3_0_0
+ Copying all files from public/docs/v2.0.0 to public/docs/v3.0.0
View
2 gemfiles/Rails-3.0
@@ -9,7 +9,7 @@ group :development do
gem 'rake', '>= 0.9.2'
gem 'rspec', '2.8.0'
gem 'rspec-rails', '2.8.0'
- gem 'generator_spec', '0.8.4'
+ gem 'generator_spec', '0.8.5'
gem 'rdoc', '>= 3.11'
gem 'simplecov', '0.5.4'
end
View
6 gemfiles/Rails-3.0.lock
@@ -3,6 +3,7 @@ PATH
specs:
versionist (0.1.3)
rails (~> 3.0)
+ yard (~> 0.7)
GEM
remote: http://rubygems.org/
@@ -40,7 +41,7 @@ GEM
diff-lcs (1.1.3)
erubis (2.6.6)
abstract (>= 1.0.0)
- generator_spec (0.8.4)
+ generator_spec (0.8.5)
rails (>= 3.0, < 4.0)
rspec-rails
i18n (0.5.0)
@@ -97,13 +98,14 @@ GEM
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.33)
+ yard (0.7.5)
PLATFORMS
ruby
DEPENDENCIES
awesome_print (= 1.0.2)
- generator_spec (= 0.8.4)
+ generator_spec (= 0.8.5)
rails (~> 3.0.0)
rake (>= 0.9.2)
rdoc (>= 3.11)
View
2 gemfiles/Rails-3.1
@@ -9,7 +9,7 @@ group :development do
gem 'rake', '>= 0.9.2'
gem 'rspec', '2.8.0'
gem 'rspec-rails', '2.8.0'
- gem 'generator_spec', '0.8.4'
+ gem 'generator_spec', '0.8.5'
gem 'rdoc', '>= 3.11'
gem 'simplecov', '0.5.4'
end
View
6 gemfiles/Rails-3.1.lock
@@ -3,6 +3,7 @@ PATH
specs:
versionist (0.1.3)
rails (~> 3.0)
+ yard (~> 0.7)
GEM
remote: http://rubygems.org/
@@ -40,7 +41,7 @@ GEM
builder (3.0.0)
diff-lcs (1.1.3)
erubis (2.7.0)
- generator_spec (0.8.4)
+ generator_spec (0.8.5)
rails (>= 3.0, < 4.0)
rspec-rails
hike (1.2.1)
@@ -107,13 +108,14 @@ GEM
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.33)
+ yard (0.7.5)
PLATFORMS
ruby
DEPENDENCIES
awesome_print (= 1.0.2)
- generator_spec (= 0.8.4)
+ generator_spec (= 0.8.5)
rails (~> 3.1.0)
rake (>= 0.9.2)
rdoc (>= 3.11)
View
2 gemfiles/Rails-3.2
@@ -9,7 +9,7 @@ group :development do
gem 'rake', '>= 0.9.2'
gem 'rspec', '2.8.0'
gem 'rspec-rails', '2.8.0'
- gem 'generator_spec', '0.8.4'
+ gem 'generator_spec', '0.8.5'
gem 'rdoc', '>= 3.11'
gem 'simplecov', '0.5.4'
end
View
6 gemfiles/Rails-3.2.lock
@@ -3,6 +3,7 @@ PATH
specs:
versionist (0.1.3)
rails (~> 3.0)
+ yard (~> 0.7)
GEM
remote: http://rubygems.org/
@@ -39,7 +40,7 @@ GEM
builder (3.0.0)
diff-lcs (1.1.3)
erubis (2.7.0)
- generator_spec (0.8.4)
+ generator_spec (0.8.5)
rails (>= 3.0, < 4.0)
rspec-rails
hike (1.2.1)
@@ -105,13 +106,14 @@ GEM
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.33)
+ yard (0.7.5)
PLATFORMS
ruby
DEPENDENCIES
awesome_print (= 1.0.2)
- generator_spec (= 0.8.4)
+ generator_spec (= 0.8.5)
rails (~> 3.2.0)
rake (>= 0.9.2)
rdoc (>= 3.11)
View
15 lib/generators/versionist/copy_api_version/USAGE
@@ -0,0 +1,15 @@
+Description:
+ Copies an existing API version to a new API version.
+
+Example:
+ rails generate versionist:copy_api_version v2.0.0 V2_0_0 v3.0.0 V3_0_0
+
+ This will do the following:
+
+ route api_version(:module => "V3__0__0", :header=>"Accept", :value=>"application/vnd.mycompany.com-v3.0.0") do
+ end
+ Copying all files from app/controllers/v2_0_0 to app/controllers/v3_0_0
+ Copying all files from spec/controllers/v2_0_0 to spec/controllers/v3_0_0
+ Copying all files from app/presenters/v2_0_0 to app/presenters/v3_0_0
+ Copying all files from spec/presenters/v2_0_0 to spec/presenters/v3_0_0
+ Copying all files from public/docs/v2.0.0 to public/docs/v3.0.0
View
130 lib/generators/versionist/copy_api_version/copy_api_version_generator.rb
@@ -0,0 +1,130 @@
+require 'yard'
+require 'fileutils'
+
+module Versionist
+ class CopyApiVersionGenerator < Rails::Generators::Base
+ include InflectorFixes
+
+ desc "copies an existing API version a new API version"
+
+ source_root File.expand_path('../templates', __FILE__)
+
+ argument :old_version, :type => :string
+ argument :old_module_name, :type => :string
+ argument :new_version, :type => :string
+ argument :new_module_name, :type => :string
+
+ def validate_old_version
+ in_root do
+ api_version_block = /api_version.*:module\s*(=>|:)\s*("|')#{module_name_for_route(old_module_name)}("|').*do/
+ matching_version_blocks = File.readlines("config/routes.rb").grep(api_version_block)
+ raise "old API version #{old_module_name} not found in config/routes.rb" if matching_version_blocks.empty?
+ raise "old API version module namespace #{old_module_name} not found in app/controllers" if !File.exists?("app/controllers/#{module_name_for_path(old_module_name)}")
+ raise "old API version module namespace #{old_module_name} not found in app/presenters" if !File.exists?("app/presenters/#{module_name_for_path(old_module_name)}")
+ case Versionist.configuration.configured_test_framework
+ when :test_unit
+ raise "old API version module namespace #{old_module_name} not found in test/functional/#{module_name_for_path(old_module_name)}" if !File.exists?("test/functional/#{module_name_for_path(old_module_name)}")
+ raise "old API version module namespace #{old_module_name} not found in test/presenters/#{module_name_for_path(old_module_name)}" if !File.exists?("test/presenters/#{module_name_for_path(old_module_name)}")
+ when :rspec
+ raise "old API version module namespace #{old_module_name} not found in spec/controllers/#{module_name_for_path(old_module_name)}" if !File.exists?("spec/controllers/#{module_name_for_path(old_module_name)}")
+ raise "old API version module namespace #{old_module_name} not found in spec/presenters/#{module_name_for_path(old_module_name)}" if !File.exists?("spec/presenters/#{module_name_for_path(old_module_name)}")
+ end
+ raise "old API version #{old_version} not found in public/docs" if !File.exists?("public/docs/#{old_version}")
+ end
+ end
+
+ def copy_routes
+ in_root do
+ parser = YARD::Parser::SourceParser.parse_string(File.read("config/routes.rb"))
+ existing_routes = nil
+ parser.enumerator.first.traverse do |node|
+ existing_routes = node.source if node.type == :fcall && node.source =~ /api_version.*:module\s*(=>|:)\s*("|')#{module_name_for_route(old_module_name)}("|')/
+ end
+ copied_routes = String.new(existing_routes)
+ copied_routes.gsub!(/"#{module_name_for_route(old_module_name)}"/, "\"#{module_name_for_route(new_module_name)}\"")
+ copied_routes.gsub!(/#{old_version}/, new_version)
+ route copied_routes
+ end
+ end
+
+ def copy_controllers
+ in_root do
+ log "Copying all files from app/controllers/#{module_name_for_path(old_module_name)} to app/controllers/#{module_name_for_path(new_module_name)}"
+ FileUtils.cp_r "app/controllers/#{module_name_for_path(old_module_name)}", "app/controllers/#{module_name_for_path(new_module_name)}"
+ Dir.glob("app/controllers/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
+ text = File.read(f)
+ File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
+ end
+ end
+ end
+
+ # due to the inflector quirks we can't use hook_for :test_framework
+ def copy_controller_tests
+ in_root do
+ case Versionist.configuration.configured_test_framework
+ when :test_unit
+ log "Copying all files from test/functional/#{module_name_for_path(old_module_name)} to test/functional/#{module_name_for_path(new_module_name)}"
+ FileUtils.cp_r "test/functional/#{module_name_for_path(old_module_name)}", "test/functional/#{module_name_for_path(new_module_name)}"
+ Dir.glob("test/functional/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
+ text = File.read(f)
+ File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
+ end
+ when :rspec
+ log "Copying all files from spec/controllers/#{module_name_for_path(old_module_name)} to spec/controllers/#{module_name_for_path(new_module_name)}"
+ FileUtils.cp_r "spec/controllers/#{module_name_for_path(old_module_name)}", "spec/controllers/#{module_name_for_path(new_module_name)}"
+ Dir.glob("spec/controllers/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
+ text = File.read(f)
+ File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
+ end
+ else
+ say "Unsupported test_framework: #{Versionist.configuration.configured_test_framework}"
+ end
+ end
+ end
+
+ def copy_presenters
+ in_root do
+ log "Copying all files from app/presenters/#{module_name_for_path(old_module_name)} to app/presenters/#{module_name_for_path(new_module_name)}"
+ FileUtils.cp_r "app/presenters/#{module_name_for_path(old_module_name)}", "app/presenters/#{module_name_for_path(new_module_name)}"
+ Dir.glob("app/presenters/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
+ text = File.read(f)
+ File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
+ end
+ end
+ end
+
+ def copy_presenter_tests
+ in_root do
+ case Versionist.configuration.configured_test_framework
+ when :test_unit
+ log "Copying all files from test/presenters/#{module_name_for_path(old_module_name)} to test/presenters/#{module_name_for_path(new_module_name)}"
+ FileUtils.cp_r "test/presenters/#{module_name_for_path(old_module_name)}", "test/presenters/#{module_name_for_path(new_module_name)}"
+ Dir.glob("test/presenters/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
+ text = File.read(f)
+ File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
+ end
+ when :rspec
+ log "Copying all files from spec/presenters/#{module_name_for_path(old_module_name)} to spec/presenters/#{module_name_for_path(new_module_name)}"
+ FileUtils.cp_r "spec/presenters/#{module_name_for_path(old_module_name)}", "spec/presenters/#{module_name_for_path(new_module_name)}"
+ Dir.glob("spec/presenters/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
+ text = File.read(f)
+ File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
+ end
+ else
+ say "Unsupported test_framework: #{Versionist.configuration.configured_test_framework}"
+ end
+ end
+ end
+
+ def copy_documentation
+ in_root do
+ log "Copying all files from public/docs/#{old_version} to public/docs/#{new_version}"
+ FileUtils.cp_r "public/docs/#{old_version}", "public/docs/#{new_version}"
+ Dir.glob("public/docs/#{new_version}/*.html").each do |f|
+ text = File.read(f)
+ File.open(f, 'w') {|f| f << text.gsub(/#{old_version}/, new_version)}
+ end
+ end
+ end
+ end
+end
View
2 lib/generators/versionist/new_api_version/new_api_version_generator.rb
@@ -12,7 +12,7 @@ class NewApiVersionGenerator < Rails::Generators::Base
def add_routes
in_root do
- api_version_block = /api_version.*:module\s*(=>|:)\s*("|')#{module_name_for_route(module_name)}("|').*do/
+ api_version_block = /api_version.*:module\s*(=>|:)\s*("|')#{module_name_for_route(module_name)}("|')/
matching_version_blocks = File.readlines("config/routes.rb").grep(api_version_block)
raise "API version already exists in config/routes.rb" if !matching_version_blocks.empty?
versioning_strategy.symbolize_keys!
View
8 lib/generators/versionist/new_api_version/templates/docs_style.rb
@@ -1,5 +1,5 @@
body {margin: 0; background-color: #fff; color: #000; font-family: Arial,sans-serif;}
-#content {margin-left: 200px;}
-#content h1 {text-align: center;}
-#operations {float: left; width: 200px; border-right: 1px solid #ccc;}
-#operations h3 {text-align: center;}
+content {margin-left: 200px;}
+content h1 {text-align: center;}
+operations {float: left; width: 200px; border-right: 1px solid #ccc;}
+operations h3 {text-align: center;}
View
2 lib/generators/versionist/new_controller/new_controller_generator.rb
@@ -12,7 +12,7 @@ def new_controller
raise "API module namespace #{module_name} doesn't exist. Please run \'rails generate versionist:new_api_version\' generator first" if !File.exists?("app/controllers/#{module_name_for_path(module_name)}")
template 'new_controller.rb', File.join("app", "controllers", "#{module_name_for_path(module_name)}", "#{file_name}_controller.rb")
- api_version_block = /api_version.*:module\s*(=>|:)\s*("|')#{module_name_for_route(module_name)}("|').*do/
+ api_version_block = /api_version.*:module\s*(=>|:)\s*("|')#{module_name_for_route(module_name)}("|')/
new_route = " resources :#{file_name}\n"
matching_version_blocks = File.readlines("config/routes.rb").grep(api_version_block)
if matching_version_blocks.empty?
View
1 lib/versionist.rb
@@ -5,6 +5,7 @@ module Versionist
autoload :Configuration
autoload :InflectorFixes, "generators/versionist/inflector_fixes"
+ autoload :CopyApiVersionGenerator, "generators/versionist/copy_api_version/copy_api_version_generator"
autoload :NewApiVersionGenerator, "generators/versionist/new_api_version/new_api_version_generator"
autoload :NewControllerGenerator, "generators/versionist/new_controller/new_controller_generator"
autoload :NewPresenterGenerator, "generators/versionist/new_presenter/new_presenter_generator"
View
350 spec/generators/copy_api_version_generator_spec.rb
@@ -0,0 +1,350 @@
+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
+ 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
+
+ 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
View
8 spec/generators/new_api_version_generator_spec.rb
@@ -112,10 +112,10 @@ class #{mod}::BasePresenter
it "should create a documentation style.css" do
assert_file "public/docs/#{ver}/style.css", <<-CONTENTS
body {margin: 0; background-color: #fff; color: #000; font-family: Arial,sans-serif;}
-#content {margin-left: 200px;}
-#content h1 {text-align: center;}
-#operations {float: left; width: 200px; border-right: 1px solid #ccc;}
-#operations h3 {text-align: center;}
+content {margin-left: 200px;}
+content h1 {text-align: center;}
+operations {float: left; width: 200px; border-right: 1px solid #ccc;}
+operations h3 {text-align: center;}
CONTENTS
end
View
2 spec/spec_helper.rb
@@ -6,7 +6,7 @@
require 'rspec-rails'
require 'simplecov'
SimpleCov.start do
- add_filter "spec/test-api"
+ add_filter "spec"
end
require 'versionist'
require 'fileutils'
View
1 versionist.gemspec
@@ -19,4 +19,5 @@ Gem::Specification.new do |s|
s.required_rubygems_version = '>= 1.3.6'
s.add_dependency('rails', '~> 3.0')
+ s.add_dependency('yard', "~> 0.7")
end

0 comments on commit c952cf6

Please sign in to comment.
Something went wrong with that request. Please try again.