Merge branch 'verbose-option'
This commit is contained in:
commit
1079609cfc
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
|
||||
Timeout.timeout(timeout) 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
|
||||
end
|
||||
end
|
||||
@ -15,5 +20,10 @@ Usage: uhwm [options]
|
||||
|
||||
options:
|
||||
-h, --help print this message
|
||||
-v, --version enable verbose mode
|
||||
eoh
|
||||
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 << options if options
|
||||
@interactive = @process = run command.join ' '
|
||||
|
@ -48,6 +48,11 @@ module Uh
|
||||
@env.print opts
|
||||
exit
|
||||
end
|
||||
|
||||
opts.on '-v', '--version', 'enable verbose mode' do
|
||||
@env.verbose = true
|
||||
@env.log_logger_level
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,15 +1,33 @@
|
||||
module Uh
|
||||
module WM
|
||||
class Env
|
||||
LOGGER_LEVEL = Logger::WARN
|
||||
LOGGER_LEVEL_VERBOSE = Logger::INFO
|
||||
LOGGER_LEVEL_STRINGS = %w[DEBUG INFO WARN ERROR FATAL UNKNOWN]
|
||||
|
||||
extend Forwardable
|
||||
def_delegator :@logger, :info, :log
|
||||
def_delegator :logger, :info, :log
|
||||
def_delegator :@output, :print
|
||||
|
||||
attr_reader :output, :logger
|
||||
attr_reader :output
|
||||
attr_accessor :verbose
|
||||
|
||||
def initialize output, logger: Logger.new(output)
|
||||
def initialize output
|
||||
@output = output
|
||||
@logger = logger
|
||||
end
|
||||
|
||||
def verbose?
|
||||
!!@verbose
|
||||
end
|
||||
|
||||
def logger
|
||||
@logger ||= Logger.new(@output).tap do |o|
|
||||
o.level = verbose? ? LOGGER_LEVEL_VERBOSE : LOGGER_LEVEL
|
||||
end
|
||||
end
|
||||
|
||||
def log_logger_level
|
||||
log "Logging at #{LOGGER_LEVEL_STRINGS[logger.level]} level"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -88,6 +88,20 @@ module Uh
|
||||
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
|
||||
let(:arguments) { %w[--unknown-option] }
|
||||
|
||||
|
@ -3,21 +3,62 @@ module Uh
|
||||
RSpec.describe Env do
|
||||
let(:output) { 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
|
||||
expect(env).not_to be_verbose
|
||||
end
|
||||
|
||||
describe '#verbose?' do
|
||||
context 'when verbose mode is disabled' do
|
||||
before { env.verbose = false }
|
||||
|
||||
it 'returns false' do
|
||||
expect(env.verbose?).to be false
|
||||
end
|
||||
end
|
||||
|
||||
context 'when verbose mode is enabled' do
|
||||
before { env.verbose = true }
|
||||
|
||||
it 'returns true' do
|
||||
expect(env.verbose?).to be true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#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
|
||||
|
||||
describe '#log' 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'
|
||||
end
|
||||
end
|
||||
|
||||
describe '#log_logger_level' do
|
||||
it 'logs the logger level' do
|
||||
expect(env.logger).to receive(:info).with /log.+(warn|info|debug).+level/i
|
||||
env.log_logger_level
|
||||
end
|
||||
end
|
||||
|
||||
describe '#print' do
|
||||
it 'prints the message to the output' do
|
||||
env.print 'some message'
|
||||
|
Loading…
x
Reference in New Issue
Block a user