Skip to content

Commit

Permalink
Test Cleanup: Improve test setup
Browse files Browse the repository at this point in the history
Use `touch` test helper instead of directly working with the `File`
class during the setup phase.

Also create [file_fixtures][] to simplify test setup.

Because the newly added file fixtures are processed with standard, I
needed to adjust the testing generator to account for a minor violation.

[file_fixtures]: https://api.rubyonrails.org/classes/ActiveSupport/Testing/FileFixtures.html#method-i-file_fixture
  • Loading branch information
stevepolitodesign committed Apr 5, 2024
1 parent 56b316c commit e53a42b
Show file tree
Hide file tree
Showing 31 changed files with 244 additions and 276 deletions.
2 changes: 1 addition & 1 deletion lib/generators/suspenders/testing_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def modify_spec_helper
allow_localhost: true,
allow: [
/(chromedriver|storage).googleapis.com/,
"googlechromelabs.github.io",
"googlechromelabs.github.io"
]
)
RUBY
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/files/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require_relative "config/application"
require "bundler/audit/task"
Bundler::Audit::Task.new

Rails.application.load_tasks
7 changes: 7 additions & 0 deletions test/fixtures/files/_flashes.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% if flash.any? %>
<div class="flashes">
<% flash.each do |type, message| -%>
<div class="flash-<%= type %>"><%= message %></div>
<% end -%>
</div>
<% end %>
5 changes: 5 additions & 0 deletions test/fixtures/files/action_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RSpec.configure do |config|
config.before(:each) do
ActionMailer::Base.deliveries.clear
end
end
14 changes: 14 additions & 0 deletions test/fixtures/files/active_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "active_job/logging"
require "active_job/log_subscriber"

ActiveSupport::Notifications.unsubscribe("enqueue.active_job")

module ActiveJob
module Logging
class EnqueueLogSubscriber < LogSubscriber
define_method :enqueue, instance_method(:enqueue)
end
end
end

ActiveJob::Logging::EnqueueLogSubscriber.attach_to(:active_job)
9 changes: 9 additions & 0 deletions test/fixtures/files/better_html.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Rails.configuration.to_prepare do
if Rails.env.test?
require "better_html"

BetterHtml.config = BetterHtml::Config.new(Rails.configuration.x.better_html)

BetterHtml.config.template_exclusion_filter = proc { |filename| !filename.start_with?(Rails.root.to_s) }
end
end
5 changes: 5 additions & 0 deletions test/fixtures/files/driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
end
end
11 changes: 11 additions & 0 deletions test/fixtures/files/email_interceptor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class EmailInterceptor
include ActiveSupport::Configurable

config_accessor :interceptor_addresses, default: []

def self.delivering_email(message)
to = interceptor_addresses

message.to = to if to.any?
end
end
5 changes: 5 additions & 0 deletions test/fixtures/files/email_interceptor_initializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Rails.application.configure do
if ENV["INTERCEPTOR_ADDRESSES"].present?
config.action_mailer.interceptors = %w[EmailInterceptor]
end
end
63 changes: 63 additions & 0 deletions test/fixtures/files/erb-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
glob: "app/views/**/*.{html,turbo_stream}{+*,}.erb"

linters:
AllowedScriptType:
enabled: true
allowed_types:
- "module"
- "text/javascript"
ErbSafety:
enabled: true
better_html_config: "config/better_html.yml"
GitHub::Accessibility::AvoidBothDisabledAndAriaDisabledCounter:
enabled: true
GitHub::Accessibility::AvoidGenericLinkTextCounter:
enabled: true
GitHub::Accessibility::DisabledAttributeCounter:
enabled: true
GitHub::Accessibility::IframeHasTitleCounter:
enabled: true
GitHub::Accessibility::ImageHasAltCounter:
enabled: true
GitHub::Accessibility::LandmarkHasLabelCounter:
enabled: true
GitHub::Accessibility::LinkHasHrefCounter:
enabled: true
GitHub::Accessibility::NestedInteractiveElementsCounter:
enabled: true
GitHub::Accessibility::NoAriaLabelMisuseCounter:
enabled: true
GitHub::Accessibility::NoPositiveTabIndexCounter:
enabled: true
GitHub::Accessibility::NoRedundantImageAltCounter:
enabled: true
GitHub::Accessibility::NoTitleAttributeCounter:
enabled: true
GitHub::Accessibility::SvgHasAccessibleTextCounter:
enabled: true
Rubocop:
enabled: true
rubocop_config:
inherit_from:
- .rubocop.yml

