aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/file_update_checker_test.rb
blob: baf29cc33759721b283557161447a95b77632041 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'abstract_unit'
require 'test/unit'
require 'active_support'
require 'fileutils'

MTIME_FIXTURES_PATH = File.expand_path("../fixtures", __FILE__)

class FileUpdateCheckerTest < Test::Unit::TestCase
  FILES = %w(1.txt 2.txt 3.txt)

  def setup
    FileUtils.touch(FILES)
  end

  def teardown
    FileUtils.rm(FILES)
  end

  def test_should_not_execute_the_block_if_no_paths_are_given
    i = 0
    checker = ActiveSupport::FileUpdateChecker.new([]){ i += 1 }
    checker.execute_if_updated
    assert_equal 0, i
  end

  def test_should_invoke_the_block_on_first_call_if_it_does_not_calculate_last_updated_at_on_load
    i = 0
    checker = ActiveSupport::FileUpdateChecker.new(FILES){ i += 1 }
    checker.execute_if_updated
    assert_equal 1, i
  end

  def test_should_not_invoke_the_block_on_first_call_if_it_calculates_last_updated_at_on_load
    i = 0
    checker = ActiveSupport::FileUpdateChecker.new(FILES, true){ i += 1 }
    checker.execute_if_updated
    assert_equal 0, i
  end

  def test_should_not_invoke_the_block_if_no_file_has_changed
    i = 0
    checker = ActiveSupport::FileUpdateChecker.new(FILES){ i += 1 }
    5.times { checker.execute_if_updated }
    assert_equal 1, i
  end

  def test_should_invoke_the_block_if_a_file_has_changed
    i = 0
    checker = ActiveSupport::FileUpdateChecker.new(FILES){ i += 1 }
    checker.execute_if_updated
    sleep(1)
    FileUtils.touch(FILES)
    checker.execute_if_updated
    assert_equal 2, i
  end
end