Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(InteractiveIgnorer): use FilePath #1624

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/brakeman.rb
Expand Up @@ -556,7 +556,7 @@ def self.filter_warnings tracker, options

if options[:interactive_ignore]
require 'brakeman/report/ignore/interactive'
config = InteractiveIgnorer.new(file, tracker.warnings).start
config = InteractiveIgnorer.new(app_tree, file, tracker.warnings).start
else
notify "[Notice] Using '#{file}' to filter warnings"
config = IgnoreConfig.new(file, tracker.warnings)
Expand Down
4 changes: 4 additions & 0 deletions lib/brakeman/file_path.rb
Expand Up @@ -50,6 +50,10 @@ def exists?
File.exist? self.absolute
end

def empty?
self.relative.to_s.empty?
end

# Compare FilePaths. Raises an ArgumentError unless both objects are FilePaths.
def <=> rhs
raise ArgumentError unless rhs.is_a? Brakeman::FilePath
Expand Down
17 changes: 11 additions & 6 deletions lib/brakeman/report/ignore/interactive.rb
Expand Up @@ -2,14 +2,15 @@

module Brakeman
class InteractiveIgnorer
def initialize file, warnings
def initialize app_tree, file, warnings
@ignore_config = Brakeman::IgnoreConfig.new(file, warnings)
@new_warnings = warnings
@skip_ignored = false
@skip_rest = false
@ignore_rest = false
@quit = false
@restart = false
@app_tree = app_tree
end

def start
Expand All @@ -35,15 +36,17 @@ def start

def file_menu
loop do
@ignore_config.file = HighLine.new.ask "Input file: " do |q|
input_file = HighLine.new.ask "Input file: " do |q|
if @ignore_config.file and not @ignore_config.file.empty?
q.default = @ignore_config.file
q.default = @ignore_config.file.relative
else
q.default = "config/brakeman.ignore"
end
end

if File.exist? @ignore_config.file
@ignore_config.file = Brakeman::FilePath.from_app_tree(@app_tree, input_file)

if @ignore_config.file && @ignore_config.file.exists?
@ignore_config.read_from_file
return
else
Expand Down Expand Up @@ -168,14 +171,16 @@ def final_menu
end

def save
@ignore_config.file = HighLine.new.ask "Output file: " do |q|
output_file = HighLine.new.ask "Output file: " do |q|
if @ignore_config.file and not @ignore_config.file.empty?
q.default = @ignore_config.file
q.default = @ignore_config.file.relative
else
q.default = "config/brakeman.ignore"
end
end

@ignore_config.file = Brakeman::FilePath.from_app_tree(@app_tree, output_file)

@ignore_config.save_with_old
end

Expand Down
11 changes: 11 additions & 0 deletions test/tests/file_path.rb
Expand Up @@ -43,6 +43,17 @@ def test_file_path_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"
Expand Down