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,326 @@
1
+ require File.expand_path '../../helper', __FILE__
2
+
3
+ TestCLI ||= Class.new MiniTest::Test
4
+
5
+ class TestCLI::Scan < MiniTest::Test
6
+ include CLIHelper
7
+
8
+ def test_initial_commit
9
+ wrapper do
10
+ @git.create_commit 'initial commit', [
11
+ mkfile('Category/test1.lua', '@version 1.0'),
12
+ mkfile('Category/Sub/test2.lua', '@version 1.0'),
13
+ ]
14
+
15
+ assert_output /2 new packages/ do
16
+ assert_equal true, @cli.run
17
+ end
18
+
19
+ assert_match 'Category/test1.lua', read_index
20
+ assert_match 'Category/Sub/test2.lua', read_index
21
+
22
+ assert_equal @git.last_commit.id, @cli.index.commit
23
+ assert_equal @git.last_commit.time, @cli.index.time
24
+ end
25
+ end
26
+
27
+ def test_normal_commit
28
+ wrapper do
29
+ @git.create_commit 'initial commit',
30
+ [mkfile('README.md', '# Hello World')]
31
+
32
+ @git.create_commit 'second commit', [
33
+ mkfile('Category/test1.lua', '@version 1.0'),
34
+ mkfile('Category/Sub/test2.lua', '@version 1.0'),
35
+ ]
36
+
37
+ assert_output "2 new categories, 2 new packages, 2 new versions\n" do
38
+ assert_equal true, @cli.run
39
+ end
40
+
41
+ assert_equal @git.last_commit.id, @cli.index.commit
42
+ assert_equal @git.last_commit.time, @cli.index.time
43
+ assert_match 'Category/test1.lua', read_index
44
+ end
45
+ end
46
+
47
+ def test_empty_branch
48
+ wrapper do
49
+ assert_output nil, /the current branch does not contains any commit/i do
50
+ assert_equal true, @cli.run
51
+ end
52
+ end
53
+ end
54
+
55
+ def test_wd_subdirectory
56
+ wrapper do
57
+ @git.create_commit 'initial commit',
58
+ [mkfile('cat/test1.lua', '@version 1.0')]
59
+
60
+ Dir.mkdir mkpath('test')
61
+ Dir.chdir mkpath('test')
62
+
63
+ assert_output /1 new package/ do
64
+ assert_equal true, @cli.run
65
+ end
66
+
67
+ assert_match 'test1.lua', read_index
68
+ end
69
+ end
70
+
71
+ def test_verbose
72
+ stdout, stderr = capture_io do
73
+ wrapper ['--verbose'] do
74
+ @git.create_commit 'initial commit', [
75
+ mkfile('cat/test.lua', '@version 1.0'),
76
+ mkfile('cat/test.png', 'file not shown in output'),
77
+ mkfile('test.png', 'not shown either'),
78
+ ]
79
+
80
+ @git.create_commit 'second commit', [
81
+ mkfile('cat/test.lua', '@version 2.0'),
82
+ mkfile('cat/test.jsfx', '@version 1.0'),
83
+ ]
84
+
85
+ File.delete mkpath('cat/test.lua')
86
+ @git.create_commit 'third commit', [mkpath('cat/test.lua')]
87
+
88
+ assert_equal true, @cli.run
89
+ end
90
+ end
91
+
92
+ assert_match /reading configuration from .+\.reapack-index\.conf/, stderr
93
+
94
+ verbose = /
95
+ processing [a-f0-9]{7}: initial commit
96
+ -> indexing added file cat\/test\.lua
97
+ processing [a-f0-9]{7}: second commit
98
+ -> indexing added file cat\/test\.jsfx
99
+ -> indexing modified file cat\/test\.lua
100
+ processing [a-f0-9]{7}: third commit
101
+ -> indexing deleted file cat\/test\.lua/i
102
+
103
+ assert_match verbose, stderr
104
+ end
105
+
106
+ def test_verbose_override
107
+ wrapper ['--verbose', '--no-verbose'] do
108
+ @git.create_commit 'initial commit', [mkfile('README.md', '# Hello World')]
109
+
110
+ stdout, stderr = capture_io do
111
+ assert_equal true, @cli.run
112
+ end
113
+
114
+ assert_equal "empty index\n", stdout
115
+ refute_match /processing [a-f0-9]{7}: initial commit/i, stderr
116
+ end
117
+ end
118
+
119
+ def test_invalid_metadata
120
+ wrapper do
121
+ @git.create_commit 'initial commit',
122
+ [mkfile('cat/test.lua', 'no version tag in this script!')]
123
+
124
+ assert_output nil, /warning: cat\/test\.lua:\n\x20\x20missing tag/i do
125
+ assert_equal true, @cli.run
126
+ end
127
+ end
128
+ end
129
+
130
+ def test_no_warnings
131
+ wrapper ['-w'] do
132
+ @git.create_commit 'initial commit',
133
+ [mkfile('cat/test.lua', 'no version tag in this script!')]
134
+
135
+ _, stderr = capture_io do
136
+ assert_equal true, @cli.run
137
+ end
138
+
139
+ refute_match /warning/i, stderr
140
+ end
141
+ end
142
+
143
+ def test_no_warnings_override
144
+ wrapper ['-w', '-W'] do
145
+ @git.create_commit 'initial commit',
146
+ [mkfile('cat/test.lua', 'no version tag in this script!')]
147
+
148
+ assert_output nil, /warning/i do
149
+ assert_equal true, @cli.run
150
+ end
151
+ end
152
+ end
153
+
154
+ def test_from_last
155
+ setup = proc {
156
+ @git.create_commit 'initial commit',
157
+ [mkfile('cat/test1.lua', '@version 1.0')]
158
+
159
+ mkfile 'index.xml', <<-XML
160
+ <?xml version="1.0" encoding="utf-8"?>
161
+ <index version="1" name="hello" commit="#{@git.last_commit.id}"/>
162
+ XML
163
+ }
164
+
165
+ wrapper [], setup: setup do
166
+ @git.create_commit 'second commit',
167
+ [mkfile('cat/test2.lua', '@version 1.0')]
168
+
169
+ assert_output nil, '' do
170
+ assert_equal true, @cli.run
171
+ end
172
+
173
+ refute_match 'test1.lua', read_index
174
+ assert_match 'test2.lua', read_index
175
+ end
176
+ end
177
+
178
+ def test_amend
179
+ wrapper ['--amend'] do
180
+ assert_equal true, @cli.index.amend
181
+ end
182
+ end
183
+
184
+ def test_no_amend
185
+ wrapper ['--no-amend'] do
186
+ assert_equal false, @cli.index.amend
187
+ end
188
+ end
189
+
190
+ def test_scan_ignore
191
+ setup = proc { Dir.chdir @git.path }
192
+
193
+ wrapper ['--ignore=Hello', '--ignore=Chunky/Bacon.lua',
194
+ '--ignore=test2.lua'], setup: setup do
195
+ @git.create_commit 'initial commit', [
196
+ mkfile('Hello/World.lua', 'konnichiwa'),
197
+ mkfile('Chunky/Bacon.lua', 'konnichiwa'),
198
+ mkfile('Directory/test2.lua', '@version 1.0'),
199
+ ]
200
+
201
+ assert_output "1 new category, 1 new package, 1 new version\n" do
202
+ assert_equal true, @cli.run
203
+ end
204
+
205
+ refute_match 'Hello/World.lua', read_index
206
+ refute_match 'Chunky/Bacon.lua', read_index
207
+ assert_match 'Directory/test2.lua', read_index
208
+ end
209
+ end
210
+
211
+ def test_remove
212
+ wrapper do
213
+ @git.create_commit 'initial commit',
214
+ [mkfile('cat/test.lua', '@version 1.0')]
215
+
216
+ File.delete mkpath('cat/test.lua')
217
+ @git.create_commit 'second commit', [mkpath('cat/test.lua')]
218
+
219
+ assert_output /1 removed package/i do
220
+ assert_equal true, @cli.run
221
+ end
222
+
223
+ refute_match 'test.lua', read_index
224
+ end
225
+ end
226
+
227
+ def test_specify_commit
228
+ # --progress to check for FloatDomainError: Infinity errors
229
+ options = ['--progress', '--scan']
230
+
231
+ setup = proc {
232
+ @git.create_commit 'initial commit',
233
+ [mkfile('cat/test1.lua', '@version 2.0')]
234
+
235
+ @git.create_commit 'second commit',
236
+ [mkfile('cat/test2.lua', '@version 1.0')]
237
+ options << @git.last_commit.id
238
+
239
+ @git.create_commit 'third commit',
240
+ [mkfile('cat/test3.lua', '@version 1.1')]
241
+ }
242
+
243
+ wrapper options, setup: setup do
244
+ capture_io { assert_equal true, @cli.run }
245
+
246
+ refute_match 'test1.lua', read_index, 'The initial commit was scanned'
247
+ assert_match 'test2.lua', read_index
248
+ refute_match 'test3.lua', read_index, 'The third commit was scanned'
249
+ end
250
+ end
251
+
252
+ def test_specify_two_commits
253
+ options = ['--scan', nil, '--scan', nil]
254
+
255
+ setup = proc {
256
+ @git.create_commit 'initial commit',
257
+ [mkfile('cat/test1.lua', '@version 2.0')]
258
+
259
+ @git.create_commit 'second commit',
260
+ [mkfile('cat/test2.lua', '@version 1.0')]
261
+ options[1] = @git.last_commit.id
262
+
263
+ @git.create_commit 'third commit',
264
+ [mkfile('cat/test3.lua', '@version 1.1')]
265
+ options[3] = @git.last_commit.id
266
+ }
267
+
268
+ wrapper options, setup: setup do
269
+ capture_io { assert_equal true, @cli.run }
270
+
271
+ refute_match 'test1.lua', read_index, 'The initial commit was scanned'
272
+ assert_match 'test2.lua', read_index
273
+ assert_match 'test3.lua', read_index
274
+ end
275
+ end
276
+
277
+ def test_reset
278
+ options = ['--scan']
279
+
280
+ setup = proc {
281
+ @git.create_commit 'initial commit',
282
+ [mkfile('cat/test1.lua', '@version 2.0')]
283
+
284
+ @git.create_commit 'second commit',
285
+ [mkfile('cat/test2.lua', '@version 1.0')]
286
+ options << @git.last_commit.id
287
+
288
+ options << '--scan'
289
+ }
290
+
291
+ wrapper options, setup: setup do
292
+ capture_io { assert_equal true, @cli.run }
293
+ assert_match 'test1.lua', read_index
294
+ end
295
+ end
296
+
297
+ def test_short_hash
298
+ options = ['--scan']
299
+
300
+ setup = proc {
301
+ @git.create_commit 'initial commit',
302
+ [mkfile('test1.lua', '@version 2.0')]
303
+ options << @git.last_commit.short_id
304
+ }
305
+
306
+ wrapper options, setup: setup do
307
+ capture_io { assert_equal true, @cli.run }
308
+ end
309
+ end
310
+
311
+ def test_invalid_hashes
312
+ INVALID_HASHES.each {|hash|
313
+ wrapper ['--scan', hash] do
314
+ @git.create_commit 'initial commit', [mkfile('README.md')]
315
+
316
+ assert_output nil, /--scan: bad revision: #{Regexp.escape hash}/i do
317
+ assert_equal false, @cli.run
318
+ end
319
+ end
320
+ }
321
+ end
322
+
323
+ def test_no_arguments
324
+ wrapper ['--scan'] do; end
325
+ end
326
+ end
@@ -1,5 +1,5 @@
1
1
  <?xml version="1.0" encoding="utf-8"?>
