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

Allow custom derived data path and ignored_files support #7

Merged
merged 5 commits into from
Mar 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/xcprofiler/danger_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@

module Danger
class DangerReporter < Xcprofiler::AbstractReporter
def initialize(dangerfile, thresholds, inline_mode, working_dir)
def initialize(dangerfile, thresholds, inline_mode, working_dir, ignored_files)
super({})
@dangerfile = dangerfile
@thresholds = thresholds
@inline_mode = inline_mode
@working_dir = working_dir
@ignored_files = ignored_files
end

def report!(executions)
executions.reject! { |execution| ignored_files.any? { |pattern| File.fnmatch(pattern, execution.path) } }

if @inline_mode
inline_report(executions)
else
Expand Down Expand Up @@ -69,5 +72,9 @@ def markdown_issues(executions, heading)

message
end

def ignored_files
[@ignored_files].flatten.compact
end
end
end
20 changes: 17 additions & 3 deletions lib/xcprofiler/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ module Danger
# xcprofiler.thresholds = { warn: 100, fail: 500 }
# xcprofiler.report 'MyApp'
#
# @example Specify a custom DerivedData directory
#
# xcprofiler.report 'MyApp' './DerivedData'
#
# @see giginet/danger-xcprofiler
# @tags xcode, ios, danger
class DangerXcprofiler < Plugin
Expand All @@ -33,13 +37,22 @@ class DangerXcprofiler < Plugin
# @return [Boolean]
attr_accessor :inline_mode

# A globbed string or array of strings which should match the files
# that you want to ignore warnings on. Defaults to nil.
# An example would be `'**/Pods/**'` to ignore warnings in Pods that your project uses.
#
# @param [String or [String]] value
# @return [[String]]
attr_accessor :ignored_files

# Search the latest .xcactivitylog by the passing product_name and profile compilation time
# @param [String] product_name Product name for the target project.
# @param [String] derived_data_path Path to the directory containing the DerivedData.
# @return [void]
def report(product_name)
profiler = Xcprofiler::Profiler.by_product_name(product_name)
def report(product_name, derived_data_path = nil)
profiler = Xcprofiler::Profiler.by_product_name(product_name, derived_data_path)
profiler.reporters = [
DangerReporter.new(@dangerfile, thresholds, inline_mode, working_dir)
DangerReporter.new(@dangerfile, thresholds, inline_mode, working_dir, ignored_files)
]
profiler.report!
rescue Xcprofiler::DerivedDataNotFound, Xcprofiler::BuildFlagIsNotEnabled => e
Expand All @@ -58,6 +71,7 @@ def thresholds

def inline_mode
return true if @inline_mode.nil?

!!@inline_mode
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Coveralls.wear!

require 'pathname'
ROOT = Pathname.new(File.expand_path('../../', __FILE__))
ROOT = Pathname.new(File.expand_path('..', __dir__))
$LOAD_PATH.unshift((ROOT + 'lib').to_s)
$LOAD_PATH.unshift((ROOT + 'spec').to_s)

Expand Down
25 changes: 23 additions & 2 deletions spec/xcprofiler_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require File.expand_path('../spec_helper', __FILE__)
require File.expand_path('spec_helper', __dir__)
require 'xcprofiler'

# rubocop:disable Metrics/ModuleLength
module Danger
describe Danger::DangerXcprofiler do
it 'should be a plugin' do
Expand All @@ -25,7 +26,7 @@ module Danger
allow(@dangerfile).to receive(:warn)
allow(@dangerfile).to receive(:fail)
allow(@dangerfile).to receive(:markdown)
allow(Xcprofiler::Profiler).to receive(:by_product_name).with(product_name).and_return(profiler)
allow(Xcprofiler::Profiler).to receive(:by_product_name).with(product_name, nil).and_return(profiler)
allow(derived_data).to receive(:flag_enabled?).and_return(true)
allow(derived_data).to receive(:executions).and_return([execution0, execution1])
[execution0, execution1].each do |execution|
Expand All @@ -34,6 +35,25 @@ module Danger
end
end

context 'with ignored files' do
let(:time0) { 49.9 }
let(:time1) { 50 }
it 'skips matching warning' do
@xcprofiler.ignored_files = ['path/**']
@xcprofiler.report(product_name)
expect(@dangerfile).to_not have_received(:warn).with('`doSomething()` takes 50.0 ms to build',
file: 'path/to/Source.swift',
line: 20)
end
it 'includes non-matching warning' do
@xcprofiler.ignored_files = ['other/path/**']
@xcprofiler.report(product_name)
expect(@dangerfile).to have_received(:warn).with('`doSomething()` takes 50.0 ms to build',
file: 'path/to/Source.swift',
line: 20)
end
end

context 'with slow execution' do
let(:time0) { 49.9 }
let(:time1) { 50 }
Expand Down Expand Up @@ -119,3 +139,4 @@ module Danger
end
end
end
# rubocop:enable Metrics/ModuleLength