From 304db803f35261e4e2de0b6bac1cfeda6196bf8e Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Tue, 7 Apr 2015 23:13:28 +0000 Subject: [PATCH] Introduce Env to manage CLI env and config --- lib/uh/wm.rb | 1 + lib/uh/wm/cli.rb | 19 +++++++++---------- lib/uh/wm/env.rb | 22 ++++++++++++++++++++++ spec/uh/wm/cli_spec.rb | 28 +++++++++++++++++++--------- spec/uh/wm/env_spec.rb | 30 ++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 19 deletions(-) create mode 100644 lib/uh/wm/env.rb create mode 100644 spec/uh/wm/env_spec.rb diff --git a/lib/uh/wm.rb b/lib/uh/wm.rb index 6199790..553cec2 100644 --- a/lib/uh/wm.rb +++ b/lib/uh/wm.rb @@ -1,6 +1,7 @@ require 'uh' require 'uh/wm/cli' +require 'uh/wm/env' module Uh module WM diff --git a/lib/uh/wm/cli.rb b/lib/uh/wm/cli.rb index 1656d88..5ea3e54 100644 --- a/lib/uh/wm/cli.rb +++ b/lib/uh/wm/cli.rb @@ -8,21 +8,21 @@ module Uh EX_USAGE = 64 class << self - def run arguments, **options - cli = new arguments, **options + def run arguments, stdout: $stdout, stderr: $stderr + cli = new arguments, stdout: stdout cli.parse_arguments! cli.run rescue ArgumentError => e - $stderr.puts e + stderr.puts e exit EX_USAGE end end - def initialize args, stdin: $stdin, stdout: $stdout, stderr: $stderr + attr_reader :env + + def initialize args, stdout: $stdout @arguments = args - @stdin = stdin - @stdout = stdout - @stderr = stderr + @env = Env.new(stdout.tap { |o| o.sync = true }) end def parse_arguments! @@ -32,10 +32,9 @@ module Uh end def run - @stdout.sync = true @display = Display.new @display.open - @stdout.puts "Connected to X server on `#{@display}'" + @env.log "Connected to X server on `#{@display}'" end @@ -48,7 +47,7 @@ module Uh opts.separator 'options:' opts.on '-h', '--help', 'print this message' do - @stdout.print opts + @env.print opts exit end end diff --git a/lib/uh/wm/env.rb b/lib/uh/wm/env.rb new file mode 100644 index 0000000..aa0d76e --- /dev/null +++ b/lib/uh/wm/env.rb @@ -0,0 +1,22 @@ +require 'logger' + +module Uh + module WM + class Env + attr_reader :output, :logger + + def initialize output, logger_: Logger.new(output) + @output = output + @logger = logger_ + end + + def log message + logger.info message + end + + def print message + @output.print message + end + end + end +end diff --git a/spec/uh/wm/cli_spec.rb b/spec/uh/wm/cli_spec.rb index 0b00cda..ff5b48d 100644 --- a/spec/uh/wm/cli_spec.rb +++ b/spec/uh/wm/cli_spec.rb @@ -8,16 +8,17 @@ module Uh let(:stdout) { StringIO.new } let(:stderr) { StringIO.new } let(:arguments) { [] } - let(:options) { { stdout: stdout, stderr: stderr } } - subject(:cli) { described_class.new arguments, **options } + subject(:cli) { described_class.new arguments, stdout: stdout } describe '.run' do - subject(:run) { described_class.run arguments, **options } + subject(:run) do + described_class.run arguments, stdout: stdout, stderr: stderr + end it 'builds a new CLI with given arguments' do expect(described_class) - .to receive(:new).with(arguments, options).and_call_original + .to receive(:new).with(arguments, stdout: stdout).and_call_original run end @@ -38,21 +39,30 @@ module Uh context 'with invalid arguments' do let(:arguments) { %w[--unknown-option] } - it 'prints the usage' do - expect { trap_exit { run } }.to output(/\AUsage: .+/).to_stderr + it 'prints the usage on standard error stream' do + trap_exit { run } + expect(stderr.string).to match /\AUsage: .+/ end it 'exits with a return status of 64' do - stderr_original = $stderr - $stderr = stderr expect { run }.to raise_error(SystemExit) do |e| expect(e.status).to eq 64 end - $stderr = stderr_original end end end + describe '#initialize' do + it 'builds an env with given stdout' do + expect(cli.env.output).to be stdout + end + + it 'syncs the output' do + expect(stdout).to receive(:sync=).with(true) + cli + end + end + describe '#run' do let(:display) { instance_spy Display } diff --git a/spec/uh/wm/env_spec.rb b/spec/uh/wm/env_spec.rb new file mode 100644 index 0000000..681327d --- /dev/null +++ b/spec/uh/wm/env_spec.rb @@ -0,0 +1,30 @@ +module Uh + module WM + RSpec.describe Env do + let(:output) { StringIO.new } + let(:logger) { Logger.new(StringIO.new) } + + subject(:env) { described_class.new output, logger_: logger } + + describe '#logger' do + it 'returns a logger' do + expect(env.logger).to be_a ::Logger + end + end + + describe '#log' do + it 'logs given message at info level' do + expect(logger).to receive(:info).with 'some message' + env.log 'some message' + end + end + + describe '#print' do + it 'prints the message to the output' do + env.print 'some message' + expect(output.string).to eq 'some message' + end + end + end + end +end