Skip to content

Commit

Permalink
Add Event#emit
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed May 2, 2024
1 parent 556dea5 commit 1986b33
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 75 deletions.
15 changes: 15 additions & 0 deletions fixtures/console/captured_output.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'sus/shared'

module Console
CapturedOutput = Sus::Shared("captured output") do
let(:capture) {Console::Capture.new}
let(:logger) {Console::Logger.new(capture)}

def around
Fiber.new do
Console.logger = logger
super
end.resume
end
end
end
4 changes: 0 additions & 4 deletions lib/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,5 @@ def fatal(...)
def call(...)
Logger.instance.call(...)
end

def failure(...)
Logger.instance.failure(...)
end
end
end
6 changes: 6 additions & 0 deletions lib/console/event/failure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def to_hash
end
end

def emit(*arguments, **options)
options[:severity] ||= :error

super
end

private

def extract(exception, hash)
Expand Down
4 changes: 4 additions & 0 deletions lib/console/event/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def as_json(...)
def to_json(...)
JSON.generate(as_json, ...)
end

def emit(*arguments, **options)
Console.call(*arguments, event: self, **options)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/console/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def initialize(output, **options)
def progress(subject, total, **options)
options[:severity] ||= :info

Progress.new(self, subject, total, **options)
Progress.new(subject, total, **options)
end

def error(subject, *arguments, **options, &block)
Expand Down
6 changes: 3 additions & 3 deletions lib/console/output/terminal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def call(subject = nil, *arguments, name: nil, severity: UNKNOWN, event: nil, **
end

if event
format_event(event, buffer)
format_event(event, buffer, width - indent_size)
end

if options&.any?
Expand All @@ -134,12 +134,12 @@ def call(subject = nil, *arguments, name: nil, severity: UNKNOWN, event: nil, **

protected

def format_event(event, buffer)
def format_event(event, buffer, width)
event = event.to_hash
type = event[:type]

if formatter = @formatters[type]
formatter.format(options, buffer, verbose: @verbose, width: width - indent_size)
formatter.format(event, buffer, verbose: @verbose, width: width)
else
format_value(::JSON.pretty_generate(event), buffer)
end
Expand Down
9 changes: 4 additions & 5 deletions lib/console/progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def self.now
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

def initialize(output, subject, total = 0, minimum_output_duration: 0.1, **options)
@output = output
def initialize(subject, total = 0, minimum_output_duration: 0.1, **options)
@subject = subject
@options = options

Expand Down Expand Up @@ -69,7 +68,7 @@ def increment(amount = 1)
@current += amount

if output?
@output.info(@subject, self.to_s, event: self.to_hash, **@options)
Console.call(@subject, self.to_s, event: self.to_hash, **@options)
@last_output_time = Progress.now
end

Expand All @@ -79,14 +78,14 @@ def increment(amount = 1)
def resize(total)
@total = total

@output.call(@subject, self.to_s, event: self.to_hash, **@options)
Console.call(@subject, self.to_s, event: self.to_hash, **@options)
@last_output_time = Progress.now

return self
end

def mark(*arguments, **options)
@output.call(@subject, *arguments, **options, **@options)
Console.call(@subject, *arguments, **options, **@options)
end

def to_s
Expand Down
2 changes: 1 addition & 1 deletion test/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def around
begin
raise StandardError, "It failed!"
rescue => error
Console.failure(self, error, name: "test")
Console::Event::Failure.for(error).emit(self, name: "test")
end

expect(capture.last).to have_keys(
Expand Down
51 changes: 19 additions & 32 deletions test/console/event/failure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

require 'console'
require 'console/capture'
require 'console/captured_output'

class TestError < StandardError
def detailed_message(...)
Expand All @@ -14,38 +15,7 @@ def detailed_message(...)
end

describe Console::Event::Failure do
let(:output) {StringIO.new}
let(:terminal) {Console::Terminal.for(output)}

with 'runtime error' do
let(:error) do
RuntimeError.new("Test").tap do |error|
error.set_backtrace([
"(irb):2:in `rescue in irb_binding'",
"(irb):1:in `irb_binding'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/workspace.rb:114:in `eval'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/workspace.rb:114:in `evaluate'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/context.rb:450:in `evaluate'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:541:in `block (2 levels) in eval_input'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:704:in `signal_status'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:538:in `block in eval_input'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/ruby-lex.rb:166:in `block (2 levels) in each_top_level_statement'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/ruby-lex.rb:151:in `loop'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/ruby-lex.rb:151:in `block in each_top_level_statement'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/ruby-lex.rb:150:in `catch'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/ruby-lex.rb:150:in `each_top_level_statement'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:537:in `eval_input'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:472:in `block in run'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:471:in `catch'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:471:in `run'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:400:in `start'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/exe/irb:11:in `<top (required)>'",
"/path/to/root/.gem/ruby/2.5.7/bin/irb:23:in `load'",
"/path/to/root/.gem/ruby/2.5.7/bin/irb:23:in `<main>'"
])
end
end
end
include_context Console::CapturedOutput

with 'test error' do
let(:error) do
Expand All @@ -67,5 +37,22 @@ def detailed_message(...)
message: be =~ /Test error!\nwith details/
)
end

it "logs error message" do
Console::Event::Failure.for(error).emit(self)

last = capture.last
expect(last).to have_keys(
severity: be == :error,
subject: be == self,
event: have_keys(
type: be == :failure,
root: be_a(String),
class: be =~ /TestError/,
message: be =~ /Test error!/,
backtrace: be_a(Array),
)
)
end
end
end
7 changes: 2 additions & 5 deletions test/console/event/progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
# Copyright, 2020-2022, by Samuel Williams.

require 'console/progress'

require 'console'
require 'console/capture'
require 'console/captured_output'

describe Console::Progress do
let(:capture) {Console::Capture.new}
let(:logger) {Console::Logger.new(capture)}
include_context Console::CapturedOutput
let(:progress) {logger.progress("My Measurement", 100)}

with '#mark' do
Expand Down
23 changes: 0 additions & 23 deletions test/console/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,6 @@
end
end

with '#failure' do
it "logs error message" do
begin
raise StandardError, "It failed!"
rescue => error
logger.failure(self, error)
end

last = output.last
expect(last).to have_keys(
severity: be == :error,
subject: be == self,
event: have_keys(
type: be == :failure,
root: be_a(String),
class: be == "StandardError",
message: be == "It failed!",
backtrace: be_a(Array),
)
)
end
end

with "level" do
let(:level) {0}
let(:logger) {subject.new(output, level: level)}
Expand Down
3 changes: 2 additions & 1 deletion test/console/terminal/formatter/progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
let(:buffer) {StringIO.new}
let(:terminal) {Console::Terminal.for(buffer)}
let(:formatter) {subject.new(terminal)}

let(:output) {Console::Output::Null.new}

let(:progress) do
Console::Progress.new(output, self, 10)
Console::Progress.new(self, 10)
end

it "can format failure events" do
Expand Down

0 comments on commit 1986b33

Please sign in to comment.