diff --git a/features/cli/verbose.feature b/features/cli/verbose.feature new file mode 100644 index 0000000..ff5b080 --- /dev/null +++ b/features/cli/verbose.feature @@ -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 diff --git a/features/steps/output_steps.rb b/features/steps/output_steps.rb index eb48120..99ee4ed 100644 --- a/features/steps/output_steps.rb +++ b/features/steps/output_steps.rb @@ -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 diff --git a/features/steps/run_steps.rb b/features/steps/run_steps.rb index 70e5a56..2a85a73 100644 --- a/features/steps/run_steps.rb +++ b/features/steps/run_steps.rb @@ -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 ' ' diff --git a/lib/uh/wm/cli.rb b/lib/uh/wm/cli.rb index bf478db..4330cc1 100644 --- a/lib/uh/wm/cli.rb +++ b/lib/uh/wm/cli.rb @@ -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 diff --git a/lib/uh/wm/env.rb b/lib/uh/wm/env.rb index 71a26b0..7a42b29 100644 --- a/lib/uh/wm/env.rb +++ b/lib/uh/wm/env.rb @@ -1,26 +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" + log "Logging at #{LOGGER_LEVEL_STRINGS[logger.level]} level" end end end diff --git a/spec/uh/wm/cli_spec.rb b/spec/uh/wm/cli_spec.rb index 7bf4482..d901983 100644 --- a/spec/uh/wm/cli_spec.rb +++ b/spec/uh/wm/cli_spec.rb @@ -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] } diff --git a/spec/uh/wm/env_spec.rb b/spec/uh/wm/env_spec.rb index e286b17..f9f1cfb 100644 --- a/spec/uh/wm/env_spec.rb +++ b/spec/uh/wm/env_spec.rb @@ -3,7 +3,7 @@ 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 @@ -29,20 +29,32 @@ module Uh 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(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 end end