From 7928a91ae2f74f56280fef93979fa6147b8a1837 Mon Sep 17 00:00:00 2001 From: Inder <352443+InderKumarRathore@users.noreply.github.com> Date: Sat, 21 Oct 2023 12:06:16 +0800 Subject: [PATCH] added code to sort the warnings Signed-off-by: Inder <352443+InderKumarRathore@users.noreply.github.com> --- lib/xcode_summary/plugin.rb | 34 +++++++++++++++++++++++--- spec/xcode_summary_spec.rb | 48 +++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/lib/xcode_summary/plugin.rb b/lib/xcode_summary/plugin.rb index dd7962c..f332274 100644 --- a/lib/xcode_summary/plugin.rb +++ b/lib/xcode_summary/plugin.rb @@ -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`. @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/spec/xcode_summary_spec.rb b/spec/xcode_summary_spec.rb index d7c09e5..dded1c1 100644 --- a/spec/xcode_summary_spec.rb +++ b/spec/xcode_summary_spec.rb @@ -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 [ + "**Carthage/Checkouts/Yams/Sources/Yams/Constructor.swift#L492**: Todo Violation: TODOs should be resolved (Should raise error if subnode ...). (todo)", + "**Carthage/Checkouts/Yams/Tests/YamsTests/SpecTests.swift#L379**: Todo Violation: TODOs should be resolved (YAML supports keys other than ...). (todo)", + "**Carthage/Checkouts/Yams/Tests/YamsTests/SpecTests.swift#L714**: Todo Violation: TODOs should be resolved (local tag parsing). (todo)", + "**Carthage/Checkouts/Yams/Sources/Yams/Representer.swift#L187**: Todo Violation: TODOs should be resolved (Support `Float80`). (todo)", + "**Carthage/Checkouts/Yams/Sources/Yams/Constructor.swift#L405**: Todo Violation: TODOs should be resolved (YAML supports keys other than ...). (todo)", + "**Carthage/Checkouts/Yams/Sources/Yams/Constructor.swift#L430**: Todo Violation: TODOs should be resolved (Should raise error on other th...). (todo)", + "**Carthage/Checkouts/Yams/Sources/Yams/Constructor.swift#L450**: Todo Violation: TODOs should be resolved (YAML supports Hashable element...). (todo)", + "**Carthage/Checkouts/Yams/Sources/Yams/Constructor.swift#L478**: Todo Violation: TODOs should be resolved (Should raise error if subnode ...). (todo)", + "**Carthage/Checkouts/Yams/Sources/Yams/Emitter.swift#L340**: Todo Violation: TODOs should be resolved (Support tags). (todo)", + "**Carthage/Checkouts/SWXMLHash/Source/XMLIndexer+XMLIndexerDeserializable.swift#L538**: 'public' modifier is redundant for instance method declared in a public extension", + "**Carthage/Checkouts/SWXMLHash/Source/XMLIndexer+XMLIndexerDeserializable.swift#L552**: 'public' modifier is redundant for instance method declared in a public extension", + "**Carthage/Checkouts/Yams/Sources/Yams/Tag.swift#L88**: Legacy Hashing Violation: Prefer using the `hash(into:)` function instead of overriding `hashValue` (legacy_hashing)", + "**Carthage/Checkouts/Result/Result/NoError.swift#L8**: Will never be executed", + "**Carthage/Checkouts/Yams/Sources/Yams/Tag.swift#L109**: Legacy Hashing Violation: Prefer using the `hash(into:)` function instead of overriding `hashValue` (legacy_hashing)", + "**Carthage/Checkouts/Yams/Sources/Yams/Decoder.swift#L102**: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)", + "**Carthage/Checkouts/Yams/Sources/Yams/Node.swift#L191**: Legacy Hashing Violation: Prefer using the `hash(into:)` function instead of overriding `hashValue` (legacy_hashing)", + "**Carthage/Checkouts/Yams/Sources/Yams/Encoder.swift#L139**: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)", + "**Carthage/Checkouts/Yams/Sources/Yams/Parser.swift#L441**: File Line Length Violation: File should contain 400 lines or less: currently contains 441 (file_length)", + "**Carthage/Checkouts/Yams/Tests/YamsTests/EncoderTests.swift#L924**: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)", + "**Carthage/Checkouts/Yams/Tests/YamsTests/EncoderTests.swift#L937**: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)", + "**Carthage/Checkouts/Yams/Tests/YamsTests/EncoderTests.swift#L253**: 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.', + "**Source/SwiftLintFramework/Extensions/QueuedPrint.swift#L13**: Use of unresolved identifier 'queue'", + "**Source/SwiftLintFramework/Extensions/QueuedPrint.swift#L17**: 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