Class: Assert::CLI

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

Constant Summary collapse

SUCCESS_EXIT_STATUS =
0
ERROR_EXIT_STATUS =
1

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.


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

def initialize(*args)
  @args = args
  @cli =
    CLIRB.new do
      option(
        "runner_seed",
        "use a given seed to run tests",
        abbrev: "s",
        value: Integer,
      )
      option(
        "changed_only",
        "only run test files with changes",
        abbrev: "c",
      )
      option(
        "changed_ref",
        "reference for changes, use with `-c` opt",
        abbrev: "r",
        value: "",
      )
      option(
        "single_test",
        "only run the test on the given file/line",
        abbrev: "t",
        value: "",
      )
      option(
        "pp_objects",
        "pretty-print objects in fail messages",
        abbrev: "p",
      )
      option(
        "capture_output",
        "capture stdout and display in result details",
        abbrev: "o",
      )
      option(
        "halt_on_fail",
        "halt a test when it fails",
        abbrev: "h",
      )
      option(
        "profile",
        "output test profile info",
        abbrev: "e",
      )
      option(
        "verbose",
        "output verbose runtime test info",
        abbrev: "v",
      )
      option(
        "list",
        "list test files on $stdout",
        abbrev: "l",
      )

      # show loaded test files, cli err backtraces, etc
      option "debug", "run in debug mode", abbrev: "d"
    end
end

Class Method Details

.bench(start_msg, &block) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/assert/cli.rb', line 30

def self.bench(start_msg, &block)
  unless Assert.config.debug
    block.call
    return
  end

  print debug_start_msg(start_msg)
  RoundedMillisecondTime
    .new(Benchmark.measure(&block).real)
    .tap do |time_in_ms|
      puts debug_finish_msg(time_in_ms)
    end
end

.debug?(args) ⇒ Boolean

Returns:

  • (Boolean)

14
15
16
# File 'lib/assert/cli.rb', line 14

def self.debug?(args)
  args.include?("-d") || args.include?("--debug")
end

.debug_finish_msg(time_in_ms) ⇒ Object


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

def self.debug_finish_msg(time_in_ms)
  " (#{time_in_ms} ms)"
end

.debug_msg(msg) ⇒ Object


18
19
20
# File 'lib/assert/cli.rb', line 18

def self.debug_msg(msg)
  "[DEBUG] #{msg}"
end

.debug_start_msg(msg) ⇒ Object


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

def self.debug_start_msg(msg)
  debug_msg("#{msg}...".ljust(30))
end

Instance Method Details

#helpObject


135
136
137
138
139
# File 'lib/assert/cli.rb', line 135

def help
  "Usage: assert [options] [TESTS]\n\n"\
  "Options:"\
  "#{@cli}"
end

#runObject


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/assert/cli.rb', line 107

def run
  fails_plus_errors_count = 0
  begin
    @cli.parse!(@args)
    catch(:halt) do
      fails_plus_errors_count =
        Assert::AssertRunner.new(Assert.config, @cli.args, @cli.opts).run
    end
  rescue CLIRB::HelpExit
    puts help
    exit(SUCCESS_EXIT_STATUS)
  rescue CLIRB::VersionExit
    puts Assert::VERSION
    exit(SUCCESS_EXIT_STATUS)
  rescue CLIRB::Error => ex
    puts "#{ex.message}\n\n"
    puts Assert.config.debug ? ex.backtrace.join("\n") : help
    exit(ERROR_EXIT_STATUS)
  rescue => ex
    puts "#{ex.class}: #{ex.message}"
    puts ex.backtrace.join("\n")
    exit(ERROR_EXIT_STATUS)
  end
  exit(
    fails_plus_errors_count == 0 ? SUCCESS_EXIT_STATUS : ERROR_EXIT_STATUS,
  )
end