reapack-index 1.0beta3 → 1.0beta4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,214 @@
1
+ class TestConflictDetector < MiniTest::Test
2
+ include XMLHelper
3
+
4
+ def test_unique
5
+ cd = ReaPack::Index::ConflictDetector.new
6
+ cd['grp', 'pkg'].push :all, 'file1.lua'
7
+ cd['grp', 'pkg'].push :all, 'file2.lua'
8
+
9
+ assert_nil cd.resolve('grp', 'pkg')
10
+ assert_nil cd.resolve('grp')
11
+ end
12
+
13
+ def test_duplicates
14
+ cd = ReaPack::Index::ConflictDetector.new
15
+ cd['grp', 'pkg'].push :all, 'file1.lua'
16
+ cd['grp', 'pkg'].push :all, 'file1.lua'
17
+ cd['grp', 'pkg'].push :all, 'file2.lua'
18
+ cd['grp', 'pkg'].push :all, 'file2.lua'
19
+
20
+ assert_equal ["duplicate file 'file1.lua'", "duplicate file 'file2.lua'"],
21
+ cd.resolve('grp', 'pkg')
22
+ end
23
+
24
+ def test_same_platform
25
+ cd = ReaPack::Index::ConflictDetector.new
26
+ cd['grp', 'test'].push :windows, 'file.lua'
27
+ cd['grp', 'test'].push :windows, 'file.lua'
28
+
29
+ assert_equal ["duplicate file 'file.lua' on windows"], cd.resolve('grp')
30
+ assert_nil cd.resolve('other bucket')
31
+ end
32
+
33
+ def test_unmatching_platform
34
+ cd = ReaPack::Index::ConflictDetector.new
35
+ cd['grp', 'test'].push :darwin, 'file.lua'
36
+ cd['grp', 'test'].push :windows, 'file.lua'
37
+
38
+ assert_nil cd.resolve('grp')
39
+ end
40
+
41
+ def test_subplatform
42
+ cd = ReaPack::Index::ConflictDetector.new
43
+ cd['grp', 'test'].push :all, 'file.lua'
44
+ cd['grp', 'test'].push :windows, 'file.lua'
45
+
46
+ assert_equal ["duplicate file 'file.lua' on windows"], cd.resolve('grp')
47
+ end
48
+
49
+ def test_subsubplatform
50
+ cd = ReaPack::Index::ConflictDetector.new
51
+ cd['grp', 'test'].push :all, 'file.lua'
52
+ cd['grp', 'test'].push :win32, 'file.lua'
53
+
54
+ assert_equal ["duplicate file 'file.lua' on win32"], cd.resolve('grp')
55
+ end
56
+
57
+ def test_conflicts
58
+ cd = ReaPack::Index::ConflictDetector.new
59
+ cd['grp', 'package1.lua'].push :all, 'file1.lua'
60
+
61
+ cd['grp', 'package2.lua'].push :all, 'file1.lua'
62
+ cd['grp', 'package2.lua'].push :all, 'file2.lua'
63
+
64
+ cd['grp', 'package3.lua'].push :windows, 'file2.lua'
65
+
66
+ cd['grp', 'package4.lua'].push :darwin, 'file1.lua'
67
+
68
+ assert_nil cd.resolve('grp', 'not_specified.lua'), 'id = not_specified'
69
+
70
+ assert_equal ["'file1.lua' conflicts with 'package2.lua'"],
71
+ cd.resolve('grp', 'package1.lua'), 'id = package1'
72
+
73
+ assert_equal ["'file1.lua' conflicts with 'package1.lua'",
74
+ "'file2.lua' conflicts with 'package3.lua' on windows"],
75
+ cd.resolve('grp', 'package2.lua'), 'id = package2'
76
+
77
+ assert_equal ["'file2.lua' conflicts with 'package2.lua'"],
78
+ cd.resolve('grp', 'package3.lua'), 'id = package3'
79
+
80
+ # this conflict might happen on every platform,
81
+ # so it should not be reported it as darwin-only
82
+ assert_equal ["'file1.lua' conflicts with 'package1.lua'"],
83
+ cd.resolve('grp', 'package4.lua'), 'id = package4'
84
+ end
85
+
86
+ def test_conflicts_bidirectional
87
+ cd1 = ReaPack::Index::ConflictDetector.new
88
+
89
+ a1 = cd1['grp', 'a']
90
+ a1.push :all, 'file.lua'
91
+
92
+ b1 = cd1['grp', 'b']
93
+ b1.push :windows, 'file.lua'
94
+
95
+ assert_equal ["'file.lua' conflicts with 'b' on windows"],
96
+ a1.resolve, 'id = a'
97
+ assert_equal ["'file.lua' conflicts with 'a'"],
98
+ b1.resolve, 'id = b'
99
+
100
+ cd2 = ReaPack::Index::ConflictDetector.new
101
+ a2 = cd1['grp', 'a']
102
+ a2.push :windows, 'file.lua'
103
+
104
+ b2 = cd1['grp', 'b']
105
+ b2.push :all, 'file.lua'
106
+
107
+ assert_equal a1.resolve, a2.resolve
108
+ assert_equal b1.resolve, b2.resolve
109
+ end
110
+
111
+ def test_conflicts_platform_selection
112
+ cd1 = ReaPack::Index::ConflictDetector.new
113
+ cd1['grp', 'a'].push :all, 'file.lua'
114
+ cd1['grp', 'b'].push :windows, 'file.lua'
115
+ cd1['grp', 'c'].push :win32, 'file.lua'
116
+
117
+ assert_equal ["'file.lua' conflicts with 'b' on windows"],
118
+ cd1.resolve('grp', 'a'), 'id = a'
119
+ end
120
+
121
+ def test_duplicate_platform_selection
122
+ cd = ReaPack::Index::ConflictDetector.new
123
+ cd['grp', 'test'].push :windows, 'file.lua'
124
+ cd['grp', 'test'].push :all, 'file.lua'
125
+ assert_equal ["duplicate file 'file.lua' on windows"], cd.resolve('grp')
126
+
127
+ cd['grp', 'test'].push :all, 'file.lua'
128
+ assert_equal ["duplicate file 'file.lua'"], cd.resolve('grp')
129
+ end
130
+
131
+ def test_platform_same_level
132
+ cd1 = ReaPack::Index::ConflictDetector.new
133
+ cd1['grp', 'test'].push :all, 'file.lua'
134
+ cd1['grp', 'test'].push :win32, 'file.lua' # win32 first
135
+ cd1['grp', 'test'].push :darwin32, 'file.lua'
136
+
137
+ assert_equal ["duplicate file 'file.lua' on win32"], cd1.resolve('grp')
138
+
139
+ cd2 = ReaPack::Index::ConflictDetector.new
140
+ cd2['grp', 'test'].push :all, 'file.lua'
141
+ cd2['grp', 'test'].push :darwin32, 'file.lua' # darwin32 first
142
+ cd2['grp', 'test'].push :win32, 'file.lua'
143
+
144
+ assert_equal cd1.resolve('grp'), cd2.resolve('grp')
145
+ end
146
+
147
+ def test_remove_by_key
148
+ cd = ReaPack::Index::ConflictDetector.new
149
+ cd['grp', 'test'].push :all, 'file'
150
+
151
+ cd['grp', 'test'].clear
152
+ cd['grp', 'test'].push :all, 'file'
153
+
154
+ assert_equal nil, cd.resolve('grp', 'test')
155
+ end
156
+
157
+ def test_clone
158
+ cd1 = ReaPack::Index::ConflictDetector.new
159
+ cd1['grp', 'test'].push :all, 'file'
160
+
161
+ cd2 = cd1.clone
162
+
163
+ cd1['grp', 'test'].push :all, 'file'
164
+ assert_nil cd2.resolve('grp')
165
+
166
+ cd2['grp', 'test'].push :all, 'file'
167
+ refute_nil cd2.resolve('grp')
168
+ end
169
+
170
+ def test_load_xml
171
+ xml = <<-XML
172
+ <index version="1">
173
+ <category name="Other">
174
+ <reapack name="test1.lua" type="script">
175
+ <version name="1.0">
176
+ <source platform="all">http://irrelevant/url</source>
177
+ <source platform="all" file="background.png">http://google.com</source>
178
+ <source platform="win32" file="background.png">http://duplicate/file/</source>
179
+ </version>
180
+ </reapack>
181
+ <reapack name="test2.lua" type="script">
182
+ <version name="1.0">
183
+ <source platform="all" file="test1.lua">http://oops/conflict/</source>
184
+ </version>
185
+ </reapack>
186
+ <reapack name="empty_ver.lua" type="script">
187
+ <version name="a"/>
188
+ </reapack>
189
+ <reapack name="empty_pkg.lua"/>
190
+ </category>
191
+ <category name="Scripts">
192
+ <reapack name="test3.lua" type="script">
193
+ <version name="1.0">
194
+ <source platform="all" file="../Other/test1.lua">http://cross/category</source>
195
+ </version>
196
+ </reapack>
197
+ </category>
198
+ </index>
199
+ XML
200
+
201
+ cd = ReaPack::Index::ConflictDetector.new
202
+ cd.load_xml make_node(xml)
203
+
204
+ assert_equal ["'Other/test1.lua' conflicts with 'Other/test2.lua'",
205
+ "duplicate file 'Other/background.png' on win32"],
206
+ cd.resolve(:script, 'Other/test1.lua'), 'test1'
207
+
208
+ assert_equal ["'Other/test1.lua' conflicts with 'Other/test1.lua'"],
209
+ cd.resolve(:script, 'Other/test2.lua'), 'test2'
210
+
211
+ assert_equal ["'Other/test1.lua' conflicts with 'Other/test1.lua'"],
212
+ cd.resolve(:script, 'Scripts/test3.lua'), 'test3'
213
+ end
214
+ end
@@ -1,64 +1,7 @@
1
1
  require File.expand_path '../helper', __FILE__
