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
11
12
# File 'lib/uspec/cli.rb', line 5

def initialize args
  usage unless (args & %w[-h --help -? /? -v --version]).empty?

  @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.



13
14
15
# File 'lib/uspec/cli.rb', line 13

def stats
  @stats
end

Instance Method Details

#die!Object



48
49
50
51
# File 'lib/uspec/cli.rb', line 48

def die!
  puts @stats.summary
  exit exit_code
end

#exit_codeObject



30
31
32
# File 'lib/uspec/cli.rb', line 30

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

#handle_interrupt!(type = Interrupt) ⇒ Object



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

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



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

def invoke
  run_specs
  die!
end

#pathsObject



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

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



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
# File 'lib/uspec/cli.rb', line 69

def run path
  spec = nil
  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.define.instance_eval(path.read, path.to_s)
  else
    warn "path not found: #{path}"
  end
rescue Exception => error

  if SignalException === error || SystemExit === error then
    exit 3
  end

  error_file, error_line, _ = error.backtrace.first.split ?:

  message = <<-MSG
    #{error.class} : #{error.message}

    Uspec encountered an error when loading a test file.
    This is probably a typo in the test file or the file it is testing.

    If you think this is a bug in Uspec please report it: https://github.com/acook/uspec/issues/new

    Error occured when loading test file `#{spec || path}`.
    The origin of the error may be in file `#{error_file}` on line ##{error_line}.

\t#{error.backtrace[0,3].join "\n\t"}
  MSG
  puts
  warn message
  stats << Uspec::Result.new(message, error, caller)

  handle_interrupt! error
end

#run_pathsObject



63
64
65
66
67
# File 'lib/uspec/cli.rb', line 63

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

#run_specsObject



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

def run_specs
  run_paths
end

#usageObject



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

def usage
  warn "uspec v#{::Uspec::VERSION} - minimalistic ruby testing framework"
  warn "usage: #{File.basename $0} [<file_or_path>...]"
  exit 1
end