Implement verbose mode
This commit is contained in:
parent
404b34acc0
commit
254efe4f74
5
features/cli/verbose.feature
Normal file
5
features/cli/verbose.feature
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
Feature: verbose CLI option
|
||||||
|
|
||||||
|
Scenario: raises the logger level to INFO
|
||||||
|
When I run uhwm with option -v
|
||||||
|
Then the current output must match /log.+info.+level/i
|
@ -1,7 +1,12 @@
|
|||||||
def uhwm_wait_output message, timeout: 1
|
def uhwm_wait_output message, timeout: 1
|
||||||
Timeout.timeout(timeout) do
|
Timeout.timeout(timeout) do
|
||||||
loop do
|
loop do
|
||||||
break if assert_partial_output_interactive message
|
break if case message
|
||||||
|
when Regexp
|
||||||
|
@process.read_stdout =~ message
|
||||||
|
when String
|
||||||
|
assert_partial_output_interactive message
|
||||||
|
end
|
||||||
sleep 0.1
|
sleep 0.1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -15,5 +20,10 @@ Usage: uhwm [options]
|
|||||||
|
|
||||||
options:
|
options:
|
||||||
-h, --help print this message
|
-h, --help print this message
|
||||||
|
-v, --version enable verbose mode
|
||||||
eoh
|
eoh
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Then /^the current output must match \/([^\/]+)\/([a-z]+)$/ do |pattern, options|
|
||||||
|
uhwm_wait_output Regexp.new(pattern, options)
|
||||||
|
end
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
def uhwm_run options = nil
|
def uhwm_run options = '-v'
|
||||||
command = %w[uhwm]
|
command = %w[uhwm]
|
||||||
command << options if options
|
command << options if options
|
||||||
@interactive = @process = run command.join ' '
|
@interactive = @process = run command.join ' '
|
||||||
|
@ -48,6 +48,11 @@ module Uh
|
|||||||
@env.print opts
|
@env.print opts
|
||||||
exit
|
exit
|
||||||
end
|
end
|
||||||
|
|
||||||
|
opts.on '-v', '--version', 'enable verbose mode' do
|
||||||
|
@env.verbose = true
|
||||||
|
@env.log_logger_level
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,26 +1,33 @@
|
|||||||
module Uh
|
module Uh
|
||||||
module WM
|
module WM
|
||||||
class Env
|
class Env
|
||||||
|
LOGGER_LEVEL = Logger::WARN
|
||||||
|
LOGGER_LEVEL_VERBOSE = Logger::INFO
|
||||||
LOGGER_LEVEL_STRINGS = %w[DEBUG INFO WARN ERROR FATAL UNKNOWN]
|
LOGGER_LEVEL_STRINGS = %w[DEBUG INFO WARN ERROR FATAL UNKNOWN]
|
||||||
|
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
def_delegator :@logger, :info, :log
|
def_delegator :logger, :info, :log
|
||||||
def_delegator :@output, :print
|
def_delegator :@output, :print
|
||||||
|
|
||||||
attr_reader :output, :logger
|
attr_reader :output
|
||||||
attr_accessor :verbose
|
attr_accessor :verbose
|
||||||
|
|
||||||
def initialize output, logger: Logger.new(output)
|
def initialize output
|
||||||
@output = output
|
@output = output
|
||||||
@logger = logger
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def verbose?
|
def verbose?
|
||||||
!!@verbose
|
!!@verbose
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def logger
|
||||||
|
@logger ||= Logger.new(@output).tap do |o|
|
||||||
|
o.level = verbose? ? LOGGER_LEVEL_VERBOSE : LOGGER_LEVEL
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def log_logger_level
|
def log_logger_level
|
||||||
log "Logging at #{LOGGER_LEVEL_STRINGS[@logger.level]} level"
|
log "Logging at #{LOGGER_LEVEL_STRINGS[logger.level]} level"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -88,6 +88,20 @@ module Uh
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'with verbose option' do
|
||||||
|
let(:arguments) { %w[-v] }
|
||||||
|
|
||||||
|
it 'sets the env as verbose' do
|
||||||
|
cli.parse_arguments!
|
||||||
|
expect(cli.env).to be_verbose
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'tells the env to log its logger level' do
|
||||||
|
expect(cli.env).to receive :log_logger_level
|
||||||
|
cli.parse_arguments!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'with invalid option' do
|
context 'with invalid option' do
|
||||||
let(:arguments) { %w[--unknown-option] }
|
let(:arguments) { %w[--unknown-option] }
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ module Uh
|
|||||||
RSpec.describe Env do
|
RSpec.describe Env do
|
||||||
let(:output) { StringIO.new }
|
let(:output) { StringIO.new }
|
||||||
let(:logger) { Logger.new(StringIO.new) }
|
let(:logger) { Logger.new(StringIO.new) }
|
||||||
subject(:env) { described_class.new output, logger: logger }
|
subject(:env) { described_class.new output }
|
||||||
|
|
||||||
it 'has verbose mode disabled' do
|
it 'has verbose mode disabled' do
|
||||||
expect(env).not_to be_verbose
|
expect(env).not_to be_verbose
|
||||||
@ -29,20 +29,32 @@ module Uh
|
|||||||
|
|
||||||
describe '#logger' do
|
describe '#logger' do
|
||||||
it 'returns a logger' do
|
it 'returns a logger' do
|
||||||
expect(env.logger).to be_a ::Logger
|
expect(env.logger).to be_a Logger
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'has logger level warn set' do
|
||||||
|
expect(env.logger.level).to be Logger::WARN
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when verbose mode is enabled' do
|
||||||
|
before { env.verbose = true }
|
||||||
|
|
||||||
|
it 'has logger level info set' do
|
||||||
|
expect(env.logger.level).to be Logger::INFO
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#log' do
|
describe '#log' do
|
||||||
it 'logs given message at info level' do
|
it 'logs given message at info level' do
|
||||||
expect(logger).to receive(:info).with 'some message'
|
expect(env.logger).to receive(:info).with 'some message'
|
||||||
env.log 'some message'
|
env.log 'some message'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#log_logger_level' do
|
describe '#log_logger_level' do
|
||||||
it 'logs the logger level' do
|
it 'logs the logger level' do
|
||||||
expect(logger).to receive(:info).with /log.+(warn|info|debug).+level/i
|
expect(env.logger).to receive(:info).with /log.+(warn|info|debug).+level/i
|
||||||
env.log_logger_level
|
env.log_logger_level
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user