<commit_before>module Specjour

  class Printer
    include Protocol
    RANDOM_PORT = 0

    attr_reader :port, :clients
    attr_accessor :tests_to_run, :example_size, :examples_complete, :profiler

    def initialize
      @host = "0.0.0.0"
      @server_socket = TCPServer.new(@host, RANDOM_PORT)
      @port = @server_socket.addr[1]
      @profiler = {}
      @clients = {}
      @tests_to_run = []
      @example_size = 0
      self.examples_complete = 0
    end

    def start
      fds = [@server_socket]
      catch(:stop) do
        while true
          reads = select(fds).first
          reads.each do |socket_being_read|
            if socket_being_read == @server_socket
              client_socket = @server_socket.accept
              fds << client_socket
              clients[client_socket] = Connection.wrap(client_socket)
            elsif socket_being_read.eof?
              socket_being_read.close
              fds.delete(socket_being_read)
              clients.delete(socket_being_read)
              disconnecting
            else
              serve(clients[socket_being_read])
            end
          end
        end
      end
    ensure
      stopping
      fds.each {|c| c.close}
    end

    def exit_status
      reporters.all? {|r| r.exit_status == true}
    end

    protected

    def serve(client)
      data = load_object(client.gets(TERMINATOR))
      case data
      when String
        $stdout.print data
        $stdout.flush
      when Array
        send data.first, *(data[1..-1].unshift(client))
      end
    end

    def ready(client)
      client.print tests_to_run.shift
      client.flush
    end

    def done(client)
      self.examples_complete += 1
    end

    def tests=(client, tests)
      self.tests_to_run = run_order(tests_to_run | tests)
      self.example_size = tests_to_run.size
    end

    def rspec_summary=(client, summary)
      rspec_report.add(summary)
    end

    def cucumber_summary=(client, summary)
      cucumber_report.add(summary)
    end

    def add_to_profiler(client, args)
      test, time = *args
      self.profiler[test] = time
    end

    def disconnecting
      if (examples_complete == example_size || clients.empty?)
        throw(:stop)
      end
    end

    def run_order(tests)
      if File.exist?('.specjour/performance')
        ordered_tests = File.readlines('.specjour/performance').map {|l| l.chop.split(':')[1]}
        (tests - ordered_tests) | (ordered_tests & tests)
      else
        tests
      end
    end

    def rspec_report
      @rspec_report ||= RSpec::FinalReport.new
    end

    def cucumber_report
      @cucumber_report ||= Cucumber::FinalReport.new
    end

    def record_performance
      File.open('.specjour/performance', 'w') do |file|
        ordered_specs = profiler.to_a.sort_by {|a| -a[1].to_f}.map do |test, time|
          file.puts "%6f:%s" % [time, test]
        end
      end
    end

    def reporters
      [@rspec_report, @cucumber_report].compact
    end
<commit_msg>Adjust #run_order for specs with line numbers<commit_after>module Specjour

  class Printer
    include Protocol
    RANDOM_PORT = 0

    attr_reader :port, :clients
    attr_accessor :tests_to_run, :example_size, :examples_complete, :profiler

    def initialize
      @host = "0.0.0.0"
      @server_socket = TCPServer.new(@host, RANDOM_PORT)
      @port = @server_socket.addr[1]
      @profiler = {}
      @clients = {}
      @tests_to_run = []
      @example_size = 0
      self.examples_complete = 0
    end

    def start
      fds = [@server_socket]
      catch(:stop) do
        while true
          reads = select(fds).first
          reads.each do |socket_being_read|
            if socket_being_read == @server_socket
              client_socket = @server_socket.accept
              fds << client_socket
              clients[client_socket] = Connection.wrap(client_socket)
            elsif socket_being_read.eof?
              socket_being_read.close
              fds.delete(socket_being_read)
              clients.delete(socket_being_read)
              disconnecting
            else
              serve(clients[socket_being_read])
            end
          end
        end
      end
    ensure
      stopping
      fds.each {|c| c.close}
    end

    def exit_status
      reporters.all? {|r| r.exit_status == true}
    end

    protected

    def serve(client)
      data = load_object(client.gets(TERMINATOR))
      case data
      when String
        $stdout.print data
        $stdout.flush
      when Array
        send data.first, *(data[1..-1].unshift(client))
      end
    end

    def ready(client)
      client.print tests_to_run.shift
      client.flush
    end

    def done(client)
      self.examples_complete += 1
    end

    def tests=(client, tests)
      self.tests_to_run = run_order(tests_to_run | tests)
      self.example_size = tests_to_run.size
    end

    def rspec_summary=(client, summary)
      rspec_report.add(summary)
    end

    def cucumber_summary=(client, summary)
      cucumber_report.add(summary)
    end

    def add_to_profiler(client, args)
      test, time = *args
      self.profiler[test] = time
    end

    def disconnecting
      if (examples_complete == example_size || clients.empty?)
        throw(:stop)
      end
    end

    def run_order(tests)
      if File.exist?('.specjour/performance')
        ordered_tests = File.readlines('.specjour/performance').map {|l| l.chop.split(':', 2)[1]}
        (tests - ordered_tests) | (ordered_tests & tests)
      else
        tests
      end
    end

    def rspec_report
      @rspec_report ||= RSpec::FinalReport.new
    end

    def cucumber_report
      @cucumber_report ||= Cucumber::FinalReport.new
    end

    def record_performance
      File.open('.specjour/performance', 'w') do |file|
        ordered_specs = profiler.to_a.sort_by {|a| -a[1].to_f}.map do |test, time|
          file.puts "%6f:%s" % [time, test]
        end
      end
    end

    def reporters
      [@rspec_report, @cucumber_report].compact
    end

{'fraction_non_alphanumeric': 0.0941059930658742, 'fraction_numerical': 0.004787848770018161, 'mean_word_length': 2.0720081135902637, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6924222', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1896, 'n_words': 403}