Lint/EmptyBlock:
Enabled: false
Layout/InitialIndentation:
Enabled: false
Layout/TrailingEmptyLines:
Enabled: false
Layout/TrailingWhitespace:
Enabled: false
Layout/LeadingEmptyLines:
Enabled: false
Style/FrozenStringLiteralComment:
Enabled: false
Style/MultilineTernaryOperator:
Enabled: false
Lint/UselessAssignment:
Exclude:
- "app/views/**/*"

EnableDefaultLinters: true
7 changes: 7 additions & 0 deletions test/fixtures/files/eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": ["@thoughtbot/eslint-config/prettier"],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
}
}
2 changes: 2 additions & 0 deletions test/fixtures/files/factories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FactoryBot.define do
end
7 changes: 7 additions & 0 deletions test/fixtures/files/factories_spec_lint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "rails_helper"

RSpec.describe "Factories" do
it "has valid factoties" do
FactoryBot.lint traits: true
end
end
9 changes: 9 additions & 0 deletions test/fixtures/files/factories_test_lint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "test_helper"

class FactoryBotsTest < ActiveSupport::TestCase
class FactoryLintingTest < FactoryBotsTest
test "linting of factories" do
FactoryBot.lint traits: true
end
end
end
3 changes: 3 additions & 0 deletions test/fixtures/files/i18n.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RSpec.configure do |config|
config.include ActionView::Helpers::TranslationHelper
end
3 changes: 3 additions & 0 deletions test/fixtures/files/inline_svg.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
InlineSvg.configure do |config|
config.raise_on_file_not_found = true
end
1 change: 1 addition & 0 deletions test/fixtures/files/prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/bundle/**
11 changes: 11 additions & 0 deletions test/fixtures/files/prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"singleQuote": true,
"overrides": [
{
"files": ["**/*.css", "**/*.scss", "**/*.html"],
"options": {
"singleQuote": false
}
}
]
}
6 changes: 6 additions & 0 deletions test/fixtures/files/shoulda_matchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
23 changes: 23 additions & 0 deletions test/fixtures/files/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "webmock/rspec"

RSpec.configure do |config|
config.example_status_persistence_file_path = "tmp/rspec_examples.txt"
config.order = :random

config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.shared_context_metadata_behavior = :apply_to_host_groups
end

WebMock.disable_net_connect!(
allow_localhost: true,
allow: [
/(chromedriver|storage).googleapis.com/,
"googlechromelabs.github.io"
]
)
3 changes: 3 additions & 0 deletions test/fixtures/files/stylelintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@thoughtbot/stylelint-config"
}
15 changes: 15 additions & 0 deletions test/fixtures/files/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
require "rails/test_help"

module ActiveSupport
class TestCase
# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors)

# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all

# Add more helper methods to be used by all tests here...
end
end
12 changes: 2 additions & 10 deletions test/generators/suspenders/advisories_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,12 @@ class AdvisoriesGeneratorTest < Rails::Generators::TestCase
end

test "modifies Rakefile" do
touch "Rakefile"
content = <<~TEXT
touch "Rakefile", content: <<~TEXT
require_relative "config/application"
Rails.application.load_tasks
TEXT
File.open(app_root("Rakefile"), "w") { _1.write content }
expected_rakefile = <<~TEXT
require_relative "config/application"
require "bundler/audit/task"
Bundler::Audit::Task.new
Rails.application.load_tasks
TEXT
expected_rakefile = file_fixture("Rakefile").read

run_generator

Expand Down
22 changes: 2 additions & 20 deletions test/generators/suspenders/email_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,7 @@ class EmailGeneratorTest < Rails::Generators::TestCase
teardown :restore_destination

test "creates a mailer intercepter" do
expected = <<~RUBY
class EmailInterceptor
include ActiveSupport::Configurable
config_accessor :interceptor_addresses, default: []
def self.delivering_email(message)
to = interceptor_addresses
message.to = to if to.any?
end
end
RUBY
expected = file_fixture("email_interceptor.rb").read

run_generator

Expand All @@ -34,13 +22,7 @@ def self.delivering_email(message)
end

test "creates initializer" do
expected = <<~RUBY
Rails.application.configure do
if ENV["INTERCEPTOR_ADDRESSES"].present?
config.action_mailer.interceptors = %w[EmailInterceptor]
end
end
RUBY
expected = file_fixture("email_interceptor_initializer.rb").read

run_generator

Expand Down
Loading

0 comments on commit e53a42b

Please sign in to comment.