Class: Danger::DangerXcodebuild

Inherits:
Plugin
  • Object
show all
Defined in:
lib/xcodebuild/plugin.rb

Overview

Exposes warnings, errors and test results. It requires a JSON generated using [xcpretty-json-formatter](github.com/marcelofabri/xcpretty-json-formatter), to be passed as an argument for it to work.

Examples:

Ensure the project compiles without warnings, errors and all tests are executed correctly


xcodebuild.parse_warnings
xcodebuild.parse_errors
xcodebuild.parse_tests
xcodebuild.perfect_build

See Also:

  • valeriomazzeo/danger-xcodebuild

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ DangerXcodebuild

Returns a new instance of DangerXcodebuild.



18
19
20
21
22
23
24
# File 'lib/xcodebuild/plugin.rb', line 18

def initialize(arg)
  super
  @warning_count = 0
  @error_count = 0
  @test_failures_count = 0
  @xcodebuild_json = nil
end

Instance Attribute Details

#build_titleString

Allows you to specify a build title to prefix all the reported messages.

Returns:

  • (String)


29
30
31
# File 'lib/xcodebuild/plugin.rb', line 29

def build_title
  @build_title
end

#json_fileObject

Allows you to specify an xcodebuild JSON file location to parse.



32
33
34
# File 'lib/xcodebuild/plugin.rb', line 32

def json_file
  @json_file
end

Class Method Details

.instance_nameObject



117
118
119
# File 'lib/xcodebuild/plugin.rb', line 117

def self.instance_name
      to_s.gsub("Danger", "").danger_underscore.split("/").last
end

Instance Method Details

#parse_errorserror_count

Parses and expose eventual errors.

Returns:

  • (error_count)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/xcodebuild/plugin.rb', line 62

def parse_errors
  errors = @xcodebuild_json["errors"].map {|x| "`#{x}`"}
  errors += @xcodebuild_json["compile_errors"].map {|x| "`[#{x["file_path"].split("/").last}] #{x["reason"]}`"}
  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
    error_string = errors.count == 1 ? "error" : "errors"
    message = Array.new
    message.push (@build_title) unless @build_title.nil?
    message.push("Build failed with **#{errors.count}** #{error_string} 🚨")
    fail(message.reject(&:empty?).join(" "))
    errors.each do |error|
      fail(error)
    end
  end
  @error_count = errors.count
  return @error_count
end

#parse_teststest_failures

Parses and exposes eventual test failures.

Returns:

  • (test_failures)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/xcodebuild/plugin.rb', line 85

def parse_tests
  test_failures = Array.new
  @xcodebuild_json["tests_failures"].each do |key, value|
    test_failures += value.map {|x| "`[#{x["file_path"].split("/").last}] [#{x["test_case"]}] #{x["reason"]}`"}
  end

  if test_failures.count > 0
    test_string = test_failures.count == 1 ? "error" : "errors"
    message = Array.new
    message.push (@build_title) unless @build_title.nil?
    message.push("Test execution failed with **#{test_failures.count}** #{test_string} 🚨")
    fail(message.reject(&:empty?).join(" "))
    test_failures.each do |test_failure|
      fail(test_failure)
    end
  end
  @test_failures_count = test_failures.count
  return @test_failures_count
end

#parse_warningswarning_count

Parses and exposes eventual warnings.

Returns:

  • (warning_count)


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/xcodebuild/plugin.rb', line 45

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
    warning_string = @warning_count == 1 ? "warning" : "warnings"
    message = Array.new
    message.push (@build_title) unless @build_title.nil?
    message.push("Please fix **#{@warning_count}** #{warning_string} 😒")
    warn(message.reject(&:empty?).join(" "))
  end
  return @warning_count
end

#perfect_buildis_perfect_build

Prints “Perfect build 👍🏻” if everything is ok after parsing.

Returns:

  • (is_perfect_build)


108
109
110
111
112
113
114
115
# File 'lib/xcodebuild/plugin.rb', line 108

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
  return is_perfect_build
end