Module: Clash::Helpers

Extended by:
Helpers
Included in:
Diff, Helpers, Test, Tests
Defined in:
lib/clash/helpers.rb

Instance Method Summary collapse

Instance Method Details

#boldit(str) ⇒ Object



106
107
108
# File 'lib/clash/helpers.rb', line 106

def boldit(str)
  colorize(str, 'bold')
end

#colorize(str, color) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/clash/helpers.rb', line 66

def colorize(str, color)
  if STDOUT.tty?
    str.send(color)
  else
    str
  end
end

#expand_list_of_numbers(only) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/clash/helpers.rb', line 5

def expand_list_of_numbers(only)
  # Used in options[:only] to expand all possibilities.
  if only.is_a?(Array)
    only = only.join(',')
  end
  only.split(',').map do |num|
    if num.include?("-")
      expand_range(num)
    else
      get_number(num)
    end
  end.flatten.sort.uniq
end

#expand_range(string_range) ⇒ Object



19
20
21
22
# File 'lib/clash/helpers.rb', line 19

def expand_range(string_range)
  lower, upper = string_range.split("-").map{|n| get_number(n)}.take(2).sort
  Array.new(upper+1 - lower).fill { |i| i + lower }
end

#get_number(num) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/clash/helpers.rb', line 24

def get_number(num)
  if num.start_with?(':')
    test_at_line_number(num)
  else
    num.to_i
  end
end

#greenit(str) ⇒ Object



94
95
96
# File 'lib/clash/helpers.rb', line 94

def greenit(str)
  colorize(str, 'green')
end

#pout(str) ⇒ Object

Print a single character without a newline



87
88
89
90
91
92
# File 'lib/clash/helpers.rb', line 87

def pout(str)
  print str
  if STDOUT.tty?
    $stdout.flush
  end
end


110
111
112
# File 'lib/clash/helpers.rb', line 110

def print_fail
  pout redit('F')
end


114
115
116
# File 'lib/clash/helpers.rb', line 114

def print_pass
  pout greenit('.')
end

#read_test_line_numbers(path) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/clash/helpers.rb', line 32

def read_test_line_numbers(path)
  @test_lines ||= []
  count = 1
  strip_tasks(File.read(path)).each_line do |line|
    @test_lines << count if line =~ /^-/
    count += 1
  end
end

#redit(str) ⇒ Object



102
103
104
# File 'lib/clash/helpers.rb', line 102

def redit(str)
  colorize(str, 'red')
end

#require_gemsObject



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/clash/helpers.rb', line 118

def require_gems
  if !ENV["CLASH_NO_BUNDLER_REQUIRE"] && (File.file?("Gemfile") || File.file?("../Gemfile"))
    require "bundler"
    Bundler.setup # puts all groups on the load path
    true
  else
    false
  end
  rescue LoadError, Bundler::GemfileNotFound
  false
end

#strip_tasks(content) ⇒ Object



41
42
43
44
45
# File 'lib/clash/helpers.rb', line 41

def strip_tasks(content)
  content.gsub(/-\s+tasks:.+?^-/im) do |match|
    match.gsub(/.+?\n/,"\n")
  end
end

#system(*cmd) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/clash/helpers.rb', line 74

def system(*cmd)
  cmd = cmd.join(' ')
  # Don't ouput to /dev/null if in trace mode
  # or if a command supplies its own ouput
  if !ENV['TRACE'] && !(cmd =~ / > /)
    cmd << " > /dev/null"
  end

  Kernel.system cmd
end

#test_at_line_number(line_number) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/clash/helpers.rb', line 47

def test_at_line_number(line_number)
  ln = line_number.sub(':', '').to_i
  test_number = nil
  lines = @test_lines
  lines.each_with_index do |line, index|
    last = index == lines.size - 1

    if line <= ln && ( last || ln <= lines[index + 1] )
      test_number = index + 1
    end
  end

  if test_number
    test_number
  else
    puts "No test found on line #{ln}"
  end
end

#yellowit(str) ⇒ Object



98
99
100
# File 'lib/clash/helpers.rb', line 98

def yellowit(str)
  colorize(str, 'yellow')
end