2
2
 
3
- require 'git'
4
-
5
- module CLIUtils
6
- class FakeIO
7
- def initialize
8
- @getch = 'n'
9
- end
10
-
11
- attr_accessor :getch
12
- end
13
-
14
- def fake_input
15
- stdin = $stdin
16
- $stdin = FakeIO.new
17
-
18
- yield $stdin
19
- ensure
20
- $stdin = stdin
21
- end
22
-
23
- def wrapper(args = [], options = {})
24
- path = Dir.mktmpdir 'test-repository'
25
- old_pwd = Dir.pwd
26
-
27
- @git = Git.init path
28
- @git.config('user.name', 'John Doe')
29
- @git.config('user.email', 'john@doe.com')
30
-
31
- if options[:remote] != false
32
- options[:remote] ||= 'git@github.com:cfillion/test-repository.git'
33
- @git.add_remote 'origin', options[:remote]
34
- end
35
-
36
- options[:setup].call if options.has_key? :setup
37
-
38
- @indexer = ReaPack::Index::CLI.new \
39
- ['--no-progress', '--no-commit'] + args + ['--', path]
40
-
41
- yield if block_given?
42
- ensure
43
- @git = @indexer = nil
44
- Dir.chdir old_pwd
45
- FileUtils.rm_r path
46
- end
47
-
48
- def mkfile(file, content = String.new)
49
- fn = File.join @git.dir.to_s, file
50
- FileUtils.mkdir_p File.dirname(fn)
51
- File.write fn, content
52
- fn
53
- end
54
-
55
- def read_index(file = 'index.xml')
56
- File.read File.expand_path(file, @git.dir.to_s)
57
- end
58
- end
59
-
60
3
  class TestCLI < MiniTest::Test
