Skip to content

Commit

Permalink
Improve generator specs using ammeter
Browse files Browse the repository at this point in the history
  • Loading branch information
sfnelson authored and louieyoung98 committed Jan 11, 2024
1 parent a022297 commit 48543de
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 28 deletions.
8 changes: 8 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ Metrics/BlockLength:
- config/**/*
- lib/tasks/*
- spec/**/*

RSpec/ExampleLength:
Max: 10

RSpec/MultipleExpectations:
Exclude:
- spec/generators/**/*
- spec/system/**/*
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ source "https://rubygems.org"
gemspec

group :development, :test do
gem "ammeter", require: false
gem "brakeman"
gem "dartsass-rails"
gem "erb_lint", require: false
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
ammeter (1.1.6)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-rails (>= 2.2)
android_key_attestation (0.3.0)
ast (2.4.2)
awrence (1.2.1)
Expand Down Expand Up @@ -450,6 +454,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
ammeter
brakeman
capybara
cuprite
Expand Down
14 changes: 14 additions & 0 deletions lib/generators/koi/admin_route/admin_route_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@ module Koi
class AdminRouteGenerator < Rails::Generators::NamedBase
include Rails::Generators::ResourceHelpers

source_root File.expand_path("templates", __dir__)

def create_routes
return if Pathname.new(destination_root).join("config/routes/admin.rb").exist?

template("routes.rb", "config/routes/admin.rb")
end

def add_route
route "resources :#{file_name.pluralize}", namespace: regular_class_path
end

def create_navigation
return if Pathname.new(destination_root).join("config/initializers/koi.rb").exist?

template("initializer.rb", "config/initializers/koi.rb")
end

def add_navigation
insert_into_file("config/initializers/koi.rb",
" \"#{[*regular_class_path.map(&:humanize),
Expand Down
12 changes: 12 additions & 0 deletions lib/generators/koi/admin_route/templates/initializer.rb.tt
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",
}
4 changes: 4 additions & 0 deletions lib/generators/koi/admin_route/templates/routes.rb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

namespace :admin do
end
28 changes: 0 additions & 28 deletions spec/generator/koi/admin_generator_spec.rb

This file was deleted.

28 changes: 28 additions & 0 deletions spec/generators/koi/active_record_generator_spec.rb
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
32 changes: 32 additions & 0 deletions spec/generators/koi/admin_controller_generator_spec.rb
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
23 changes: 23 additions & 0 deletions spec/generators/koi/admin_generator_spec.rb
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
97 changes: 97 additions & 0 deletions spec/generators/koi/admin_route_generator_spec.rb
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
34 changes: 34 additions & 0 deletions spec/generators/koi/admin_views_generator_spec.rb
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
24 changes: 24 additions & 0 deletions spec/support/generators.rb
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

0 comments on commit 48543de

Please sign in to comment.