<commit_before>require 'shellwords'
require 'fileutils'

describe 'bin/htmlbeautifier' do
  def path_to(*partial)
    File.join(File.expand_path('../..', __FILE__), *partial)
  end

  def command
    'ruby -I%s %s' % [
      escape(path_to('lib')),
      escape(path_to('bin', 'htmlbeautifier'))
    ]
  end

  def escape(s)
    Shellwords.escape(s)
  end

  it 'beautifies a file in place' do
    FileUtils.mkdir_p path_to('tmp')
    input = "<p>\nfoo\n</p>"
    expected = "<p>\n  foo\n</p>\n"
    path = path_to('tmp', 'in-place.html')
    File.open(path, 'w') do |f|
      f.write input
    end

    system '%s %s' % [command, escape(path)]

    output = File.read(path)
    expect(output).to eq(expected)
  end

  it 'beautifies a file from stdin to stdout' do
    FileUtils.mkdir_p path_to('tmp')
    input = "<p>\nfoo\n</p>"
    expected = "<p>\n  foo\n</p>\n"
    in_path = path_to('tmp', 'input.html')
    out_path = path_to('tmp', 'output.html')
    File.open(in_path, 'w') do |f|
      f.write input
    end

    system '%s < %s > %s' % [command, escape(in_path), escape(out_path)]

    output = File.read(out_path)
    expect(output).to eq(expected)
  end

  it 'allows a configurable number of tab stops' do
    FileUtils.mkdir_p path_to('tmp')
    input = "<p>\nfoo\n</p>"
    expected = "<p>\n   foo\n</p>\n"
    path = path_to('tmp', 'in-place.html')
    File.open(path, 'w') do |f|
      f.write input
    end

    system '%s --tab-stops=3 %s' % [command, escape(path)]

    output = File.read(path)
    expect(output).to eq(expected)
  end
end<commit_msg>Remove duplication in executable spec<commit_after>require 'shellwords'
require 'fileutils'

describe 'bin/htmlbeautifier' do
  before do
    FileUtils.mkdir_p path_to('tmp')
  end

  def write(path, content)
    File.open(path, 'w') do |f|
      f.write content
    end
  end

  def read(path)
    File.read(path)
  end

  def path_to(*partial)
    File.join(File.expand_path('../..', __FILE__), *partial)
  end

  def command
    'ruby -I%s %s' % [
      escape(path_to('lib')),
      escape(path_to('bin', 'htmlbeautifier'))
    ]
  end

  def escape(s)
    Shellwords.escape(s)
  end

  it 'beautifies a file in place' do
    input = "<p>\nfoo\n</p>"
    expected = "<p>\n  foo\n</p>\n"
    path = path_to('tmp', 'in-place.html')
    write path, input

    system '%s %s' % [command, escape(path)]

    expect(read(path)).to eq(expected)
  end

  it 'beautifies a file from stdin to stdout' do
    input = "<p>\nfoo\n</p>"
    expected = "<p>\n  foo\n</p>\n"
    in_path = path_to('tmp', 'input.html')
    out_path = path_to('tmp', 'output.html')
    write in_path, input

    system '%s < %s > %s' % [command, escape(in_path), escape(out_path)]

    expect(read(out_path)).to eq(expected)
  end

  it 'allows a configurable number of tab stops' do
    input = "<p>\nfoo\n</p>"
    expected = "<p>\n   foo\n</p>\n"
    path = path_to('tmp', 'in-place.html')
    write path, input

    system '%s --tab-stops=3 %s' % [command, escape(path)]

    expect(read(path)).to eq(expected)
  end
end
{'fraction_non_alphanumeric': 0.17123287671232876, 'fraction_numerical': 0.0006523157208088715, 'mean_word_length': 2.848180677540778, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '5272307', 'n_tokens_mistral': 1193, 'n_tokens_neox': 1155, 'n_words': 277}