61
- include CLIUtils
4
+ include CLIHelper
62
5
 
63
6
  def test_help
64
7
  assert_output /--help/, '' do
@@ -97,308 +40,6 @@ class TestCLI < MiniTest::Test
97
40
  end
98
41
  end
99
42
 
100
- def test_empty_branch
101
- wrapper do
102
- assert_output nil, /the current branch does not contains any commit/i do
103
- assert_equal true, @indexer.run
104
- end
105
- end
106
- end
107
-
108
- def test_scan_initial_commit
109
- wrapper do
110
- @git.add mkfile('test1.lua', '@version 1.0')
111
- @git.add mkfile('Category/test2.lua', '@version 1.0')
112
- @git.add mkfile('Category/Sub/test3.lua', '@version 1.0')
113
- @git.commit 'initial commit'
114
-
115
- assert_output /3 new packages/ do
116
- assert_equal true, @indexer.run
117
- end
118
-
119
- assert_match 'Category/test2.lua', read_index
120
- assert_match "raw/#{@git.log(1).last.sha}/test1.lua", read_index
121
- assert_match 'https://github.com/cfillion/test-repository/raw', read_index
122
-
123
- assert_match @git.log(1).last.date.utc.iso8601, read_index
124
- end
125
- end
126
-
127
- def test_scan_normal_commit
128
- wrapper do
129
- @git.add mkfile('README.md', '# Hello World')
130
- @git.commit 'initial commit'
131
-
132
- @git.add mkfile('test1.lua', '@version 1.0')
133
- @git.add mkfile('Category/test2.lua', '@version 1.0')
134
- @git.add mkfile('Category/Sub/test3.lua', '@version 1.0')
135
- @git.commit 'second commit'
136
-
137
- assert_output "3 new categories, 3 new packages, 3 new versions\n" do
138
- assert_equal true, @indexer.run
139
- end
140
-
141
- assert_match 'Category/test2.lua', read_index
142
- end
143
- end
144
-
145
- def test_pwd_is_subdirectory
146
- wrapper do
147
- @git.add mkfile('test1.lua', '@version 1.0')
148
- @git.commit 'initial commit'
149
-
150
- pwd = File.join(@git.dir.to_s, 'test')
151
- Dir.mkdir pwd
152
- Dir.chdir pwd
153
-
154
- assert_output /1 new package/ do
155
- assert_equal true, @indexer.run
156
- end
157
-
158
- assert_match 'test1.lua', read_index
159
- end
160
- end
161
-
162
- def test_verbose
163
- stdout, stderr = capture_io do
164
- wrapper ['--verbose'] do
165
- @git.add mkfile('test.lua', '@version 1.0')
166
- @git.add mkfile('test.png')
167
- @git.commit 'initial commit'
168
-
169
- assert_equal true, @indexer.run
170
- end
171
- end
172
-
173
- assert_equal "1 new category, 1 new package, 1 new version\n", stdout
174
- assert_match /reading configuration from .+\.reapack-index\.conf/i, stderr
175
- assert_match /processing [a-f0-9]{7}: initial commit/i, stderr
176
- assert_match /indexing new file test.lua/, stderr
177
- refute_match /indexing new file test.png/, stderr
178
- end
179
-
180
- def test_verbose_override
181
- wrapper ['--verbose', '--no-verbose'] do
182
- @git.add mkfile('README.md', '# Hello World')
183
- @git.commit 'initial commit'
184
-
185
- stdout, stderr = capture_io do
186
- assert_equal true, @indexer.run
187
- end
188
-
189
- assert_equal "empty index\n", stdout
190
- refute_match /processing [a-f0-9]{7}: initial commit/i, stderr
191
- end
192
- end
193
-
194
- def test_invalid_metadata
195
- wrapper do
196
- @git.add mkfile('test.lua', 'no version tag in this script!')
197
- @git.commit 'initial commit'
198
-
199
- assert_output nil, /Warning: test\.lua: Invalid metadata/i do
200
- assert_equal true, @indexer.run
201
- end
202
- end
203
- end
204
-
205
- def test_no_warnings
206
- wrapper ['-w'] do
207
- @git.add mkfile('test.lua', 'no version tag in this script!')
208
- @git.commit 'initial commit'
209
-
210
- _, stderr = capture_io do
211
- assert_equal true, @indexer.run
212
- end
213
-
214
- refute_match /Warning: test\.lua: Invalid metadata/i, stderr
215
- end
216
- end
217
-
218
- def test_no_warnings_override
219
- wrapper ['-w', '-W'] do
220
- @git.add mkfile('test.lua', 'no version tag in this script!')
221
- @git.commit 'initial commit'
222
-
223
- assert_output nil, /Warning: test\.lua: Invalid metadata/i do
224
- assert_equal true, @indexer.run
225
- end
226
- end
227
- end
228
-
229
- def test_index_from_last
230
- setup = proc {
231
- @git.add mkfile('test1.lua', '@version 1.0')
232
- @git.commit 'initial commit'
233
-
234
- mkfile 'index.xml', <<-XML
235
- <?xml version="1.0" encoding="utf-8"?>
236
- <index version="1" name="hello" commit="#{@git.log(1).last.sha}"/>
237
- XML
238
- }
239
-
240
- wrapper [], setup: setup do
241
- @git.add mkfile('test2.lua', '@version 1.0')
242
- @git.commit 'second commit'
243
-
244
- assert_output nil, '' do
245
- assert_equal true, @indexer.run
246
- end
247
-
248
- refute_match 'test1.lua', read_index
249
- assert_match 'test2.lua', read_index
250
- end
251
- end
252
-
253
- def test_index_from_invalid
254
- setup = proc {
255
- @git.add mkfile('test1.lua', '@version 1.0')
256
- @git.commit 'initial commit'
257
-
258
- mkfile 'index.xml', <<-XML
259
- <?xml version="1.0" encoding="utf-8"?>
260
- <index version="1" name="hello" commit="hello world"/>
261
- XML
262
- }
263
-
264
- wrapper [], setup: setup do
265
- @git.add mkfile('test2.lua', '@version 1.0')
266
- @git.commit 'second commit'
267
-
268
- assert_output nil, '' do
269
- assert_equal true, @indexer.run
270
- end
271
-
272
- assert_match 'test1.lua', read_index
273
- assert_match 'test2.lua', read_index
274
- end
275
- end
276
-
277
- def test_index_from_inexistent
278
- setup = proc {
279
- @git.add mkfile('test.lua', '@version 1.0')
280
- @git.commit 'initial commit'
281
-
282
- mkfile 'index.xml', <<-XML
283
- <?xml version="1.0" encoding="utf-8"?>
284
- <index version="1" commit="0000000000000000000000000000000000000000"/>
285
- XML
286
- }
287
-
288
- wrapper [], setup: setup do
289
- assert_output nil, nil do
290
- assert_equal true, @indexer.run
291
- end
292
- end
293
- end
294
-
295
- def test_index_from_long_hash
296
- setup = proc {
297
- @git.add mkfile('test.lua', '@version 1.0')
298
- @git.commit 'initial commit'
299
-
300
- mkfile 'index.xml', <<-XML
301
- <?xml version="1.0" encoding="utf-8"?>
302
- <index version="1" commit="0000000000000000000000000000000000000deadbeef"/>
303
- XML
304
- }
305
-
306
- wrapper [], setup: setup do
307
- assert_output nil, nil do
308
- assert_equal true, @indexer.run
309
- end
310
- end
311
- end
312
-
313
- def test_no_amend
314
- setup = proc {
315
- @git.add mkfile('Test/test.lua', '@version 1.0')
316
- @git.commit 'initial commit'
317
-
318
- mkfile 'index.xml', <<-XML
319
- <?xml version="1.0" encoding="utf-8"?>
320
- <index version="1" commit="#{@git.log(1).last.sha}">
321
- <category name="Test">
322
- <reapack name="test.lua" type="script">
323
- <version name="1.0"/>
324
- </reapack>
325
- </category>
326
- </index>
327
- XML
328
- }
329
-
330
- wrapper ['--no-amend'], setup: setup do
331
- @git.add mkfile('Test/test.lua', "@version 1.0\n@author cfillion")
332
- @git.commit 'second commit'
333
-
334
- assert_output '', /nothing to do/i do
335
- assert_equal true, @indexer.run
336
- end
337
-
338
- refute_match 'cfillion', read_index
339
- end
340
- end
341
-
342
- def test_amend
343
- setup = proc {
344
- @git.add mkfile('Test/test.lua', '@version 1.0')
345
- @git.commit 'initial commit'
346
-
347
- mkfile 'index.xml', <<-XML
348
- <?xml version="1.0" encoding="utf-8"?>
349
- <index version="1" name="hello" commit="#{@git.log(1).last.sha}">
350
- <category name="Test">
351
- <reapack name="test.lua" type="script">
352
- <version name="1.0"/>
353
- </reapack>
354
- </category>
355
- </index>
356
- XML
357
- }
358
-
359
- wrapper ['--amend'], setup: setup do
360
- @git.add mkfile('Test/test.lua', "@version 1.0\n@author cfillion")
361
- @git.commit 'second commit'
362
-
363
- assert_output /1 modified package/i, '' do
364
- assert_equal true, @indexer.run
365
- end
366
-
367
- assert_match 'cfillion', read_index
368
- end
369
- end
370
-
371
- def test_remove
372
- wrapper do
373
- script = mkfile 'test.lua', '@version 1.0'
374
-
375
- @git.add script
376
- @git.commit 'initial commit'
377
-
378
- @git.remove script
379
- @git.commit 'second commit'
380
-
381
- assert_output /1 removed package/i do
382
- assert_equal true, @indexer.run
383
- end
384
-
385
- refute_match 'test.lua', read_index
386
- end
387
- end
388
-
389
- def test_output
390
- wrapper ['-o output.xml'] do
391
- @git.add mkfile('test.lua', '@version 1.0')
392
- @git.commit 'initial commit'
393
-
394
- capture_io do
395
- assert_equal true, @indexer.run
396
- end
397
-
398
- assert_match 'test.lua', read_index('output.xml')
399
- end
400
- end
401
-
402
43
  def test_missing_argument
