Skip to content

Commit

Permalink
Get rid of all assert_no_generated_errors calls in tests
Browse files Browse the repository at this point in the history
Instead replace them with an assertion that generated errors is empty
in the base test class after block. To allow tests to still generate
and test errors, added a `expect_dsl_compiler_errors!` call which sets
a flag to skip generated errors assertion after the test.
  • Loading branch information
paracycle committed Feb 3, 2022
1 parent 480845f commit ba17012
Show file tree
Hide file tree
Showing 26 changed files with 15 additions and 221 deletions.
12 changes: 7 additions & 5 deletions spec/dsl_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ class DslSpec < Minitest::Spec
# Require the file that the target class should be loaded from
require(self.class.target_class_file)
use_dsl_compiler(self.class.target_class)
@expecting_errors = false
end

after do
assert_empty(generated_errors) unless @expecting_errors
generated_errors.clear
end

sig { returns(T.nilable(T::Boolean)) }
def expect_dsl_compiler_errors!
@expecting_errors = T.let(true, T.nilable(T::Boolean))
end

sig { returns(Class) }
def self.spec_test_class
# It should be the one that directly inherits from DslSpec
Expand Down Expand Up @@ -59,9 +66,4 @@ def self.underscore(class_name)
word.downcase!
word
end

sig { void }
def assert_no_generated_errors
T.unsafe(self).assert_empty(generated_errors)
end
end
8 changes: 0 additions & 8 deletions spec/tapioca/compilers/dsl/aasm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
class Tapioca::Compilers::Dsl::AASMSpec < DslSpec
describe "Tapioca::Compilers::Dsl::AASM" do
describe "initialize" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "gathers no constants if there are no classes that include AASM" do
assert_empty(gathered_constants)
end
Expand All @@ -27,10 +23,6 @@ class StateMachine
end

describe "decorate" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "generates empty RBI when AASM is included but no AASM call has been made" do
add_ruby_file("content.rb", <<~RUBY)
class StateMachine
Expand Down
8 changes: 0 additions & 8 deletions spec/tapioca/compilers/dsl/action_controller_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
class Tapioca::Compilers::Dsl::ActionControllerHelpersSpec < DslSpec
describe "Tapioca::Compilers::Dsl::ActionControllerHelpers" do
describe "initialize" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "gathers no constants if there are no classes" do
assert_empty(gathered_constants)
end
Expand Down Expand Up @@ -74,10 +70,6 @@ class HomeController < ActionController::Base
end

describe "decorate" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "generates empty helper module when there are no helper methods specified" do
add_ruby_file("controller.rb", <<~RUBY)
class UserController < ActionController::Base
Expand Down
8 changes: 0 additions & 8 deletions spec/tapioca/compilers/dsl/action_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
class Tapioca::Compilers::Dsl::ActionMailerSpec < DslSpec
describe "Tapioca::Compilers::Dsl::ActionMailer" do
describe "initialize" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "gathers no constants if there are no ActionMailer subclasses" do
assert_empty(gathered_constants)
end
Expand Down Expand Up @@ -53,10 +49,6 @@ class AbstractMailer < ActionMailer::Base
end

describe "decorate" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "generates empty RBI file if there are no methods" do
add_ruby_file("mailer.rb", <<~RUBY)
class NotifierMailer < ActionMailer::Base
Expand Down
8 changes: 0 additions & 8 deletions spec/tapioca/compilers/dsl/active_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
class Tapioca::Compilers::Dsl::ActiveJobSpec < DslSpec
describe "Tapioca::Compilers::Dsl::ActiveJob" do
describe "initialize" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "gathers no constants if there are no ActiveJob subclasses" do
assert_empty(gathered_constants)
end
Expand Down Expand Up @@ -40,10 +36,6 @@ class SecondaryNotifyJob < NotifyJob
end

