Sha256: 28a4a89d92c9127349624c591035d2a99418ddca4fcf2450c63c3102ff4d91df

Contents?: true

Size: 1.92 KB

Versions: 30

Compression:

Stored size: 1.92 KB

Contents

require 'open3'

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

      def run_command(cmd, opts={})
        script = create_script(cmd)
        result = execute_script %Q{#{powershell} -encodedCommand #{encode_script(script)}}

        if @example
          @example.metadata[:command] = script
          @example.metadata[:stdout]  = result[:stdout] + result[:stderr]
        end
        CommandResult.new :stdout => result[:stdout], :stderr => result[:stderr],
          :exit_status => result[:status]
      end

      def execute_script script
        if Open3.respond_to? :capture3
          stdout, stderr, status = Open3.capture3(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 = `#{script} 2>&1`
          { :stdout => stdout, :stderr => nil, :status => $? }
        end
      end

      def check_os
        # Dirty hack for specs
        'Windows'
      end

      private

      def powershell
        architecture = @example.metadata[:architecture] || Specinfra.configuration.architecture

        case architecture
        when :i386 then x86_powershell
        when :x86_64 then x64_powershell
        else raise ArgumentError, "invalid architecture [#{architecture}]"
        end
      end

      def x64_powershell
        find_powershell(%w(sysnative system32))
      end

      def x86_powershell
        find_powershell(%w(syswow64 system32))
      end

      def find_powershell(dirs)
        ( dirs.map { |dir| "#{ENV['WINDIR']}\\#{dir}\\WindowsPowerShell\\v1.0\\powershell.exe" } ).find { |exe| File.exists?(exe) } || 'powershell'
      end

    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
specinfra-2.0.0.beta32 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta31 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta30 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta29 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta28 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta27 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta26 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta25 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta23 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta22 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta21 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta20 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta19 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta18 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta17 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta16 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta15 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta14 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta13 lib/specinfra/backend/cmd.rb
specinfra-2.0.0.beta12 lib/specinfra/backend/cmd.rb