cartage 1.2 → 2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  gem 'minitest'
4
4
  require 'minitest/autorun'
@@ -6,68 +6,6 @@ require 'minitest/pretty_diff'
6
6
  require 'minitest/focus'
7
7
  require 'minitest/moar'
8
8
  require 'minitest/bisect'
9
+ require 'minitest-bonus-assertions'
9
10
 
10
- require 'cartage'
11
-
12
- module Minitest::ENVStub
13
- def stub_env env, options = {}, *block_args, &block
14
- mock = lambda { |key|
15
- env.fetch(key) { |k|
16
- if options[:passthrough]
17
- ENV.send(:"__minitest_stub__[]", k)
18
- else
19
- nil
20
- end
21
- }
22
- }
23
-
24
- if defined? Minitest::Moar::Stubbing
25
- stub ENV, :[], mock, *block_args, &block
26
- else
27
- ENV.stub :[], mock, *block_args, &block
28
- end
29
- end
30
-
31
- def stub_cartage_repo_url value = nil, &block
32
- instance_stub Cartage, :repo_url,
33
- -> { value || 'git://host/repo-url.git' }, &block
34
- end
35
-
36
- def stub_backticks value
37
- Kernel.send(:alias_method, :__stub_backticks__, :`)
38
- Kernel.send(:define_method, :`) { |*| value }
39
- yield
40
- ensure
41
- Kernel.send(:undef_method, :`)
42
- Kernel.send(:alias_method, :`, :__stub_backticks__)
43
- Kernel.send(:undef_method, :__stub_backticks__)
44
- end
45
-
46
- def stub_file_open_for_write expected, string_io = StringIO.new
47
- mfile = File.singleton_class
48
- mfile.send(:alias_method, :__stub_open__, :open)
49
-
50
- fopen = ->(*, &block) {
51
- if block
52
- block.call(string_io)
53
- else
54
- string_io
55
- end
56
- }
57
-
58
- mfile.send(:define_method, :open, &fopen)
59
- yield
60
- actual = String.new(string_io.string)
61
- ensure
62
- mfile.send(:undef_method, :open)
63
- mfile.send(:alias_method, :open, :__stub_open__)
64
- mfile.send(:undef_method, :__stub_open__)
65
- assert_equal expected, actual
66
- end
67
-
68
- def stub_pathname_expand_path value, &block
69
- instance_stub Pathname, :expand_path, Pathname(value), &block
70
- end
71
-
72
- Minitest::Test.send(:include, self)
73
- end
11
+ require 'cartage/minitest'
@@ -1,161 +1,218 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'minitest_config'
2
4
 
3
5
  describe Cartage do
4
- before do
5
- @cartage = Cartage.new
6
+ let(:config) { nil }
7
+ let(:cartage) { Cartage.new(config) }
8
+
9
+ describe '#initialize' do
10
+ it 'loads an empty configuration when created without a config' do
11
+ assert_equal Cartage::Config.new, cartage.config
12
+ end
6
13
  end
7
14
 
8
- describe '#name' do
15
+ describe '#name, #name=' do
9
16
  it 'defaults to #default_name' do
10
17
  stub_cartage_repo_url do
11
- assert_equal 'repo-url', @cartage.name
12
- assert_equal @cartage.default_name, @cartage.name
18
+ assert_equal 'repo-url', cartage.name
19
+ assert_equal cartage.default_name, cartage.name
13
20
  end
14
21
  end
15
22
 
16
23
  it 'can be overridden' do
17
24
  stub_cartage_repo_url do
18
- @cartage.name = 'xyz'
19
- assert_equal 'xyz', @cartage.name
25
+ cartage.name = 'xyz'
26
+ assert_equal 'xyz', cartage.name
20
27
  end
21
28
  end
22
29
  end
23
30
 
24
- describe '#root_path' do
31
+ describe '#root_path, #root_path=' do
25
32
  it 'defaults to #default_root_path' do
26
- stub_backticks '/x/y/z' do
27
- assert_equal Pathname('/x/y/z'), @cartage.root_path
28
- assert_equal @cartage.default_root_path, @cartage.root_path
33
+ stub_cartage_repo_url do
34
+ stub_backticks '/x/y/z' do
35
+ assert_equal Pathname('/x/y/z'), cartage.root_path
36
+ assert_equal cartage.default_root_path, cartage.root_path
37
+ end
29
38
  end
30
39
  end
31
40
 
32
41
  it 'can be overridden and creates a Pathname' do
