Class: Trainer::TestParser

Inherits:
Object
  • Object
show all
Defined in:
lib/trainer/test_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, config = {}) ⇒ TestParser

Returns a new instance of TestParser.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/trainer/test_parser.rb', line 64

def initialize(path, config = {})
  path = File.expand_path(path)
  UI.user_error!("File not found at path '#{path}'") unless File.exist?(path)

  if File.directory?(path) && path.end_with?(".xcresult")
    parse_xcresult(path)
  else
    self.file_content = File.read(path)
    self.raw_json = Plist.parse_xml(self.file_content)

    return if self.raw_json["FormatVersion"].to_s.length.zero? # maybe that's a useless plist file

    ensure_file_valid!
    parse_content(config[:xcpretty_naming])
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/trainer/test_parser.rb', line 3

def data
  @data
end

#file_contentObject

Returns the value of attribute file_content.



5
6
7
# File 'lib/trainer/test_parser.rb', line 5

def file_content
  @file_content
end

#raw_jsonObject

Returns the value of attribute raw_json.



7
8
9
# File 'lib/trainer/test_parser.rb', line 7

def raw_json
  @raw_json
end

Class Method Details

.auto_convert(config) ⇒ Object

Returns a hash with the path being the key, and the value defining if the tests were successful



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/trainer/test_parser.rb', line 11

def self.auto_convert(config)
  FastlaneCore::PrintTable.print_values(config: config,
                                         title: "Summary for trainer #{Trainer::VERSION}")

  containing_dir = config[:path]
  # Xcode < 10
  files = Dir["#{containing_dir}/**/Logs/Test/*TestSummaries.plist"]
  files += Dir["#{containing_dir}/Test/*TestSummaries.plist"]
  files += Dir["#{containing_dir}/*TestSummaries.plist"]
  # Xcode 10
  files += Dir["#{containing_dir}/**/Logs/Test/*.xcresult/TestSummaries.plist"]
  files += Dir["#{containing_dir}/Test/*.xcresult/TestSummaries.plist"]
  files += Dir["#{containing_dir}/*.xcresult/TestSummaries.plist"]
  files += Dir[containing_dir] if containing_dir.end_with?(".plist") # if it's the exact path to a plist file
  # Xcode 11
  files += Dir["#{containing_dir}/**/Logs/Test/*.xcresult"]
  files += Dir["#{containing_dir}/Test/*.xcresult"]
  files += Dir["#{containing_dir}/*.xcresult"]
  files << containing_dir if File.extname(containing_dir) == ".xcresult"

  if files.empty?
    UI.user_error!("No test result files found in directory '#{containing_dir}', make sure the file name ends with 'TestSummaries.plist' or '.xcresult'")
  end

  return_hash = {}
  files.each do |path|
    if config[:output_directory]
      FileUtils.mkdir_p(config[:output_directory])
      # Remove .xcresult or .plist extension
      if path.end_with?(".xcresult")
        filename = File.basename(path).gsub(".xcresult", config[:extension])
      else
        filename = File.basename(path).gsub(".plist", config[:extension])
      end
      to_path = File.join(config[:output_directory], filename)
    else
      # Remove .xcresult or .plist extension
      if path.end_with?(".xcresult")
        to_path = path.gsub(".xcresult", config[:extension])
      else
        to_path = path.gsub(".plist", config[:extension])
      end
    end

    tp = Trainer::TestParser.new(path, config)
    File.write(to_path, tp.to_junit)
    puts "Successfully generated '#{to_path}'"

    return_hash[to_path] = tp.tests_successful?
  end
  return_hash
end

Instance Method Details

#tests_successful?Bool

Returns were all tests successful? Is false if at least one test failed.

Returns:

  • (Bool)

    were all tests successful? Is false if at least one test failed



87
88
89
# File 'lib/trainer/test_parser.rb', line 87

def tests_successful?
  self.data.collect { |a| a[:number_of_failures] }.all?(&:zero?)
end

#to_junitObject

Returns the JUnit report as String



82
83
84
# File 'lib/trainer/test_parser.rb', line 82

def to_junit
  JunitGenerator.new(self.data).generate
end