/
integration_2.rb
384 lines (326 loc) · 12 KB
/
integration_2.rb
1
2
3
4
5
6
7
8
9
10
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# ------------------------------------ #
# CocoaPods Integration tests take 2 #
# ------------------------------------ #
#-----------------------------------------------------------------------------#
# The following integrations tests are based on file comparison.
#
# 1. For each test there is a folder with a `before` and `after` subfolders.
# 2. The contents of the before folder are copied to the `TMP_DIR` folder and
# then the given arguments are passed to the `POD_BINARY`.
# 3. After the pod command completes the execution the each file in the
# `after` subfolder is compared to be to the contents of the temporary
# directory. If the contents of the file do not match an error is
# registered. Xcode projects are compared in an UUID agnostic way.
#
# Notes:
#
# - The output of the pod command is saved in the `execution_output.txt` file
# which should be added to the `after` folder to test the CocoaPods UI.
# - To create a new test, just create a before folder with the environment to
# test, copy it to the after folder and run the tested pod command inside.
# Then just add the tests below this files with the name of the folder and
# the arguments.
#
# Rationale:
#
# - Have a way to track precisely the evolution of the artifacts (and of the
# UI) produced by CocoaPods (git diff of the after folders).
# - Allow uses to submit pull requests with the environment necessary to
# reproduce an issue.
# - Have robust tests which don't depend on the programmatic interface of
# CocoaPods. These tests depend only the binary and its arguments an thus are
# suitable for testing CP regardless of the implementation (they could even
# work for an Objective-C one)
#-----------------------------------------------------------------------------#
# The spec helper is not required on purpose to keep those tests segregated.
# It would also create issues because it clears the temp folder before every
# requirement (`it` call).
require 'pathname'
# @return [Pathname] The root of the repo.
#
ROOT = Pathname.new(File.expand_path('../../', __FILE__)) unless defined? ROOT
require 'rubygems'
require 'bundler/setup'
require 'pretty_bacon'
require 'colored'
require 'diffy'
require 'Xcodeproj' # For Differ
# @return [Pathname The folder where the CocoaPods binary should operate.
#
TMP_DIR = ROOT + 'tmp' unless defined? TMP_DIR
# @return [String] The CocoaPods binary to use for the tests.
#
POD_BINARY = "ruby " + ROOT.to_s + '/bin/pod' unless defined? POD_BINARY
#-----------------------------------------------------------------------------#
# @!group Helpers
# Copies the before subdirectory of the given tests folder in the temporary
# directory.
#
# @param [String] folder
# the name of the folder of the tests.
#
def copy_files(folder)
source = File.expand_path("../integration/#{folder}/before", __FILE__)
destination = TMP_DIR + folder
destination.mkpath
FileUtils.cp_r(Dir.glob("#{source}/*"), destination)
end
# Runs the Pod executable with the given arguments in the temporary directory.
#
# @param [String] arguments
# the arguments to pass to the CocoaPods binary.
#
# @note If the pod binary is called with the ruby executable it requires
# bundler ensuring that the execution is performed in the correct
# environment.
#
def launch_binary(arguments, folder)
command = "CP_AGGRESSIVE_CACHE=TRUE #{POD_BINARY} #{arguments} --verbose --no-color 2>&1"
Dir.chdir(TMP_DIR + folder) do
output = `#{command}`
it "$ pod #{arguments}" do
$?.should.satisfy("Pod binary failed\n\n#{output}") do
$?.success?
end
end
File.open('execution_output.txt', 'w') do |file|
file.write(command.gsub(POD_BINARY, '$ pod'))
file.write(output.gsub(ROOT.to_s, 'ROOT').gsub(%r[/Users/.*/Library/Caches/CocoaPods/],"CACHES_DIR/"))
end
end
$?.success?
end
# Creates a YAML representation of the Xcodeproj files which should be used as
# a reference.
#
def run_post_execution_actions(folder)
Dir.glob("#{TMP_DIR + folder}/**/*.xcodeproj") do |project_path|
xcodeproj = Xcodeproj::Project.new(project_path)
require 'yaml'
pretty_print = xcodeproj.pretty_print
sections = []
sorted_keys = ['File References', 'Targets', 'Build Configurations']
sorted_keys.each do |key|
yaml = { key => pretty_print[key]}.to_yaml
sections << yaml
end
file_contents = (sections * "\n\n").gsub!("---",'')
File.open("#{project_path}.yaml", 'w') do |file|
file.write(file_contents)
end
end
end
# Creates a requirement which compares every file in the after folder with the
# artifacts created by the pod executable in the temporary directory according
# to its file type.
#
# @param [String] folder
# the name of the folder of the tests.
#
def check_with_folder(folder)
source = File.expand_path("../integration/#{folder}", __FILE__)
Dir.glob("#{source}/after/**/*") do |expected_path|
next unless File.file?(expected_path)
relative_path = expected_path.gsub("#{source}/after/", '')
expected = Pathname.new(expected_path)
produced = TMP_DIR + folder + relative_path
case expected_path
when %r[/xcuserdata/], %r[\.pbxproj$]
# Projects are compared through the more readable yaml representation
next
when %r[execution_output.txt$]
# TODO The output from the caches changes on Travis
next
end
it relative_path do
case expected_path
when %r[Podfile\.lock$], %r[xcodeproj\.yaml$]
file_should_exist(produced)
yaml_should_match(expected, produced)
else
file_should_exist(produced)
file_should_match(expected, produced)
end
end
end
end
#--------------------------------------#
# @!group File Comparisons
# Checks that the file exits.
#
# @param [Pathname] file
# The file to check.
#
def file_should_exist(file)
file.should.exist?
end
# Compares two lockfiles because CocoaPods 0.16 doesn't oder them in 1.8.7.
#
# @param [Pathname] expected
# The reference in the `after` folder.
#
# @param [Pathname] produced
# The file in the temporary directory after running the pod command.
#
def yaml_should_match(expected, produced)
expected_yaml = YAML::load(File.open(expected))
produced_yaml = YAML::load(File.open(produced))
# Remove CocoaPods version
expected_yaml.delete('COCOAPODS')
produced_yaml.delete('COCOAPODS')
desc = []
desc << "YAML comparison error `#{expected}`"
diff_options = {:key_1 => "$produced", :key_2 => "$expected"}
diff = Xcodeproj::Differ.diff(produced_yaml, expected_yaml, diff_options).to_yaml
diff.gsub!("$produced", "produced".green)
diff.gsub!("$expected", "expected".red)
desc << ("--- DIFF " << "-" * 70)
Diffy::Diff.new(expected.to_s, produced.to_s, :source => 'files', :context => 3).each do |line|
case line
when /^\+/ then desc << line.gsub("\n",'').green
when /^-/ then desc << line.gsub("\n",'').red
else desc << line.gsub("\n",'')
end
end
desc << ("--- DIFF " << "-" * 70)
desc << diff
desc << ("--- END " << "-" * 70)
expected_yaml.should.satisfy(desc * "\n\n") do
if RUBY_VERSION < "1.9"
true # CP is not sorting array derived from hashes whose order is
# undefined in 1.8.7
else
expected_yaml == produced_yaml
end
end
end
# Compares two Xcode projects in an UUID insensitive fashion and producing a
# clear diff to highlight the differences.
#
# @param [Pathname] expected @see #yaml_should_match
# @param [Pathname] produced @see #yaml_should_match
#
# def xcodeproj_should_match(expected, produced)
# expected_proj = Xcodeproj::Project.new(expected + '..')
# produced_proj = Xcodeproj::Project.new(produced + '..')
# diff = produced_proj.to_tree_hash.recursive_diff(expected_proj.to_tree_hash, "#produced#", "#reference#")
# desc = "Project comparison error `#{expected}`"
# if diff
# desc << "\n\n#{diff.inspect.cyan}"
# pretty_yaml = diff.to_yaml
# pretty_yaml = pretty_yaml.gsub(/['"]#produced#['"]/,'produced'.cyan)
# pretty_yaml = pretty_yaml.gsub(/['"]#reference#['"]/,'reference'.magenta)
# desc << "\n\n#{pretty_yaml}"
# end
# diff.should.satisfy(desc) do |diff|
# diff.nil?
# end
# end
# Compares two files to check if they are identical and produces a clear diff
# to highlight the differences.
#
# @param [Pathname] expected @see #yaml_should_match
# @param [Pathname] produced @see #yaml_should_match
#
def file_should_match(expected, produced)
is_equal = FileUtils.compare_file(expected, produced)
description = []
description << "File comparison error `#{expected}`"
description << ""
description << ("--- DIFF " << "-" * 70)
Diffy::Diff.new(expected.to_s, produced.to_s, :source => 'files', :context => 3).each do |line|
case line
when /^\+/ then description << line.gsub("\n",'').green
when /^-/ then description << line.gsub("\n",'').red
else description << line.gsub("\n",'')
end
end
description << ("--- END " << "-" * 70)
description << ""
is_equal.should.satisfy(description * "\n") do
is_equal == true
end
end
#-----------------------------------------------------------------------------#
# @!group Description implementation
# Performs the checks for the test with the given folder using the given
# arguments.
#
# @param [String] arguments
# The arguments to pass to the Pod executable.
#
# @param [String] folder
# The name of the folder which contains the `before` and `after`
# subfolders.
#
def check(arguments, folder)
focused_check(arguments, folder)
end
# Shortcut to focus on a test: Comment the implementation of #check and
# call this from the relevant test.
#
def focused_check(arguments, folder)
copy_files(folder)
executed = launch_binary(arguments, folder)
run_post_execution_actions(folder)
check_with_folder(folder) if executed
end
#-----------------------------------------------------------------------------#
describe "Integration take 2" do
TMP_DIR.rmtree if TMP_DIR.exist?
TMP_DIR.mkpath
describe "Pod install" do
# Test installation with no integration
# Test subspecs inheritance
describe "Integrates a project with CocoaPods" do
check "install --no-repo-update", "install_new"
end
describe "Adds a Pod to an existing installation" do
check "install --no-repo-update", "install_add_pod"
end
describe "Removes a Pod from an existing installation" do
check "install --no-repo-update", "install_remove_pod"
end
describe "Creates an installation with multiple target definitions" do
check "install --no-repo-update", "install_multiple_targets"
end
describe "Installs a Pod with different subspecs activated across different targets" do
check "install --no-repo-update", "install_subspecs"
end
describe "Installs a Pod with a local source" do
check "install --no-repo-update", "install_local_source"
end
describe "Installs a Pod with an external source" do
check "install --no-repo-update", "install_external_source"
end
describe "Installs a Pod given the podspec" do
check "install --no-repo-update", "install_podspec"
end
describe "Performs an installation using a custom workspace" do
check "install --no-repo-update", "install_custom_workspace"
end
# @todo add tests for all the hooks API
#
describe "Runs the Podfile callbacks" do
check "install --no-repo-update", "install_podfile_callbacks"
end
# @todo add tests for all the hooks API
#
describe "Runs the specification callbacks" do
check "install --no-repo-update", "install_spec_callbacks"
end
end
#--------------------------------------#
describe "Pod update" do
describe "Updates an existing installation" do
check "update --no-repo-update", "update"
end
end
#--------------------------------------#
describe "Pod lint" do
describe "Lints a Pod" do
check "spec lint --quick", "spec_lint"
end
end
#--------------------------------------#
end