Class: Uspec::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



5
6
7
8
9
10
# File 'lib/uspec/cli.rb', line 5

def initialize args
  @paths = args
  @pwd = Pathname.pwd.freeze
  @stats = Uspec::Stats.new
  @harness = Uspec::Harness.new self
end

Instance Attribute Details

#statsObject (readonly)

Returns the value of attribute stats.



11
12
13
# File 'lib/uspec/cli.rb', line 11

def stats
  @stats
end

Instance Method Details

#die!Object



50
51
52
53
# File 'lib/uspec/cli.rb', line 50

def die!
  puts @stats.summary
  exit exit_code
end

#exit_codeObject



32
33
34
# File 'lib/uspec/cli.rb', line 32

def exit_code
  [@stats.failure.size, 255].min
end

#handle_interrupt!(type = Interrupt) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/uspec/cli.rb', line 36

def handle_interrupt! type = Interrupt
  if SignalException === type || SystemExit === type then
    if type === Module then
      err = type
      msg = "signal"
    else
      err = type.class
      msg = type.message
    end
    puts "Uspec received #{err} #{msg} - exiting!"
    die!
  end
end

#invokeObject



26
27
28
29
30
# File 'lib/uspec/cli.rb', line 26

def invoke
  parse_options @paths
  run_specs
  die!
end

#parse_options(args) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/uspec/cli.rb', line 89

def parse_options args
  args.each_with_index do |arg, i|
    if arg == '--' then
      return args
    elsif arg == '--full_backtrace' then
      Uspec::Errors.full_backtrace!
      args.delete_at i
    elsif !(args & %w[-h --help -? /? -v --version]).empty?
      usage
    elsif arg[0] == ?- then
      warn "unknown option: #{arg}"
      args.delete_at i
    end
  end
end

#pathsObject



55
56
57
58
59
60
61
62
63
# File 'lib/uspec/cli.rb', line 55

def paths
  if @paths.empty? then
    ['spec', 'uspec', 'test'].each do |path|
      @paths << path if Pathname.new(path).directory?
    end
  end

  @paths
end

#run(path) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/uspec/cli.rb', line 73

def run path
  p, line = path.to_s.split(?:)
  path = Pathname.new p

  if path.directory? then
    Pathname.glob(path.join('**', '**_spec.rb')).each do |spec|
      run spec
    end
  elsif path.exist? then
    puts "#{path.basename path.extname}:"
    harness.file_eval path, line
  else
    warn "path not found: #{path}"
  end
end

#run_pathsObject



65
66
67
68
69
70
71
# File 'lib/uspec/cli.rb', line 65

def run_paths
  check_options = true

  paths.each do |path|
    run @pwd.join path
  end
end

#run_specsObject



22
23
24
# File 'lib/uspec/cli.rb', line 22

def run_specs
  run_paths
end

#usageObject



13
14
15
16
17
18
19
20
# File 'lib/uspec/cli.rb', line 13

def usage
  warn "uspec v#{::Uspec::VERSION} - minimalistic ruby testing framework"
  warn "usage: #{File.basename $0} [<file_or_path>...]"
  warn ""
  warn "\t\t--full_backtrace\tshow full backtrace"
  warn "\t\t--\t\t\tstop checking paths for options (good if a path begins with a dash)"
  exit 1
end