33
42
  stub_cartage_repo_url do
34
- @cartage.root_path = '/a/b/c'
35
- assert_equal Pathname('/a/b/c'), @cartage.root_path
43
+ cartage.root_path = '/a/b/c'
44
+ assert_equal Pathname('/a/b/c'), cartage.root_path
36
45
  end
37
46
  end
38
47
 
39
48
  it 'expands the created Pathname' do
40
49
  stub_cartage_repo_url do
41
- @cartage.root_path = '/a/b/../c'
42
- assert_equal Pathname('/a/c'), @cartage.root_path
50
+ cartage.root_path = '/a/b/../c'
51
+ assert_equal Pathname('/a/c'), cartage.root_path
43
52
  end
44
53
  end
45
54
  end
46
55
 
47
- describe '#timestamp' do
56
+ describe '#timestamp, #timestamp=' do
48
57
  it 'defaults to #now.utc.strftime("%Y%m%d%H%M%S")' do
49
- stub Time, :now, -> { Time.new(2014, 12, 31, 23, 59, 59, "+00:00") } do
50
- assert_equal '20141231235959', @cartage.timestamp
51
- assert_equal @cartage.default_timestamp, @cartage.timestamp
58
+ stub Time, :now, -> { Time.new(2014, 12, 31, 23, 59, 59, '+00:00') } do
59
+ assert_equal '20141231235959', cartage.timestamp
60
+ assert_equal cartage.default_timestamp, cartage.timestamp
52
61
  end
53
62
  end
54
63
 
55
64
  it 'does no validation of assigned values' do
56
- @cartage.timestamp = 'not a timestamp'
57
- assert_equal 'not a timestamp', @cartage.timestamp
65
+ cartage.timestamp = 'not a timestamp'
66
+ assert_equal 'not a timestamp', cartage.timestamp
67
+ end
68
+ end
69
+
70
+ describe '#target, #target=' do
71
+ it 'defaults to tmp/ as a Pathname' do
72
+ assert_equal Pathname('tmp'), cartage.target
73
+ assert_equal cartage.default_target, cartage.target
74
+ end
75
+
76
+ it 'it converts the assigned value into a pathname' do
77
+ cartage.target = '/a/b/c'
78
+ assert_equal Pathname('/a/b/c'), cartage.target
58
79
  end
59
80
  end
60
81
 
61
- describe '#without_groups' do
62
- it 'defaults to %w(development test)' do
63
- assert_equal %w(development test), @cartage.without_groups
64
- assert_equal @cartage.default_without_groups, @cartage.without_groups
82
+ describe '#compression, #compression=' do
83
+ it 'defaults to :bzip2' do
84
+ assert_equal :bzip2, cartage.compression
85
+ end
86
+
87
+ it 'accepts bzip2, none, and gzip as valid values' do
88
+ candidates = %i(bzip2 none gzip)
89
+ candidates += candidates.map(&:to_s)
90
+ candidates.each { |candidate|
91
+ cartage.compression = candidate
92
+ assert_equal candidate, cartage.compression
93
+ }
94
+ end
95
+
96
+ it 'fails if given an unknown type' do
97
+ assert_raises_with_message ArgumentError, 'Invalid compression type nil' do
98
+ cartage.compression = nil
99
+ end
100
+ assert_raises_with_message ArgumentError, 'Invalid compression type 3' do
101
+ cartage.compression = 3
102
+ end
103
+ assert_raises_with_message ArgumentError, 'Invalid compression type "foo"' do
104
+ cartage.compression = 'foo'
105
+ end
106
+ end
107
+
108
+ it 'affects #tar_compression_extension' do
109
+ assert_equal '.bz2', cartage.tar_compression_extension
110
+
111
+ cartage.compression = 'none'
112
+ assert_equal '', cartage.tar_compression_extension
113
+
114
+ cartage.compression = 'gzip'
115
+ assert_equal '.gz', cartage.tar_compression_extension
65
116
  end
66
117
 
67
- it 'wraps arrays correctly' do
68
- @cartage.without_groups = 'dit'
69
- assert_equal %w(dit), @cartage.without_groups
118
+ it 'affects #tar_compression_flag' do
119
+ assert_equal 'j', cartage.tar_compression_flag
120
+
121
+ cartage.compression = 'none'
122
+ assert_equal '', cartage.tar_compression_flag
123
+
124
+ cartage.compression = 'gzip'
125
+ assert_equal 'z', cartage.tar_compression_flag
70
126
  end
