Skip to content

Fix RSpec with --force-color (and the default Split by Test Examples)#298

Merged
3v0k4 merged 1 commit intomasterfrom
dry-run
Apr 17, 2025
Merged

Fix RSpec with --force-color (and the default Split by Test Examples)#298
3v0k4 merged 1 commit intomasterfrom
dry-run

Conversation

@3v0k4
Copy link
Copy Markdown
Contributor

@3v0k4 3v0k4 commented Apr 16, 2025

Story

https://trello.com/c/KmjAHr7U/688-bugknapsackpro-force-color-option-breaks-knapsack-pro

Description

Running Knapsack Pro for RSpec with --force-color (and the default Split by Test Examples) does not work because when generating the report, Knapsack Pro passes --no-color:

And RSpec aborts in such situations:

https://github.com/rspec/rspec/blob/d064fd0e9f00af834dfcfbe55e74b066a76e98e6/rspec-core/lib/rspec/core/option_parser.rb#L148-L160

The --no-color was added in #275.

This PR removes the --no-color fix (so users can use either --no-color or --force-color) and instead prints the messages from the report individually so that not only the ANSI code are not displayed as escaped, but they actually colorize the output as they should.

Also, whenever ::RSpec::Core::ConfigurationOptions.new(cli_args) aborts, Knapsack Pro catches it so that error handling is consistent regardless of where the failure happens (there or the line after).

I managed to remove the wall of text of the error in two ways:

  • Avoiding a raise means we don't print a stacktrace
  • Compressing the error in one line, which I think captures all the concepts that were scattered in all the previous logs

Examples

Before

$ RAILS_ENV=test KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN="spec/models/commit_author_spec.rb" KNAPSACK_PRO_RSPEC_OPTIONS="--force-color --no-color" bin/rake knapsack_pro:rspec_test_example_detector
Please only use one of --force-color and --no-color
$ echo $?
1

After

