Introduce Env to manage CLI env and config

This commit is contained in:
Thibault Jouan
2015-04-07 23:13:28 +00:00
parent 12075323c5
commit 304db803f3
5 changed files with 81 additions and 19 deletions

View File

@@ -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 }

30
spec/uh/wm/env_spec.rb Normal file
View File

@@ -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