Skip to content

Commit

Permalink
Option to stop danger-xcodebuild from posting messages (#1)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ky1ejs authored and valeriomazzeo committed Oct 16, 2016
1 parent 7ffb445 commit bffa7c1
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 11 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 15 additions & 7 deletions lib/xcodebuild/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -39,14 +40,19 @@ 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]
#
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?
Expand All @@ -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?
Expand All @@ -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?
Expand All @@ -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

Expand Down
51 changes: 51 additions & 0 deletions spec/xcodebuild_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bffa7c1

Please sign in to comment.