-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve generator specs using ammeter
- Loading branch information
1 parent
a022297
commit 48543de
Showing
13 changed files
with
282 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
lib/generators/koi/admin_route/templates/initializer.rb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Koi::Menu.priority = { | ||
"Navigations" => "/admin/navigation", | ||
} | ||
|
||
Koi::Menu.modules = { | ||
|
||
} | ||
|
||
Koi::Menu.advanced = { | ||
"Admin Users" => "/admin/admin_users", | ||
"URL Rewriter" => "/admin/url_rewrites", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :admin do | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
require "generators/koi/active_record/active_record_generator" | ||
|
||
RSpec.describe Koi::ActiveRecordGenerator do | ||
subject(:output) do | ||
gen = generator(%w(test title:string slug:string content:rich_text)) | ||
Ammeter::OutputCapturer.capture(:stdout) { gen.invoke_all } | ||
end | ||
|
||
it "runs the expected creation steps" do | ||
expect(output.lines.grep(/create/).map { |l| l.split.last }).to contain_exactly( | ||
"app/models/test.rb", | ||
%r{db/migrate/\d+_create_tests.rb}, | ||
"spec/factories/tests.rb", | ||
"spec/models/test_spec.rb", | ||
) | ||
end | ||
|
||
it "creates the expected files" do | ||
output | ||
expect(Pathname.new(file("app/models/test.rb"))).to exist | ||
expect(Pathname.new(migration_file("db/migrate/create_tests.rb"))).to exist | ||
expect(Pathname.new(file("spec/models/test_spec.rb"))).to exist | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
require "generators/koi/admin_controller/admin_controller_generator" | ||
|
||
RSpec.describe Koi::AdminControllerGenerator do | ||
subject(:output) do | ||
gen = generator(%w(test title:string slug:string content:rich_text)) | ||
Ammeter::OutputCapturer.capture(:stdout) { gen.invoke_all } | ||
end | ||
|
||
it "runs the expected creation steps" do | ||
expect(output.lines.grep(/create/).grep(/controller/).map { |l| l.split.last }).to contain_exactly( | ||
"app/controllers/admin/tests_controller.rb", | ||
"spec/requests/admin/tests_controller_spec.rb", | ||
) | ||
end | ||
|
||
it "invokes generators" do | ||
expect(output.lines.grep(/invoke/).map { |l| l.split.last }).to contain_exactly( | ||
"admin_route", | ||
"admin_views", | ||
) | ||
end | ||
|
||
it "creates the expected files" do | ||
output | ||
expect(Pathname.new(file("app/controllers/admin/tests_controller.rb"))).to exist | ||
expect(Pathname.new(file("spec/requests/admin/tests_controller_spec.rb"))).to exist | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
require "generators/koi/admin/admin_generator" | ||
|
||
RSpec.describe Koi::AdminGenerator do | ||
subject(:output) do | ||
gen = generator(%w(test title:text content:rich_text)) | ||
Ammeter::OutputCapturer.capture(:stdout) { gen.invoke_all } | ||
end | ||
|
||
it "invokes the expected generators" do | ||
expect(output.lines.grep(/invoke/).map { |line| line.split.last }).to contain_exactly( | ||
"active_record", | ||
"admin_controller", | ||
"admin_route", | ||
"admin_views", | ||
"factory_bot", | ||
"rspec", | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
require "generators/koi/admin_route/admin_route_generator" | ||
|
||
RSpec.describe Koi::AdminRouteGenerator do | ||
subject(:output) do | ||
Ammeter::OutputCapturer.capture(:stdout) { gen.invoke_all } | ||
end | ||
|
||
let(:gen) { generator(%w(test)) } | ||
|
||
it "updates routes and menus" do | ||
expect(output.lines.grep(/insert/).map { |l| l.split.last }).to contain_exactly( | ||
"config/initializers/koi.rb", | ||
"config/routes/admin.rb", | ||
) | ||
end | ||
|
||
describe "menu changes" do | ||
subject(:admin_menu) { file("config/initializers/koi.rb") } | ||
|
||
let(:stubs) {} | ||
|
||
before do | ||
stubs | ||
Ammeter::OutputCapturer.capture(:stdout) { gen.invoke_all } | ||
end | ||
|
||
it { is_expected.to contain('"Tests" => "/admin/tests",') } | ||
end | ||
|
||
describe "routes changes" do | ||
subject(:admin_routes) { file("config/routes/admin.rb") } | ||
|
||
let(:stubs) {} | ||
|
||
before do | ||
stubs | ||
Ammeter::OutputCapturer.capture(:stdout) { gen.invoke_all } | ||
end | ||
|
||
it { is_expected.to contain("resources :tests") } | ||
|
||
context "when admin routes file exists" do | ||
let(:stubs) do | ||
RSpec::Support::DirectoryMaker.mkdir_p(file("config/routes")) | ||
File.write(file("config/routes/admin.rb"), <<~RUBY) | ||
namespace :admin do | ||
end | ||
RUBY | ||
|
||
set_shell_prompt_responses(gen, ask: "y") | ||
end | ||
|
||
it { is_expected.to contain(<<~RUBY) } | ||
namespace :admin do | ||
resources :tests | ||
end | ||
RUBY | ||
end | ||
|
||
context "when the module is nested" do | ||
let(:gen) { generator(%w(nested/test)) } | ||
|
||
it { is_expected.to contain(<<-RUBY) } | ||
namespace :nested do | ||
resources :tests | ||
end | ||
RUBY | ||
end | ||
|
||
context "when the module is nested and the namespace already exists" do | ||
let(:gen) { generator(%w(nested/test)) } | ||
let(:stubs) do | ||
RSpec::Support::DirectoryMaker.mkdir_p(file("config/routes")) | ||
File.write(file("config/routes/admin.rb"), <<~RUBY) | ||
namespace :admin do | ||
namespace :nested do | ||
resources :other | ||
end | ||
end | ||
RUBY | ||
|
||
set_shell_prompt_responses(gen, ask: "y") | ||
end | ||
|
||
it { is_expected.to contain(<<-RUBY) } | ||
namespace :nested do | ||
resources :tests | ||
resources :other | ||
end | ||
RUBY | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
require "generators/koi/admin_views/admin_views_generator" | ||
|
||
RSpec.describe Koi::AdminViewsGenerator do | ||
subject(:output) do | ||
gen = generator(%w(test)) | ||
Ammeter::OutputCapturer.capture(:stdout) { gen.invoke_all } | ||
end | ||
|
||
it "generates the expected output" do | ||
expect(output.lines.map { |l| l.split.last }).to contain_exactly( | ||
"app/views/admin/tests", | ||
"app/views/admin/tests/index.html.erb", | ||
"app/views/admin/tests/edit.html.erb", | ||
"app/views/admin/tests/show.html.erb", | ||
"app/views/admin/tests/new.html.erb", | ||
"app/views/admin/tests/_fields.html.erb", | ||
"app/views/admin/tests/_test.html+row.erb", | ||
) | ||
end | ||
|
||
it "creates the expected files" do | ||
output | ||
expect(Pathname.new(file("app/views/admin/tests/index.html.erb"))).to exist | ||
expect(Pathname.new(file("app/views/admin/tests/edit.html.erb"))).to exist | ||
expect(Pathname.new(file("app/views/admin/tests/show.html.erb"))).to exist | ||
expect(Pathname.new(file("app/views/admin/tests/new.html.erb"))).to exist | ||
expect(Pathname.new(file("app/views/admin/tests/_fields.html.erb"))).to exist | ||
expect(Pathname.new(file("app/views/admin/tests/_test.html+row.erb"))).to exist | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require "ammeter/init" | ||
|
||
module GeneratorHelper | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
# Tell the generator where to put its output (what it thinks of as Rails.root) | ||
destination Koi::Engine.root.join("tmp/dummy") | ||
|
||
before do | ||
prepare_destination | ||
end | ||
|
||
after do | ||
FileUtils.rm_rf(destination_root) | ||
end | ||
end | ||
end | ||
|
||
RSpec.configure do |config| | ||
config.include GeneratorHelper, type: :generator | ||
end |