Skip to content

Commit f278250

Browse files
Test Cleanup: Improve test setup
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
1 parent f6c6f45 commit f278250

31 files changed

+244
-276
lines changed

lib/generators/suspenders/testing_generator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def modify_spec_helper
4343
allow_localhost: true,
4444
allow: [
4545
/(chromedriver|storage).googleapis.com/,
46-
"googlechromelabs.github.io",
46+
"googlechromelabs.github.io"
4747
]
4848
)
4949
RUBY

test/fixtures/files/Rakefile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require_relative "config/application"
2+
require "bundler/audit/task"
3+
Bundler::Audit::Task.new
4+
5+
Rails.application.load_tasks

test/fixtures/files/_flashes.html.erb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<% if flash.any? %>
2+
<div class="flashes">
3+
<% flash.each do |type, message| -%>
4+
<div class="flash-<%= type %>"><%= message %></div>
5+
<% end -%>
6+
</div>
7+
<% end %>

test/fixtures/files/action_mailer.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RSpec.configure do |config|
2+
config.before(:each) do
3+
ActionMailer::Base.deliveries.clear
4+
end
5+
end

test/fixtures/files/active_job.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require "active_job/logging"
2+
require "active_job/log_subscriber"
3+
4+
ActiveSupport::Notifications.unsubscribe("enqueue.active_job")
5+
6+
module ActiveJob
7+
module Logging
8+
class EnqueueLogSubscriber < LogSubscriber
9+
define_method :enqueue, instance_method(:enqueue)
10+
end
11+
end
12+
end
13+
14+
ActiveJob::Logging::EnqueueLogSubscriber.attach_to(:active_job)

test/fixtures/files/better_html.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Rails.configuration.to_prepare do
2+
if Rails.env.test?
3+
require "better_html"
4+
5+
BetterHtml.config = BetterHtml::Config.new(Rails.configuration.x.better_html)
6+
7+
BetterHtml.config.template_exclusion_filter = proc { |filename| !filename.start_with?(Rails.root.to_s) }
8+
end
9+
end

test/fixtures/files/driver.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RSpec.configure do |config|
2+
config.before(:each, type: :system) do
3+
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
4+
end
5+
end
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class EmailInterceptor
2+
include ActiveSupport::Configurable
3+
4+
config_accessor :interceptor_addresses, default: []
5+
6+
def self.delivering_email(message)
7+
to = interceptor_addresses
8+
9+
message.to = to if to.any?
10+
end
11+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Rails.application.configure do
2+
if ENV["INTERCEPTOR_ADDRESSES"].present?
3+
config.action_mailer.interceptors = %w[EmailInterceptor]
4+
end
5+
end

test/fixtures/files/erb-lint.yml

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
glob: "app/views/**/*.{html,turbo_stream}{+*,}.erb"
3+
4+
linters:
5+
AllowedScriptType:
6+
enabled: true
7+
allowed_types:
8+
- "module"
9+
- "text/javascript"
10+
ErbSafety:
11+
enabled: true
12+
better_html_config: "config/better_html.yml"
13+
GitHub::Accessibility::AvoidBothDisabledAndAriaDisabledCounter:
14+
enabled: true
15+
GitHub::Accessibility::AvoidGenericLinkTextCounter:
16+
enabled: true
17+
GitHub::Accessibility::DisabledAttributeCounter:
18+
enabled: true
19+
GitHub::Accessibility::IframeHasTitleCounter:
20+
enabled: true
21+
GitHub::Accessibility::ImageHasAltCounter:
22+
enabled: true
23+
GitHub::Accessibility::LandmarkHasLabelCounter:
24+
enabled: true
25+
GitHub::Accessibility::LinkHasHrefCounter:
26+
enabled: true
27+
GitHub::Accessibility::NestedInteractiveElementsCounter:
28+
enabled: true
29+
GitHub::Accessibility::NoAriaLabelMisuseCounter:
30+
enabled: true
31+
GitHub::Accessibility::NoPositiveTabIndexCounter:
32+
enabled: true
33+
GitHub::Accessibility::NoRedundantImageAltCounter:
34+
enabled: true
35+
GitHub::Accessibility::NoTitleAttributeCounter:
36+
enabled: true
37+
GitHub::Accessibility::SvgHasAccessibleTextCounter:
38+
enabled: true
39+
Rubocop:
40+
enabled: true
41+
rubocop_config:
42+
inherit_from:
43+
- .rubocop.yml
44+
45+
Lint/EmptyBlock:
46+
Enabled: false
47+
Layout/InitialIndentation:
48+
Enabled: false
49+
Layout/TrailingEmptyLines:
50+
Enabled: false
51+
Layout/TrailingWhitespace:
52+
Enabled: false
53+
Layout/LeadingEmptyLines:
54+
Enabled: false
55+
Style/FrozenStringLiteralComment:
56+
Enabled: false
57+
Style/MultilineTernaryOperator:
58+
Enabled: false
59+
Lint/UselessAssignment:
60+
Exclude:
61+
- "app/views/**/*"
62+
63+
EnableDefaultLinters: true