describe "decorate" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "generates an empty RBI file if there is no perform method" do
add_ruby_file("job.rb", <<~RUBY)
class NotifyJob < ActiveJob::Base
Expand Down
8 changes: 0 additions & 8 deletions spec/tapioca/compilers/dsl/active_model_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
class Tapioca::Compilers::Dsl::ActiveModelAttributesSpec < DslSpec
describe "Tapioca::Compilers::Dsl::ActiveModelAttributes" do
describe "initialize" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "gathers no constants if there are no classes using ActiveModel::Attributes" do
assert_empty(gathered_constants)
end
Expand Down Expand Up @@ -39,10 +35,6 @@ class Post < ActiveRecord::Base
end

describe "decorate" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "generates empty RBI file if there are no attributes in the class" do
add_ruby_file("shop.rb", <<~RUBY)
class Shop
Expand Down
24 changes: 8 additions & 16 deletions spec/tapioca/compilers/dsl/active_record_associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
class Tapioca::Compilers::Dsl::ActiveRecordAssociationsSpec < DslSpec
describe "Tapioca::Compilers::Dsl::ActiveRecordAssociationsSpec" do
describe "initialize" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "gathers no constants if there are no ActiveRecord subclasses" do
assert_empty(gathered_constants)
end
Expand Down Expand Up @@ -60,10 +56,6 @@ class Current < ActiveRecord::Base
end

describe "without errors" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "generates empty RBI file if there are no associations" do
add_ruby_file("post.rb", <<~RUBY)
class Post < ActiveRecord::Base
Expand Down Expand Up @@ -622,6 +614,10 @@ def reload_post; end
end

describe "with errors" do
before do
expect_dsl_compiler_errors!
end

it "generates RBI file for broken associations" do
add_ruby_file("schema.rb", <<~RUBY)
ActiveRecord::Migration.suppress_messages do
Expand Down Expand Up @@ -748,10 +744,6 @@ def shop=(value); end

describe "without relations enabled" do
describe "without errors" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "generates empty RBI file if there are no associations" do
add_ruby_file("post.rb", <<~RUBY)
class Post < ActiveRecord::Base
Expand Down Expand Up @@ -1310,6 +1302,10 @@ def reload_post; end
end

describe "with errors" do
before do
expect_dsl_compiler_errors!
end

it "generates RBI file for broken associations" do
add_ruby_file("schema.rb", <<~RUBY)
ActiveRecord::Migration.suppress_messages do
Expand Down Expand Up @@ -1460,10 +1456,6 @@ class Dummy < Rails::Application
RUBY
end

after do
T.unsafe(self).assert_no_generated_errors
end

it "generates RBI file for has_one_attached ActiveStorage association" do
add_ruby_file("post.rb", <<~RUBY)
class Post < ActiveRecord::Base
Expand Down
12 changes: 0 additions & 12 deletions spec/tapioca/compilers/dsl/active_record_columns_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
class Tapioca::Compilers::Dsl::ActiveRecordColumnsSpec < DslSpec
describe "Tapioca::Compilers::Dsl::ActiveRecordColumns" do
describe "initialize" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "gathers no constants if there are no ActiveRecord subclasses" do
assert_empty(gathered_constants)
end
Expand Down Expand Up @@ -51,10 +47,6 @@ class Current < ActiveRecord::Base
end

describe "by default" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "generates default columns with strong types" do
add_ruby_file("schema.rb", <<~RUBY)
ActiveRecord::Migration.suppress_messages do
Expand Down Expand Up @@ -825,10 +817,6 @@ def cost=(value); end
end

describe "when StrongTypeGeneration is defined" do
after do
T.unsafe(self).assert_no_generated_errors
end

before do
add_ruby_file("strong_type_generation.rb", <<~RUBY)
module StrongTypeGeneration
Expand Down
8 changes: 0 additions & 8 deletions spec/tapioca/compilers/dsl/active_record_enum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
class Tapioca::Compilers::Dsl::ActiveRecordEnumSpec < DslSpec
describe "Tapioca::Compilers::Dsl::ActiveRecordEnum" do
describe "initialize" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "gathers no constants if there are no ActiveRecord classes" do
assert_empty(gathered_constants)
end
Expand All @@ -32,10 +28,6 @@ class User
end

