Class: Rdm::SpecRunner::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/rdm/spec_runner/runner.rb

Constant Summary collapse

RUNIGNORE_PATH =
'tests/.runignore'.freeze
RUNIGNORE_COMMENT =
'#'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package: nil, spec_matcher: nil, path: nil, show_missing_packages: true, skip_ignored_packages: false, stdout: STDOUT, show_output: true) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rdm/spec_runner/runner.rb', line 9

def initialize(
  package:               nil, 
  spec_matcher:          nil, 
  path:                  nil, 
  show_missing_packages: true,
  skip_ignored_packages: false,
  stdout:                STDOUT,
  show_output:           true
)
  @package_name          = package
  @no_specs_packages     = []
  @spec_matcher          = spec_matcher.to_s.split(':')[0]
  @spec_string_number    = spec_matcher.to_s.split(':')[1].to_i
  @path                  = path
  @run_all               = @package_name.nil?
  @show_missing_packages = show_missing_packages
  @skip_ignored_packages = skip_ignored_packages
  @skipped_packages      = []
  @stdout                = stdout
  @show_output           = show_output
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



7
8
9
# File 'lib/rdm/spec_runner/runner.rb', line 7

def command
  @command
end

#no_specs_packagesObject

Returns the value of attribute no_specs_packages.



5
6
7
# File 'lib/rdm/spec_runner/runner.rb', line 5

def no_specs_packages
  @no_specs_packages
end

#prepared_command_paramsObject

Returns the value of attribute prepared_command_params.



6
7
8
# File 'lib/rdm/spec_runner/runner.rb', line 6

def prepared_command_params
  @prepared_command_params
end

Instance Method Details

#check_input_params!Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rdm/spec_runner/runner.rb', line 74

def check_input_params!
  if @package_name
    unless is_package_included?(@package_name)
      exit_with_message(
        view.package_not_found_message(@package_name, prepared_command_params)
      )
    end

    if no_specs_packages.include?(@package_name)
      exit_with_message(
        view.no_specs_for_package(@package_name)
      )
    end
  end
end

#display_ignored_specsObject



185
186
187
188
189
# File 'lib/rdm/spec_runner/runner.rb', line 185

def display_ignored_specs
  if !@skipped_packages.empty?
    print_message view.skipping_specs_message(@skipped_packages)
  end
end

#display_missing_specsObject



179
180
181
182
183
# File 'lib/rdm/spec_runner/runner.rb', line 179

def display_missing_specs
  if !no_specs_packages.empty?
    print_message view.missing_specs_message(no_specs_packages)
  end
end

#execute_commandObject



191
192
193
194
195
196
197
# File 'lib/rdm/spec_runner/runner.rb', line 191

def execute_command
  eval(command)
  if $? && !$?.success?
    exit(1)
  end

end

#exit_with_message(msg) ⇒ Object



69
70
71
72
# File 'lib/rdm/spec_runner/runner.rb', line 69

def exit_with_message(msg)
  print_message(msg)
  exit 1
end

#is_package_included?(package_name) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/rdm/spec_runner/runner.rb', line 90

def is_package_included?(package_name)
  !prepared_command_params.select do |x|
    x.package_name == package_name
  end.empty?
end

#packagesObject



56
57
58
# File 'lib/rdm/spec_runner/runner.rb', line 56

def packages
  @packages ||= Rdm::SpecRunner::PackageFetcher.new(@path).packages
end

#prepare!Object



96
97
98
99
100
101
102
103
# File 'lib/rdm/spec_runner/runner.rb', line 96

def prepare!
  prepared_command_params = []
  no_specs_packages       = []
  command                 = nil
  prepare_command_params
  prepare_no_specs_packages
  prepare_command
end

#prepare_commandObject



124
125
126
127
128
129
130
131
132
# File 'lib/rdm/spec_runner/runner.rb', line 124

def prepare_command
  @command ||= begin
    if @package_name
      prepare_single_package_command(@package_name)
    else
      prepare_command_for_packages(prepared_command_params)
    end
  end
end

