splitwise / super_diff

A more helpful way to view differences between complex data structures in RSpec.
https://splitwise.github.io/super_diff/
MIT License
988 stars 53 forks source link

super_diff seems to ignore --no-color #260

Closed dgmstuart closed 1 month ago

dgmstuart commented 1 month ago

In environments with no colors, it's important to be able to generate test output without color codes, otherwise the output looks like this:

|| Run options: include {:locations=>{"./spec/services/linked/url_builder_spec.rb"=>[17]}}
|| F
|| 
|| Failures:
|| 
||   1) Linked::UrlBuilder#booking_details_url returns a url for accessing a booking with token authentication
||      Failure/Error: expect(token).to eq("no-a-real-id")
||        Expected "not-a-real-id" to eq "no-a-real-id".
||      # ./spec/services/linked/url_builder_spec.rb:21:in `block (3 levels) in <main>'
||      # ./spec/support/config/brand_filters.rb:47:in `block (3 levels) in <main>'

...but it seems like after adding super_diff, we always get color in the output, even when explicitly disabled by passing the --no-color flag to the rspec command.

dgmstuart commented 1 month ago

Is it because of this?

https://github.com/splitwise/super_diff/blob/2d06e39794ba1d43200693826d7fd4fa5d88b01c/lib/super_diff/core/configuration.rb#L187-L189

SuperDiff is working out whether colours should be used in its own way? But RSpec already has its own ideas about whether colors should be used, and that isn't being taken into account?

dgmstuart commented 1 month ago

Patched in my spec/rails_helper.rb:

RSpec.configure do |config|
  # ...

  SuperDiff.configure do |superdiff_config|
    superdiff_config.color_enabled = config.color_enabled?
  end
end

...but this feels like a bit of a hack?

The SuperDiff config needs to be inside the RSpec config block to ensure that RSpec is fully configured before we try to fetch the color settings from it?

jas14 commented 1 month ago

@dgmstuart , thanks for submitting this issue. I agree that since SuperDiff is designed to work seamlessly with RSpec, it should adopt its configuration by default.

Since SuperDiff can be used directly without RSpec, we should make sure to preserve the default behavior when RSpec isn't loaded. But when RSpec is loaded, we should use it if someone hasn't explicitly set SuperDiff.configuration.color_enabled.