describe "decorate" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "generates RBI file for classes with an enum attribute" do
add_ruby_file("conversation.rb", <<~RUBY)
class Conversation < ActiveRecord::Base
Expand Down
8 changes: 0 additions & 8 deletions spec/tapioca/compilers/dsl/active_record_fixtures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
class Tapioca::Compilers::Dsl::ActiveRecordFixturesSpec < DslSpec
describe "Tapioca::Compilers::Dsl::ActiveRecordFixtures" do
describe "initialize" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "gathers only the ActiveSupport::TestCase base class" do
add_ruby_file("post_test.rb", <<~RUBY)
class PostTest < ActiveSupport::TestCase
Expand All @@ -31,10 +27,6 @@ class User
define_fake_rails_app
end

after do
T.unsafe(self).assert_no_generated_errors
end

it "does nothing if there are no fixtures" do
expected = <<~RBI
# typed: strong
Expand Down
12 changes: 0 additions & 12 deletions spec/tapioca/compilers/dsl/active_record_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
class Tapioca::Compilers::Dsl::ActiveRecordScopeSpec < DslSpec
describe "Tapioca::Compilers::Dsl::ActiveRecordScope" do
describe "initialize" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "gathers no constants if there are no ActiveRecord classes" do
assert_empty(gathered_constants)
end
Expand All @@ -32,10 +28,6 @@ class User
end

describe "decorate" do
after do
T.unsafe(self).assert_no_generated_errors
end

describe "with relations enabled" do
before do
require "tapioca/compilers/dsl/active_record_relations"
Expand Down Expand Up @@ -343,10 +335,6 @@ def post_scope(*args, &blk); end
end

describe "decorate_active_storage" do
after do
assert_no_generated_errors
end

before do
require "tapioca/compilers/dsl/active_record_relations"
activate_other_dsl_compilers(Tapioca::Compilers::Dsl::ActiveRecordRelations)
Expand Down
8 changes: 0 additions & 8 deletions spec/tapioca/compilers/dsl/active_record_typed_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ def before_setup
end

describe "initialize" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "gathers no constants if there are no ActiveRecordTypedStore classes" do
assert_empty(gathered_constants)
end
Expand Down Expand Up @@ -42,10 +38,6 @@ class User
end

describe "decorate" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "generates no definitions if there are no accessors to define" do
add_ruby_file("post.rb", <<~RUBY)
class Post < ActiveRecord::Base
Expand Down
8 changes: 0 additions & 8 deletions spec/tapioca/compilers/dsl/active_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
class Tapioca::Compilers::Dsl::ActiveResourceSpec < DslSpec
describe "Tapioca::Compilers::Dsl::ActiveResource" do
describe "initialize" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "gathers no constants if there are no ActiveResource classes" do
assert_empty(gathered_constants)
end
Expand All @@ -31,10 +27,6 @@ class User
end

describe "decorate" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "generates RBI file for ActiveResource classes with an integer schema field" do
add_ruby_file("post.rb", <<~RUBY)
class Post < ActiveResource::Base
Expand Down
8 changes: 0 additions & 8 deletions spec/tapioca/compilers/dsl/active_storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ class Tapioca::Compilers::Dsl::ActiveStorageSpec < DslSpec
end

describe "initialize" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "gathers no constants if there are no ActiveRecord classes" do
assert_empty(gathered_constants)
end
Expand All @@ -43,10 +39,6 @@ class User
end

describe "decorate" do
after do
T.unsafe(self).assert_no_generated_errors
end

it "generates an empty RBI file for ActiveRecord classes with no attachment" do
add_ruby_file("post.rb", <<~RUBY)
class Post < ActiveRecord::Base
Expand Down
Loading

0 comments on commit ba17012

Please sign in to comment.