forked from presidentbeef/brakeman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_path.rb
102 lines (78 loc) · 2.74 KB
/
file_path.rb
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
100
101
102
require_relative '../test'
require 'brakeman/app_tree'
require 'brakeman/file_path'
class FilePathTests < Minitest::Test
def test_relative_from_app_tree
at = Brakeman::AppTree.new("/tmp/blah")
fp = Brakeman::FilePath.from_app_tree at, "thing.rb"
assert_equal "thing.rb", fp.relative
assert_equal "/tmp/blah/thing.rb", fp.absolute
end
def test_absolute_from_app_tree
at = Brakeman::AppTree.new("/tmp/blah")
fp = Brakeman::FilePath.from_app_tree at, "/tmp/blah/thing.rb"
assert_equal "thing.rb", fp.relative
assert_equal "/tmp/blah/thing.rb", fp.absolute
end
def test_from_app_tree_already_file_path
at = Brakeman::AppTree.new("/tmp/blah")
fp1 = Brakeman::FilePath.from_app_tree at, "/tmp/blah/thing.rb"
fp2 = Brakeman::FilePath.from_app_tree at, fp1
assert_same fp1, fp2
end
def test_from_tracker_already_file_path
at = Brakeman::AppTree.new("/tmp/blah")
fp1 = at.file_path "/tmp/blah/thing.rb"
fp2 = at.file_path fp1
assert_same fp1, fp2
end
def test_file_path_to_str
at = Brakeman::AppTree.new("/tmp/blah")
fp = Brakeman::FilePath.from_app_tree at, "/tmp/blah/thing.rb"
assert_equal "/tmp/blah/thing.rb", fp.to_str
assert_equal "/tmp/blah/thing.rb", "#{fp}"
end
def test_file_path_empty?
at = Brakeman::AppTree.new("/tmp/blah")
fp1 = Brakeman::FilePath.from_app_tree at, "/tmp/blah/thing.rb"
fp2 = Brakeman::FilePath.from_app_tree at, "/tmp/blah/thing/"
fp3 = Brakeman::FilePath.from_app_tree at, ""
refute fp1.empty?
refute fp2.empty?
assert fp3.empty?
end
def test_file_path_equality
at = Brakeman::AppTree.new("/tmp/blah")
fp1 = Brakeman::FilePath.from_app_tree at, "/tmp/blah/thing.rb"
fp2 = Brakeman::FilePath.from_app_tree at, "thing.rb"
fp3 = Brakeman::FilePath.from_app_tree at, "thing2.rb"
assert_equal fp1, fp2
assert_equal fp2, fp1
refute_equal fp1, fp3
refute_equal fp3, fp2
assert_includes [fp1], fp2
assert_includes [fp2], fp1
refute_includes [fp1], fp3
refute_includes [fp3], fp2
end
def test_file_path_equality_not_cached
fp1 = Brakeman::FilePath.new("/tmp/blah/thing.rb", "thing.rb")
fp2 = Brakeman::FilePath.new("/tmp/blah/thing.rb", "thing.rb")
assert_equal fp1, fp2
assert_equal fp2, fp1
assert_equal fp1.hash, fp2.hash
assert fp1.eql?(fp2)
assert fp2.eql?(fp1)
# Ensure FilePaths used as hash keys are equal
h = {fp1 => 1}
assert_equal 1, h[fp1]
assert_equal 1, h[fp2]
end
def test_file_path_cache
at = Brakeman::AppTree.new("/tmp/blah")
fp1 = Brakeman::FilePath.from_app_tree at, "/tmp/blah/thing.rb"
fp2 = Brakeman::FilePath.from_app_tree at, "thing.rb"
assert_same fp1, fp2
assert_same fp2, fp1
end
end