diff --git a/fixtures/my_module.rb b/fixtures/my_module.rb index d7170f5..d4c3b85 100644 --- a/fixtures/my_module.rb +++ b/fixtures/my_module.rb @@ -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 diff --git a/lib/console/event/failure.rb b/lib/console/event/failure.rb index 841cd6c..1cddb64 100644 --- a/lib/console/event/failure.rb +++ b/lib/console/event/failure.rb @@ -4,6 +4,8 @@ # Copyright, 2019-2022, by Samuel Williams. # Copyright, 2021, by Robert Schulze. +require_relative 'generic' + module Console module Event # Represents a failure event. @@ -11,7 +13,7 @@ module 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 @@ -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 @@ -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 diff --git a/lib/console/event/generic.rb b/lib/console/event/generic.rb new file mode 100644 index 0000000..3fba110 --- /dev/null +++ b/lib/console/event/generic.rb @@ -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 diff --git a/lib/console/event/progress.rb b/lib/console/event/progress.rb index a92c991..f6ad795 100644 --- a/lib/console/event/progress.rb +++ b/lib/console/event/progress.rb @@ -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 @@ -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 diff --git a/lib/console/event/spawn.rb b/lib/console/event/spawn.rb index 762f162..2763a10 100644 --- a/lib/console/event/spawn.rb +++ b/lib/console/event/spawn.rb @@ -3,6 +3,8 @@ # Released under the MIT License. # Copyright, 2019-2022, by Samuel Williams. +require_relative 'generic' + module Console module Event # Represents a spawn event. @@ -10,7 +12,7 @@ module 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) diff --git a/lib/console/terminal/formatter/failure.rb b/lib/console/terminal/formatter/failure.rb index 86719b7..e7755cf 100644 --- a/lib/console/terminal/formatter/failure.rb +++ b/lib/console/terminal/formatter/failure.rb @@ -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) @@ -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) @@ -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) diff --git a/test.rb b/test.rb new file mode 100755 index 0000000..0401b4e --- /dev/null +++ b/test.rb @@ -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 diff --git a/test/console/event/failure.rb b/test/console/event/failure.rb index 4c3dd16..58b16d6 100644 --- a/test/console/event/failure.rb +++ b/test/console/event/failure.rb @@ -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 @@ -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 diff --git a/test/console/progress.rb b/test/console/event/progress.rb similarity index 76% rename from test/console/progress.rb rename to test/console/event/progress.rb index 4284464..bdafceb 100644 --- a/test/console/progress.rb +++ b/test/console/event/progress.rb @@ -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)} @@ -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 diff --git a/test/console/logger.rb b/test/console/logger.rb index 1b96082..b56ffa0 100644 --- a/test/console/logger.rb +++ b/test/console/logger.rb @@ -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 diff --git a/test/console/serialized/logger.rb b/test/console/serialized/logger.rb index fdbbe40..88a76a5 100644 --- a/test/console/serialized/logger.rb +++ b/test/console/serialized/logger.rb @@ -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