2
- <index version="1" commit="399f5609cff3e6fd92b5542d444fbf86da0443c6">
2
+ <index version="1" commit="f572d396fae9206628714fb2ce00f72e94f2258f">
3
3
  <category name="Category Name">
4
4
  <reapack name="Hello World.lua" type="script">
5
5
  <version name="1.0" author="cfillion">
@@ -16,10 +16,99 @@ SimpleCov.start {
16
16
  require 'reapack/index'
17
17
  require 'minitest/autorun'
18
18
 
19
+ String.disable_colorization = true
20
+
19
21
  module XMLHelper
20
22
  def make_node(markup)
21
23
  Nokogiri::XML(markup, &:noblanks).root
22
24
  end
23
25
  end
24
26
 
25
- String.disable_colorization = true
27
+ module IndexHelper
28
+ def setup
29
+ @real_path = File.expand_path '../data/index.xml', __FILE__
30
+ @dummy_path = Dir::Tmpname.create('index.xml') {|path| path }
31
+
32
+ @commit = '399f5609cff3e6fd92b5542d444fbf86da0443c6'
33
+ end
34
+
35
+ def teardown
36
+ File.delete @dummy_path if File.exist? @dummy_path
37
+ end
38
+ end
39
+
40
+ module GitHelper
41
+ INVALID_HASHES = [
42
+ 'hello world', '0000000000000000000000000000000000000000',
43
+ '0000000000000000000000000000000000000deadbeef',
44
+ ].freeze
45
+
46
+ def init_git
47
+ path = Dir.mktmpdir 'test-repository'
48
+ repo = Rugged::Repository.init_at path
49
+ repo.config['user.name'] = 'John Doe'
50
+ repo.config['user.email'] = 'john@doe.com'
51
+ repo.config['commit.gpgsign'] = false
52
+
53
+ @git = ReaPack::Index::Git.new path
54
+ [path, repo]
55
+ end
56
+
57
+ def mkpath(file)
58
+ File.join @git.path, file
59
+ end
60
+
61
+ def mkfile(file, content = String.new)
62
+ fn = mkpath file
63
+ FileUtils.mkdir_p File.dirname(fn)
64
+ File.write fn, content
65
+ fn
66
+ end
67
+ end
68
+
69
+ module CLIHelper
70
+ include GitHelper
71
+
72
+ class FakeIO
73
+ def initialize
74
+ @getch = 'n'
75
+ end
76
+
77
+ attr_accessor :getch
78
+ end
79
+
80
+ def fake_input
81
+ stdin = $stdin
82
+ $stdin = FakeIO.new
83
+
84
+ yield $stdin
85
+ ensure
86
+ $stdin = stdin
87
+ end
88
+
89
+ def wrapper(args = [], options = {})
90
+ old_wd = Dir.pwd
91
+
92
+ path, repo = init_git
93
+
94
+ if options[:remote] != false
95
+ options[:remote] ||= 'git@github.com:cfillion/test-repository.git'
96
+ repo.remotes.create 'origin', options[:remote]
97
+ end
98
+
99
+ options[:setup].call if options.has_key? :setup
100
+
101
+ @cli = ReaPack::Index::CLI.new \
102
+ ['--no-progress', '--no-commit'] + args + ['--', path]
103
+
104
+ yield if block_given?
105
+ ensure
106
+ @git = @cli = nil
107
+ Dir.chdir old_wd
108
+ FileUtils.rm_r path
109
+ end
110
+
111
+ def read_index(file = 'index.xml')
112
+ File.read File.expand_path(file, @git.path)
113
+ end
114
+ end
@@ -0,0 +1,111 @@
1
+ require File.expand_path '../../helper', __FILE__
2
+
3
+ TestIndex ||= Class.new MiniTest::Test
4
+
5
+ class TestIndex::Metadata < MiniTest::Test
6
+ include IndexHelper
7
+
8
+ def test_add_anonymous_link
9
+ index = ReaPack::Index.new @dummy_path
10
+
11
+ assert_equal 0, index.links(:website).size
12
+ index.eval_link :website, 'http://test.com'
13
+ assert_equal 1, index.links(:website).size
14
+
15
+ assert_equal '1 new website link, empty index', index.changelog
16
+
17
+ index.write!
18
+ end
19
+
20
+ def test_add_named_link
21
+ index = ReaPack::Index.new @dummy_path
22
+
23
+ assert_equal 0, index.links(:website).size
24
+ index.eval_link :website, 'Test=http://test.com/hello=world'
25
+ assert_equal 1, index.links(:website).size
26
+
27
+ assert_equal '1 new website link, empty index', index.changelog
28
+
29
+ index.write!
30
+ expected = <<-XML
31
+ <?xml version="1.0" encoding="utf-8"?>
32
+ <index version="1">
33
+ <metadata>
34
+ <link rel="website" href="http://test.com/hello=world">Test</link>
35
+ </metadata>
36
+ </index>
37
+ XML
38
+
39
+ index.write!
40
+ assert_equal expected, File.read(index.path)
41
+ end
42
+
43
+ def test_edit_link
44
+ index = ReaPack::Index.new @dummy_path
45
+
46
+ index.eval_link :website, 'Test=http://test.com'
47
+ index.eval_link :website, 'Test=http://test.com'
48
+ assert_equal '1 new website link, empty index', index.changelog
49
+
50
+ index.eval_link :website, 'Test=http://test.com/hello'
51
+ assert_equal '1 new website link, 1 modified website link, empty index',
52
+ index.changelog
53
+ end
54
+
55
+ def test_remove_link_by_name
56
+ index = ReaPack::Index.new @dummy_path
57
+ index.eval_link :website, 'Test=http://test.com'
58
+ index.eval_link :website, '-Test'
59
+
60
+ assert_equal '1 new website link, 1 removed website link, empty index', index.changelog
61
+ end
62
+
63
+ def test_remove_link_by_url
64
+ index = ReaPack::Index.new @dummy_path
65
+
66
+ index.eval_link :website, 'Test=http://test.com'
67
+ index.eval_link :website, '-http://test.com'
68
+
69
+ assert_equal '1 new website link, 1 removed website link, empty index', index.changelog
70
+ end
71
+
72
+ def test_description
73
+ index = ReaPack::Index.new @dummy_path
74
+ index.write!
75
+
76
+ assert_empty index.description
77
+ assert_equal false, index.modified?
78
+
79
+ index.description = 'Hello World'
80
+ refute_empty index.description
81
+ assert_equal true, index.modified?
82
+ assert_equal '1 modified metadata', index.changelog
83
+
84
+ index.write!
85
+
86
+ index.description = 'Hello World'
87
+ assert_equal false, index.modified?
88
+ end
89
+
90
+ def test_name
91
+ index = ReaPack::Index.new @dummy_path
92
+ assert_empty index.name
93
+
94
+ index.name = 'Hello World'
95
+ assert_equal '1 modified metadata, empty index', index.changelog
96
+
97
+ error = assert_raises ReaPack::Index::Error do index.name = '.'; end
98
+ assert_raises ReaPack::Index::Error do index.name = 'hello/world'; end
99
+ assert_equal "invalid name '.'", error.message
100
+
101
+ assert_equal 'Hello World', index.name
102
+
103
+ expected = <<-XML
104
+ <?xml version="1.0" encoding="utf-8"?>
105
+ <index version="1" name="Hello World"/>
106
+ XML
107
+
108
+ index.write!
109
+ assert_equal expected, File.read(index.path)
110
+ end
111
+ end