Class: Clash::Tests

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/clash/tests.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, #strip_tasks, #system, #test_at_line_number, #yellowit

Constructor Details

#initialize(options = {}) ⇒ Tests

Returns a new instance of Tests.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/clash/tests.rb', line 7

def initialize(options={})
  @options = options

  ENV['JEKYLL_ENV'] = 'test'

  if @options[:debug]
    ENV['DEBUG'] = 'true'
  end

  @results = []
  @passed = []
  @failed = []
  @tasks = {}

  @options[:only]    ||= []
  @options[:exit]    ||= true
  @options[:path]    ||= '.'
  @options[:file]    ||= '_clash.yml'

  @clashfile = read_config
  @tests = read_tests
end

Instance Attribute Details

#testsObject

Returns the value of attribute tests.



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

def tests
  @tests
end

Instance Method Details

#config_path(file = nil) ⇒ Object



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

def config_path(file=nil)
  file ||= @options[:file]
  path = File.join('./', @options[:path])
  paths = []

  (path.count('/') + 1).times do
    paths << File.join(path, file)
    path.sub!(/\/[^\/]+$/, '')
  end

  paths.find {|p| File.file?(p) }
end

#listObject



30
31
32
33
34
35
36
37
# File 'lib/clash/tests.rb', line 30

def list
  @tests.each_with_index do |options, index|
    # If tests are limited, only show specified tests
    #
    next if options.nil?
    list_test(options, index)
  end
end

#list_test(options, index) ⇒ Object



39
40
41
42
43
# File 'lib/clash/tests.rb', line 39

def list_test(options, index)
  number = boldit((index + 1).to_s.rjust(3))
  title = options['title'] || "Untitled test"
  puts "#{number}) #{title}"
end

#list_tests(tests) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/clash/tests.rb', line 145

def list_tests(tests)
  if tests.empty?
    ''
  else
    "- Tests: #{tests.join(',')}"
  end
end


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/clash/tests.rb', line 129

def print_results


  puts boldit("\n\nFailures:") unless @results.empty?
  @results.each do |results|
    puts "\n#{results.join('')}"
  end

  puts boldit("\n\nTest summary:")
  puts yellowit(" Tests run: #{@passed.dup.concat(@failed).size}")
  puts "#{greenit(" Passed #{@passed.size}")} #{list_tests(@passed)}"
  puts "#{redit(" Failed #{@failed.size}")} #{list_tests(@failed)}"

  exit 1 if @options[:exit] && !@results.empty?
end

#read_configObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/clash/tests.rb', line 103

def read_config
  # Find the config file (fall back to legacy filename)
  if path = config_path || config_path('.clash.yml')
    read_test_line_numbers(path)
    config = SafeYAML.load_file(path)
    config = [config] unless config.is_a?(Array)
    config
  else
    # If config file still not found, complain
    raise "Config file #{@options[:file]} not found."
  end
end

#read_testsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/clash/tests.rb', line 75

def read_tests
  index = 0
  delete_tests = []
  @options[:only] = expand_list_of_numbers(@options[:only])

  tests = @clashfile.map do |test|
    if !test['tasks'].nil?
      @tasks.merge! test['tasks']
      delete_tests << test
    else
      index += 1

      # Admit all tests if no tests are excluded
      if @options[:only].empty?
        test
      # Only admit selected tests
      elsif @options[:only].include?(index)
        test
      # Remove tests not selected
      else
        nil
      end
    end
  end
  tests - [delete_tests]

end

#runObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/clash/tests.rb', line 45

def run
  Dir.chdir(@options[:path]) do
    @tests.each_with_index do |options, index|
      # If tests are limited, only run specified tests
      #
      next if options.nil?
      run_test(options, index)
    end
  end

  print_results
end

#run_test(options, index) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/clash/tests.rb', line 58

def run_test(options, index)

  options['index'] = index + 1
  options['context'] = @options[:context]
  options['tasks'] = @tasks
  options['build_only'] = @options[:build_only]

  results = Test.new(options).run

  if results.nil?
    @passed << index + 1
  else
    @failed << index + 1
    @results << results
  end
end