403
44
  assert_output nil, /missing argument/ do
404
45
  i = ReaPack::Index::CLI.new ['--output']
@@ -406,92 +47,51 @@ class TestCLI < MiniTest::Test
406
47
  end
407
48
  end
408
49
 
409
- def test_multibyte_filename
410
- wrapper do
411
- script = mkfile("\342\200\224.lua")
412
-
413
- @git.add script
414
- @git.commit 'initial commit'
415
-
416
- @git.remove script
417
- @git.commit 'remove test'
418
-
419
- assert_output { @indexer.run }
420
- end
421
- end
422
-
423
- def test_invalid_unicode_sequence
424
- wrapper do
425
- @git.add mkfile('.gitkeep')
426
- @git.commit 'initial commit'
427
-
428
- @git.add mkfile('test.lua', "@version 1.0\n\n\x97")
429
- @git.commit 'second commit'
430
-
431
- assert_output { @indexer.run }
50
+ def test_output
51
+ wrapper ['-o output.xml'] do
52
+ assert_equal mkpath('output.xml'), @cli.index.path
432
53
  end
433
54
  end
434
55
 
435
56
  def test_create_commit
436
57
  wrapper ['--commit'] do
437
- @git.add mkfile('.gitkeep')
438
- @git.commit 'initial commit'
439
-
440
- mkfile 'ignored1'
441
- @git.add mkfile('ignored2')
442
-
443
- assert_output("empty index\n", /commit created\n/) { @indexer.run }
58
+ assert_output("empty index\n", /commit created\n/) { @cli.run }
444
59
 