test/fixtures/files/eslintrc.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": ["@thoughtbot/eslint-config/prettier"],
3+
"parserOptions": {
4+
"ecmaVersion": "latest",
5+
"sourceType": "module"
6+
}
7+
}

test/fixtures/files/factories.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FactoryBot.define do
2+
end

test/fixtures/files/factories_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Factories" do
4+
it "has valid factoties" do
5+
FactoryBot.lint traits: true
6+
end
7+
end

test/fixtures/files/factories_test.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "test_helper"
2+
3+
class FactoryBotsTest < ActiveSupport::TestCase
4+
class FactoryLintingTest < FactoryBotsTest
5+
test "linting of factories" do
6+
FactoryBot.lint traits: true
7+
end
8+
end
9+
end

test/fixtures/files/i18n.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RSpec.configure do |config|
2+
config.include ActionView::Helpers::TranslationHelper
3+
end

test/fixtures/files/inline_svg.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
InlineSvg.configure do |config|
2+
config.raise_on_file_not_found = true
3+
end

test/fixtures/files/prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/bundle/**

test/fixtures/files/prettierrc.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"singleQuote": true,
3+
"overrides": [
4+
{
5+
"files": ["**/*.css", "**/*.scss", "**/*.html"],
6+
"options": {
7+
"singleQuote": false
8+
}
9+
}
10+
]
11+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Shoulda::Matchers.configure do |config|
2+
config.integrate do |with|
3+
with.test_framework :rspec
4+
with.library :rails
5+
end
6+
end

test/fixtures/files/spec_helper.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require "webmock/rspec"
2+
3+
RSpec.configure do |config|
4+
config.example_status_persistence_file_path = "tmp/rspec_examples.txt"
5+
config.order = :random
6+
7+
config.expect_with :rspec do |expectations|
8+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
9+
end
10+
11+
config.mock_with :rspec do |mocks|
12+
mocks.verify_partial_doubles = true
13+
end
14+
config.shared_context_metadata_behavior = :apply_to_host_groups
15+
end
16+
17+
WebMock.disable_net_connect!(
18+
allow_localhost: true,
19+
allow: [
20+
/(chromedriver|storage).googleapis.com/,
21+
"googlechromelabs.github.io"
22+
]
23+
)

test/fixtures/files/stylelintrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@thoughtbot/stylelint-config"
3+
}

test/fixtures/files/test_helper.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ENV["RAILS_ENV"] ||= "test"
2+
require_relative "../config/environment"
3+
require "rails/test_help"
4+
5+
module ActiveSupport
6+
class TestCase
7+
# Run tests in parallel with specified workers
8+
parallelize(workers: :number_of_processors)
9+
10+
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
11+
fixtures :all
12+
13+
# Add more helper methods to be used by all tests here...
14+
end
15+
end

test/generators/suspenders/advisories_generator_test.rb

+2-10
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,12 @@ class AdvisoriesGeneratorTest < Rails::Generators::TestCase
4343
end
4444

4545
test "modifies Rakefile" do
46-
touch "Rakefile"
47-
content = <<~TEXT
46+
touch "Rakefile", content: <<~TEXT
4847
require_relative "config/application"
4948
5049
Rails.application.load_tasks
5150
TEXT
52-
File.open(app_root("Rakefile"), "w") { _1.write content }
53-
expected_rakefile = <<~TEXT
54-
require_relative "config/application"
55-
require "bundler/audit/task"
56-
Bundler::Audit::Task.new
57-
58-
Rails.application.load_tasks
59-
TEXT
51+
expected_rakefile = file_fixture("Rakefile").read
6052

6153
run_generator
6254

test/generators/suspenders/email_generator_test.rb

+2-20
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,7 @@ class EmailGeneratorTest < Rails::Generators::TestCase
1212
teardown :restore_destination
1313

1414
test "creates a mailer intercepter" do
15-
expected = <<~RUBY
16-
class EmailInterceptor
17-
include ActiveSupport::Configurable
18-
19-
config_accessor :interceptor_addresses, default: []
20-
21-
def self.delivering_email(message)
22-
to = interceptor_addresses
23-
24-
message.to = to if to.any?
25-
end
26-
end
27-
RUBY
15+
expected = file_fixture("email_interceptor.rb").read
2816

2917
run_generator
3018

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

3624
test "creates initializer" do
37-
expected = <<~RUBY
38-
Rails.application.configure do
39-
if ENV["INTERCEPTOR_ADDRESSES"].present?
40-
config.action_mailer.interceptors = %w[EmailInterceptor]
41-
end
42-
end
43-
RUBY
25+
expected = file_fixture("email_interceptor_initializer.rb").read
4426

4527
run_generator
4628

0 commit comments

Comments
 (0)