Module: Differ

Defined in:
lib/rspec/flaky/differ.rb

Constant Summary collapse

EXPECTED_CONTENT =
%w(failed.json passed.json)

Class Method Summary collapse

Class Method Details

.contains_passed_and_failed_jsons?(tables_dir) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/rspec/flaky/differ.rb', line 54

def contains_passed_and_failed_jsons? tables_dir
  actual_content = tables_dir.children.map(&:basename).map(&:to_s)
  EXPECTED_CONTENT & actual_content == EXPECTED_CONTENT
end

.create_summary_folderObject



27
28
29
# File 'lib/rspec/flaky/differ.rb', line 27

def create_summary_folder
  FileUtils.mkdir_p(Pathes.summary_path)
end

.get_diffs(tables_dir) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rspec/flaky/differ.rb', line 31

def get_diffs tables_dir
  return 'no_content' unless contains_passed_and_failed_jsons? tables_dir
  result = []
  jsons = tables_dir.children.select { |child| EXPECTED_CONTENT.include? child.basename.to_s }
  jsons = read_jsons(jsons)
  
  return 'empty_table' if jsons.values.all? &:empty?
  jsons.values.map(&:length).max.times do |idx|
    passed_record = jsons["passed"].try(:[], idx) || {}
    failed_record = jsons["failed"].try(:[], idx) || {}
    result << Hashdiff.diff(passed_record, failed_record)
  end
  result
end

.get_resultObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rspec/flaky/differ.rb', line 9

def get_result
  create_summary_folder
  @diffs = []
  Pathes.summary_path.children.each do |example_dir|
    next unless example_dir.directory?
    next unless example_dir.basename.to_s.start_with?(".:")
    example_dir.children.select do |tables_dir|
      next unless tables_dir.directory?
      @diffs << {
        location: Pathes.relative_path(example_dir),
        table: tables_dir.basename.to_s,
        result: get_diffs(tables_dir)
      }
    end
  end
  Drawer.draw @diffs
end

.read_jsons(pathes) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/rspec/flaky/differ.rb', line 46

def read_jsons pathes
  {}.tap do |hash|
    pathes.each do |path|
      hash[path.basename.to_s.split('.')[0]] = JSON.parse(File.read(Pathes.base_path.join(path)))
    end
  end
end