445
- commit = @git.log(1).last
60
+ commit = @git.last_commit
446
61
  assert_equal 'index: empty index', commit.message
447
- assert_equal ['index.xml'], commit.diff_parent.map {|d| d.path }
448
- end
449
- end
450
-
451
- def test_create_initial_commit
452
- wrapper ['--commit'] do
453
- mkfile 'ignored1'
454
- @git.add mkfile('ignored2')
455
-
456
- assert_output("empty index\n", /commit created\n/) { @indexer.run }
457
-
458
- commit = @git.log(1).last
459
- assert_equal 'index: empty index', commit.message
460
- assert_equal ['index.xml'], commit.gtree.files.keys
62
+ assert_equal ['index.xml'], commit.each_diff.map {|d| d.file }
461
63
  end
462
64
  end
463
65
 
464
66
  def test_create_commit_accept
465
67
  wrapper ['--prompt-commit'] do
466
- @git.add mkfile('.gitkeep')
467
- @git.commit 'initial commit'
68
+ @git.create_commit 'initial commit', [mkfile('.gitkeep')]
468
69
 
469
70
  fake_input do |fio|
470
71
  fio.getch = 'y'
471
- _, stderr = capture_io { @indexer.run }
72
+ _, stderr = capture_io { @cli.run }
472
73
  assert_match /commit created/i, stderr
473
74
  end
474
75
 
475
- commit = @git.log(1).last
76
+ commit = @git.last_commit
476
77
  assert_equal 'index: empty index', commit.message
477
- assert_equal ['index.xml'], commit.diff_parent.map {|d| d.path }
78
+ assert_equal ['index.xml'], commit.each_diff.map {|d| d.file }
478
79
  end
479
80
  end
480
81
 
481
82
  def test_create_commit_decline
482
83
  wrapper ['--prompt-commit'] do
483
- @git.add mkfile('.gitkeep')
484
- @git.commit 'initial commit'
84
+ @git.create_commit 'initial commit', [mkfile('.gitkeep')]
485
85
 
486
86
  fake_input do |fio|
487
87
  fio.getch = 'n'
488
- _, stderr = capture_io { @indexer.run }
88
+ _, stderr = capture_io { @cli.run }
489
89
  refute_match /commit created/i, stderr
490
90
  end
491
91
 
492
- commit = @git.log(1).last
92
+ commit = @git.last_commit
493
93
  refute_equal 'index: empty index', commit.message
494
- refute_equal ['index.xml'], commit.diff_parent.map {|d| d.path }
94
+ refute_equal ['index.xml'], commit.each_diff.map {|d| d.file }
495
95
  end
496
96
  end
497
97
 
@@ -520,10 +120,11 @@ class TestCLI < MiniTest::Test
520
120
 
521
121
  _, stderr = capture_io do
522
122
  wrapper ['--warnings'], setup: setup do
523
- @git.add mkfile('test.lua', 'no version tag in this script!')
524
- @git.commit 'initial commit'
123
+ @git.create_commit 'initial commit', [
124
+ mkfile('cat/test.lua', 'no version tag in this script!')
125
+ ]
525
126
 
526
- assert_equal true, @indexer.run
127
+ assert_equal true, @cli.run
527
128
  end
528
129
  end
529
130
 
@@ -536,7 +137,7 @@ class TestCLI < MiniTest::Test
536
137
  mkfile '.reapack-index.conf', '--help'
537
138
  mkfile 'Category/.gitkeep'
538
139
 
539
- Dir.chdir File.join(@git.dir.to_s, 'Category')
140
+ Dir.chdir File.join(@git.path, 'Category')
540
141
 
541
142
  assert_output /--help/ do
542
143
  ReaPack::Index::CLI.new
@@ -546,11 +147,12 @@ class TestCLI < MiniTest::Test
546
147
 
547
148
  def test_working_directory_with_options
548
149
  wrapper do
549
- @git.add mkfile('README.md', '# Hello World')
550
- @git.commit 'initial commit'
150
+ @git.create_commit 'initial commit',
151
+ [mkfile('README.md', '# Hello World')]
551
152
 
552
- Dir.chdir @git.dir.to_s
153
+ Dir.chdir @git.path
553
154
 
155
+ # no error = repository is found
554
156
  assert_output '', '' do
555
157
  i2 = ReaPack::Index::CLI.new ['--no-commit', '--quiet']
556
158
  i2.run
