Add Env class

This commit is contained in:
Thibault Jouan 2013-07-29 21:07:58 +00:00
parent 30e1930719
commit d199ba56fd
7 changed files with 50 additions and 11 deletions

View File

@ -1,4 +1,5 @@
require 'producer/core/cli' require 'producer/core/cli'
require 'producer/core/env'
require 'producer/core/recipe' require 'producer/core/recipe'
require 'producer/core/task' require 'producer/core/task'
require 'producer/core/version' require 'producer/core/version'

View File

@ -13,7 +13,9 @@ module Producer
def run! def run!
print_usage_and_exit(64) unless @arguments.length == 2 print_usage_and_exit(64) unless @arguments.length == 2
Recipe.from_file(@arguments[1]).evaluate recipe = Recipe.from_file(@arguments[1])
env = Env.new(recipe)
recipe.evaluate env
end end

11
lib/producer/core/env.rb Normal file
View File

@ -0,0 +1,11 @@
module Producer
module Core
class Env
attr_reader :current_recipe
def initialize(recipe)
@current_recipe = recipe
end
end
end
end

View File

@ -12,8 +12,8 @@ module Producer
@filepath = filepath @filepath = filepath
end end
def evaluate def evaluate(env)
dsl = DSL.new(@code).evaluate dsl = DSL.new(@code).evaluate(env)
dsl.tasks.each.map(&:evaluate) dsl.tasks.each.map(&:evaluate)
end end
@ -27,7 +27,7 @@ module Producer
@tasks = [] @tasks = []
end end
def evaluate def evaluate(env)
if @code if @code
instance_eval @code instance_eval @code
else else

View File

@ -13,16 +13,26 @@ module Producer::Core
end end
end end
# FIXME: spec/method need refactoring
describe '#run!' do describe '#run!' do
it 'builds a recipe' do it 'builds a recipe' do
expect(Recipe).to receive(:from_file).with(arguments[1]).and_call_original expect(Recipe).to receive(:from_file).with(arguments[1]).and_call_original
cli.run! cli.run!
end end
it 'evaluates the recipe' do it 'builds an environment with the current recipe' do
recipe = double('recipe').as_null_object
allow(Recipe).to receive(:from_file).and_return(recipe)
expect(Env).to receive(:new).with(recipe).and_call_original
cli.run!
end
it 'evaluates the recipe with the environment' do
recipe = double('recipe') recipe = double('recipe')
allow(Recipe).to receive(:from_file).and_return(recipe) allow(Recipe).to receive(:from_file).and_return(recipe)
expect(recipe).to receive(:evaluate) env = double('env')
allow(Env).to receive(:new).and_return(env)
expect(recipe).to receive(:evaluate).with(env)
cli.run! cli.run!
end end

View File

@ -0,0 +1,14 @@
require 'spec_helper'
module Producer::Core
describe Env do
let(:recipe) { Recipe.new(Proc.new { nil }) }
subject(:env) { Env.new(recipe) }
describe '#current_recipe' do
it 'returns the assigned current recipe' do
expect(env.current_recipe).to eq recipe
end
end
end
end

View File

@ -5,6 +5,7 @@ module Producer::Core
include FixturesHelpers include FixturesHelpers
let(:code) { 'nil' } let(:code) { 'nil' }
let(:env) { double('env') }
subject(:recipe) { Recipe.new(code) } subject(:recipe) { Recipe.new(code) }
describe '.from_file' do describe '.from_file' do
@ -35,7 +36,7 @@ module Producer::Core
describe '#evaluate' do describe '#evaluate' do
it 'builds a recipe DSL sandbox' do it 'builds a recipe DSL sandbox' do
expect(Recipe::DSL).to receive(:new).with(code).and_call_original expect(Recipe::DSL).to receive(:new).with(code).and_call_original
recipe.evaluate recipe.evaluate(env)
end end
end end
@ -65,23 +66,23 @@ module Producer::Core
describe '#evaluate' do describe '#evaluate' do
it 'evaluates its code' do it 'evaluates its code' do
dsl = Recipe::DSL.new { raise 'error from recipe' } dsl = Recipe::DSL.new { raise 'error from recipe' }
expect { dsl.evaluate }.to raise_error(RuntimeError, 'error from recipe') expect { dsl.evaluate(env) }.to raise_error(RuntimeError, 'error from recipe')
end end
it 'returns itself' do it 'returns itself' do
expect(dsl.evaluate).to eq dsl expect(dsl.evaluate(env)).to eq dsl
end end
end end
context 'DSL specific methods' do context 'DSL specific methods' do
subject(:dsl) { Recipe::DSL.new(&code).evaluate } subject(:dsl) { Recipe::DSL.new(&code).evaluate(env) }
describe '#source' do describe '#source' do
let(:code) { "source '#{fixture_path_for 'recipes/error'}'" } let(:code) { "source '#{fixture_path_for 'recipes/error'}'" }
subject(:dsl) { Recipe::DSL.new code } subject(:dsl) { Recipe::DSL.new code }
it 'sources the recipe given as argument' do it 'sources the recipe given as argument' do
expect { dsl.evaluate }.to raise_error(RuntimeError, 'error from recipe') expect { dsl.evaluate(env) }.to raise_error(RuntimeError, 'error from recipe')
end end
end end