From 3dec6869bb18826edd31d44937f7666dbe581fed Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 19 Mar 2018 15:02:51 -0700 Subject: [PATCH] Update trigger unit tests --- .../plugins/kernel_v2/config/trigger_test.rb | 100 ++++++++++++++++++ .../kernel_v2/config/vm_trigger_test.rb | 13 ++- 2 files changed, 110 insertions(+), 3 deletions(-) diff --git a/test/unit/plugins/kernel_v2/config/trigger_test.rb b/test/unit/plugins/kernel_v2/config/trigger_test.rb index 79199bd84..a14f2a1ad 100644 --- a/test/unit/plugins/kernel_v2/config/trigger_test.rb +++ b/test/unit/plugins/kernel_v2/config/trigger_test.rb @@ -35,4 +35,104 @@ describe VagrantPlugins::Kernel_V2::TriggerConfig do subject.finalize! assert_valid end + + let (:hash_block) { {info: "hi", run: {inline: "echo 'hi'"}} } + let (:splat) { [:up, :destroy, :halt] } + let (:arr) { [[:up, :destroy, :halt]] } + + describe "creating a before trigger" do + it "creates a trigger with the splat syntax" do + allow(subject).to receive(:create_trigger).and_return([:foo]) + subject.before(:up, hash_block) + bf_trigger = subject.instance_variable_get(:@_before_triggers) + expect(bf_trigger.size).to eq(1) + end + + it "creates a trigger with the array syntax" do + allow(subject).to receive(:create_trigger).and_return([:foo]) + subject.before([:up], hash_block) + bf_trigger = subject.instance_variable_get(:@_before_triggers) + expect(bf_trigger.size).to eq(1) + end + + it "creates a trigger with the block syntax" do + allow(subject).to receive(:create_trigger).and_return([:foo]) + subject.before :up do |trigger| + trigger.name = "rspec" + end + bf_trigger = subject.instance_variable_get(:@_before_triggers) + expect(bf_trigger.size).to eq(1) + end + + it "creates multiple triggers with the splat syntax" do + allow(subject).to receive(:create_trigger).and_return([:foo]) + subject.before(splat, hash_block) + bf_trigger = subject.instance_variable_get(:@_before_triggers) + expect(bf_trigger.size).to eq(3) + end + + it "creates multiple triggers with the block syntax" do + allow(subject).to receive(:create_trigger).and_return([:foo]) + subject.before splat do |trigger| + trigger.name = "rspec" + end + bf_trigger = subject.instance_variable_get(:@_before_triggers) + expect(bf_trigger.size).to eq(3) + end + + it "creates multiple triggers with the array syntax" do + allow(subject).to receive(:create_trigger).and_return([:foo]) + subject.before(arr, hash_block) + bf_trigger = subject.instance_variable_get(:@_before_triggers) + expect(bf_trigger.size).to eq(3) + end + end + + describe "creating an after trigger" do + it "creates a trigger with the splat syntax" do + allow(subject).to receive(:create_trigger).and_return([:foo]) + subject.after(:up, hash_block) + af_trigger = subject.instance_variable_get(:@_after_triggers) + expect(af_trigger.size).to eq(1) + end + + it "creates a trigger with the array syntax" do + allow(subject).to receive(:create_trigger).and_return([:foo]) + subject.after([:up], hash_block) + af_trigger = subject.instance_variable_get(:@_after_triggers) + expect(af_trigger.size).to eq(1) + end + + it "creates a trigger with the block syntax" do + allow(subject).to receive(:create_trigger).and_return([:foo]) + subject.after :up do |trigger| + trigger.name = "rspec" + end + af_trigger = subject.instance_variable_get(:@_after_triggers) + expect(af_trigger.size).to eq(1) + end + + it "creates multiple triggers with the splat syntax" do + allow(subject).to receive(:create_trigger).and_return([:foo]) + subject.after(splat, hash_block) + af_trigger = subject.instance_variable_get(:@_after_triggers) + expect(af_trigger.size).to eq(3) + end + + it "creates multiple triggers with the block syntax" do + allow(subject).to receive(:create_trigger).and_return([:foo]) + subject.after splat do |trigger| + trigger.name = "rspec" + end + af_trigger = subject.instance_variable_get(:@_after_triggers) + expect(af_trigger.size).to eq(3) + end + + it "creates multiple triggers with the array syntax" do + allow(subject).to receive(:create_trigger).and_return([:foo]) + subject.after(arr, hash_block) + af_trigger = subject.instance_variable_get(:@_after_triggers) + expect(af_trigger.size).to eq(3) + end + end end diff --git a/test/unit/plugins/kernel_v2/config/vm_trigger_test.rb b/test/unit/plugins/kernel_v2/config/vm_trigger_test.rb index c7b7fb139..71a04bb4b 100644 --- a/test/unit/plugins/kernel_v2/config/vm_trigger_test.rb +++ b/test/unit/plugins/kernel_v2/config/vm_trigger_test.rb @@ -35,8 +35,15 @@ describe VagrantPlugins::Kernel_V2::VagrantConfigTrigger do subject.name = "foo" end - it "is valid with test defaults" do - subject.finalize! - assert_valid + describe "with defaults" do + it "is valid with test defaults" do + subject.finalize! + assert_valid + end + + it "sets a command" do + expect(subject.command).to eq(command) + end end + end