<commit_before>require_relative 'helper'

class TestCron < TestWithBasicSetup
  @@command_start  = 'start'
  @@command_end    = 'end'
  @@command_patrol = 'patrol'

  @@command_sweep_indeed = 'sweep indeed'
  @@command_burn_indeed  = 'burn indeed'

  def register_with_cron!
    exec(@@command_start)
    raise 'Failed to register with "cron"' if $? != 0
  end

  def unregister_from_cron!
    exec(@@command_end)
    raise 'Failed to unregister from "cron"' if $? != 0
  end

  def get_cron_contents
    `crontab -l`
  end

  def confirm_registered_line(contents, pwd)
    [@@command_sweep_indeed, @@command_burn_indeed].each do |command|
      r = /cd #{pwd} && #{BIN} #{command}\n/
      assert r.match(contents)
    end
  end

  def test_basic_cron_setup
    original = get_cron_contents

    register_with_cron!
    contents = get_cron_contents
    assert_true contents.include?("#{BIN} #{@@command_patrol}")
    confirm_registered_line(contents, PWD)

    unregister_from_cron!
    assert_equal original, get_cron_contents
  end

  def test_patrol_should_delete_invalid_lines
    original = get_cron_contents

    dir_name = 'new_dir'
    Dir.mkdir(dir_name)
    Dir.chdir(dir_name)
    initialize_kic!
    register_with_cron!
    confirm_registered_line(get_cron_contents, Dir.pwd)

    Dir.chdir(PWD)
    FileUtils.remove_entry(dir_name)

    exec(@@command_patrol)
    assert_equal original, get_cron_contents
  end
end<commit_msg>[Test] Enhance test for "patrol"<commit_after>require_relative 'helper'

class TestCron < TestWithBasicSetup
  @@command_start  = 'start'
  @@command_end    = 'end'
  @@command_patrol = 'patrol'

  @@command_sweep_indeed = 'sweep indeed'
  @@command_burn_indeed  = 'burn indeed'

  def register_with_cron!
    exec(@@command_start)
    raise 'Failed to register with "cron"' if $? != 0
  end

  def unregister_from_cron!
    exec(@@command_end)
    raise 'Failed to unregister from "cron"' if $? != 0
  end

  def get_cron_contents
    `crontab -l`
  end

  def confirm_registered_line(contents, pwd)
    [@@command_sweep_indeed, @@command_burn_indeed].each do |command|
      r = /cd #{pwd} && #{BIN} #{command}\n/
      assert r.match(contents)
    end
  end

  def leave_invalid_commands_in_cron
    dir_name = 'new_dir'

    Dir.mkdir(dir_name)
    Dir.chdir(dir_name)
    initialize_kic!
    register_with_cron!
    confirm_registered_line(get_cron_contents, Dir.pwd)

    Dir.chdir(PWD)
    FileUtils.remove_entry(dir_name)
  end

  def test_basic_cron_setup
    original = get_cron_contents

    register_with_cron!
    contents = get_cron_contents
    assert_true contents.include?("#{BIN} #{@@command_patrol}")
    confirm_registered_line(contents, PWD)

    unregister_from_cron!
    assert_equal original, get_cron_contents
  end

  def test_patrol_should_delete_invalid_lines
    original = get_cron_contents

    leave_invalid_commands_in_cron

    exec(@@command_patrol)
    assert_equal original, get_cron_contents
  end

  def test_patrol_should_delete_invalid_lines_without_deleting_valid_lines
    register_with_cron!
    correct = get_cron_contents
    unregister_from_cron!

    leave_invalid_commands_in_cron

    register_with_cron!
    exec(@@command_patrol)
    assert_equal correct, get_cron_contents
  end
end
{'fraction_non_alphanumeric': 0.08946564885496183, 'fraction_numerical': 0.0012213740458015267, 'mean_word_length': 4.055555555555555, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4193609', 'n_tokens_mistral': 1218, 'n_tokens_neox': 1154, 'n_words': 218}