-
Notifications
You must be signed in to change notification settings - Fork 715
/
file_parser.rb
67 lines (52 loc) · 1.66 KB
/
file_parser.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
require_relative '../test'
require 'tempfile'
class FileParserTests < Minitest::Test
def setup
@tracker = BrakemanTester.new_tracker
timeout = 10
@file_parser = Brakeman::FileParser.new(@tracker.app_tree, timeout)
end
def test_parse_error
tempfile = Tempfile.new
tempfile.write("x = ")
tempfile.close
@file_parser.parse_files([tempfile.path])
@tracker.add_errors(@file_parser.errors)
assert_equal 1, @tracker.errors.length
ensure
tempfile.unlink
end
def test_parse_error_shows_newer_failure
tempfile = Tempfile.new
tempfile.write <<-RUBY
blah(x: 1)
thing do
RUBY
tempfile.close
@file_parser.parse_files([tempfile.path])
@tracker.add_errors(@file_parser.errors)
assert_equal 1, @tracker.errors.length
if RubyParser::Parser::VERSION.split(".").map(&:to_i).zip([3,14,0]).all? { |a, b| a >= b }
assert_match(/parse error on value false \(\$end\)/, @tracker.errors.first[:error])
else
assert_match(/parse error on value \"\$end\" \(\$end\)/, @tracker.errors.first[:error])
end
end
def test_read_files_reports_error
tempfile = Tempfile.new
tempfile.write("x = ")
tempfile.close
@file_parser.read_files([tempfile.path]) do |path, contents|
@file_parser.parse_ruby contents, path
end
@tracker.add_errors(@file_parser.errors)
assert_equal 1, @tracker.errors.length
end
def test_parse_ruby_accepts_file_path
file_path = Brakeman::FilePath.from_app_tree @tracker.app_tree, "config/test.rb"
parsed = @file_parser.parse_ruby <<-'RUBY', file_path
"#{__FILE__}"
RUBY
assert_equal s(:str, "config/test.rb"), parsed
end
end