Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed May 1, 2024
1 parent 1584fc2 commit 12b5c34
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 43 deletions.
2 changes: 1 addition & 1 deletion fixtures/my_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def self.test_logger
logger.warn "Something didn't work as expected!"
logger.error "There be the dragons!", (raise RuntimeError, "Bits have been rotated incorrectly!" rescue $!)

logger.info(self) {Console::Shell.for({LDFLAGS: "-lm"}, "gcc", "-o", "stuff.o", "stuff.c", chdir: "/tmp/compile")}
logger.info(self) {Console::Event::Spawn.for({LDFLAGS: "-lm"}, "gcc", "-o", "stuff.o", "stuff.c", chdir: "/tmp/compile")}

logger.info(Object.new) {"Where would we be without Object.new?"}
end
Expand Down
15 changes: 11 additions & 4 deletions lib/console/event/failure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
# Copyright, 2019-2022, by Samuel Williams.
# Copyright, 2021, by Robert Schulze.

require_relative 'generic'

module Console
module Event
# Represents a failure event.
#
# ```ruby
# Console.error(self, **Console::Event::Failure.for(exception))
# ````
class Failure
class Failure < Generic
def self.default_root
Dir.getwd
rescue # e.g. Errno::EMFILE
Expand All @@ -22,7 +24,7 @@ def self.for(exception)
self.new(exception, self.default_root)
end

def initialize(exception, root)
def initialize(exception, root = Dir.getwd)
@exception = exception
@root = root
end
Expand All @@ -38,10 +40,15 @@ def to_hash
private

def extract(exception, hash)
hash[:title] = exception.class
hash[:class] = exception.class.name

if exception.respond_to?(:detailed_message)
hash[:message] = exception.detailed_message
message = exception.detailed_message

# We want to remove the trailling exception class as we format it differently:
message.sub!(/\s*\(.*?\)$/, '')

hash[:message] = message
else
hash[:message] = exception.message
end
Expand Down
18 changes: 18 additions & 0 deletions lib/console/event/generic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2022, by Samuel Williams.

module Console
module Event
class Generic
def as_json(...)
to_hash
end

def to_json(...)
JSON.generate(as_json, ...)
end
end
end
end
8 changes: 5 additions & 3 deletions lib/console/event/progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

require_relative '../clock'

require_relative 'generic'

module Console
module Event
class Progress
class Progress < Generic
def self.now
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
Expand Down Expand Up @@ -83,8 +85,8 @@ def resize(total)
return self
end

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

def to_s
Expand Down
4 changes: 3 additions & 1 deletion lib/console/event/spawn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
# Released under the MIT License.
# Copyright, 2019-2022, by Samuel Williams.

require_relative 'generic'

module Console
module Event
# Represents a spawn event.
#
# ```ruby
# Console.info(self, **Console::Event::Spawn.for("ls", "-l"))
# ```
class Spawn
class Spawn < Generic
def self.for(*arguments, **options)
# Extract out the command environment:
if arguments.first.is_a?(Hash)
Expand Down
8 changes: 4 additions & 4 deletions lib/console/terminal/formatter/failure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ module Formatter
class Failure
KEY = :failure

def initialize(terminal, root: Dir.pwd)
def initialize(terminal)
@terminal = terminal
@root = root

@terminal[:exception_title] ||= @terminal.style(:red, nil, :bold)
@terminal[:exception_detail] ||= @terminal.style(:yellow)
Expand All @@ -22,9 +21,10 @@ def initialize(terminal, root: Dir.pwd)
end

def format(event, output, prefix: nil, verbose: false, width: 80)
title = event[:title]
title = event[:class]
message = event[:message]
backtrace = event[:backtrace]
root = event[:root]

lines = message.lines.map(&:chomp)

Expand All @@ -34,7 +34,7 @@ def format(event, output, prefix: nil, verbose: false, width: 80)
output.puts " #{@terminal[:exception_detail]}#{line}#{@terminal.reset}"
end

root_pattern = /^#{@root}\// if @root
root_pattern = /^#{root}\// if root

backtrace&.each_with_index do |line, index|
path, offset, message = line.split(":", 3)
Expand Down
13 changes: 13 additions & 0 deletions test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env ruby

require_relative 'lib/console'

begin
begin
raise ArgumentError, "it failed"
rescue => error
raise RuntimeError, "subsequent error"
end
rescue => error
Console.logger.failure(self, error)
end
13 changes: 4 additions & 9 deletions test/console/event/failure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ def detailed_message(...)
])
end
end

it "formats exception removing root path" do
event = Console::Event::Failure.new(error, "/path/to/root")
event.format(output, terminal, true)
expect(output.string.lines[3..-1]).to have_value(be =~ /^\s+\.gem/)
end
end

with 'test error' do
Expand All @@ -68,9 +62,10 @@ def detailed_message(...)
expect(error.detailed_message).to be =~ /with details/

event = Console::Event::Failure.new(error)
event.format(output, terminal, true)
expect(output.string).to be =~ /Test error!/
expect(output.string).to be =~ /with details/

expect(event.to_hash).to have_keys(
message: be =~ /Test error!\nwith details/
)
end
end
end
23 changes: 7 additions & 16 deletions test/console/progress.rb → test/console/event/progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
# Released under the MIT License.
# Copyright, 2020-2022, by Samuel Williams.

require 'console/event/progress'

require 'console'
require 'console/progress'
require 'console/capture'

describe Console::Progress do
describe Console::Event::Progress do
let(:capture) {Console::Capture.new}
let(:logger) {Console::Logger.new(capture)}
let(:progress) {logger.progress("My Measurement", 100)}
Expand Down Expand Up @@ -64,21 +65,11 @@
expect(last).to have_keys(
severity: be == :info,
subject: be == "My Measurement",
message: be_a(Console::Event::Progress),
event: be == :progress,
current: be == 100,
total: be == 100,
arguments: be == ["100/100 completed in 0.0s, 0.0s remaining."]
)
end

it 'can generate a progress bar' do
progress.increment(50)

last = capture.last
message = last[:message]

terminal = Console::Terminal::Text.new($stderr)
output = StringIO.new
message.format(output, terminal, true)

expect(output.string).to be == "███████████████████████████████████ 50.00%\n"
end
end
end
15 changes: 12 additions & 3 deletions test/console/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,22 @@

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

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

Expand Down
5 changes: 3 additions & 2 deletions test/console/serialized/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@
let(:event) {Console::Event::Spawn.for("ls -lah")}

it "can log structured events" do
logger.call(subject, event)
logger.call(subject, **event)

expect(record).to have_keys(
subject: be == subject.name,
message: be == ["Console::Event::Spawn", {:arguments => ["ls -lah"]}]
event: be == "spawn",
arguments: be == ["ls -lah"],
)
end
end
Expand Down

0 comments on commit 12b5c34

Please sign in to comment.