@@ -576,13 +178,21 @@ class TestCLI < MiniTest::Test
576
178
  end
577
179
  end
578
180
 
181
+ def test_invalid_index
182
+ assert_output '', /\A'.+index\.xml' is not a ReaPack index file\Z/ do
183
+ setup = proc { mkfile 'index.xml', "\0" }
184
+ wrapper [], setup: setup do
185
+ end
186
+ end
187
+ end
188
+
579
189
  def test_progress
580
190
  wrapper ['--progress'] do
581
- @git.add mkfile('README.md', '# Hello World')
582
- @git.commit 'initial commit'
191
+ @git.create_commit 'initial commit',
192
+ [mkfile('README.md', '# Hello World')]
583
193
 
584
194
  stdout, stderr = capture_io do
585
- assert_equal true, @indexer.run
195
+ assert_equal true, @cli.run
586
196
  end
587
197
 
588
198
  assert_equal "empty index\n", stdout
@@ -593,420 +203,82 @@ class TestCLI < MiniTest::Test
593
203
 
594
204
  def test_progress_no_new_commit
595
205
  setup = proc {
596
- @git.add mkfile('test1.lua', '@version 1.0')
597
- @git.commit 'initial commit'
206
+ @git.create_commit 'initial commit',
207
+ [mkfile('cat/test1.lua', '@version 1.0')]
598
208
 
599
209
  mkfile 'index.xml', <<-XML
600
210
  <?xml version="1.0" encoding="utf-8"?>
601
- <index version="1" commit="#{@git.log(1).last.sha}"/>
211
+ <index version="1" commit="#{@git.last_commit.id}"/>
602
212
  XML
603
213
  }
604
214
 
605
215
  wrapper ['--progress'], setup: setup do
606
- assert_output '', /Nothing to do!\n/ do
607
- @indexer.run
216
+ assert_output '', /nothing to do/i do
217
+ @cli.run
608
218
  end
609
219
  end
610
220
  end
611
221
 
612
222
  def test_progress_warnings
613
223
  wrapper ['--progress'] do
614
- @git.add mkfile('test.lua', 'no version tag in this script!')
615
- @git.commit 'initial commit'
224
+ @git.create_commit 'initial commit',
225
+ [mkfile('cat/test.lua', 'no version tag in this script!')]
616
226
 
617
- assert_output nil, /\nWarning:/i do
618
- assert_equal true, @indexer.run
227
+ # must output a new line before 'warning:'
228
+ assert_output nil, /\nwarning:/i do
229
+ assert_equal true, @cli.run
619
230
  end
620
231
  end
621
232
  end
622
233
 
623
234
  def test_quiet_mode
624
235
  wrapper ['--verbose', '--progress', '--quiet'] do
625
- @git.add mkfile('test.lua', 'no version tag in this script!')
626
- @git.commit 'initial commit'
236
+ @git.create_commit 'initial commit',
237
+ [mkfile('cat/test.lua', 'no version tag in this script!')]
627
238
 
628
239
  assert_output '', '' do
629
- assert_equal true, @indexer.run
630
- end
631
- end
632
- end
633
-
634
- def test_website_link
635
- wrapper ['-l http://cfillion.tk'] do
636
- assert_output "1 new website link, empty index\n" do
637
- assert_equal true, @indexer.run
638
- end
639
-
640
- assert_match 'rel="website">http://cfillion.tk</link>', read_index
641
- end
642
- end
643
-
644
- def test_donation_link
645
- wrapper ['--donation-link', 'Link Label=http://cfillion.tk'] do
646
- assert_output "1 new donation link, empty index\n" do
647
- assert_equal true, @indexer.run
648
- end
649
-
650
- assert_match 'rel="donation" href="http://cfillion.tk">Link Label</link>',
651
- read_index
652
- end
653
- end
654
-
655
- def test_invalid_link
656
- wrapper ['--link', 'shinsekai yori', '--donation-link', 'hello world',
657
- '--link', 'http://cfillion.tk'] do
658
- stdout, stderr = capture_io do
659
- assert_equal true, @indexer.run
660
- end
661
-
662
- assert_equal "1 new website link, empty index\n", stdout
663
- assert_match /warning: --link: invalid link: shinsekai yori/i, stderr
664
- assert_match /warning: --donation-link: invalid link: hello world/i, stderr
665
- assert_match 'rel="website">http://cfillion.tk</link>', read_index
666
- end
667
- end
668
-
669
- def test_remove_link
670
- wrapper ['--link', 'http://test.com', '--link', '-http://test.com'] do
671
- assert_output "1 new website link, 1 removed website link, empty index\n" do
672
- assert_equal true, @indexer.run
673
- end
674
-
675
- refute_match 'rel="website">http://test.com</link>', read_index
676
- end
677
- end
678
-
679
- def test_list_links
680
- setup = proc {
681
- mkfile 'index.xml', <<-XML
682
- <?xml version="1.0" encoding="utf-8"?>
683
- <index version="1">
684
- <metadata>
685
- <link rel="website" href="http://anidb.net/a9002">Shinsekai Yori</link>
686
- <link rel="donation" href="http://paypal.com">Donate!</link>
687
- <link rel="website">http://cfillion.tk</link>
688
- XML
689
- }
690
-
691
- wrapper ['--ls-links'], setup: setup do
692
- stdin, stderr = capture_io do
693
- assert_equal true, @indexer.run
240
+ assert_equal true, @cli.run
694
241
  end
