class RSpecCommand::MatchFixture

@api private @since 1.0.0

Public Class Methods

new(fixture_root, local_root, fixture_path, local_path=nil) click to toggle source

Create a new matcher for a fixture.

@param fixture_root [String] Absolute path to the fixture folder. @param local_root [String] Absolute path to test folder to compare against. @param fixture_path [String] Relative path to the fixture to compare against. @param local_path [String] Optional relative path to the test data to compare against.

# File lib/rspec_command/match_fixture.rb, line 28
def initialize(fixture_root, local_root, fixture_path, local_path=nil)
  @fixture = FileList.new(fixture_root, fixture_path)
  @local = FileList.new(local_root, local_path)
end

Public Instance Methods

description() click to toggle source

Callback for RSpec. Returns a human-readable description for the matcher.

@return [String]

# File lib/rspec_command/match_fixture.rb, line 44
def description
  "match fixture #{@fixture.path}"
end
failure_message() click to toggle source

Callback fro RSpec. Returns a human-readable failure message.

@return [String]

# File lib/rspec_command/match_fixture.rb, line 51
def failure_message
  matching_files = @fixture.files & @local.files
  fixture_only_files = @fixture.files - @local.files
  local_only_files = @local.files - @fixture.files
  buf = "expected fixture #{@fixture.path} to match files:\n"
  (@fixture.files | @local.files).sort.each do |file|
    if matching_files.include?(file)
      local_file = @local.absolute(file)
      fixture_file = @fixture.absolute(file)
      if File.directory?(local_file) && File.directory?(fixture_file)
        # Do nothing
      elsif File.directory?(fixture_file)
        buf << "  #{file} should be a directory\n"
      elsif File.directory?(local_file)
        buf << "  #{file} should not be a directory"
      else
        actual = IO.read(local_file)
        expected = IO.read(fixture_file)
        if actual != expected
          # Show a diff
          buf << "  #{file} does not match fixture:"
          buf << differ.diff(actual, expected).split(/\n/).map {|line| '    '+line }.join("\n")
        end
      end
    elsif fixture_only_files.include?(file)
      buf << "  #{file} is not found\n"
    elsif local_only_files.include?(file)
      buf << "  #{file} should not exist\n"
    end
  end
  buf
end
matches?(cmd) click to toggle source

Primary callback for RSpec matcher API.

@param cmd Ignored. @return [Boolean]

# File lib/rspec_command/match_fixture.rb, line 37
def matches?(cmd)
  files_match? && file_content_match?
end

Private Instance Methods

differ() click to toggle source

Return a Differ object to make diffs.

@note This is using a nominally private API. It could break in the future. @return [RSpec::Support::Differ] @example

differ.diff(actual, expected)
# File lib/rspec_command/match_fixture.rb, line 112
def differ
  RSpec::Expectations.differ
end
file_content_match?() click to toggle source

Do the file contents match?

@return [Boolean]

# File lib/rspec_command/match_fixture.rb, line 96
def file_content_match?
  @fixture.full_files.zip(@local.full_files).all? do |fixture_file, local_file|
    if File.directory?(fixture_file)
      File.directory?(local_file)
    else
      !File.directory?(local_file) && IO.read(fixture_file) == IO.read(local_file)
    end
  end
end
files_match?() click to toggle source

Do the file entries match? Doesn't check content.

@return [Boolean]

# File lib/rspec_command/match_fixture.rb, line 89
def files_match?
  @fixture.files == @local.files
end