<commit_before>require "spec_helper"
require "tcr"
require "net/protocol"
require "net/http"
require "net/imap"
require "net/smtp"
require 'thread'
require "mail"


describe TCR do
  before(:each) do
    TCR.configuration.reset_defaults!
  end

  around(:each) do |example|
    File.unlink("test.json") if File.exists?("test.json")
    File.unlink("test.yaml") if File.exists?("test.yaml")
    example.run
    File.unlink("test.json") if File.exists?("test.json")
    File.unlink("test.yaml") if File.exists?("test.yaml")
  end

  describe ".configuration" do
     it "has a default cassette location configured" do
       TCR.configuration.cassette_library_dir.should == "fixtures/tcr_cassettes"
     end

     it "has an empty list of hook ports by default" do
       TCR.configuration.hook_tcp_ports.should == []
     end

     it "defaults to erroring on read/write mismatch access" do
       TCR.configuration.block_for_reads.should be_falsey
     end
   end

   describe ".configure" do
     it "configures cassette location" do
       expect {
         TCR.configure { |c| c.cassette_library_dir = "some/dir" }
       }.to change{ TCR.configuration.cassette_library_dir }.from("fixtures/tcr_cassettes").to("some/dir")
     end

     it "configures tcp ports to hook" do
       expect {
         TCR.configure { |c| c.hook_tcp_ports = [2525] }
       }.to change{ TCR.configuration.hook_tcp_ports }.from([]).to([2525])
     end

     it "configures allowing a blocking read mode" do
       expect {
         TCR.configure { |c| c.block_for_reads = true }
       }.to change{ TCR.configuration.block_for_reads }.from(false).to(true)
     end
   end

   it "raises an error if you connect to a hooked port without using a cassette" do
     TCR.configure { |c| c.hook_tcp_ports = [2525] }
     expect {
       tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
     }.to raise_error(TCR::NoCassetteError)
   end

  describe ".turned_off" do
    it "requires a block to call" do
      expect {
        TCR.turned_off
      }.to raise_error(ArgumentError)
    end

    it "disables hooks within the block" do
      TCR.configure { |c| c.hook_tcp_ports = [2525] }
      TCR.turned_off do
        TCR.configuration.hook_tcp_ports.should == []
      end
    end

    it "makes real TCPSocket.open calls even when hooks are setup" do
      TCR.configure { |c| c.hook_tcp_ports = [2525] }
      expect(TCPSocket).to receive(:real_open)
      TCR.turned_off do
        tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
      end
    end
  end

  describe "block_for_reads" do
    before(:each) {
      TCR.configure { |c|
        c.hook_tcp_ports = [9999]
        c.cassette_library_dir = '.'
      }
    }

    it "blocks read thread until data is available instead of raising mismatch error" do
      TCR.configure { |c| c.block_for_reads = true }
      reads = Queue.new

      TCR.use_cassette("spec/fixtures/block_for_reads") do
        sock = TCPSocket.open("google.com", 9999)

        t = Thread.new do
          reads << sock.gets
        end

        expect(reads.size).to eq(0)
        sock.print("hello\n")
        t.value
        expect(reads.size).to eq(1)
      end
    end

    context "when disabled" do
      it "raises mismatch error" do
        TCR.use_cassette("spec/fixtures/block_for_reads") do
          sock = TCPSocket.open("google.com", 9999)
          expect {
            Timeout::timeout(1) { sock.gets }
          }.to raise_error(TCR::DirectionMismatchError)
        end
      end
    end
  end

  describe ".use_cassette" do
    before(:each) {
      TCR.configure { |c|
        c.hook_tcp_ports = [2525]
        c.cassette_library_dir = "."
      }
    }

    it "requires a block to call" do
      expect {
        TCR.use_cassette("test")
      }.to raise_error(ArgumentError)
    end

    it "resets the cassette after use" do
      expect(TCR.cassette).to be_nil
      TCR.use_cassette("test") { }
      expect(TCR.cassette).to be_nil
    end

    it "resets the cassette after an error" do
      expect(TCR.cassette).to be_nil
      expect {
        TCR.use_cassette("test") { raise "Whoops!" }
      }.to raise_error("Whoops!")
      expect(TCR.cassette).to be_nil
    end

    it "creates a cassette file on use" do
      expect {
        TCR.use_cassette("test") do
          tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
        end
      }.to change{ File.exists?("./test.json") }.from(false).to(true)
    end

    it "creates a cassette file on use with yaml" do
      TCR.configure { |c| c.format = "yaml" }

      expect {
        TCR.use_cassette("test") do
          tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
        end
      }.to change{ File.exists?("./test.yaml") }.from(false).to(true)
    end

    it "records the tcp session data into the file" do
      TCR.use_cassette("test") do
        tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
        io = Net::InternetMessageIO.new(tcp_socket)
        line = io.readline
        tcp_socket.close
      end
      cassette_contents = File.open("test.json") { |f| f.read }
      cassette_contents.include?("220 smtp.mandrillapp.com ESMTP").should == true
    end

    it "plays back tcp sessions without opening a real connection" do
      expect(TCPSocket).to_not receive(:real_open)

      TCR.use_cassette("spec/fixtures/mandrill_smtp") do
        tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
        io = Net::InternetMessageIO.new(tcp_socket)
        line = io.readline.should include("220 smtp.mandrillapp.com ESMTP")
      end
    end

    it "raises an error if the recording gets out of order (i.e., we went to write but it expected a read)" do
      expect {
        TCR.use_cassette("spec/fixtures/mandrill_smtp") do
          tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
          io = Net::InternetMessageIO.new(tcp_socket)
          io.write("hi")
        end
      }.to raise_error(TCR::DirectionMismatchError)
    end
