Class: Crspec::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/crspec/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/crspec/cli.rb', line 13

def initialize(args)
  @args = args
  @concurrency = Crspec.configuration.concurrency
  @fibers = nil
  @paths = []
  @requires = []
  @init_mode = false
  @fail_fast = false
  @seed = nil
  @only_failures = false
  @processes = 1
  @tags = {}
  @locations = []
end

Class Method Details

.run(args) ⇒ Object



9
10
11
# File 'lib/crspec/cli.rb', line 9

def self.run(args)
  new(args).run
end

Instance Method Details

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
102
103
104
105
106
107
108
109
# File 'lib/crspec/cli.rb', line 28

def run
  parse_options

  if @init_mode
    created = Generators::Init.generate
    if created.empty?
      puts "No helper files were created (already exists or not a Rails project)."
    else
      created.each { |f| puts "  create #{f}" }
    end
    return true
  end

  load_requires
  load_specs

  concurrency = @concurrency || Crspec.configuration.concurrency
  fibers = @fibers || Crspec.configuration.fibers
  persistence_path = Crspec.configuration.example_status_persistence_file_path ||
                     File.join("tmp", "crspec_status.json")
  multi_process = @processes == :auto || (@processes.is_a?(Integer) && @processes > 1)
  if multi_process && !ProcessRunner.fork_supported?
    if @processes == :auto
      # auto = "use the best multi-core strategy"; on engines without a
      # GVL, threads already are that strategy.
      puts "#{RUBY_ENGINE} has no fork; --processes auto falling back to #{concurrency} threads."
      multi_process = false
    else
      warn "Error: --processes requires fork, which #{RUBY_ENGINE} does not support."
      warn "On #{RUBY_ENGINE} threads use all cores; use -c/--concurrency instead."
      return false
    end
  end
  runner = if multi_process
             ProcessRunner.new(processes: @processes, concurrency: concurrency,
                               fibers: fibers, fail_fast: @fail_fast, seed: @seed,
                               only_failures: @only_failures,
                               persistence_path: persistence_path)
           else
             Runner.new(concurrency: concurrency, fibers: fibers, fail_fast: @fail_fast,
                        seed: @seed, only_failures: @only_failures,
                        persistence_path: persistence_path,
                        tags: @tags.empty? ? nil : @tags,
                        locations: @locations.empty? ? nil : @locations)
           end
  groups = Crspec.world.example_groups
  Crspec.world.freeze!

  if groups.empty?
    puts "No example groups found."
    return true
  end

  banner = "Running Crspec suite with concurrency #{concurrency}"
  banner += ", #{@processes == :auto ? 'auto' : @processes} processes" if multi_process
  banner += ", #{fibers} fibers/worker" if fibers && fibers > 1
  banner += ", seed #{@seed}" if @seed
  puts "#{banner}..."
  runner.run(groups)

  puts "\nFinished in #{runner.total_duration.round(4)} seconds"
  total = runner.passed_examples.size + runner.failed_examples.size + runner.pending_examples.size
  summary = "#{total} examples, #{runner.failed_examples.size} failures"
  summary += ", #{runner.pending_examples.size} pending" unless runner.pending_examples.empty?
  puts summary

  unless runner.failed_examples.empty?
    puts "\nFailures:"
    runner.failed_examples.each_with_index do |ex, idx|
      puts "#{idx + 1}) #{ex.description}"
      if ex.respond_to?(:error) && ex.error
        puts "   Failure/Error: #{ex.error.message}"
        puts "   #{ex.error.backtrace&.first}" if ex.error.backtrace
      elsif ex.respond_to?(:error_message)
        puts "   Failure/Error: #{ex.error_message}"
        puts "   #{ex.error_backtrace&.first}" if ex.error_backtrace
      end
    end
  end

  runner.success?
end