File: file_cache_map_test.rb

package info (click to toggle)
ruby-rgen 0.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,428 kB
  • sloc: ruby: 11,344; xml: 1,368; yacc: 72; makefile: 10
file content (99 lines) | stat: -rw-r--r-- 3,257 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
$:.unshift(File.dirname(__FILE__)+"/../../lib")

require 'minitest/autorun'
require 'fileutils'
require 'rgen/util/file_cache_map'

class FileCacheMapTest < Minitest::Test

  TestDir = File.dirname(__FILE__)+"/file_cache_map_test/testdir"
 
  def setup
    FileUtils.rm_r(Dir[TestDir+"/*"])
    # * doesn't include dot files
    FileUtils.rm_r(Dir[TestDir+"/.cache"])
    @cm = RGen::Util::FileCacheMap.new(".cache", ".test")
  end
   
  def test_nocache
    reasons = []
    assert_equal(:invalid, @cm.load_data(TestDir+"/fileA", :invalidation_reasons => reasons))
    assert_equal [:no_cachefile], reasons
  end

  def test_storeload
    keyFile = TestDir+"/fileA"  
    File.open(keyFile, "w") {|f| f.write("somedata")}
    @cm.store_data(keyFile, "valuedata")
    assert(File.exist?(TestDir+"/.cache/fileA.test"))
    assert_equal("valuedata", @cm.load_data(keyFile))
  end

  def test_storeload_subdir
    keyFile = TestDir+"/subdir/fileA"
    FileUtils.mkdir(TestDir+"/subdir")
    File.open(keyFile, "w") {|f| f.write("somedata")}
    @cm.store_data(keyFile, "valuedata")
    assert(File.exist?(TestDir+"/subdir/.cache/fileA.test"))
    assert_equal("valuedata", @cm.load_data(keyFile))
  end

  def test_storeload_postfix
    keyFile = TestDir+"/fileB.txt"  
    File.open(keyFile, "w") {|f| f.write("somedata")}
    @cm.store_data(keyFile, "valuedata")
    assert(File.exist?(TestDir+"/.cache/fileB.txt.test"))
    assert_equal("valuedata", @cm.load_data(keyFile))
  end

  def test_storeload_empty
    keyFile = TestDir+"/fileA"  
    File.open(keyFile, "w") {|f| f.write("")}
    @cm.store_data(keyFile, "valuedata")
    assert(File.exist?(TestDir+"/.cache/fileA.test"))
    assert_equal("valuedata", @cm.load_data(keyFile))
  end

  def test_corruptcache
    keyFile = TestDir+"/fileA"
    File.open(keyFile, "w") {|f| f.write("somedata")}
    @cm.store_data(keyFile, "valuedata")
    File.open(TestDir+"/.cache/fileA.test","a") {|f| f.write("more data")}
    reasons = []
    assert_equal(:invalid, @cm.load_data(keyFile, :invalidation_reasons => reasons))
    assert_equal [:cachefile_corrupted], reasons
  end  

  def test_changedcontent
    keyFile = TestDir+"/fileA"
    File.open(keyFile, "w") {|f| f.write("somedata")}
    @cm.store_data(keyFile, "valuedata")
    File.open(keyFile, "a") {|f| f.write("more data")}
    reasons = []
    assert_equal(:invalid, @cm.load_data(keyFile, :invalidation_reasons => reasons))
    assert_equal [:keyfile_changed], reasons
  end 

  def test_versioninfo
    keyFile = TestDir+"/fileA"  
    File.open(keyFile, "w") {|f| f.write("somedata")}
    @cm.version_info = "123"
    @cm.store_data(keyFile, "valuedata")
    assert(File.exist?(TestDir+"/.cache/fileA.test"))
    assert_equal("valuedata", @cm.load_data(keyFile))
  end

  def test_changed_version
    keyFile = TestDir+"/fileA"  
    File.open(keyFile, "w") {|f| f.write("somedata")}
    @cm.version_info = "123"
    @cm.store_data(keyFile, "valuedata")
    @cm.version_info = "456"
    reasons = []
    assert_equal(:invalid, @cm.load_data(keyFile, :invalidation_reasons => reasons))
    assert_equal [:keyfile_changed], reasons
  end

end