71
127
  end
72
128
 
73
- describe '#base_config' do
74
- it 'defaults to an empty OpenStruct' do
75
- assert_equal OpenStruct.new, @cartage.base_config
76
- assert_equal @cartage.default_base_config, @cartage.base_config
77
- refute_same @cartage.default_base_config, @cartage.base_config
129
+ describe '#dependency_cache, #dependency_cache_path, #dependency_cache_path=' do
130
+ it 'defaults to #tmp_path{,/dependency-cache.tar.bz2}' do
131
+ assert_equal cartage.tmp_path, cartage.dependency_cache_path
132
+ assert_equal cartage.tmp_path.join('dependency-cache.tar.bz2'),
133
+ cartage.dependency_cache
134
+ end
135
+
136
+ it 'is affected by the compression setting' do
137
+ cartage.compression = 'none'
138
+ assert_equal cartage.tmp_path.join('dependency-cache.tar'),
139
+ cartage.dependency_cache
140
+
141
+ cartage.compression = 'gzip'
142
+ assert_equal cartage.tmp_path.join('dependency-cache.tar.gz'),
143
+ cartage.dependency_cache
144
+ end
145
+
146
+ it 'becomes a Pathname' do
147
+ cartage.dependency_cache_path = 'foo'
148
+ assert_equal Pathname('foo').expand_path, cartage.dependency_cache_path
149
+ assert_equal Pathname('foo/dependency-cache.tar.bz2').expand_path,
150
+ cartage.dependency_cache
78
151
  end
79
152
  end
80
153
 
81
154
  describe '#config' do