$ RAILS_ENV=test KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN="spec/models/commit_author_spec.rb" KNAPSACK_PRO_RSPEC_OPTIONS="--force-color --no-color" bin/rake knapsack_pro:rspec_test_example_detector
Please only use one of --force-color and --no-color
E, [2025-04-16T17:36:00.045592 #6419] ERROR -- : [knapsack_pro] Failed to generate the slow test files report: bundle exec rspec --force-color --no-color --format json --dry-run --out .knapsack_pro/test_case_detectors/rspec/rspec_dry_run_json_report_node_0.json --default-path spec spec/models/commit_author_spec.rb
$ echo $?
1

Before

$ RAILS_ENV=test KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN="spec/models/commit_author_spec.rb" KNAPSACK_PRO_RSPEC_OPTIONS="--no-color" bin/rake knapsack_pro:rspec_test_example_detector
E, [2025-04-16T17:38:02.162443 #6711] ERROR -- : [knapsack_pro] ---------- START of actionable error message --------------------------------------------------
E, [2025-04-16T17:38:02.162588 #6711] ERROR -- : [knapsack_pro] RSpec (with a dry-run option) had a problem generating the report with test examples for the slow test files. Here is what you can do:
E, [2025-04-16T17:38:02.162639 #6711] ERROR -- : [knapsack_pro] a) Please look for an error message from RSpec in the output above or below. If you don't see anything, that is fine. Sometimes RSpec does not produce any errors in the output.
E, [2025-04-16T17:38:02.162793 #6711] ERROR -- : [knapsack_pro] b) Check if RSpec generated the report file .knapsack_pro/test_case_detectors/rspec/rspec_dry_run_json_report_node_0.json. If the report exists, it may contain an error message. Here is a preview of the report file:
E, [2025-04-16T17:38:02.163008 #6711] ERROR -- : [knapsack_pro] {"version":"3.13.3","messages":["\nAn error occurred while loading ./spec/models/commit_author_spec.rb.\nFailure/Error:\n    sit \"when project gets deleted it does not cascade\" do\n      organization = build(:organization)\n      project = build(:project, organization: organization)\n      test_suite = create(:test_suite, project: project)\n      create(:commit_author, organization_id: organization.id, test_suite_id: test_suite.id)\n  \n      expect(CommitAuthor.count).to eq 1\n  \n      project.destroy\n  \n\nNoMethodError:\n  undefined method 'sit' for class RSpec::ExampleGroups::CommitAuthor\n# ./spec/models/commit_author_spec.rb:2:in 'block in \u003ctop (required)\u003e'\n# ./spec/models/commit_author_spec.rb:1:in '\u003ctop (required)\u003e'\n# /Users/riccardoodone/code/knapsack/knapsack_pro-ruby/lib/knapsack_pro/test_case_detectors/rspec_test_example_detector.rb:41:in 'KnapsackPro::TestCaseDetectors::RSpecTestExampleDetector#generate_json_report'\n# /Users/riccardoodone/code/knapsack/knapsack_pro-ruby/lib/tasks/rspec.rake:16:in 'block (2 levels) in \u003ctop (required)\u003e'\n","No examples found."],"examples":[],"summary":{"duration":8.3e-05,"example_count":0,"failure_count":0,"pending_count":0,"errors_outside_of_examples_count":1},"summary_line":"0 examples, 0 failures, 1 error occurred outside of examples"}
E, [2025-04-16T17:38:02.163137 #6711] ERROR -- : [knapsack_pro] c) To reproduce the error manually, please run the following RSpec command. This way, you can find out what is causing the error. Please ensure you run the command in the same environment where the error occurred. For instance, if the error happens on the CI server, you should run the command in the CI environment:
E, [2025-04-16T17:38:02.163309 #6711] ERROR -- : [knapsack_pro] bundle exec rspec --no-color --format json --dry-run --no-color --out .knapsack_pro/test_case_detectors/rspec/rspec_dry_run_json_report_node_0.json --default-path spec spec/models/commit_author_spec.rb
E, [2025-04-16T17:38:02.163391 #6711] ERROR -- : [knapsack_pro] ---------- END of actionable error message --------------------------------------------------
rake aborted!
There was a problem while generating test examples for the slow test files. Please read the actionable error message above.
/Users/riccardoodone/code/knapsack/knapsack_pro-ruby/lib/knapsack_pro/test_case_detectors/rspec_test_example_detector.rb:60:in 'KnapsackPro::TestCaseDetectors::RSpecTestExampleDetector#generate_json_report'
/Users/riccardoodone/code/knapsack/knapsack_pro-ruby/lib/tasks/rspec.rake:16:in 'block (2 levels) in <top (required)>'
Tasks: TOP => knapsack_pro:rspec_test_example_detector
(See full trace by running task with --trace)
$ echo $?
1

After

$ RAILS_ENV=test KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN="spec/models/commit_author_spec.rb" KNAPSACK_PRO_RSPEC_OPTIONS="--no-color" bin/rake knapsack_pro:rspec_test_example_detector

An error occurred while loading ./spec/models/commit_author_spec.rb.
Failure/Error:
    sit "when project gets deleted it does not cascade" do
      organization = build(:organization)
      project = build(:project, organization: organization)
      test_suite = create(:test_suite, project: project)
      create(:commit_author, organization_id: organization.id, test_suite_id: test_suite.id)

      expect(CommitAuthor.count).to eq 1

      project.destroy


NoMethodError:
  undefined method 'sit' for class RSpec::ExampleGroups::CommitAuthor
# ./spec/models/commit_author_spec.rb:2:in 'block in <top (required)>'
# ./spec/models/commit_author_spec.rb:1:in '<top (required)>'
# /Users/riccardoodone/code/knapsack/knapsack_pro-ruby/lib/knapsack_pro/test_case_detectors/rspec_test_example_detector.rb:43:in 'KnapsackPro::TestCaseDetectors::RSpecTestExampleDetector#generate_json_report'
# /Users/riccardoodone/code/knapsack/knapsack_pro-ruby/lib/tasks/rspec.rake:16:in 'block (2 levels) in <top (required)>'
No examples found.
E, [2025-04-16T17:36:37.230772 #6491] ERROR -- : [knapsack_pro] Failed to generate the slow test files report: bundle exec rspec --no-color --format json --dry-run --out .knapsack_pro/test_case_detectors/rspec/rspec_dry_run_json_report_node_0.json --default-path spec spec/models/commit_author_spec.rb
$ echo $?
1

Before

RAILS_ENV=test KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN="spec/models/commit_author_spec.rb" KNAPSACK_PRO_RSPEC_OPTIONS="--force-color" bin/rake knapsack_pro:rspec_test_example_detector
# As above

After

Screenshot 2025-04-16 at 17 59 40

Checklist reminder

  • You added the changes to the UNRELEASED section of the CHANGELOG.md, including the needed bump (ie, patch, minor, major)
  • You follow the architecture outlined below for RSpec in Queue Mode, which is a work in progress (feel free to propose changes):
    • Pure: lib/knapsack_pro/pure/queue/rspec_pure.rb contains pure functions that are unit tested.
    • Extension: lib/knapsack_pro/extensions/rspec_extension.rb encapsulates calls to RSpec internals and is integration and e2e tested.
    • Runner: lib/knapsack_pro/runners/queue/rspec_runner.rb invokes the pure code and the extension to produce side effects, which are integration and e2e tested.

@3v0k4 3v0k4 force-pushed the dry-run branch 5 times, most recently from c63d627 to 5973858 Compare April 16, 2025 17:21
@3v0k4 3v0k4 changed the title fix: rspec_test_example_detector with --force-color Fix RSpec with --force-color (and the default Split by Test Examples) Apr 16, 2025
Comment thread lib/knapsack_pro/test_case_detectors/rspec_test_example_detector.rb
Comment thread lib/knapsack_pro/test_case_detectors/rspec_test_example_detector.rb
@3v0k4 3v0k4 merged commit c776167 into master Apr 17, 2025
25 checks passed
@3v0k4 3v0k4 deleted the dry-run branch April 17, 2025 08:27
3v0k4 added a commit to KnapsackPro/docs.knapsackpro.com that referenced this pull request Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants