Add a new class for run control file evaluation
This commit is contained in:
parent
66c1b78740
commit
b1aa430ef3
@ -7,6 +7,7 @@ require 'uh/wm/cli'
|
|||||||
require 'uh/wm/dispatcher'
|
require 'uh/wm/dispatcher'
|
||||||
require 'uh/wm/env'
|
require 'uh/wm/env'
|
||||||
require 'uh/wm/manager'
|
require 'uh/wm/manager'
|
||||||
|
require 'uh/wm/run_control'
|
||||||
require 'uh/wm/runner'
|
require 'uh/wm/runner'
|
||||||
|
|
||||||
module Uh
|
module Uh
|
||||||
|
21
lib/uh/wm/run_control.rb
Normal file
21
lib/uh/wm/run_control.rb
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
module Uh
|
||||||
|
module WM
|
||||||
|
class RunControl
|
||||||
|
class << self
|
||||||
|
def evaluate env
|
||||||
|
rc_path = File.expand_path(env.rc_path)
|
||||||
|
rc = new env
|
||||||
|
rc.evaluate File.read(rc_path) if File.exist?(rc_path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize env
|
||||||
|
@env = env
|
||||||
|
end
|
||||||
|
|
||||||
|
def evaluate code
|
||||||
|
instance_eval code
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -32,8 +32,7 @@ module Uh
|
|||||||
end
|
end
|
||||||
|
|
||||||
def evaluate_run_control
|
def evaluate_run_control
|
||||||
rc_path = File.expand_path(@env.rc_path)
|
RunControl.evaluate(env)
|
||||||
eval File.read(rc_path) if File.exist?(rc_path)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def register_event_hooks
|
def register_event_hooks
|
||||||
|
53
spec/uh/wm/run_control_spec.rb
Normal file
53
spec/uh/wm/run_control_spec.rb
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
require 'support/filesystem_helpers'
|
||||||
|
|
||||||
|
module Uh
|
||||||
|
module WM
|
||||||
|
RSpec.describe RunControl do
|
||||||
|
include FileSystemHelpers
|
||||||
|
|
||||||
|
let(:code) { :run_control_code }
|
||||||
|
let(:env) { Env.new(StringIO.new) }
|
||||||
|
subject(:rc) { described_class.new env }
|
||||||
|
|
||||||
|
describe '.evaluate' do
|
||||||
|
around do |example|
|
||||||
|
with_file ':run_control_code' do |f|
|
||||||
|
env.rc_path = f.path
|
||||||
|
example.run
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'builds a new instance with given env' do
|
||||||
|
expect(described_class).to receive(:new).with(env).and_call_original
|
||||||
|
described_class.evaluate env
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'tells the new instance to evaluate run control file content' do
|
||||||
|
expect(rc).to receive(:evaluate).with ':run_control_code'
|
||||||
|
allow(described_class).to receive(:new) { rc }
|
||||||
|
described_class.evaluate env
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when run control file is not present' do
|
||||||
|
before { env.rc_path = 'non_existent_rc_file.rb' }
|
||||||
|
|
||||||
|
it 'does not raise any error' do
|
||||||
|
expect { described_class.evaluate env }.not_to raise_error
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#evaluate' do
|
||||||
|
it 'evaluates given code' do
|
||||||
|
expect { rc.evaluate 'throw :run_control_code' }
|
||||||
|
.to throw_symbol :run_control_code
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'provides access to assigned env' do
|
||||||
|
expect { rc.evaluate 'fail @env.object_id.to_s' }
|
||||||
|
.to raise_error env.object_id.to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -1,5 +1,3 @@
|
|||||||
require 'support/filesystem_helpers'
|
|
||||||
|
|
||||||
SomeLayout = Class.new do
|
SomeLayout = Class.new do
|
||||||
define_method(:register) { |*args| }
|
define_method(:register) { |*args| }
|
||||||
end
|
end
|
||||||
@ -7,8 +5,6 @@ end
|
|||||||
module Uh
|
module Uh
|
||||||
module WM
|
module WM
|
||||||
RSpec.describe Runner do
|
RSpec.describe Runner do
|
||||||
include FileSystemHelpers
|
|
||||||
|
|
||||||
let(:env) { Env.new(StringIO.new) }
|
let(:env) { Env.new(StringIO.new) }
|
||||||
subject(:runner) { described_class.new env }
|
subject(:runner) { described_class.new env }
|
||||||
|
|
||||||
@ -60,19 +56,9 @@ module Uh
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe '#evaluate_run_control' do
|
describe '#evaluate_run_control' do
|
||||||
context 'when run control file is present' do
|
it 'evaluates the run control file with RunControl and current env' do
|
||||||
it 'evaluates the run control file' do
|
expect(RunControl).to receive(:evaluate).with env
|
||||||
with_file 'throw :run_control' do |f|
|
runner.evaluate_run_control
|
||||||
env.rc_path = f.path
|
|
||||||
expect { runner.evaluate_run_control }.to throw_symbol :run_control
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when run control file is not present' do
|
|
||||||
it 'does not raise any error' do
|
|
||||||
expect { runner.evaluate_run_control }.not_to raise_error
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user