695
-
696
- expected = <<-OUT
697
- [website] Shinsekai Yori (http://anidb.net/a9002)
698
- [website] http://cfillion.tk
699
- [donation] Donate! (http://paypal.com)
700
- OUT
701
-
702
- assert_equal expected, stdin
703
- assert_empty stderr
704
- end
705
- end
706
-
707
- def test_no_git_remote
708
- wrapper [], remote: '*' do
709
- # no crash :)
710
- assert_output { @indexer.run }
711
- end
712
- end
713
-
714
- def test_weird_git_remote_url
715
- wrapper [], remote: 'scp://hello.world/$path' do
716
- _, stderr = capture_io { @indexer.run }
717
- refute_match /invalid url/i, stderr
718
- refute_match '$path', stderr
719
- end
720
- end
721
-
722
- def test_auto_url_template_ssh
723
- wrapper [], remote: 'git@github.com:User/Repo.git' do
724
- @git.add mkfile('hello.lua', '@version 1.0')
725
- @git.commit 'initial commit'
726
-
727
- assert_output { @indexer.run }
728
- assert_match "https://github.com/User/Repo/raw/#{@git.log(1).last.sha}/hello.lua", read_index
729
- end
730
- end
731
-
732
- def test_auto_url_template_https
733
- wrapper [], remote: 'https://github.com/User/Repo.git' do
734
- @git.add mkfile('hello.lua', '@version 1.0')
735
- @git.commit 'initial commit'
736
-
737
- assert_output { @indexer.run }
738
- assert_match "https://github.com/User/Repo/raw/#{@git.log(1).last.sha}/hello.lua", read_index
739
242
  end
740
243
  end
741
244
 
742
245
  def test_url_template
743
- wrapper ['--url-template=http://host/$path'], remote: false do
744
- @git.add mkfile('hello.lua', '@version 1.0')
745
- @git.commit 'initial commit'
746
-
747
- assert_output { @indexer.run }
748
- assert_match 'http://host/hello.lua', read_index
749
- end
750
- end
751
-
752
- def test_url_template_override_git
753
- wrapper ['--url-template=http://host/$path'] do
754
- @git.add mkfile('hello.lua', '@version 1.0')
755
- @git.commit 'initial commit'
756
-
757
- assert_output { @indexer.run }
758
- assert_match 'http://host/hello.lua', read_index
759
- end
760
- end
761
-
762
- def test_url_template_invalid
763
- wrapper ['--url-template=minoshiro'] do
764
- @git.add mkfile('hello.lua', '@version 1.0')
765
- @git.commit 'initial commit'
766
-
767
- _, stderr = capture_io { @indexer.run }
768
- assert_match /--url-template: \$path placeholder is missing/i, stderr
769
- end
770
- end
771
-
772
- def test_about
773
- opts = ['--about']
774
- setup = proc { opts << mkfile('README.md', '# Hello World') }
775
-
776
- wrapper opts, setup: setup do
777
- assert_output "1 modified metadata, empty index\n" do
778
- assert_equal true, @indexer.run
779
- end
780
-
781
- assert_match 'Hello World', read_index
782
- end
783
- end
784
-
785
- def test_about_file_not_found
786
- # 404.md is read in the working directory
787
- wrapper ['--about=404.md'] do
788
- assert_output "empty index\n",
789
- /warning: --about: no such file or directory - 404.md/i do
790
- assert_equal true, @indexer.run
791
- end
792
- end
793
- end
794
-
795
- def test_about_pandoc_not_found
796
- old_path = ENV['PATH']
797
-
798
- opts = ['--about']
799
-
800
- setup = proc {
801
- opts << mkfile('README.md', '# Hello World')
802
- }
803
-
804
- wrapper opts, setup: setup do
805
- assert_output "empty index\n", /pandoc executable cannot be found/i do
806
- ENV['PATH'] = String.new
807
- assert_equal true, @indexer.run
808
- end
809
- end
810
- ensure
811
- ENV['PATH'] = old_path
812
- end
813
-
814
- def test_about_clear
815
- setup = proc {
816
- mkfile 'index.xml', <<-XML
817
- <index>
818
- <metadata>
819
- <description><![CDATA[Hello World]]></description>
820
- </metadata>
821
- </index>
822
- XML
823
- }
824
-
825
- wrapper ['--remove-about'], setup: setup do
826
- assert_output "1 modified metadata\n" do
827
- assert_equal true, @indexer.run
828
- end
829
-
830
- refute_match 'Hello World', read_index
831
- end
832
- end
833
-
834
- def test_about_dump
835
- setup = proc {
836
- mkfile 'index.xml', <<-XML
837
- <index>
838
- <metadata>
839
- <description><![CDATA[Hello World]]></description>
840
- </metadata>
841
- </index>
842
- XML
843
- }
844
-
845
- wrapper ['--dump-about'], setup: setup do
846
- assert_output 'Hello World' do
847
- assert_equal true, @indexer.run
848
- end
849
- end
850
- end
851
-
852
- def test_check_pass
853
- expected = <<-STDERR
854
- ..
855
-
856
- Finished checks for 2 packages with 0 failures
857
- STDERR
858
-
859
- wrapper ['--check'] do
860
- mkfile 'test1.lua', '@version 1.0'
861
- mkfile 'test2.lua', '@version 1.0'
862
-
863
- assert_output nil, expected do
864
- assert_equal true, @indexer.run
865
- end
866
- end
867
- end
868
-
869
- def test_check_failure
870
- expected = <<-STDERR
871
- F.
872
-
873
- test1.lua contains invalid metadata:
874
- - missing tag "version"
875
- - invalid value for tag "author"
876
-
877
- Finished checks for 2 packages with 1 failure
878
- STDERR
879
-
880
- wrapper ['--check'] do
881
- mkfile 'test1.lua', '@author'
882
- mkfile 'test2.lua', '@version 1.0'
883
-
884
- assert_output nil, expected do
885
- assert_equal false, @indexer.run
886
- end
887
- end
888
- end
889
-
890
- def test_check_quiet
891
- expected = <<-STDERR
892
- test1.lua contains invalid metadata:
893
- - missing tag "version"
894
- - invalid value for tag "author"
895
-
896
- test2.lua contains invalid metadata:
897
- - missing tag "version"
898
- STDERR
899
-
900
- wrapper ['--check', '--quiet'] do
901
- mkfile 'test1.lua', '@author'
902
- mkfile 'test2.lua'
903
- mkfile 'test3.lua', '@version 1.0'
904
-
905
- assert_output nil, expected do
906
- assert_equal false, @indexer.run
246
+ assert_output '', '' do
247
+ wrapper ['--url-template=http://host/$path'], remote: false do
248
+ assert_equal 'http://host/$path', @cli.index.url_template
907
249
  end
908
250
  end
909
251
  end
910
252
 
911
- def test_check_ignore
912
- setup = proc { Dir.chdir @git.dir.to_s }
913
-
914
- expected = <<-STDERR
915
- .
916
-
917
- Finished checks for 1 package with 0 failures
918
- STDERR
919
-
920
- wrapper ['--check', '--ignore=Hello',
921
- '--ignore=Chunky/Bacon.lua', '--ignore=test2.lua'], setup: setup do
922
- mkfile 'Hello/World.lua', 'konnichiwa'
923
- mkfile 'Chunky/Bacon.lua', 'konnichiwa'
924
- mkfile 'Directory/test2.lua', '@version 1.0'
925
-
926
- assert_output nil, expected do
927
- @indexer.run
253
+ def test_url_template_override_vcs
254
+ assert_output '', '' do
255
+ wrapper ['--url-template=http://host/$path'] do
256
+ assert_equal 'http://host/$path', @cli.index.url_template
928
257
  end
929
258
  end
930
259
  end
931
260
 
932
- def test_ignore_config
933
- expected = <<-STDERR
934
- .
935
-
936
- Finished checks for 1 package with 0 failures
937
- STDERR
938
-
939
- setup = proc {
940
- mkfile '.reapack-index.conf', <<-CONFIG
941
- --ignore=Hello
942
- --ignore=Chunky/Bacon.lua
943
- --ignore=test2.lua
944
- CONFIG
945
- }
946
-
947
- wrapper ['--check'], setup: setup do
948
- mkfile 'Hello/World.lua', 'konnichiwa'
949
- mkfile 'Chunky/Bacon.lua', 'konnichiwa'
950
- mkfile 'Directory/test2.lua', '@version 1.0'
951
-
952
- assert_output nil, expected do
953
- @indexer.run
261
+ def test_url_template_invalid
262
+ _, stderr = capture_io do
263
+ wrapper ['--url-template=minoshiro'] do
264
+ assert_nil @cli.index.url_template
954
265
  end
955
266
  end
956
- end
957
-
958
- def test_scan_ignore
959
- setup = proc { Dir.chdir @git.dir.to_s }
960
-
961
- wrapper ['--ignore=Hello', '--ignore=Chunky/Bacon.lua',
962
- '--ignore=test2.lua'], setup: setup do
963
- @git.add mkfile('README.md', '# Hello World')
964
- @git.commit 'initial commit'
965
267
 
966
- @git.add mkfile('Hello/World.lua', 'konnichiwa')
967
- @git.add mkfile('Chunky/Bacon.lua', 'konnichiwa')
968
- @git.add mkfile('Directory/test2.lua', '@version 1.0')
969
- @git.commit 'second commit'
970
-
971
- assert_output "1 new category, 1 new package, 1 new version\n" do
972
- assert_equal true, @indexer.run
973
- end
974
-
975
- refute_match 'Hello/World.lua', read_index
976
- refute_match 'Chunky/Bacon.lua', read_index
977
- assert_match 'Directory/test2.lua', read_index
978
- end
268
+ assert_match /--url-template: .+\$path placeholder/i, stderr
979
269
  end
980
270
 
981
- def test_noname
982
- wrapper do
983
- assert_output nil, /The name of this index is unset/i do
984
- assert_equal true, @indexer.run
985
- end
986
-
987
- refute_match 'name', read_index
988
- end
989
- end
990
-
991
- def test_set_name
992
- wrapper ['--name=Hello World'] do
993
- _, stderr = capture_io do
994
- assert_equal true, @indexer.run
995
- end
996
-
997
- refute_match /The name of this index is unset/i, stderr
998
- assert_match 'name="Hello World"', read_index
271
+ def test_scan_check_mutally_exclusive
272
+ wrapper ['--check', '--scan'] do
273
+ _, stderr = capture_io { @cli.run }
274
+ refute_match /finished checks/i, stderr
275
+ read_index # index exists
999
276
  end
1000
- end
1001
-
1002
- def test_set_name_invalid
1003
- wrapper ['--name=Hello/World'] do
1004
- _, stderr = capture_io do
1005
- assert_equal true, @indexer.run
1006
- end
1007
277
 
1008
- refute_match /The name of this index is unset/i, stderr
1009
- assert_match /invalid name: 'Hello\/World'/i, stderr
278
+ wrapper ['--scan', '--check'] do
279
+ _, stderr = capture_io { @cli.run }
280
+ assert_match /finished checks/i, stderr
281
+ assert_raises(Errno::ENOENT) { read_index }
1010
282
  end
1011
283
  end
1012
284
  end