Introduce Env to manage CLI env and config
This commit is contained in:
parent
12075323c5
commit
304db803f3
@ -1,6 +1,7 @@
|
||||
require 'uh'
|
||||
|
||||
require 'uh/wm/cli'
|
||||
require 'uh/wm/env'
|
||||
|
||||
module Uh
|
||||
module WM
|
||||
|
@ -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
|
||||
|
22
lib/uh/wm/env.rb
Normal file
22
lib/uh/wm/env.rb
Normal file
@ -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
|
@ -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
30
spec/uh/wm/env_spec.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user