Skip to content

Commit

Permalink
print all warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
greeneca committed Sep 26, 2018
1 parent 2d6dacd commit 602cbea
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
36 changes: 30 additions & 6 deletions lib/roku_builder/plugins/analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def self.dependencies
[Loader]
end

def analyze(options:)
def analyze(options:, quiet: false)
@options = options
@warnings = []
analyzer_config = get_analyzer_config
Expand All @@ -41,17 +41,18 @@ def analyze(options:)
@warnings.concat(line_inspector.run(file_path))
end
if file_path.end_with?("__MACOSX")
add_warning(warning: :packageMacosxDirectory, path: file_path, dir: dir)
add_warning(warning: :packageMacosxDirectory, path: file_path)
end
if file_path.end_with?(".zip", ".md", ".pkg")
add_warning(warning: :packageExtraneousFiles, path: file_path, dir: dir)
add_warning(warning: :packageExtraneousFiles, path: file_path)
end
has_source_dir = true if file_path.end_with?("source")
end
unless has_source_dir
add_warning(warning: :packageSourceDirectory, path: "source")
end
@warnings.concat(raf_inspector.run(analyzer_config[:inspectors]))
print_warnings(dir) unless quiet
end
@warnings
end
Expand All @@ -66,12 +67,35 @@ def get_analyzer_config
JSON.parse(File.open(file).read, {symbolize_names: true})
end

def add_warning(warning:, path:, dir: nil)
def add_warning(warning:, path:)
@warnings.push(@inspector_config[warning].deep_dup)
path = path.dup
path.slice!(dir) if dir
@warnings.last[:path] = path
end

def print_warnings(dir)
logger = ::Logger.new(STDOUT)
logger.level = @logger.level
logger.formatter = proc {|severity, _datetime, _progname, msg|
"%5s: %s\n\r" % [severity, msg]
}
@warnings.each do |warning|
message = warning[:message]
if warning[:path]
warning[:path].slice!(dir) if dir
warning[:path].slice!(/^\//)
message += ". pkg:/"+warning[:path]
message += ":"+warning[:line].to_s if warning[:line]
end
case(warning[:severity])
when "error"
logger.error(message)
when "warning"
logger.warn(message)
when "info"
logger.info(message)
end
end
end
end
RokuBuilder.register_plugin(Analyzer)
end
Expand Down
31 changes: 27 additions & 4 deletions test/roku_builder/plugins/test_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ def test_manifest_file
assert_equal 1, warnings.count
assert_match(/Manifest.*missing/, warnings[0][:message])
end
def test_logging_error
test_logger_with_file_content(text: "stop", severity: :error)
end
def test_logging_warning
test_logger_with_file_content(text: "\"roCaptionRenderer\"", severity: :warn)
end
def test_logging_info
test_logger_with_file_content(text: "\"roSGScreen\"", severity: :info)
end


private
Expand All @@ -289,20 +298,34 @@ def use_manifest(manifest_file)
FileUtils.cp(File.join(@root_dir, manifest_file), File.join(@root_dir, "manifest"))
end

def test_file(text:, file: nil)
def test_file(text:, file: nil, quiet: true)
file ||= "test.brs"
test_file = File.join(@root_dir, "source", file)
File.open(test_file, "w") do |file|
file.write(text)
end
warnings = test
warnings = test(quiet)
FileUtils.rm(test_file) if File.exist?(test_file)
warnings
end

def test
def test_logger_with_file_content(text:, severity:)
logger = Minitest::Mock.new

logger.expect(:level=, nil, [Integer])
logger.expect(:formatter=, nil, [Proc])
logger.expect(severity, nil, [String])

::Logger.stub :new, logger do
warnings = test_file(text: text, quiet: false)
end

logger.verify
end

def test(quiet=true)
analyzer = Analyzer.new(config: @config)
analyzer.analyze(options: @options)
analyzer.analyze(options: @options, quiet: quiet)
end

def print_all(warnings)
Expand Down

0 comments on commit 602cbea

Please sign in to comment.