<commit_msg>spec yaml record<commit_after>require "spec_helper"
require "tcr"
require "net/protocol"
require "net/http"
require "net/imap"
require "net/smtp"
require 'thread'
require "mail"


describe TCR do
  before(:each) do
    TCR.configuration.reset_defaults!
  end

  around(:each) do |example|
    File.unlink("test.json") if File.exists?("test.json")
    File.unlink("test.yaml") if File.exists?("test.yaml")
    example.run
    File.unlink("test.json") if File.exists?("test.json")
    File.unlink("test.yaml") if File.exists?("test.yaml")
  end

  describe ".configuration" do
     it "has a default cassette location configured" do
       TCR.configuration.cassette_library_dir.should == "fixtures/tcr_cassettes"
     end

     it "has an empty list of hook ports by default" do
       TCR.configuration.hook_tcp_ports.should == []
     end

     it "defaults to erroring on read/write mismatch access" do
       TCR.configuration.block_for_reads.should be_falsey
     end
   end

   describe ".configure" do
     it "configures cassette location" do
       expect {
         TCR.configure { |c| c.cassette_library_dir = "some/dir" }
       }.to change{ TCR.configuration.cassette_library_dir }.from("fixtures/tcr_cassettes").to("some/dir")
     end

     it "configures tcp ports to hook" do
       expect {
         TCR.configure { |c| c.hook_tcp_ports = [2525] }
       }.to change{ TCR.configuration.hook_tcp_ports }.from([]).to([2525])
     end

     it "configures allowing a blocking read mode" do
       expect {
         TCR.configure { |c| c.block_for_reads = true }
       }.to change{ TCR.configuration.block_for_reads }.from(false).to(true)
     end
   end

   it "raises an error if you connect to a hooked port without using a cassette" do
     TCR.configure { |c| c.hook_tcp_ports = [2525] }
     expect {
       tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
     }.to raise_error(TCR::NoCassetteError)
   end

  describe ".turned_off" do
    it "requires a block to call" do
      expect {
        TCR.turned_off
      }.to raise_error(ArgumentError)
    end

    it "disables hooks within the block" do
      TCR.configure { |c| c.hook_tcp_ports = [2525] }
      TCR.turned_off do
        TCR.configuration.hook_tcp_ports.should == []
      end
    end

    it "makes real TCPSocket.open calls even when hooks are setup" do
      TCR.configure { |c| c.hook_tcp_ports = [2525] }
      expect(TCPSocket).to receive(:real_open)
      TCR.turned_off do
        tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
      end
    end
  end

  describe "block_for_reads" do
    before(:each) {
      TCR.configure { |c|
        c.hook_tcp_ports = [9999]
        c.cassette_library_dir = '.'
      }
    }

    it "blocks read thread until data is available instead of raising mismatch error" do
      TCR.configure { |c| c.block_for_reads = true }
      reads = Queue.new

      TCR.use_cassette("spec/fixtures/block_for_reads") do
        sock = TCPSocket.open("google.com", 9999)

        t = Thread.new do
          reads << sock.gets
        end

        expect(reads.size).to eq(0)
        sock.print("hello\n")
        t.value
        expect(reads.size).to eq(1)
      end
    end

    context "when disabled" do
      it "raises mismatch error" do
        TCR.use_cassette("spec/fixtures/block_for_reads") do
          sock = TCPSocket.open("google.com", 9999)
          expect {
            Timeout::timeout(1) { sock.gets }
          }.to raise_error(TCR::DirectionMismatchError)
        end
      end
    end
  end

  describe ".use_cassette" do
    before(:each) {
      TCR.configure { |c|
        c.hook_tcp_ports = [2525]
        c.cassette_library_dir = "."
      }
    }

    it "requires a block to call" do
      expect {
        TCR.use_cassette("test")
      }.to raise_error(ArgumentError)
    end

    it "resets the cassette after use" do
      expect(TCR.cassette).to be_nil
      TCR.use_cassette("test") { }
      expect(TCR.cassette).to be_nil
    end

    it "resets the cassette after an error" do
      expect(TCR.cassette).to be_nil
      expect {
        TCR.use_cassette("test") { raise "Whoops!" }
      }.to raise_error("Whoops!")
      expect(TCR.cassette).to be_nil
    end

    it "creates a cassette file on use" do
      expect {
        TCR.use_cassette("test") do
          tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
        end
      }.to change{ File.exists?("./test.json") }.from(false).to(true)
    end

    it "creates a cassette file on use with yaml" do
      TCR.configure { |c| c.format = "yaml" }

      expect {
        TCR.use_cassette("test") do
          tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
        end
      }.to change{ File.exists?("./test.yaml") }.from(false).to(true)
    end

    it "records the tcp session data into the file" do
      TCR.use_cassette("test") do
        tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
        io = Net::InternetMessageIO.new(tcp_socket)
        line = io.readline
        tcp_socket.close
      end
      cassette_contents = File.open("test.json") { |f| f.read }
      cassette_contents.include?("220 smtp.mandrillapp.com ESMTP").should == true
    end

    it "records the tcp session data into the yaml file" do
      TCR.configure { |c| c.format = "yaml" }

      TCR.use_cassette("test") do
        tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
        io = Net::InternetMessageIO.new(tcp_socket)
        line = io.readline
        tcp_socket.close
      end
      cassette_contents = File.open("test.yaml") { |f| f.read }
      cassette_contents.include?("---").should == true
      cassette_contents.include?("220 smtp.mandrillapp.com ESMTP").should == true
    end

    it "plays back tcp sessions without opening a real connection" do
      expect(TCPSocket).to_not receive(:real_open)

      TCR.use_cassette("spec/fixtures/mandrill_smtp") do
        tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
        io = Net::InternetMessageIO.new(tcp_socket)
        line = io.readline.should include("220 smtp.mandrillapp.com ESMTP")
      end
    end

    it "raises an error if the recording gets out of order (i.e., we went to write but it expected a read)" do
      expect {
        TCR.use_cassette("spec/fixtures/mandrill_smtp") do
          tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
          io = Net::InternetMessageIO.new(tcp_socket)
          io.write("hi")
        end
      }.to raise_error(TCR::DirectionMismatchError)
    end

{'fraction_non_alphanumeric': 0.11302895322939867, 'fraction_numerical': 0.01216990136811963, 'mean_word_length': 2.4770464601769913, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '607429', 'n_tokens_mistral': 4448, 'n_tokens_neox': 4146, 'n_words': 997}