Class: Pod::TestProject

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-unit-test/project.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, simulator, path = Dir.pwd) ⇒ TestProject

Returns a new instance of TestProject.



19
20
21
22
23
# File 'lib/cocoapods-unit-test/project.rb', line 19

def initialize(name, simulator, path = Dir.pwd)
  @destination = "platform=iOS Simulator,name=#{simulator}"
  @target = name
  @path = File.expand_path(path)
end

Instance Attribute Details

#cov_shellObject

Returns the value of attribute cov_shell.



17
18
19
# File 'lib/cocoapods-unit-test/project.rb', line 17

def cov_shell
  @cov_shell
end

#derived_data_pathObject

Returns the value of attribute derived_data_path.



15
16
17
# File 'lib/cocoapods-unit-test/project.rb', line 15

def derived_data_path
  @derived_data_path
end

#destinationObject

Returns the value of attribute destination.



16
17
18
# File 'lib/cocoapods-unit-test/project.rb', line 16

def destination
  @destination
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/cocoapods-unit-test/project.rb', line 9

def path
  @path
end

#podfileObject

Returns the value of attribute podfile.



10
11
12
# File 'lib/cocoapods-unit-test/project.rb', line 10

def podfile
  @podfile
end

#pods_projectObject

Returns the value of attribute pods_project.



11
12
13
# File 'lib/cocoapods-unit-test/project.rb', line 11

def pods_project
  @pods_project
end

#schemesObject

Returns the value of attribute schemes.



13
14
15
# File 'lib/cocoapods-unit-test/project.rb', line 13

def schemes
  @schemes
end

#targetObject

Returns the value of attribute target.



14
15
16
# File 'lib/cocoapods-unit-test/project.rb', line 14

def target
  @target
end

#workspaceObject

Returns the value of attribute workspace.



12
13
14
# File 'lib/cocoapods-unit-test/project.rb', line 12

def workspace
  @workspace
end

Instance Method Details

#archives_path(scheme) ⇒ Object



82
83
84
# File 'lib/cocoapods-unit-test/project.rb', line 82

def archives_path(scheme)
  File.expand_path("build/#{scheme}/archives", path)
end

#pod_installObject

Raises:

  • (Informative)


96
97
98
99
100
101
102
103
# File 'lib/cocoapods-unit-test/project.rb', line 96

def pod_install
  UI.puts "pod install..."
  raise Informative, "Please use a Gemfile or run pod install by yourself" unless File.file?("Gemfile")
  system( "bundle install")
  system( "bundle exec pod install")
  raise Informative, "XcodeCoverage not found" unless File.file?(cov_shell)
  raise Informative, "workspace not found" unless File.directory?(workspace)
end

#result_bundle_path(scheme) ⇒ Object



70
71
72
# File 'lib/cocoapods-unit-test/project.rb', line 70

def result_bundle_path(scheme)
  File.expand_path("build/#{scheme}/Test.xcresult", path)
end

#result_json_path(scheme) ⇒ Object



74
75
76
# File 'lib/cocoapods-unit-test/project.rb', line 74

def result_json_path(scheme)
  File.expand_path("build/#{scheme}/TestResult.json", path)
end

#run(install_flag) ⇒ Object



151
152
153
154
# File 'lib/cocoapods-unit-test/project.rb', line 151

def run(install_flag)
  pod_install if install_flag
  run_test
end

#run_testObject



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cocoapods-unit-test/project.rb', line 136

def run_test
  UI.title "Test target: #{target}" do 
    schemes.each { |e| UI.puts "Test Schemes: #{e}" }
  end

  system( "rm -fr #{derived_data_path}")
  system( "mkdir -p #{derived_data_path}")

  schemes.each do |scheme|
    UI.puts "Testing #{scheme}..."
    run_test_with_scheme(scheme)
  end
end

#run_test_with_scheme(scheme) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/cocoapods-unit-test/project.rb', line 105

def run_test_with_scheme(scheme)
  system("rm -fr #{result_bundle_path(scheme)}")
  system("rm -fr #{result_json_path(scheme)}")
  system("rm -fr #{archives_path(scheme)}")
  system("mkdir -p #{archives_path(scheme)}")

  cmd = %W(xcodebuild test -workspace #{workspace} -scheme #{scheme}  -UseModernBuildSystem=NO 
    -derivedDataPath #{derived_data_path} -resultBundlePath #{result_bundle_path(scheme)}) + [
    "-destination '#{destination}'",
    "| xcpretty",
  ]

  UI.puts cmd.join(" ")
  system(cmd.join(" "))

  cmd = %W(xcrun xcresulttool get --path #{result_bundle_path(scheme)} --format json) + [
    ">  #{result_json_path(scheme)}",
  ]
  UI.puts cmd.join(" ")
  system(cmd.join(" "))

  cmd = %W(#{cov_shell} -x -o #{archives_path(scheme)})
  UI.puts cmd.join(" ")
  system(cmd.join(" "))

  index_file = File.join(archives_path(scheme),"lcov/index.html")
  TestResult.new(result_json_path(scheme),index_file).parse()
  UI.puts "Test result path #{index_file}"
  system("open #{index_file}") if File.file?(index_file)
end

#validate!Object

Raises:

  • (Informative)


90
91
92
93
94
# File 'lib/cocoapods-unit-test/project.rb', line 90

def validate!
  raise Informative, "No Podfile!" unless File.file?(podfile)
  validate_target!
  raise Informative, "No Test schemes!" if schemes.empty?
end

#validate_target!Object

Raises:

  • (Informative)


58
59
60
61
62
63
64
# File 'lib/cocoapods-unit-test/project.rb', line 58

def validate_target!
  project = Xcodeproj::Project.open(pods_project)
  return true if project.unit_test_dev_pods().include?(@target)
  return true if project.unit_test_dependency_pods().include?(@target)
  raise Informative, "can not find test target #{@target} in Pods.xcodeproj"
  return false
end