<commit_before># frozen_string_literal: true

require "tmpdir"
require "pathname"

RSpec.describe TTY::Pager::SystemPager do
  around :each do |example|
    Dir.mktmpdir do |dir|
      FileUtils.cd(dir) do
        example.run
      end
    end
  end

  it "paginates some text" do
    output_path = Pathname.new("output.txt")
    pager_command = "ruby #{fixtures_path("cat.rb")} > #{output_path}"
    described_class.page("Some text", command: pager_command)

    expect(output_path.read).to eq("Some text")
  end

  it "paginates text from a file path" do
    file_path = fixtures_path("copy.txt")
    output_path = Pathname.new("output.txt")
    pager_command = "ruby #{fixtures_path("cat.rb")} > #{output_path}"

    described_class.page(path: file_path, command: pager_command)

    expect(output_path.read).to eq("one\ntwo\nthree\n")
  end

  it "allows pagination to happen asynchronously" do
    output_path = Pathname.new("output.txt")
    pager_command = "ruby #{fixtures_path("external_pager.rb")} #{output_path}"

    described_class.page(command: pager_command) do |pager|
      pager.puts("one")
      pager.write("two\n")
      pager.try_write("three\n")
    end
<commit_msg>Change to replace FileUtils with Dir to change the current directory<commit_after># frozen_string_literal: true

require "tmpdir"
require "pathname"

RSpec.describe TTY::Pager::SystemPager do
  around :each do |example|
    ::Dir.mktmpdir do |dir|
      ::Dir.chdir(dir, &example)
    end
  end

  it "paginates some text" do
    output_path = Pathname.new("output.txt")
    pager_command = "ruby #{fixtures_path("cat.rb")} > #{output_path}"
    described_class.page("Some text", command: pager_command)

    expect(output_path.read).to eq("Some text")
  end

  it "paginates text from a file path" do
    file_path = fixtures_path("copy.txt")
    output_path = Pathname.new("output.txt")
    pager_command = "ruby #{fixtures_path("cat.rb")} > #{output_path}"

    described_class.page(path: file_path, command: pager_command)

    expect(output_path.read).to eq("one\ntwo\nthree\n")
  end

  it "allows pagination to happen asynchronously" do
    output_path = Pathname.new("output.txt")
    pager_command = "ruby #{fixtures_path("external_pager.rb")} #{output_path}"

    described_class.page(command: pager_command) do |pager|
      pager.puts("one")
      pager.write("two\n")
      pager.try_write("three\n")
    end

{'fraction_non_alphanumeric': 0.13148071339693074, 'fraction_numerical': 0.0, 'mean_word_length': 3.9834710743801653, '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': '1441876', 'n_tokens_mistral': 850, 'n_tokens_neox': 821, 'n_words': 180}