Skip to content

Commit 6e0a159

Browse files
committed
fixed simplecov on ci
1 parent 0ac46a1 commit 6e0a159

File tree

4 files changed

+60
-18
lines changed

4 files changed

+60
-18
lines changed

rails_test_app/create_app.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,39 @@ def copy_template(file, target_path = nil)
294294
copy_template(entry)
295295
end
296296

297+
boot_path = File.join(RAILS_APP_DIR, "config", "boot.rb")
298+
unless File.read(boot_path).include?("LOGSTRUCT_SIMPLECOV_BOOT")
299+
simplecov_boot = <<~RUBY
300+
301+
if ENV['RAILS_ENV'] == 'test' && ENV['LOGSTRUCT_SIMPLECOV_BOOT'] != '1'
302+
ENV['LOGSTRUCT_SIMPLECOV_BOOT'] = '1'
303+
begin
304+
require 'simplecov'
305+
require 'simplecov-json'
306+
SimpleCov.formatters = [
307+
SimpleCov::Formatter::HTMLFormatter,
308+
SimpleCov::Formatter::JSONFormatter
309+
]
310+
SimpleCov.start do
311+
root_path = File.expand_path('../../..', __dir__)
312+
coverage_dir File.join(root_path, 'coverage_rails')
313+
add_filter '/rails_test_app/'
314+
enable_coverage :branch
315+
primary_coverage :branch
316+
end
317+
SimpleCov.at_exit do
318+
SimpleCov.result
319+
end
320+
rescue LoadError
321+
# SimpleCov not available; skip instrumentation
322+
end
323+
end
324+
325+
RUBY
326+
327+
File.write(boot_path, File.read(boot_path) + simplecov_boot)
328+
end
329+
297330
# Ensure HostAuthorization does not block example test hosts
298331
test_env_path = File.join(RAILS_APP_DIR, "config", "environments", "test.rb")
299332
if File.exist?(test_env_path)

rails_test_app/templates/config/initializers/logstruct.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# typed: strict
22

3+
require "log_struct"
4+
35
# Configure LogStruct
46
LogStruct.configure do |config|
57
# Specify which environments to enable in

rails_test_app/templates/test/integration/boot_logs_integration_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def test_rails_runner_emits_dotenv_structured_logs_and_ends_with_true
2020
refute_empty output, "Expected some output from rails runner"
2121

2222
lines = output.split("\n").map(&:strip).reject(&:empty?)
23+
lines.reject! do |line|
24+
line.start_with?("Coverage report generated", "Line Coverage:", "Branch Coverage:")
25+
end
2326
# Ensure the last non-empty line is 'true'
2427
last_line = lines.last
2528

@@ -59,6 +62,9 @@ def test_rails_runner_emits_original_dotenv_logs_when_disabled
5962
refute_empty output, "Expected some output from rails runner"
6063

6164
lines = output.split("\n").map(&:strip).reject(&:empty?)
65+
lines.reject! do |line|
66+
line.start_with?("Coverage report generated", "Line Coverage:", "Branch Coverage:")
67+
end
6268
last_line = lines.last
6369

6470
assert_equal "false", last_line, "Expected final line to be 'false'"

rails_test_app/templates/test/test_helper.rb

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
# typed: true
22

3-
require "simplecov"
3+
require "simplecov" unless defined?(SimpleCov)
44
require "simplecov-json"
55
require "sorbet-runtime"
66
require "debug"
77

8-
# Configure SimpleCov for Rails integration tests
9-
SimpleCov.formatters = [
10-
SimpleCov::Formatter::HTMLFormatter,
11-
SimpleCov::Formatter::JSONFormatter
12-
]
8+
unless SimpleCov.running
9+
SimpleCov.formatters = [
10+
SimpleCov::Formatter::HTMLFormatter,
11+
SimpleCov::Formatter::JSONFormatter
12+
]
1313

14-
# We need to configure SimpleCov before loading any application code
15-
SimpleCov.start do
16-
T.bind(self, T.all(SimpleCov::Configuration, Kernel))
14+
SimpleCov.start do
15+
T.bind(self, T.all(SimpleCov::Configuration, Kernel))
1716

18-
gem_path = File.expand_path("../../../../", __FILE__)
19-
SimpleCov.root(gem_path)
17+
gem_path = File.expand_path("../../../../", __FILE__)
18+
SimpleCov.root(gem_path)
2019

21-
add_filter "rails_test_app"
20+
add_filter "rails_test_app"
2221

23-
# Coverage is stored in a directory relative to the Rails app
24-
coverage_dir "coverage_rails"
25-
# command_name "test:integration"
22+
coverage_dir "coverage_rails"
2623

27-
# Enable branch coverage
28-
enable_coverage :branch
29-
primary_coverage :branch
24+
enable_coverage :branch
25+
primary_coverage :branch
26+
end
27+
28+
SimpleCov.at_exit do
29+
SimpleCov.result
30+
end
3031
end
3132

3233
# Require logstruct after starting SimpleCov

0 commit comments

Comments
 (0)