#prepare_command_for_packages(packages_command_params) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rdm/spec_runner/runner.rb', line 141

def prepare_command_for_packages(packages_command_params)
  if @skip_ignored_packages && !@package_name
    runignore_path = File.expand_path(File.join(Rdm.root_dir, RUNIGNORE_PATH))
    package_list   = Rdm::SourceParser.read_and_init_source(Rdm.root).packages.keys

    skipped_package_list = File.read(runignore_path)
      .lines
      .map(&:strip)
      .reject(&:empty?) rescue []
    
    @skipped_packages       = skipped_package_list.select {|line| package_list.include?(line)}
    comment_runignore_lines = skipped_package_list.select {|line| line.start_with?(RUNIGNORE_COMMENT)}
    invalid_ignore_packages = skipped_package_list - @skipped_packages - comment_runignore_lines

    if !invalid_ignore_packages.empty?
      @stdout.puts "WARNING: #{RUNIGNORE_PATH} contains invalid package names: \n#{invalid_ignore_packages.join("\n")}"
    end
  else
    @skipped_packages = []
  end

  running_packages = packages_command_params
    .select  { |cmd_params| cmd_params.spec_count > 0 }
    .reject  { |cmd_params| @skipped_packages.include?(cmd_params.package_name) }
    .sort_by { |cmd_params| - cmd_params.spec_count }

  if @run_all
    puts <<~EOF
      Rspec tests will run for packages:
      #{(packages.keys - no_specs_packages).map {|pkg| " - #{pkg}"}.join("\n")}\n
    EOF
  end
  
  running_packages
    .map(&:command)
    .join(' && ')
end

#prepare_command_paramsObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rdm/spec_runner/runner.rb', line 105

def prepare_command_params
  @prepared_command_params ||= begin
    packages.map do |_name, package|
      Rdm::SpecRunner::CommandGenerator.new(
        package_name: package.name, 
        package_path: package.path, 
        spec_matcher: @spec_matcher,
        show_output:  @show_output
      ).generate
    end
  end
end

#prepare_no_specs_packagesObject



118
119
120
121
122
# File 'lib/rdm/spec_runner/runner.rb', line 118

def prepare_no_specs_packages
  prepared_command_params
    .select { |cp| cp.spec_count == 0 }
    .map { |cp| no_specs_packages << cp.package_name }
end

#prepare_single_package_command(package_name) ⇒ Object



134
135
136
137
138
139
# File 'lib/rdm/spec_runner/runner.rb', line 134

def prepare_single_package_command(package_name)
  selected = prepared_command_params.select do |cmd_params|
    cmd_params.package_name == package_name
  end
  prepare_command_for_packages(selected)
end


64
65
66
67
# File 'lib/rdm/spec_runner/runner.rb', line 64

def print_message(msg)
  @stdout.puts msg
  true
end

#runObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rdm/spec_runner/runner.rb', line 31

def run
  if !@spec_matcher.nil? && !@spec_matcher.empty?
    @spec_file_matches = Rdm::SpecRunner::SpecFilenameMatcher.find_matches(package_path: packages[@package_name].path, spec_matcher: @spec_matcher)
    case @spec_file_matches.size
    when 0
      raise Rdm::Errors::SpecMatcherNoFiles, "No specs were found for '#{@spec_matcher}'"
    when 1
      format_string_number = @spec_string_number == 0 ? "" : ":#{@spec_string_number}" 
      @spec_matcher = @spec_file_matches.first + format_string_number

      @stdout.puts "Following spec matches your input: #{@spec_matcher}"
    else
      raise Rdm::Errors::SpecMatcherMultipleFiles, @spec_file_matches.join("\n")
    end
  end

  prepare!
  check_input_params!
  display_missing_specs if @show_missing_packages
  display_ignored_specs if @skip_ignored_packages
  print_message view.specs_header_message if @show_missing_packages || @skip_ignored_packages
  
  execute_command
end

#viewObject



60
61
62
# File 'lib/rdm/spec_runner/runner.rb', line 60

def view
  @view ||= Rdm::SpecRunner::View.new
end