<commit_before>require 'stringio'
require 'cgi'
require 'shellwords'
require 'open3'
require 'yaml'

module RSpec
  module Mate
    class Runner
      LAST_REMEMBERED_FILE_CACHE = "/tmp/textmate_rspec_last_remembered_file_cache.txt"
      LAST_RUN_CACHE             = "/tmp/textmate_rspec_last_run.yml"
      
      def run_files(options={})
        files = ENV['TM_SELECTED_FILES'] ? Shellwords.shellwords(ENV['TM_SELECTED_FILES']) : ["spec/"]
        options.merge!({:files => files})
        run(options)
      end

      def run_file(options={})
        options.merge!({:files => [single_file]})
        run(options)
      end

      def run_last_remembered_file(options={})
        options.merge!({:files => [last_remembered_single_file]})
        run(options)
      end

      def run_again
        run(:run_again => true)
      end
      
      def run_focussed(options={})
        options.merge!(
          {
            :files => [single_file],
            :line  => ENV['TM_LINE_NUMBER']
          }
        )

        run(options)
      end

      def run(options)
        if options.delete(:run_again)
          argv = load_argv_from_last_run
        else
          argv = build_argv_from_options(options)
          save_as_last_run(argv)
        end
        run_rspec(argv)
      end
      
      def run_rspec(argv)
        stderr     = StringIO.new
        old_stderr = $stderr
        $stderr    = stderr

        Dir.chdir(project_directory) do
          cmd = 
            if use_binstub?
              %w(bin/rspec) + argv
            elsif gemfile?
              %w(bundle exec rspec) + argv
            else
              %w(rspec) + argv
            end
          Open3.popen3(*cmd) do |i, out, err, thread|
            stderr_thread = Thread.new do
              while (line = err.gets) do
                stderr.puts line
              end
            end
            while (line = out.gets) do
              $stdout.puts line
              $stdout.flush<commit_msg>Make sure stdin is closed when running rspec.<commit_after>require 'stringio'
require 'cgi'
require 'shellwords'
require 'open3'
require 'yaml'

module RSpec
  module Mate
    class Runner
      LAST_REMEMBERED_FILE_CACHE = "/tmp/textmate_rspec_last_remembered_file_cache.txt"
      LAST_RUN_CACHE             = "/tmp/textmate_rspec_last_run.yml"
      
      def run_files(options={})
        files = ENV['TM_SELECTED_FILES'] ? Shellwords.shellwords(ENV['TM_SELECTED_FILES']) : ["spec/"]
        options.merge!({:files => files})
        run(options)
      end

      def run_file(options={})
        options.merge!({:files => [single_file]})
        run(options)
      end

      def run_last_remembered_file(options={})
        options.merge!({:files => [last_remembered_single_file]})
        run(options)
      end

      def run_again
        run(:run_again => true)
      end
      
      def run_focussed(options={})
        options.merge!(
          {
            :files => [single_file],
            :line  => ENV['TM_LINE_NUMBER']
          }
        )

        run(options)
      end

      def run(options)
        if options.delete(:run_again)
          argv = load_argv_from_last_run
        else
          argv = build_argv_from_options(options)
          save_as_last_run(argv)
        end
        run_rspec(argv)
      end
      
      def run_rspec(argv)
        stderr     = StringIO.new
        old_stderr = $stderr
        $stderr    = stderr

        Dir.chdir(project_directory) do
          cmd = 
            if use_binstub?
              %w(bin/rspec) + argv
            elsif gemfile?
              %w(bundle exec rspec) + argv
            else
              %w(rspec) + argv
            end
          Open3.popen3(*cmd) do |i, out, err, thread|
            i.close
            stderr_thread = Thread.new do
              while (line = err.gets) do
                stderr.puts line
              end
            end
            while (line = out.gets) do
              $stdout.puts line
              $stdout.flush
{'fraction_non_alphanumeric': 0.09573412698412699, 'fraction_numerical': 0.001488095238095238, 'mean_word_length': 1.6307893020221786, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '2991807', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1220, 'n_words': 243}