From bffa7c1917c05991a64a8111f3c98151c2ed82b1 Mon Sep 17 00:00:00 2001 From: Kyle McAlpine Date: Sun, 16 Oct 2016 12:59:49 +0100 Subject: [PATCH] Option to stop danger-xcodebuild from posting messages (#1) * Giving ability to specify whether message should be added * Syntax corrections * Switching to name parameters * README updated * Switching post_messages to an instance variable * Adding tests * Correcting post message test description * Fixing initialisation of test JSON * Correcting README * Adding assertions for parsing on post_messages = true --- README.md | 12 ++++++---- lib/xcodebuild/plugin.rb | 22 +++++++++++------ spec/xcodebuild_spec.rb | 51 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a1c306f..cffdcb5 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,14 @@ Exposes warnings, errors and test results. It requires a JSON generated using [x xcodebuild.json_file = "./fastlane/reports/xcpretty-json-formatter-results.json" - xcodebuild.parse_warnings - xcodebuild.parse_errors - xcodebuild.parse_tests - xcodebuild.perfect_build + xcodebuild.parse_warnings # returns number of warnings + xcodebuild.parse_errors # returns number of errors + xcodebuild.parse_tests # returns number of test failures + xcodebuild.perfect_build # returns a bool indicating if the build was perfect + +Messages are posted by default. You can stop them like this: + + xcodebuild.post_messages = false ## Development diff --git a/lib/xcodebuild/plugin.rb b/lib/xcodebuild/plugin.rb index b5f765a..5abb564 100644 --- a/lib/xcodebuild/plugin.rb +++ b/lib/xcodebuild/plugin.rb @@ -21,6 +21,7 @@ def initialize(arg) @error_count = 0 @test_failures_count = 0 @xcodebuild_json = nil + @post_messages = true end # Allows you to specify a build title to prefix all the reported messages. @@ -39,6 +40,11 @@ def json_file=(value) @xcodebuild_json = JSON.parse(File.open(value, 'r').read) end + # Allows you to specify whether default messages should be posted when parsing + # @return [Bool] + # + attr_accessor :post_messages + # Parses and exposes eventual warnings. # @return [warning_count] # @@ -46,7 +52,7 @@ def parse_warnings @warning_count = @xcodebuild_json["warnings"].count @warning_count = @warning_count + @xcodebuild_json["ld_warnings"].count @warning_count = @warning_count + @xcodebuild_json["compile_warnings"].count - if @warning_count > 0 + if @warning_count > 0 && post_messages warning_string = @warning_count == 1 ? "warning" : "warnings" message = Array.new message.push (@build_title) unless @build_title.nil? @@ -65,7 +71,7 @@ def parse_errors errors += @xcodebuild_json["file_missing_errors"].map {|x| "`[#{x["file_path"].split("/").last}] #{x["reason"]}`"} errors += @xcodebuild_json["undefined_symbols_errors"].map {|x| "`#{x["message"]}`"} errors += @xcodebuild_json["duplicate_symbols_errors"].map {|x| "`#{x["message"]}`"} - if errors.count > 0 + if errors.count > 0 && post_messages error_string = errors.count == 1 ? "error" : "errors" message = Array.new message.push (@build_title) unless @build_title.nil? @@ -88,7 +94,7 @@ def parse_tests test_failures += value.map {|x| "`[#{x["file_path"].split("/").last}] [#{x["test_case"]}] #{x["reason"]}`"} end - if test_failures.count > 0 + if test_failures.count > 0 && post_messages test_string = test_failures.count == 1 ? "error" : "errors" message = Array.new message.push (@build_title) unless @build_title.nil? @@ -107,10 +113,12 @@ def parse_tests # def perfect_build is_perfect_build = @warning_count == 0 && @error_count == 0 && @test_failures_count == 0 - message = Array.new - message.push (@build_title) unless @build_title.nil? - message.push ("Perfect build 👍🏻") - message(message.reject(&:empty?).join(" ")) if is_perfect_build + if post_messages + message = Array.new + message.push (@build_title) unless @build_title.nil? + message.push ("Perfect build 👍🏻") + message(message.reject(&:empty?).join(" ")) if is_perfect_build + end return is_perfect_build end diff --git a/spec/xcodebuild_spec.rb b/spec/xcodebuild_spec.rb index 87ecd50..8003246 100644 --- a/spec/xcodebuild_spec.rb +++ b/spec/xcodebuild_spec.rb @@ -137,6 +137,57 @@ module Danger end + describe "with post messages set to false" do + + before do + data = { + :warnings => ['warning1', 'warning2'], + :ld_warnings => ['ld_warnings'], + :compile_warnings => ['compile_warnings'], + :errors => ['error1', 'error2'], + :compile_errors => [ + { :file_path => '/tmp/file1.m', :reason => 'reason1' }, + { :file_path => '/tmp/file2.m', :reason => 'reason2' } + ], + :file_missing_errors => [ + { :file_path => '/tmp/missing_file1.m', :reason => 'reason1' }, + { :file_path => '/tmp/missing_file2.m', :reason => 'reason2' } + ], + :undefined_symbols_errors => [{ :message => 'undefined_symbols' }], + :duplicate_symbols_errors => [{ :message => 'duplicate_symbols' }], + :tests_failures => { + :suite1 => [{ :file_path => '/tmp/file1.m', :test_case => "testCase1", :reason => 'reason1' }], + :suite2 => [{ :file_path => '/tmp/file2.m', :test_case => "testCase2", :reason => 'reason2' }] + } + }.to_json + + filename = 'xcodebuild_tests.json' + allow(File).to receive(:open).with(filename, 'r').and_return(StringIO.new(data)) + + @xcodebuild.json_file = filename + end + + it "must not post messages" do + @xcodebuild.post_messages = false + warnings = @xcodebuild.parse_warnings + errors = @xcodebuild.parse_errors + tests_failures = @xcodebuild.parse_tests + is_perfect_build = @xcodebuild.perfect_build + + expect(warnings).to eq(4) + expect(@dangerfile.status_report[:warnings]).to be_empty + + expect(errors).to eq(8) + expect(tests_failures).to eq(2) + expect(@dangerfile.status_report[:errors]).to be_empty + + expect(is_perfect_build).to eq(false) + expect(@dangerfile.status_report[:messages]).to be_empty + expect(@dangerfile.status_report[:markdowns]).to be_empty + end + + end + end end end