Class: Clash::Diff

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/clash/diff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#boldit, #colorize, #expand_list_of_numbers, #expand_range, #get_number, #greenit, #pout, #print_fail, #print_pass, #read_test_line_numbers, #redit, #require_gems, #strip_tasks, #system, #test_at_line_number, #yellowit

Constructor Details

#initialize(a, b, options = {}) ⇒ Diff

Returns a new instance of Diff.



7
8
9
10
11
12
13
# File 'lib/clash/diff.rb', line 7

def initialize(a, b, options={})
  @diffs   = {}
  @a       = a
  @b       = b
  @context = options[:context] || 2
  @test_failures = []
end

Instance Attribute Details

#test_failuresObject

Returns the value of attribute test_failures.



5
6
7
# File 'lib/clash/diff.rb', line 5

def test_failures
  @test_failures
end

Instance Method Details

#diffObject



15
16
17
18
19
20
21
22
23
# File 'lib/clash/diff.rb', line 15

def diff
  if File.directory?(@a)
    diff_dirs(@a, @b)
  else
    diff_files(@a, @b)
  end

  @diffs
end

#diff_dirs(dir1, dir2) ⇒ Object

Recursively diff common files between dir1 and dir2



39
40
41
42
43
44
45
# File 'lib/clash/diff.rb', line 39

def diff_dirs(dir1, dir2)
  mattching_dir_files(dir1, dir2).each do |file|
    a = File.join(dir1, file)
    b = File.join(dir2, file)
    diff_files(a,b)
  end
end

#diff_files(a, b) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/clash/diff.rb', line 25

def diff_files(a, b)
  if exists(a) && exists(b)
    diffy = Diffy::Diff.new(a,b, :source => 'files', :context => @context)
    file_diff = diffy.to_a

    if !file_diff.empty?
      file_diff = format_diff(file_diff)
      @diffs[yellowit("\nCompared #{a} to #{b}:\n")] = file_diff 
    end
  end
end

#dir_files(dir) ⇒ Object

Find all files in a given directory



63
64
65
# File 'lib/clash/diff.rb', line 63

def dir_files(dir)
  Find.find(dir).to_a.reject!{|f| File.directory?(f) }
end

#exists(f) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/clash/diff.rb', line 81

def exists(f)
  file_exists = File.exists?(f)

  if !file_exists
    @test_failures << "#{redit('File not found:')} #{f}\n"
  end

  file_exists
end

#format_diff(diff) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/clash/diff.rb', line 92

def format_diff(diff)
  count = 0

  diff = diff.map { |line|
    case line
    when /^\+/ then 
      count = 0
      "  #{greenit(line)}"
    when /^-/ then 
      count = 0
      "  #{redit(line)}"
    else 
      if count == @context
        count = 0
        "...\n  #{line}"
      else
        count += 1
        "  #{line}"
      end
    end
  }
  diff.join('')
end

#mattching_dir_files(dir1, dir2) ⇒ Object

Return files that exist in both directories (without dir names)



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/clash/diff.rb', line 49

def mattching_dir_files(dir1, dir2)
  dir1_files = dir_files(dir1).map {|f| f.sub("#{dir1}/",'') }
  dir2_files = dir_files(dir2).map {|f| f.sub("#{dir2}/",'') }

  matches = dir1_files & dir2_files

  unique_files(dir1, dir2_files, matches)
  unique_files(dir2, dir1_files, matches)

  matches
end

#unique_files(dir, dir_files, common_files) ⇒ Object

Find files which aren’t common to both directories



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/clash/diff.rb', line 69

def unique_files(dir, dir_files, common_files)
  unique = dir_files - common_files
  if !unique.empty?
    @test_failures << yellowit("\nMissing from directory #{dir}:\n")
    unique.each do |f| 
      failure = "  - #{f}"
      failure << "\n" if unique.last == f
      @test_failures << failure
    end
  end
end