Skip to content

Commit

Permalink
Merge pull request #81 from InderKumarRathore/sorted-warnings-new
Browse files Browse the repository at this point in the history
Provide `sort_warnings_by` attribute to sort warnings
  • Loading branch information
diogot authored Feb 14, 2024
2 parents 335245b + 7928a91 commit d677c48
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
34 changes: 31 additions & 3 deletions lib/xcode_summary/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Danger
class DangerXcodeSummary < Plugin
Location = Struct.new(:file_name, :file_path, :line)
Result = Struct.new(:message, :location)
Warning = Struct.new(:message, :sticky, :location)

# The project root, which will be used to make the paths relative.
# Defaults to `pwd`.
Expand All @@ -39,7 +40,7 @@ class DangerXcodeSummary < Plugin
# A block that filters specific results.
# An example would be `lambda { |result| result.message.start_with?('ld') }` to ignore results for ld_warnings.
#
# @param [Block value
# @param [Block] value
# @return [Block]
attr_accessor :ignored_results

Expand All @@ -55,6 +56,14 @@ class DangerXcodeSummary < Plugin
# @return [Boolean]
attr_accessor :test_summary

# A block that sorts the warning results.
# An example would be `lambda { |warning| warning.message.include?("deprecated") ? 1 : 0 }` to sort results for
# deprecated warnings.
#
# @param [Block] value
# @return [Block]
attr_accessor :sort_warnings_by

# Defines if using inline comment or not.
# Defaults to `false`.
# @param [Boolean] value
Expand Down Expand Up @@ -84,6 +93,10 @@ def ignored_files
[@ignored_files].flatten.compact
end

def sort_warnings_by(&block)
@sort_warnings_by ||= block
end

def ignored_results(&block)
@ignored_results ||= block
end
Expand Down Expand Up @@ -155,13 +168,16 @@ def warning_error_count(file_path)

def format_summary(xcode_summary)
messages(xcode_summary).each { |s| message(s, sticky: sticky_summary) }
all_warnings = []
xcode_summary.actions_invocation_record.actions.each do |action|
warnings(action).each do |result|
warning_object = nil
if inline_mode && result.location
warn(result.message, sticky: false, file: result.location.file_path, line: result.location.line)
warning_object = Warning.new(result.message, false, result.location)
else
warn(result.message, sticky: false)
warning_object = Warning.new(result.message, false, nil)
end
all_warnings << warning_object
end
errors(action).each do |result|
if inline_mode && result.location
Expand All @@ -179,6 +195,18 @@ def format_summary(xcode_summary)
end
end
end
sort_and_log_warnings(all_warnings)
end

def sort_and_log_warnings(all_warnings)
all_warnings = all_warnings.sort_by(&sort_warnings_by)
all_warnings.each do |warning|
if inline_mode && warning.location
warn(warning.message, sticky: warning.sticky, file: warning.location.file_path, line: warning.location.line)
else
warn(warning.message, sticky: warning.sticky)
end
end
end

def messages(xcode_summary)
Expand Down
48 changes: 48 additions & 0 deletions spec/xcode_summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,54 @@ module Danger
expect(result).to eq '{"warnings":21,"errors":3}'
end
end

