Sha256: 7feee570bf2ae2a7ca039072cbc6d8543c55cdbdbac553fd1c33c4e2d2f9a99a

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

require 'open3'

module Serverspec
  module Backend
    class Cmd < Base
      include PowerShell::ScriptHelper

      def run_command(cmd, opts={})
        script = create_script(cmd)
        result = execute_script script

        if @example
          @example.metadata[:command] = script
          @example.metadata[:stdout]  = result[:stdout] + result[:stderr]
        end
        { :stdout => result[:stdout], :stderr => result[:stderr],
          :exit_status => result[:status], :exit_signal => nil }
      end

      def execute_script script
        ps_script = %Q{powershell -encodedCommand #{encode_script(script)}}
        if Open3.respond_to? :capture3
          stdout, stderr, status = Open3.capture3(ps_script)
          # powershell still exits with 0 even if there are syntax errors, although it spits the error out into stderr
          # so we have to resort to return an error exit code if there is anything in the standard error
          status = 1 if status == 0 and !stderr.empty?
          { :stdout => stdout, :stderr => stderr, :status => status }
        else
          stdout = `#{ps_script} 2>&1`
          { :stdout => stdout, :stderr => nil, :status => $? }
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
serverspec-0.9.4 lib/serverspec/backend/cmd.rb
serverspec-0.9.3 lib/serverspec/backend/cmd.rb
serverspec-0.9.2 lib/serverspec/backend/cmd.rb
serverspec-0.9.1 lib/serverspec/backend/cmd.rb
serverspec-0.9.0 lib/serverspec/backend/cmd.rb