82
- before do
83
- data = {
155
+ let(:data) {
156
+ {
157
+ release_hashref: '12345',
84
158
  x: 1,
85
159
  plugins: {
86
160
  test: {
87
161
  x: 2
88
162
  }
89
163
  },
90
- development: {
91
- x: 3,
92
- plugins: {
93
- test: {
94
- x: 4
95
- }
164
+ commands: {
165
+ test: {
166
+ x: 3
96
167
  }
97
168
  }
98
169
  }
170
+ }
171
+ let(:config) { Cartage::Config.new(Cartage::Config.send(:ostructify, data)) }
99
172
 
100
- @config = Cartage::Config.new(Cartage::Config.send(:ostructify, data))
101
-
102
- @cartage.environment = :development
103
- @cartage.base_config = @config
104
- end
105
-
106
- it 'knows the subset for "development"' do
107
- assert_equal @config.development, @cartage.config
173
+ it 'knows the plugin "test"' do
174
+ assert_equal config.plugins.test, cartage.config(for_plugin: :test)
108
175
  end
109
176
 
110
- it 'knows the subset for the "test" plugin in "development"' do
111
- assert_equal @config.development.plugins.test,
112
- @cartage.config(for_plugin: 'test')
177
+ it 'knows the command "test"' do
178
+ assert_equal config.commands.test, cartage.config(for_command: :test)
113
179
  end
114
180
 
115
- it 'knows the subset "test" plugin without environment' do
116
- assert_equal @config.plugins.test,
117
- @cartage.config(with_environment: false, for_plugin: :test)
181
+ it 'knows the whole config' do
182
+ assert_equal config, cartage.config
118
183
  end
119
184
 
120
- it 'knows the whole config without environment' do
121
- assert_equal @config, @cartage.config(with_environment: false)
185
+ it 'fails with an error when asking for plugin and command together' do
186
+ exception = assert_raises ArgumentError do
187
+ cartage.config(for_plugin: :test, for_command: :test)
188
+ end
189
+ assert_match(/Cannot get config/, exception.message)
122
190
  end
123
191
  end
124
192
 
125
- describe '#bundle_cache' do
126
- def assert_bundle_cache_equal(path)
127
- path = Pathname(path).join('vendor-bundle.tar.bz2')
128
- expected = Pathname(".").join(path)
129
-
130
- instance_stub Pathname, :expand_path, expected do
131
- yield if block_given?
132
- assert_equal expected, @cartage.bundle_cache
133
- end
193
+ describe '#dependency_cache' do
194
+ def assert_dependency_cache_equal(path)
195
+ yield if block_given?
196
+ assert_equal Pathname('.').join(path).expand_path, cartage.dependency_cache
134
197
  end
135
198
 
136
- it 'defaults to "tmp/vendor-bundle.tar.bz2"' do
137
- assert_bundle_cache_equal 'tmp/vendor-bundle.tar.bz2'
199
+ it 'defaults to "tmp/dependency-cache.tar.bz2"' do
200
+ assert_dependency_cache_equal 'tmp/dependency-cache.tar.bz2'
138
201
  end
139
202
 
140
- it 'can be overridden' do
141
- assert_bundle_cache_equal 'tmp/vendor-bundle.tar.bz2'
142
- assert_bundle_cache_equal 'tmpx/vendor-bundle.tar.bz2' do
143
- @cartage.bundle_cache 'tmpx'
203
+ it 'can be overridden through #dependency_cache_path=' do
204
+ assert_dependency_cache_equal 'tmp/dependency-cache.tar.bz2'
205
+ assert_dependency_cache_equal 'tmpx/dependency-cache.tar.bz2' do
206
+ cartage.dependency_cache_path = 'tmpx'
144
207
  end
145
208
  end
146
209
  end
147
210
 
148
211
  describe '#release_hashref' do
149
212
  it 'returns the current hashref' do
150
- stub_backticks '12345' do
151
- assert_equal '12345', @cartage.release_hashref
152
- end
153
- end
154
-
155
- it 'saves the hashref to a file with save_to:' do
156
- stub_backticks '54321' do
157
- stub_file_open_for_write '54321' do
158
- @cartage.release_hashref(save_to: 'x')
213
+ stub_cartage_repo_url do
214
+ stub_backticks '12345' do
215
+ assert_equal '12345', cartage.release_hashref
159
216
  end
160
217
  end
161
218
  end
@@ -177,85 +234,172 @@ describe Cartage do
177
234
  origin
178
235
  stub_backticks origin do
179
236
  assert_equal 'git@github.com:KineticCafe/cartage.git',
180
- @cartage.repo_url
237
+ cartage.repo_url
181
238
  end
182
239
  end
183
240
  end
184
241
 
185
- describe '#final_tarball, #final_release_hashref' do
242
+ describe 'computed paths' do
186
243
  before do
187
- @cartage.name = 'test'
188
- @cartage.timestamp = 'value'
189
- @cartage.instance_variable_set(:@root_path, Pathname('source'))
244
+ cartage.root_path = '/a/b/c'
245
+ cartage.name = 'test'
246
+ cartage.timestamp = 'value'
247
+ end
248
+
249
+ it '#tmp_path is #root_path/tmp' do
250
+ assert_equal Pathname('/a/b/c/tmp'), cartage.tmp_path
190
251
  end
191
252
 
192
- it 'sets the final tarball name correctly' do
193
- assert_equal Pathname('source/tmp/test-value.tar.bz2'),
194
- @cartage.final_tarball
253
+ it '#work_path is #tmp_path/#name' do
254
+ assert_equal Pathname('/a/b/c/tmp/test'), cartage.work_path
195
255
  end
196
256
 
197
- it 'sets the final release hashref name correctly' do
198
- assert_equal Pathname('source/tmp/test-value-release-hashref.txt'),
199
- @cartage.final_release_hashref
257
+ it '#final_name is #tmp_path/#name-#timestamp' do
258
+ assert_equal Pathname('/a/b/c/tmp/test-value'), cartage.final_name
259
+ end
260
+
261
+ # it '#build_tarball.final_tarball is #final_name.tar#compression_extension' do
262
+ # assert_equal Pathname('/a/b/c/tmp/test-value.tar.bz2'),
263
+ # cartage.build_tarball.final_tarball
264
+ #
265
+ # cartage.compression = 'none'
266
+ # assert_equal Pathname('/a/b/c/tmp/test-value.tar'),
267
+ # cartage.build_tarball.final_tarball
268
+ #
269
+ # cartage.compression = 'gzip'
270
+ # assert_equal Pathname('/a/b/c/tmp/test-value.tar.gz'),
271
+ # cartage.build_tarball.final_tarball
272
+ # end
273
+
274
+ it '#final_release_metadata_json is #final_name-release-metadata.json' do
275
+ assert_equal Pathname('/a/b/c/tmp/test-value-release-metadata.json'),
276
+ cartage.final_release_metadata_json
200
277
  end
201
278
  end
202
279
 
203
280
  describe '#display' do
204
281
  it 'does not display a message if not verbose' do
205
- @cartage.verbose = false
282
+ cartage.verbose = false
206
283
  assert_silent do
207
- @cartage.display 'hello'
284
+ cartage.display 'hello'
208
285
  end
209
286
  end
210
287
 
211
288
  it 'displays a message if verbose' do
212
- @cartage.verbose = true
289
+ cartage.verbose = true
213
290
  assert_output "hello\n" do
214
- @cartage.display 'hello'
291
+ cartage.display 'hello'
215
292
  end
216
293
  end
217
294
 
218
295
  it 'does not display a message if verbose and quiet' do
219
- @cartage.verbose = true
220
- @cartage.quiet = true
296
+ cartage.verbose = true
297
+ cartage.quiet = true
221
298
  assert_silent do
222
- @cartage.display 'hello'
299
+ cartage.display 'hello'
223
300
  end
224
301
  end
225
302
  end
226
303
 
227
- describe '#run (private)' do
304
+ describe '#run' do
228
305
  before do
229
306
  @command = %w(echo yes)
230
307
  end
231
308
 
232
309
  it 'displays the output if not verbose and not quiet' do
233
310
  assert_output "yes\n" do
234
- @cartage.send(:run, @command)
311
+ cartage.send(:run, @command)
235
312
  end
236
313
  end
237
314
 
238
315
  it 'displays the command and output if verbose and not quiet' do
239
- @cartage.verbose = true
316
+ cartage.verbose = true
240
317
  assert_output "echo yes\nyes\n" do
241
- @cartage.send(:run, @command)
318
+ cartage.send(:run, @command)
242
319
  end
243
320
  end
244
321
 
245
322
  it 'displays nothing if quiet' do
246
- @cartage.verbose = true
247
- @cartage.quiet = true
323
+ cartage.verbose = true
324
+ cartage.quiet = true
248
325
  assert_silent do
249
- @cartage.send(:run, @command)
326
+ cartage.send(:run, @command)
250
327
  end
251
328
  end
252
329
 
253
330
  it 'raises an exception on error' do
254
- @cartage.quiet = true
331
+ cartage.quiet = true
255
332
  ex = assert_raises StandardError do
256
- @cartage.send(:run, %w(false))
333
+ cartage.send(:run, %w(false))
257
334
  end
258
- assert_equal %Q(Error running 'false'), ex.message
335
+ assert_equal "Error running 'false'", ex.message
336
+ end
337
+ end
338
+
339
+ describe '#release_metadata' do
340
+ it 'has the structure we want' do
341
+ stub_cartage_repo_url do
342
+ cartage.send(:release_hashref=, '12345')
343
+ cartage.timestamp = 3
344
+
345
+ expected = {
346
+ package: {
347
+ name: 'repo-url',
348
+ repo: {
349
+ type: 'git',
350
+ url: 'git://host/repo-url.git'
351
+ },
352
+ hashref: '12345',
353
+ timestamp: 3
354
+ }
355
+ }
356
+
357
+ assert_equal expected, cartage.release_metadata
358
+ end
359
+ end
360
+ end
361
+
362
+ describe '#build_package' do
363
+ def disable_unsafe_method(*names, &block)
364
+ block ||= -> {}
365
+ names.each { |name| cartage.define_singleton_method name, &block }
366
+ end
367
+
368
+ before do
369
+ disable_unsafe_method :prepare_work_area, :restore_modified_files,
370
+ :save_release_metadata, :extract_dependency_cache
371
+ disable_unsafe_method :create_dependency_cache, &->(_) {}
372
+ end
373
+
374
+ it 'requests :vendor_dependencies and :vendor_dependencies#path' do
375
+ disable_unsafe_method :request_build_package
376
+
377
+ verify_request = ->(f) { assert_equal :vendor_dependencies, f }
378
+ verify_request_map = ->(f, m) {
379
+ assert_equal :vendor_dependencies, f
380
+ assert_equal :path, m
381
+ []
382
+ }
383
+
384
+ instance_stub Cartage::Plugins, :request, verify_request do
385
+ instance_stub Cartage::Plugins, :request_map, verify_request_map do
386
+ cartage.build_package
387
+ end
388
+ end
389
+ end
390
+
391
+ it 'requests :pre_build_package, :build_package, and :post_build_package' do
392
+ disable_unsafe_method :vendor_dependencies
393
+
394
+ requests = %i(pre_build_package build_package post_build_package)
395
+
396
+ verify_request = ->(f) { assert_equal requests.shift, f }
397
+
398
+ instance_stub Cartage::Plugins, :request, verify_request do
399
+ cartage.build_package
400
+ end
401
+
402
+ assert_empty requests
259
403
  end
260
404
  end
261
405
  end