context 'with sort_warnings_by' do
before do
@xcode_summary.sort_warnings_by do |warning|
warning.message.include?('TODOs') ? 0 : 1
end
end
it 'sorts compile warnings' do
@xcode_summary.report('spec/fixtures/swiftlint.xcresult')
expect(@dangerfile.status_report[:warnings]).to eq [
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Sources/Yams/Constructor.swift#L492'>Carthage/Checkouts/Yams/Sources/Yams/Constructor.swift#L492</a>**: Todo Violation: TODOs should be resolved (Should raise error if subnode ...). (todo)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Tests/YamsTests/SpecTests.swift#L379'>Carthage/Checkouts/Yams/Tests/YamsTests/SpecTests.swift#L379</a>**: Todo Violation: TODOs should be resolved (YAML supports keys other than ...). (todo)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Tests/YamsTests/SpecTests.swift#L714'>Carthage/Checkouts/Yams/Tests/YamsTests/SpecTests.swift#L714</a>**: Todo Violation: TODOs should be resolved (local tag parsing). (todo)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Sources/Yams/Representer.swift#L187'>Carthage/Checkouts/Yams/Sources/Yams/Representer.swift#L187</a>**: Todo Violation: TODOs should be resolved (Support `Float80`). (todo)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Sources/Yams/Constructor.swift#L405'>Carthage/Checkouts/Yams/Sources/Yams/Constructor.swift#L405</a>**: Todo Violation: TODOs should be resolved (YAML supports keys other than ...). (todo)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Sources/Yams/Constructor.swift#L430'>Carthage/Checkouts/Yams/Sources/Yams/Constructor.swift#L430</a>**: Todo Violation: TODOs should be resolved (Should raise error on other th...). (todo)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Sources/Yams/Constructor.swift#L450'>Carthage/Checkouts/Yams/Sources/Yams/Constructor.swift#L450</a>**: Todo Violation: TODOs should be resolved (YAML supports Hashable element...). (todo)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Sources/Yams/Constructor.swift#L478'>Carthage/Checkouts/Yams/Sources/Yams/Constructor.swift#L478</a>**: Todo Violation: TODOs should be resolved (Should raise error if subnode ...). (todo)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Sources/Yams/Emitter.swift#L340'>Carthage/Checkouts/Yams/Sources/Yams/Emitter.swift#L340</a>**: Todo Violation: TODOs should be resolved (Support tags). (todo)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/SWXMLHash/Source/XMLIndexer+XMLIndexerDeserializable.swift#L538'>Carthage/Checkouts/SWXMLHash/Source/XMLIndexer+XMLIndexerDeserializable.swift#L538</a>**: 'public' modifier is redundant for instance method declared in a public extension",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/SWXMLHash/Source/XMLIndexer+XMLIndexerDeserializable.swift#L552'>Carthage/Checkouts/SWXMLHash/Source/XMLIndexer+XMLIndexerDeserializable.swift#L552</a>**: 'public' modifier is redundant for instance method declared in a public extension",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Sources/Yams/Tag.swift#L88'>Carthage/Checkouts/Yams/Sources/Yams/Tag.swift#L88</a>**: Legacy Hashing Violation: Prefer using the `hash(into:)` function instead of overriding `hashValue` (legacy_hashing)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Result/Result/NoError.swift#L8'>Carthage/Checkouts/Result/Result/NoError.swift#L8</a>**: Will never be executed",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Sources/Yams/Tag.swift#L109'>Carthage/Checkouts/Yams/Sources/Yams/Tag.swift#L109</a>**: Legacy Hashing Violation: Prefer using the `hash(into:)` function instead of overriding `hashValue` (legacy_hashing)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Sources/Yams/Decoder.swift#L102'>Carthage/Checkouts/Yams/Sources/Yams/Decoder.swift#L102</a>**: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Sources/Yams/Node.swift#L191'>Carthage/Checkouts/Yams/Sources/Yams/Node.swift#L191</a>**: Legacy Hashing Violation: Prefer using the `hash(into:)` function instead of overriding `hashValue` (legacy_hashing)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Sources/Yams/Encoder.swift#L139'>Carthage/Checkouts/Yams/Sources/Yams/Encoder.swift#L139</a>**: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Sources/Yams/Parser.swift#L441'>Carthage/Checkouts/Yams/Sources/Yams/Parser.swift#L441</a>**: File Line Length Violation: File should contain 400 lines or less: currently contains 441 (file_length)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Tests/YamsTests/EncoderTests.swift#L924'>Carthage/Checkouts/Yams/Tests/YamsTests/EncoderTests.swift#L924</a>**: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Tests/YamsTests/EncoderTests.swift#L937'>Carthage/Checkouts/Yams/Tests/YamsTests/EncoderTests.swift#L937</a>**: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Carthage/Checkouts/Yams/Tests/YamsTests/EncoderTests.swift#L253'>Carthage/Checkouts/Yams/Tests/YamsTests/EncoderTests.swift#L253</a>**: Superfluous Disable Command Violation: 'unused_private_declaration' is not a valid SwiftLint rule. Please remove it from the disable command. (superfluous_disable_command)"
]
end

it 'formats errors' do
@xcode_summary.report('spec/fixtures/build_error.xcresult')
expect(@dangerfile.status_report[:errors]).to eq [
'Testing cancelled because the build failed.',
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Source/SwiftLintFramework/Extensions/QueuedPrint.swift#L13'>Source/SwiftLintFramework/Extensions/QueuedPrint.swift#L13</a>**: Use of unresolved identifier 'queue'",
"**<a href='https://github.com/realm/SwiftLint/blob/f211694e7def13785ff62047386437534541d7b3/Source/SwiftLintFramework/Extensions/QueuedPrint.swift#L17'>Source/SwiftLintFramework/Extensions/QueuedPrint.swift#L17</a>**: Use of unresolved identifier 'queue'"
]
end

it 'report warning and error counts' do
result = @xcode_summary.warning_error_count('spec/fixtures/build_error.xcresult')
expect(result).to eq '{"warnings":21,"errors":3}'
end
end
end
end

Expand Down

0 comments on commit d677c48

Please sign in to comment.