Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test generators #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .inch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
files:
excluded:
- lib/generators
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ AllCops:
DisabledByDefault: true
Exclude:
- lib/generators/**/templates/**/*
- tmp/**/*

Gemspec/OrderedDependencies:
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--exclude /lib/generators
17 changes: 17 additions & 0 deletions spec/generators/decorator_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

RSpec.describe "decorator", type: :generator do
before { with_app { generate("dekorator:install") } }

it "generates a Decorator file for a simple Object" do
generate("decorator User")

expect("app/decorators/user_decorator.rb").to match_contents(%r{UserDecorator})
end

it "generates a Decorator file for a namespaced Object" do
generate("decorator Product::Attribute")

expect("app/decorators/product/attribute_decorator.rb").to match_contents(%r{Product::AttributeDecorator})
end
end
9 changes: 9 additions & 0 deletions spec/generators/dekorator_install_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

RSpec.describe "dekorator:install", type: :generator do
it "creates ApplicationDecorator file" do
with_app { generate("dekorator:install") }

expect("app/decorators/application_decorator.rb").to match_contents(%r{ApplicationDecorator})
end
end
25 changes: 25 additions & 0 deletions spec/lib/dekorator/decorators_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require "active_support/core_ext/module/delegation"
require "dekorator/decorators_helper"

require File.expand_path("../fixtures/models.rb", __dir__)
require File.expand_path("../fixtures/decorators.rb", __dir__)

RSpec.describe DecoratorsHelper do
let(:comment) { Comment.new(author_name: "john.doe", body: "comment message") }
let(:post) { Post.new(name: "post title", body: "a" * 200, comments: [comment]) }

describe "#decorate" do
context "with object" do
it { expect(described_class.decorate(post)).to be_a(PostDecorator) }
it { expect(described_class.decorate(post)).to be_a(PostDecorator) }
end

context "with enumerable" do
it { expect(described_class.decorate([post, post, post])).to all(be_a(PostDecorator)) }
it { expect(described_class.decorate([post, post, post])).to all(be_a(PostDecorator)) }
it { expect(described_class.decorate([post, post, post])).to be_a(Enumerable) }
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/dekorator_base_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
#

require File.expand_path("../fixtures/models.rb", __dir__)
require File.expand_path("../fixtures/decorators.rb", __dir__)

Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
SimpleCov.start

require "bundler/setup"
Bundler.require(:default, :test)

require "dekorator"

Dir["./spec/support/**/*.rb"].each { |file| require file }

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
Expand Down
135 changes: 135 additions & 0 deletions spec/support/generators.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# frozen_string_literal: true

module GeneratorTestHelpers
APP_NAME = "dummy_app"

def remove_project_directory
FileUtils.rm_rf(project_path)
end

def create_tmp_directory
FileUtils.mkdir_p(tmp_path)
end

def remove_decorators_directory
FileUtils.rm_rf(decorators_path)
FileUtils.rm_rf(decorators_spec_path)
FileUtils.rm_rf(decorators_test_path)
end

def with_app
remove_project_directory
rails_new

yield
end

def rails_new
run_in_tmp do
debug `#{system_rails_bin} new #{APP_NAME} --skip-webpack-install`

Dir.chdir(APP_NAME) do
File.open("Gemfile", "a") do |file|
file.puts %{gem "dekorator", path: #{root_path.inspect}}
end
end
end
end

def generate(generator)
run_in_project do
debug `bin/spring stop`
debug `#{project_rails_bin} generate #{generator}`
end
end

def destroy(generator)
run_in_project do
`bin/spring stop`
`#{project_rails_bin} destroy #{generator}`
end
end

def project_path
@project_path ||= Pathname.new("#{tmp_path}/#{APP_NAME}")
end

def decorators_path
@decorators_path ||= Pathname.new("#{tmp_path}/#{APP_NAME}/app/decorators")
end

def decorators_spec_path
@decorators_path ||= Pathname.new("#{tmp_path}/#{APP_NAME}/spec/decorators")
end

def decorators_test_path
@decorators_path ||= Pathname.new("#{tmp_path}/#{APP_NAME}/test/decorators")
end

private

def tmp_path
@tmp_path ||= Pathname.new("#{root_path}/tmp")
end

def system_rails_bin
"rails"
end

def project_rails_bin
"bin/rails"
end

def root_path
File.expand_path('../../../', __FILE__)
end

def with_env(name, new_value)
had_key = ENV.has_key?(name)
prior = ENV[name]
ENV[name] = new_value.to_s

yield

ensure
ENV.delete(name)

if had_key
ENV[name] = prior
end
end

def run_in_tmp
create_tmp_directory

Dir.chdir(tmp_path) do
Bundler.with_clean_env do
yield
end
end
end

def run_in_project
Dir.chdir(project_path) do
Bundler.with_clean_env do
yield
end
end
end

def debug(output)
if ENV["DEBUG"]
warn output
end

output
end
end

RSpec.configure do |config|
config.include GeneratorTestHelpers

config.before(:each, type: :generator) do
remove_project_directory
end
end
8 changes: 8 additions & 0 deletions spec/support/match_contents_matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

RSpec::Matchers.define :match_contents do |regexp|
match do |filename|
contents = IO.read(File.join(project_path, filename))
contents =~ regexp
end
end