<commit_before>require 'singleton'

module Serverspec
  module Backend
    class Exec < Base

      def run_command(cmd, opts={})
        cmd = build_command(cmd)
        cmd = add_pre_command(cmd)
        stdout = `#{build_command(cmd)} 2>&1`
        # In ruby 1.9, it is possible to use Open3.capture3, but not in 1.8
        #stdout, stderr, status = Open3.capture3(cmd)

        if @example
          @example.metadata[:command] = cmd
          @example.metadata[:stdout]  = stdout
        end

        { :stdout => stdout, :stderr => nil,
          :exit_status => $?.exitstatus, :exit_signal => nil }
      end

      def build_command(cmd)
        path = Serverspec.configuration.path || RSpec.configuration.path
        if path
          cmd = "env PATH=#{path}:$PATH #{cmd}"<commit_msg>[Backend::Exec] ensure no Ruby envvars are present when running command.<commit_after>require 'singleton'

module Serverspec
  module Backend
    class Exec < Base

      def run_command(cmd, opts={})
        cmd = build_command(cmd)
        cmd = add_pre_command(cmd)
        stdout = run_with_no_ruby_environment do
          `#{build_command(cmd)} 2>&1`
        end
        # In ruby 1.9, it is possible to use Open3.capture3, but not in 1.8
        #stdout, stderr, status = Open3.capture3(cmd)

        if @example
          @example.metadata[:command] = cmd
          @example.metadata[:stdout]  = stdout
        end

        { :stdout => stdout, :stderr => nil,
          :exit_status => $?.exitstatus, :exit_signal => nil }
      end

      def run_with_no_ruby_environment
        keys = %w[BUNDLER_EDITOR BUNDLE_BIN_PATH BUNDLE_GEMFILE
          RUBYOPT GEM_HOME GEM_PATH GEM_CACHE]

        keys.each { |key| ENV["_SERVERSPEC_#{key}"] = ENV[key] ; ENV.delete(key) }
        yield
      ensure
        keys.each { |key| ENV[key] = ENV.delete("_SERVERSPEC_#{key}") }
      end

      def build_command(cmd)
        path = Serverspec.configuration.path || RSpec.configuration.path
        if path
          cmd = "env PATH=#{path}:$PATH #{cmd}"
{'fraction_non_alphanumeric': 0.11431362970200293, 'fraction_numerical': 0.009770395701025891, 'mean_word_length': 2.3684210526315788, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1184864', 'n_tokens_mistral': 667, 'n_tokens_neox': 646, 'n_words': 159}