-
Notifications
You must be signed in to change notification settings - Fork 25
/
test_source.rb
373 lines (352 loc) · 11.5 KB
/
test_source.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
# Copyright (c) 2014-2022 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
require 'minitest/autorun'
require 'tmpdir'
require_relative '../lib/pdd'
require_relative '../lib/pdd/sources'
# Source test.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2014-2022 Yegor Bugayenko
# License:: MIT
class TestSource < Minitest::Test
def test_parsing
Dir.mktmpdir 'test' do |dir|
file = File.join(dir, 'a.txt')
File.write(
file,
"
* \x40todo #44 привет,
* how are you\t\tdoing?
* -something else
Something else
~~ \x40todo #ABC-3 this is another puzzle
~~ and it also has to work
"
)
stub_source_find_github_user(file, 'hey') do |source|
list = source.puzzles
assert_equal 2, list.size
puzzle = list.first
assert_equal '2-4', puzzle.props[:lines]
assert_equal 'привет, how are you doing? -something else', \
puzzle.props[:body]
assert_equal '44', puzzle.props[:ticket]
assert puzzle.props[:author].nil?
assert puzzle.props[:email].nil?
assert puzzle.props[:time].nil?
end
end
end
def test_parsing_leading_spaces
Dir.mktmpdir 'test' do |dir|
file = File.join(dir, 'a.txt')
File.write(
file,
"
* \x40todo #56:30min this is a
* multi-line
* comment!
"
)
stub_source_find_github_user(file, 'hey') do |source|
list = source.puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '2-4', puzzle.props[:lines]
assert_equal 'this is a multi-line comment!', puzzle.props[:body]
assert_equal '56', puzzle.props[:ticket]
end
end
end
def test_no_prefix_multiline_puzzle_block
Dir.mktmpdir 'test' do |dir|
file = File.join(dir, 'a.txt')
File.write(
file,
"
<!--
\x40todo #01:30min correctly formatted multi-line puzzle, with no
comment prefix before todo marker
-->
"
)
stub_source_find_github_user(file, 'hey') do |source|
PDD.opts = nil
assert_equal 1, source.puzzles.size
puzzle = source.puzzles.last
assert_equal '3-4', puzzle.props[:lines]
assert_equal 'correctly formatted multi-line puzzle, with no ' \
'comment prefix before todo marker', puzzle.props[:body]
assert_equal '01', puzzle.props[:ticket]
end
end
end
def test_multiple_puzzles_single_comment_block
Dir.mktmpdir 'test' do |dir|
file = File.join(dir, 'a.txt')
File.write(
file,
"
/*
* \x40todo #1 First one with
* a few lines
* \x40todo #1 Second one also
* with a few lines
*/
"
)
stub_source_find_github_user(file, 'hey') do |source|
PDD.opts = nil
assert_equal 2, source.puzzles.size
puzzle = source.puzzles.last
assert_equal '5-6', puzzle.props[:lines]
assert_equal 'Second one also with a few lines', puzzle.props[:body]
assert_equal '1', puzzle.props[:ticket]
end
end
end
def test_succeed_despite_bad_puzzles
Dir.mktmpdir 'test' do |dir|
file = File.join(dir, 'a.txt')
File.write(
file,
"
* \x40todo #44 this is a correctly formatted puzzle,
* with a second line without a leading space
Another badly formatted puzzle
* \x40todo this bad puzzle misses ticket name/number
Something else
* \x40todo #123 This puzzle is correctly formatted
"
)
PDD.opts = { 'skip-errors' => true }
stub_source_find_github_user(file, 'hey') do |source|
list = source.puzzles
PDD.opts = nil
assert_equal 2, list.size
puzzle = list.first
assert_equal '2-3', puzzle.props[:lines]
assert_equal 'this is a correctly formatted puzzle, with a second ' \
'line without a leading space', puzzle.props[:body]
assert_equal '44', puzzle.props[:ticket]
end
end
end
def test_succeed_utf8_encoded_body
Dir.mktmpdir 'test' do |dir|
file = File.join(dir, 'a.txt')
File.write(
file,
"
* \x40todo #44 Привет, мир, мне кофе
* вторая линия
"
)
list = PDD::VerboseSource.new(file, PDD::Source.new(file, 'hey')).puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '2-3', puzzle.props[:lines]
assert_equal 'Привет, мир, мне кофе вторая линия', puzzle.props[:body]
assert_equal '44', puzzle.props[:ticket]
end
end
def test_failing_on_incomplete_puzzle
Dir.mktmpdir 't5' do |dir|
file = File.join(dir, 'ff.txt')
File.write(
file,
"
* \x40todo this puzzle misses ticket name/number
"
)
error = assert_raises PDD::Error do
stub_source_find_github_user(file, 'ff', &:puzzles)
end
assert !error.to_s.index("\x40todo is not followed by").nil?
end
end
def test_failing_on_broken_unicode
Dir.mktmpdir 'test' do |dir|
file = File.join(dir, 'xx.txt')
File.write(file, " * \\x40todo #44 this is a broken unicode: #{0x92.chr}")
assert_raises PDD::Error do
stub_source_find_github_user(file, 'xx', &:puzzles)
end
end
end
def test_failing_on_invalid_puzzle_without_hash_sign
Dir.mktmpdir 'test' do |dir|
file = File.join(dir, 'a.txt')
File.write(
file,
"
* \x40todo 44 this puzzle is not formatted correctly
"
)
error = assert_raises PDD::Error do
stub_source_find_github_user(file, 'hey', &:puzzles)
end
assert !error.message.index('is not followed by a puzzle marker').nil?
end
end
def test_failing_on_puzzle_without_leading_space
Dir.mktmpdir 'test' do |dir|
file = File.join(dir, 'hey.txt')
File.write(
file,
"
*\x40todo #999 this is an incorrectly formatted puzzle!
"
)
error = assert_raises PDD::Error do
stub_source_find_github_user(file, 'x', &:puzzles)
end
assert !error.message.index("\x40todo must have a leading space").nil?
end
end
def test_failing_on_puzzle_with_space_after_dash
Dir.mktmpdir 'test' do |dir|
file = File.join(dir, 'hey-you.txt')
File.write(
file,
"
* \x40todo # 123 This puzzle has an unnecessary space before the dash
"
)
error = assert_raises PDD::Error do
stub_source_find_github_user(file, 'x', &:puzzles)
end
assert !error.message.index('an unexpected space').nil?
end
end
def test_reads_git_author
skip if Gem.win_platform?
Dir.mktmpdir 'test' do |dir|
raise unless system("
set -e
cd '#{dir}'
git init --quiet .
git config user.email test@teamed.io
git config user.name test_unknown
echo '\x40todo #1 this is the puzzle' > a.txt
git add a.txt
git commit --quiet -am 'first version'
")
stub_source_find_github_user(File.join(dir, 'a.txt')) do |source|
list = source.puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '1-de87adc8', puzzle.props[:id]
assert_equal '1-1', puzzle.props[:lines]
assert_equal 'this is the puzzle', puzzle.props[:body]
assert_equal 'test_unknown', puzzle.props[:author]
assert_equal 'test@teamed.io', puzzle.props[:email]
assert_match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/,
puzzle.props[:time])
end
end
end
def test_skips_invalid_git_mail
skip if Gem.win_platform?
Dir.mktmpdir 'test' do |dir|
raise unless system("
set -e
cd '#{dir}'
git init --quiet .
git config user.email invalid-email
git config user.name test
echo '\x40todo #1 this is the puzzle' > a.txt
git add a.txt
git commit --quiet -am 'first version'
")
stub_source_find_github_user(File.join(dir, 'a.txt')) do |source|
list = source.puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '1-de87adc8', puzzle.props[:id]
assert_equal '1-1', puzzle.props[:lines]
assert_equal 'this is the puzzle', puzzle.props[:body]
assert_equal 'test', puzzle.props[:author]
assert_nil puzzle.props[:email]
assert_match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/,
puzzle.props[:time])
end
end
end
def test_uses_github_login
skip if Gem.win_platform?
Dir.mktmpdir 'test' do |dir|
raise unless system("
cd '#{dir}'
git init --quiet .
git config user.email yegor256@gmail.com
git config user.name test
echo '\x40todo #1 this is the puzzle' > a.txt
git add a.txt
git commit --quiet -am 'first version'
")
stub_source_find_github_user(File.join(dir, 'a.txt')) do |source|
list = source.puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '@yegor256', puzzle.props[:author]
end
end
end
def test_skips_uncommitted_changes
skip if Gem.win_platform?
Dir.mktmpdir 'test' do |dir|
raise unless system("
cd '#{dir}'
git init --quiet .
git config user.email yegor256@gmail.com
git config user.name test
echo 'hi' > a.txt
git add a.txt
git commit --quiet -am 'first version'
echo '\x40todo #1 this is a puzzle uncommitted' > a.txt
")
stub_source_find_github_user(File.join(dir, 'a.txt')) do |source|
list = source.puzzles
assert_equal 1, list.size
puzzle = list.first
assert_nil puzzle.props[:email]
assert_equal 'Not Committed Yet', puzzle.props[:author]
end
end
end
def test_skips_thymeleaf_close_tag
Dir.mktmpdir 'test' do |dir|
file = File.join(dir, 'a.txt')
File.write(
file,
'<!--/* @todo #123 puzzle info */-->'
)
stub_source_find_github_user(file, 'hey') do |source|
list = source.puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '1-1', puzzle.props[:lines]
assert_equal 'puzzle info', puzzle.props[:body]
assert_equal '123', puzzle.props[:ticket]
end
end
end
end