Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using without RSpec? #151

Open
glenjamin opened this issue May 5, 2022 · 2 comments
Open

Using without RSpec? #151

glenjamin opened this issue May 5, 2022 · 2 comments

Comments

@glenjamin
Copy link

I was writing a little rake task to compare some data in our DB to fixture files, and found this module as the best one for doing pretty printed diffs of object.

I had a little bit of trouble finding out how to produce a diff without using Rspec though - I managed to get something working using:

puts SuperDiff::Differs::Main.call(from_dump, from_db)

However this lacks headings to say which item is before/after, and doesn't give a result I can use as an exit code.

Is there a good way to use this library in a standalone mode?

@mcmire
Copy link
Collaborator

mcmire commented May 5, 2022

Hi @glenjamin,

I'm afraid there isn't, although there should be. If you want the same header that gets printed out under RSpec as well as an exit code, you could try something like this (the header is here, for reference):

def report_diff(expected, actual)
  diff = SuperDiff::RSpec::Differ.diff(actual, expected)

  if diff.empty?
    diff
  else
    header = SuperDiff::Helpers.style do
      header "Diff:"
      newline
      newline
      border "┌ (Key) ──────────────────────────┐"
      newline
      border "│ "
      expected "‹-› in expected, not in actual"
      border "  │"
      newline
      border "│ "
      actual "‹+› in actual, not in expected"
      border "  │"
      newline
      border "│ "
      normal "‹ › in both expected and actual"
      border "  │"
      newline
      border "└─────────────────────────────────┘"
      newline
      newline
    end

    "#{header}#{diff}"
  end
end

Now you can call report_diff on your values. If this returns an empty string, then both were the same; if not, then they weren't. You should be able to use this fact to return an exit code.

@jeremygpeterson
Copy link

I needed a way to use SuperDiff in production, it's a long story, but here's my approach. As a caveat, all values were stored in a json field.

initializer

require 'super_diff'

SuperDiff.configure do |config|
  config.diff_elision_enabled = true
  config.diff_elision_maximum = 3
  config.color_enabled = %w[development test].include? Rails.env
end

method used

  def snapshot_diff(expected, actual)
    return '' if expected.eql? actual

    SuperDiff::Differs::Main.call(expected, actual, omit_empty: true)
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants