<commit_before>require "neovim/plugin"
require "helper"
require "fileutils"

RSpec.describe Neovim do
  let(:nvim_exe) { ENV.fetch("NVIM_EXECUTABLE") }
  let(:nvim_argv) { %w(--headless -u NONE -i NONE -N -n) }

  describe ".attach_tcp" do
    it "attaches to a TCP socket" do
      port = Support.port
      env = {"NVIM_LISTEN_ADDRESS" => "0.0.0.0:#{port}"}
      pid = Process.spawn(env, nvim_exe, *nvim_argv, [:out, :err] => "/dev/null")

      begin
        wait_socket = TCPSocket.open("0.0.0.0", port)
      rescue Errno::ECONNREFUSED
        retry
      end
      wait_socket.close

      begin
        expect(Neovim.attach_tcp("0.0.0.0", port).strwidth("hi")).to eq(2)
      ensure
        Process.kill(:TERM, pid)
      end
    end
  end

  describe ".attach_unix" do
    it "attaches to a UNIX socket" do
      env = {"NVIM_LISTEN_ADDRESS" => Support.socket_path}
      pid = Process.spawn(env, nvim_exe, *nvim_argv, [:out, :err] => "/dev/null")

      loop { break if File.socket?(Support.socket_path) }

      begin
        expect(Neovim.attach_unix(Support.socket_path).strwidth("hi")).to eq(2)
      ensure
        Process.kill(:TERM, pid)
      end
    end
  end

  describe ".attach_child" do
    it "spawns and attaches to a child process" do
      nvim = Neovim.attach_child(nvim_argv)
      expect(nvim.strwidth("hi")).to eq(2)
    end
  end

  describe ".plugin" do
    it "registers the plugin to Neovim.__configured_plugin_manifest" do
      mock_manifest = double(:manifest)
      mock_plugin = double(:plugin)
      mock_path = "/source/path"

      allow(Neovim).to receive(:__configured_plugin_path).and_return(mock_path)
      allow(Neovim).to receive(:__configured_plugin_manifest).and_return(mock_manifest)

      expect(Neovim::Plugin).to receive(:from_config_block).with(mock_path).and_return(mock_plugin)
      expect(mock_manifest).to receive(:register).with(mock_plugin)

      Neovim.plugin
    end
  end<commit_msg>More UNIX socket waiting, cleanup<commit_after>require "neovim/plugin"
require "helper"
require "fileutils"

RSpec.describe Neovim do
  let(:nvim_exe) { ENV.fetch("NVIM_EXECUTABLE") }
  let(:nvim_argv) { %w(--headless -u NONE -i NONE -N -n) }

  describe ".attach_tcp" do
    it "attaches to a TCP socket" do
      port = Support.port
      env = {"NVIM_LISTEN_ADDRESS" => "0.0.0.0:#{port}"}
      pid = Process.spawn(env, nvim_exe, *nvim_argv, [:out, :err] => "/dev/null")

      begin
        TCPSocket.open("0.0.0.0", port).close
      rescue Errno::ECONNREFUSED
        retry
      end

      begin
        expect(Neovim.attach_tcp("0.0.0.0", port).strwidth("hi")).to eq(2)
      ensure
        Process.kill(:TERM, pid)
      end
    end
  end

  describe ".attach_unix" do
    it "attaches to a UNIX socket" do
      socket_path = Support.socket_path
      env = {"NVIM_LISTEN_ADDRESS" => socket_path}
      pid = Process.spawn(env, nvim_exe, *nvim_argv, [:out, :err] => "/dev/null")

      begin
        UNIXSocket.new(socket_path).close
      rescue Errno::ENOENT, Errno::ECONNREFUSED
        retry
      end

      begin
        expect(Neovim.attach_unix(socket_path).strwidth("hi")).to eq(2)
      ensure
        Process.kill(:TERM, pid)
        Process.waitpid(pid)
      end
    end
  end

  describe ".attach_child" do
    it "spawns and attaches to a child process" do
      nvim = Neovim.attach_child(nvim_argv)
      expect(nvim.strwidth("hi")).to eq(2)
    end
  end

  describe ".plugin" do
    it "registers the plugin to Neovim.__configured_plugin_manifest" do
      mock_manifest = double(:manifest)
      mock_plugin = double(:plugin)
      mock_path = "/source/path"

      allow(Neovim).to receive(:__configured_plugin_path).and_return(mock_path)
      allow(Neovim).to receive(:__configured_plugin_manifest).and_return(mock_manifest)

      expect(Neovim::Plugin).to receive(:from_config_block).with(mock_path).and_return(mock_plugin)
      expect(mock_manifest).to receive(:register).with(mock_plugin)

      Neovim.plugin
    end
  end
{'fraction_non_alphanumeric': 0.12683777722402192, 'fraction_numerical': 0.0074757039621231, 'mean_word_length': 2.970326409495549, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '2534919', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1409, 'n_words': 283}