diff --git a/lib/vagrant_spec/command/init.rb b/lib/vagrant_spec/command/init.rb index bae11df..9eac8b2 100644 --- a/lib/vagrant_spec/command/init.rb +++ b/lib/vagrant_spec/command/init.rb @@ -1,5 +1,7 @@ # encoding: UTF-8 +require 'fileutils' + require 'vagrant_spec/ansible_inventory' require 'vagrant_spec/machine_data' require 'vagrant_spec/config' @@ -30,6 +32,7 @@ def initialize(argv, env) def execute return unless parse_opts + FileUtils.mkdir @directory unless Dir.exist? @directory unless @ansible_inventory == DEFAULTS['ansible_inventory'] VagrantSpec::AnsibleInventory.new(@env).generate end diff --git a/lib/vagrant_spec/test_plan.rb b/lib/vagrant_spec/test_plan.rb index 32f899d..13f5635 100644 --- a/lib/vagrant_spec/test_plan.rb +++ b/lib/vagrant_spec/test_plan.rb @@ -27,14 +27,21 @@ def initialize(env) # This will fail if any of the tests fail, but it will allow all tests to # run def run + print_banner + @test_plan.each do |plan| + found_nodes = nodes(plan) + if found_nodes + found_nodes.each { |node| execute_plan_tests(node, plan) } + end + end + exit @ret_code + end + + def print_banner @env.ui.info('*******************************************************') @env.ui.info('***************** ServerSpec Test Run *****************') @env.ui.info('*******************************************************') @env.ui.info('') - @test_plan.each do |plan| - nodes(plan).each { |node| execute_plan_tests(node, plan) } - end - exit @ret_code end # Return array of active Vagrant machines diff --git a/scripts/poor_mans_smoke_test.sh b/scripts/poor_mans_smoke_test.sh index a57d9a6..2463d2e 100755 --- a/scripts/poor_mans_smoke_test.sh +++ b/scripts/poor_mans_smoke_test.sh @@ -16,6 +16,7 @@ bundle exec vagrant spec test -h bundle exec vagrant spec no_command -h rm -f "serverspec/spec_helper.rb" rm -f ".vagrantspec_machine_data" +rm -f "vagrantspec_inventory" bundle exec vagrant up bundle exec vagrant spec init ansible-playbook site.yml -i vagrantspec_inventory diff --git a/spec/unit/vagrant_spec_test/command_spec/init_spec.rb b/spec/unit/vagrant_spec_test/command_spec/init_spec.rb index d589d55..b1d9fab 100644 --- a/spec/unit/vagrant_spec_test/command_spec/init_spec.rb +++ b/spec/unit/vagrant_spec_test/command_spec/init_spec.rb @@ -32,6 +32,8 @@ def execute_proc allow_any_instance_of(VagrantSpec::MachineData).to receive(:generate) allow(mock_ansible_inventory).to receive(:generate) allow(mock_machine_data).to receive(:generate) + allow(Dir).to receive(:exist?) { false } + allow(FileUtils).to receive(:mkdir) end end @@ -55,6 +57,14 @@ def execute_protection_proc end end + context 'when the @directory does not exist' do + it '#execute creates the @directory' do + execute_protection_proc.call + expect(FileUtils).to receive(:mkdir).with(subject.directory) + subject.execute + end + end + context 'and when @ansible_inventory eq empty hash,' do it '#execute creates an instance of VagrantSpec::SpecHelper' do execute_protection_proc.call diff --git a/spec/unit/vagrant_spec_test/test_plan_spec.rb b/spec/unit/vagrant_spec_test/test_plan_spec.rb index c565919..1d11d0a 100644 --- a/spec/unit/vagrant_spec_test/test_plan_spec.rb +++ b/spec/unit/vagrant_spec_test/test_plan_spec.rb @@ -10,10 +10,6 @@ describe VagrantSpec::TestPlan do include RSpec::Support::InSubProcess include_context 'unit' - - ############################################################################## - # mocks - include_examples 'shared_mocks' let(:mock_plan) do @@ -40,9 +36,6 @@ let(:mock_ssh_backend) { double(Specinfra::Backend::Ssh) } - ############################################################################## - # Stubs - before do allow_any_instance_of(VagrantSpec::MachineFinder) .to receive(:match_nodes) { [double(Vagrant::Machine)] } @@ -63,6 +56,11 @@ subject { VagrantSpec::TestPlan.new(iso_env) } + it '#print_banner prints the testing initialization banner' do + expect(mock_ui).to receive(:info) + subject.print_banner + end + context 'when passing a Regexp object' do it '#nodes calls match_nodes on a machine_finder instance' do expect(subject.m_finder).to receive(:match_nodes) @@ -93,13 +91,6 @@ end end - ############################################################################## - # Testing #execute_plan_tests - # - # These tests must be executed in a sub process because execute_plan_tests - # executes clear_examples. clear_examples modifies global state, so we must - # contain it. - def execute_plan_tests_proc proc do allow(subject).to receive(:close_ssh) @@ -129,4 +120,14 @@ def execute_plan_tests_proc expect(subject).to receive(:configure_serverspec) end end + + context 'when nodes returns nil' do + it '#run does not fail with NoMethodError' do + allow(subject).to receive(:execute_plan_tests) + allow(subject).to receive(:exit) + allow(subject).to receive(:nodes) { nil } + + expect { subject.run }.not_to raise_error + end + end end