<commit_before>require 'test_helper'

module Wolfe

  class CleanupTest < Minitest::Test

    #
    # validations
    #

    def test_configuration_must_be_a_hash
      assert_raises ArgumentError do
        Cleanup.new("wat")
      end
    end

    def test_configuration_must_include_valid_paths
      assert_raises ArgumentError do
        Cleanup.new(configuration("/not/really/a/path/", "test_backup-%{year}-%{month}-%{day}-%{hour}", "3.days", "1.days"))
      end
    end

    def test_configuration_must_include_all_keys
      assert_raises ArgumentError do
        Cleanup.new('test_backup' => { 'path' => test_directory, 'one_per_day_timespan' => "30.days", 'one_per_month_timespan' => "1.year" })
      end
    end

    def test_configuration_must_have_valid_timespans
      assert_raises ArgumentError do
        Cleanup.new(configuration(test_directory, "test_backup-%{year}-%{month}-%{day}-%{hour}", "5.days", "Dir.exist?('/tmp')" ) )
      end
    end

    #
    # actual cleanup
    #

    def test_start_should_correctly_delete_daily_backups
      Date.today.downto(15.days.ago.to_date) do |date|
        FileUtils.touch "#{test_directory}/#{backup_filename(date)}"
      end
      cleanup = Cleanup.new(configuration(test_directory, "test_backup-%{year}-%{month}-%{day}-%{hour}", "3.days", "1.year"))
      cleanup.start

      if ( Date.today - 15.days ).month != Date.today.month
        assert_equal 6, Dir.entries(test_directory).count
        assert File.exist?("#{test_directory}/#{backup_filename( (Date.today - 15.days).end_of_month )}")
      else
        assert_equal 5, Dir.entries(test_directory).count
      end

      assert File.exist?("#{test_directory}/#{backup_filename(Date.today)}")
      assert File.exist?("#{test_directory}/#{backup_filename(Date.today - 1.day)}")
      assert File.exist?("#{test_directory}/#{backup_filename(Date.today - 2.days)}")
    end

    def test_start_correctly_delete_monthly_backups
      Date.today.downto(6.months.ago.to_date) do |date|
        FileUtils.touch "#{test_directory}/#{backup_filename(date)}"
      end
      cleanup = Cleanup.new(configuration(test_directory, "test_backup-%{year}-%{month}-%{day}-%{hour}", "1.days", "3.months"))
      cleanup.start

      assert_equal 5, Dir.entries(test_directory).count
      assert File.exist?("#{test_directory}/#{backup_filename(Date.today)}")
      assert File.exist?("#{test_directory}/#{backup_filename(1.month.ago.end_of_month.to_date)}")
      assert File.exist?("#{test_directory}/#{backup_filename(2.months.ago.end_of_month.to_date)}")
    end

    #
    # setup/teardown
    #

    def setup
      Dir.mkdir(test_directory) unless Dir.exist?(test_directory)
    end

    def teardown
      FileUtils.remove_dir(test_directory, true) if Dir.exist?(test_directory)
    end

    private

      def test_directory
        File.join( File.expand_path(File.dirname(__FILE__)), '..', 'tmp' )<commit_msg>DRY: moved testfile creation in a method<commit_after>require 'test_helper'

module Wolfe

  class CleanupTest < Minitest::Test

    #
    # validations
    #

    def test_configuration_must_be_a_hash
      assert_raises ArgumentError do
        Cleanup.new("wat")
      end
    end

    def test_configuration_must_include_valid_paths
      assert_raises ArgumentError do
        Cleanup.new(configuration("/not/really/a/path/", "test_backup-%{year}-%{month}-%{day}-%{hour}", "3.days", "1.days"))
      end
    end

    def test_configuration_must_include_all_keys
      assert_raises ArgumentError do
        Cleanup.new('test_backup' => { 'path' => test_directory, 'one_per_day_timespan' => "30.days", 'one_per_month_timespan' => "1.year" })
      end
    end

    def test_configuration_must_have_valid_timespans
      assert_raises ArgumentError do
        Cleanup.new(configuration(test_directory, "test_backup-%{year}-%{month}-%{day}-%{hour}", "5.days", "Dir.exist?('/tmp')" ) )
      end
    end

    #
    # actual cleanup
    #

    def test_start_should_correctly_delete_daily_backups
      create_test_files_downto 15.days.ago
      cleanup = Cleanup.new(configuration(test_directory, "test_backup-%{year}-%{month}-%{day}-%{hour}", "3.days", "1.year"))
      cleanup.start

      if ( Date.today - 15.days ).month != Date.today.month
        assert_equal 6, Dir.entries(test_directory).count
        assert File.exist?("#{test_directory}/#{backup_filename( (Date.today - 15.days).end_of_month )}")
      else
        assert_equal 5, Dir.entries(test_directory).count
      end

      assert File.exist?("#{test_directory}/#{backup_filename(Date.today)}")
      assert File.exist?("#{test_directory}/#{backup_filename(Date.today - 1.day)}")
      assert File.exist?("#{test_directory}/#{backup_filename(Date.today - 2.days)}")
    end

    def test_start_should_correctly_delete_monthly_backups
      create_test_files_downto 6.months.ago
      cleanup = Cleanup.new(configuration(test_directory, "test_backup-%{year}-%{month}-%{day}-%{hour}", "1.days", "3.months"))
      cleanup.start

      assert_equal 5, Dir.entries(test_directory).count
      assert File.exist?("#{test_directory}/#{backup_filename(Date.today)}")
      assert File.exist?("#{test_directory}/#{backup_filename(1.month.ago.end_of_month.to_date)}")
      assert File.exist?("#{test_directory}/#{backup_filename(2.months.ago.end_of_month.to_date)}")
    end

    #
    # setup/teardown
    #

    def setup
      Dir.mkdir(test_directory) unless Dir.exist?(test_directory)
    end

    def teardown
      FileUtils.remove_dir(test_directory, true) if Dir.exist?(test_directory)
    end

    private

      def create_test_files_downto datetime
        Date.today.downto(datetime.to_date) do |date|
          FileUtils.touch "#{test_directory}/#{backup_filename(date)}"
        end
      end

      def test_directory
        File.join( File.expand_path(File.dirname(__FILE__)), '..', 'tmp' )
{'fraction_non_alphanumeric': 0.14029142663503896, 'fraction_numerical': 0.008132836326668925, 'mean_word_length': 3.4618291761148905, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '3714153', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1927, 'n_words': 284}