From 9433f080dbd77b6d11550b357c7daa285ab189b8 Mon Sep 17 00:00:00 2001 From: Jeff Bonhag Date: Fri, 13 Dec 2019 15:25:13 -0500 Subject: [PATCH] Basic version of Windows-flavored SSH run --- lib/vagrant/action/builtin/ssh_run.rb | 12 ++++++++- .../vagrant/action/builtin/ssh_run_test.rb | 27 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/vagrant/action/builtin/ssh_run.rb b/lib/vagrant/action/builtin/ssh_run.rb index 9ea58ca50..6b411f334 100644 --- a/lib/vagrant/action/builtin/ssh_run.rb +++ b/lib/vagrant/action/builtin/ssh_run.rb @@ -36,7 +36,17 @@ module Vagrant # Get the command and wrap it in a login shell command = ShellQuote.escape(env[:ssh_run_command], "'") - command = "#{env[:machine].config.ssh.shell} -c '#{command}'" + + # This will always default to 'bash -l' unless it is explicitly set in + # the Vagrantfile, because the base SSHConfig object has already been + # finalized. + shell = env[:machine].config.ssh.shell + + if env[:machine].config.vm.communicator == :winssh && shell == "cmd" + command = "#{shell} /C #{command}" + else + command = "#{shell} -c '#{command}'" + end # Execute! opts = env[:ssh_opts] || {} diff --git a/test/unit/vagrant/action/builtin/ssh_run_test.rb b/test/unit/vagrant/action/builtin/ssh_run_test.rb index 56609607d..b053cf06e 100644 --- a/test/unit/vagrant/action/builtin/ssh_run_test.rb +++ b/test/unit/vagrant/action/builtin/ssh_run_test.rb @@ -18,8 +18,14 @@ describe Vagrant::Action::Builtin::SSHRun do ) end + let(:vm) do + double("vm", + communicator: nil + ) + end + # Configuration mock - let(:config) { double("config", ssh: ssh) } + let(:config) { double("config", ssh: ssh, vm: vm) } let(:machine) do double("machine", @@ -80,4 +86,23 @@ describe Vagrant::Action::Builtin::SSHRun do env[:ssh_run_command] = "echo test" described_class.new(app, env).call(env) end + + context "when using the WinSSH communicator" do + before do + expect(vm).to receive(:communicator).and_return(:winssh) + expect(ssh).to receive(:shell).and_return("cmd") + end + + it "should use Windows-specific flags for cmd" do + ssh_info = { foo: :bar } + opts = {:extra_args=>["-t", "cmd /C dir"], :subprocess=>true} + + expect(ssh_klass).to receive(:exec). + with(ssh_info, opts) + + env[:ssh_info] = ssh_info + env[:ssh_run_command] = "dir" + described_class.new(app, env).call(env) + end + end end