From 82d73ebe3cad0443c5dc118506140055b6f582fe Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 13 Oct 2010 18:40:12 -0700 Subject: [PATCH] General validation middleware --- lib/vagrant/action/general/validate.rb | 19 ++++++++++++ test/vagrant/action/general/validate_test.rb | 31 ++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 lib/vagrant/action/general/validate.rb create mode 100644 test/vagrant/action/general/validate_test.rb diff --git a/lib/vagrant/action/general/validate.rb b/lib/vagrant/action/general/validate.rb new file mode 100644 index 000000000..78a5d5e20 --- /dev/null +++ b/lib/vagrant/action/general/validate.rb @@ -0,0 +1,19 @@ +module Vagrant + class Action + module General + # Simply validates the configuration of the current Vagrant + # environment. + class Validate + def initialize(app, env) + @app = app + @env = env + end + + def call(env) + @env["config"].validate! if !@env.has_key?("validate") || @env["validate"] + @app.call(@env) + end + end + end + end +end diff --git a/test/vagrant/action/general/validate_test.rb b/test/vagrant/action/general/validate_test.rb new file mode 100644 index 000000000..00814bb77 --- /dev/null +++ b/test/vagrant/action/general/validate_test.rb @@ -0,0 +1,31 @@ +require "test_helper" + +class ValidateGeneralActionTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Action::General::Validate + @app, @env = action_env + end + + should "initialize fine" do + @klass.new(@app, @env) + end + + should "validate and call up" do + @instance = @klass.new(@app, @env) + + seq = sequence("seq") + @env["config"].expects(:validate!).once.in_sequence(seq) + @app.expects(:call).with(@env).once.in_sequence(seq) + @instance.call(@env) + end + + should "not validate if env says not to" do + @env["validate"] = false + @instance = @klass.new(@app, @env) + + seq = sequence("seq") + @env["config"].expects(:validate!).never + @app.expects(:call).with(@env).once.in_sequence(seq) + @instance.call(@env) + end +end