<commit_before># Adapter watch
#
# @param [Listen::Listener] listener the adapter listener
# @param [String] path the path to watch
#
def watch(listener, *paths)
  callback = lambda { |changed_dirs, options| @called = true; listener.on_change(changed_dirs, options) }
  @adapter = Listen::Adapter.select_and_initialize(paths, { :latency => test_latency }, &callback)
  @adapter.start(false)

  yield

  t = Thread.new { sleep(test_latency * 5); @adapter.stop }
  @adapter.wait_for_callback
ensure
  Thread.kill(t) if t
  @adapter.stop
end

shared_examples_for 'a filesystem adapter' do
  subject { described_class.new(File.dirname(__FILE__)) }

  describe '#start' do
    context 'with the blocking param set to true' do
      it 'blocks the current thread after starting the workers' do
        @called = false
        t = Thread.new { subject.start(true); @called = true }
        sleep test_latency
        Thread.kill(t) if t
        @called.should be_false
      end
    end

    context 'with the blocking param set to false' do
      it 'does not block the current thread after starting the workers' do
        @called = false
        subject.start(false)
        t = Thread.new { subject.start(false); @called = true }
        sleep test_latency
        Thread.kill(t) if t
        @called.should be_true
      end
    end
  end
end

shared_examples_for 'an adapter that call properly listener#on_change' do |*args|
  options = (args.first && args.first.is_a?(Hash)) ? args.first : {}
  let(:listener) { mock(Listen::Listener) }
  before { described_class.stub(:works?) { true } }

  context 'single file operations' do
    context 'when a file is created' do
      it 'detects the added file' do
        fixtures do |path|
          if options[:recursive]
            listener.should_receive(:on_change).once.with([path], :recursive => true)
          else<commit_msg>Close adapters after starting them in specs<commit_after># Adapter watch
#
# @param [Listen::Listener] listener the adapter listener
# @param [String] path the path to watch
#
def watch(listener, *paths)
  callback = lambda { |changed_dirs, options| @called = true; listener.on_change(changed_dirs, options) }
  @adapter = Listen::Adapter.select_and_initialize(paths, { :latency => test_latency }, &callback)
  @adapter.start(false)

  yield

  t = Thread.new { sleep(test_latency * 5); @adapter.stop }
  @adapter.wait_for_callback
ensure
  Thread.kill(t) if t
  @adapter.stop
end

shared_examples_for 'a filesystem adapter' do
  subject { described_class.new(File.dirname(__FILE__), &Proc.new {}) }

  describe '#start' do
     after { subject.stop }

    context 'with the blocking param set to true' do
      it 'blocks the current thread after starting the workers' do
        @called = false
        t = Thread.new { subject.start(true); @called = true }
        sleep test_latency
        Thread.kill(t) if t
        @called.should be_false
      end
    end

    context 'with the blocking param set to false' do
      it 'does not block the current thread after starting the workers' do
        @called = false
        t = Thread.new { subject.start(false); @called = true }
        sleep test_latency
        Thread.kill(t) if t
        @called.should be_true
      end
    end
  end
end

shared_examples_for 'an adapter that call properly listener#on_change' do |*args|
  options = (args.first && args.first.is_a?(Hash)) ? args.first : {}
  let(:listener) { mock(Listen::Listener) }
  before { described_class.stub(:works?) { true } }

  context 'single file operations' do
    context 'when a file is created' do
      it 'detects the added file' do
        fixtures do |path|
          if options[:recursive]
            listener.should_receive(:on_change).once.with([path], :recursive => true)
          else
{'fraction_non_alphanumeric': 0.10242232754081096, 'fraction_numerical': 0.000526592943654555, 'mean_word_length': 3.107027027027027, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1053962', 'n_tokens_mistral': 1142, 'n_tokens_neox': 1119, 'n_words': 359}