From 9321f3cb6cbac452e0b7c3a9d1f9731446c60f75 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 13 Jul 2018 16:57:24 -0700 Subject: [PATCH 1/2] Ensure the vagrantfile_name option is stubbed when using plugin commands --- bin/vagrant | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/vagrant b/bin/vagrant index 7720d0915..83fc5a887 100755 --- a/bin/vagrant +++ b/bin/vagrant @@ -44,6 +44,7 @@ argv.each_index do |i| # Do not load plugins when performing plugin operations if arg == "plugin" + opts[:vagrantfile_name] = "" ENV['VAGRANT_NO_PLUGINS'] = "1" # Only initialize plugins when listing installed plugins if argv[i+1] != "list" From 516e1d2621969192c02a02335a2502722fd99307 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Mon, 16 Jul 2018 16:54:15 -0700 Subject: [PATCH 2/2] Add test coverage on the vagrant bin file behavior --- test/unit/bin/vagrant_test.rb | 147 ++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 test/unit/bin/vagrant_test.rb diff --git a/test/unit/bin/vagrant_test.rb b/test/unit/bin/vagrant_test.rb new file mode 100644 index 000000000..4b5dfbe5b --- /dev/null +++ b/test/unit/bin/vagrant_test.rb @@ -0,0 +1,147 @@ +require File.expand_path("../../base", __FILE__) + +describe "vagrant bin" do + include_context "unit" + + let(:env) { isolated_environment } + let(:ui) { double(:ui) } + let(:argv) { [] } + let(:exit_code) { 0 } + + let(:run_vagrant) { + lambda { + begin + instance_eval( + File.read( + File.expand_path( + "../../../../bin/vagrant", __FILE__))) + rescue SystemExit => e + e.status + end + }.call + } + + before do + allow(env).to receive(:ui).and_return(ui) + allow(ARGV).to receive(:dup).and_return(argv) + allow(env).to receive(:unload) + allow(env).to receive(:cli).and_return(exit_code) + allow(Kernel).to receive(:at_exit) + allow(Kernel).to receive(:exit) + allow(Vagrant::Environment).to receive(:new).and_return(env) + allow(Vagrant).to receive(:in_installer?).and_return(true) + end + + after { expect(run_vagrant).to eq(exit_code) } + + it "should run the CLI and exit successfully" do + expect(env).to receive(:cli).with(argv).and_return(exit_code) + expect(run_vagrant).to eq(exit_code) + end + + context "with flag" do + describe "--version" do + let(:argv) { ["--version"] } + before { allow(self).to receive(:require_relative).with(/version/) } + + it "should output the current version" do + expect($stdout).to receive(:puts).with(/#{Regexp.escape(Vagrant::VERSION.to_s)}/) + end + end + + describe "--timestamp" do + let(:argv) { ["--timestamp"] } + + it "should enable timestamps on logs" do + expect(ENV).to receive(:[]=).with("VAGRANT_LOG_TIMESTAMP", "1") + end + end + + describe "--debug-timestamp" do + let(:argv) { ["--debug-timestamp"] } + + it "should enable debugging and log timestamps" do + expect(ENV).to receive(:[]=).with("VAGRANT_LOG_TIMESTAMP", "1") + expect(ENV).to receive(:[]=).with("VAGRANT_LOG", "debug") + end + end + + describe "--no-color" do + let(:argv) { ["--no-color"] } + + it "should remove flag from argv" do + expect(env).to receive(:cli).with([]).and_return(exit_code) + end + + it "should pass a Basic UI instance" do + expect(Vagrant::Environment).to receive(:new). + with(hash_including(ui_class: Vagrant::UI::Basic)) + end + end + + describe "--color" do + let(:argv) { ["--color"] } + + it "should remove flag from argv" do + expect(env).to receive(:cli).with([]).and_return(exit_code) + end + + it "should pass a Colored UI instance" do + expect(Vagrant::Environment).to receive(:new). + with(hash_including(ui_class: Vagrant::UI::Colored)) + end + end + end + + context "when not in installer" do + let(:warning) { "INSTALLER WARNING" } + + before do + expect(Vagrant).to receive(:in_installer?).and_return(false) + allow(I18n).to receive(:t).with(/not_in_installer/).and_return(warning) + end + + context "when vagrant is not very quiet" do + before { expect(Vagrant).to receive(:very_quiet?).and_return(false) } + + it "should output a warning" do + expect(env.ui).to receive(:warn).with(/#{warning}/, any_args) + end + end + + context "when vagrant is very quiet" do + before { expect(Vagrant).to receive(:very_quiet?).and_return(true) } + + it "should not output a warning" do + expect(env.ui).not_to receive(:warn).with(/#{warning}/, any_args) + end + end + end + + context "plugin commands" do + let(:argv) { ["plugin"] } + + before { allow(ENV).to receive(:[]=) } + + it "should unset vagrantfile" do + expect(Vagrant::Environment).to receive(:new). + with(hash_including(vagrantfile_name: "")).and_return(env) + end + + it "should set the no plugins environment variable" do + expect(ENV).to receive(:[]=).with("VAGRANT_NO_PLUGINS", "1") + end + + it "should set the disable plugin init environment variable" do + expect(ENV).to receive(:[]=).with("VAGRANT_DISABLE_PLUGIN_INIT", "1") + end + + context "list" do + let(:argv) { ["plugin", "list"] } + + it "should not set the disable plugin init environment variable" do + expect(ENV).not_to receive(:[]=).with("VAGRANT_DISABLE_PLUGIN_INIT", "1") + end + end + end +end