15699: Remove unused variables.
[arvados.git] / sdk / ruby / test / test_collection.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 require "arvados/collection"
6 require "minitest/autorun"
7 require "sdk_fixtures"
8
9 class CollectionTest < Minitest::Test
10   include SDKFixtures
11
12   TWO_BY_TWO_BLOCKS = SDKFixtures.random_blocks(2, 9)
13   TWO_BY_TWO_MANIFEST_A =
14     [". #{TWO_BY_TWO_BLOCKS.first} 0:5:f1 5:4:f2\n",
15      "./s1 #{TWO_BY_TWO_BLOCKS.last} 0:5:f1 5:4:f3\n"]
16   TWO_BY_TWO_MANIFEST_S = TWO_BY_TWO_MANIFEST_A.join("")
17
18   def abcde_blocks
19     ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+9", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+9", "cccccccccccccccccccccccccccccccc+9", "dddddddddddddddddddddddddddddddd+9", "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee+9"]
20   end
21
22   ### .new
23
24   def test_empty_construction
25     coll = Arv::Collection.new
26     assert_equal("", coll.manifest_text)
27   end
28
29   def test_successful_construction
30     [:SIMPLEST_MANIFEST, :MULTIBLOCK_FILE_MANIFEST, :MULTILEVEL_MANIFEST].
31         each do |manifest_name|
32       manifest_text = SDKFixtures.const_get(manifest_name)
33       coll = Arv::Collection.new(manifest_text)
34       assert_equal(manifest_text, coll.manifest_text,
35                    "did not get same manifest back out from #{manifest_name}")
36     end
37   end
38
39   def test_non_manifest_construction_error
40     ["word", ". abc def", ". #{random_block} 0:", ". / !"].each do |m_text|
41       assert_raises(ArgumentError,
42                     "built collection from manifest #{m_text.inspect}") do
43         Arv::Collection.new(m_text)
44       end
45     end
46   end
47
48   def test_file_directory_conflict_construction_error
49     assert_raises(ArgumentError) do
50       Arv::Collection.new(NAME_CONFLICT_MANIFEST)
51     end
52   end
53
54   def test_no_implicit_normalization
55     coll = Arv::Collection.new(NONNORMALIZED_MANIFEST)
56     assert_equal(NONNORMALIZED_MANIFEST, coll.manifest_text)
57   end
58
59   ### .normalize
60
61   def test_non_posix_path_handling
62     m_text = "./.. #{random_block(9)} 0:5:. 5:4:..\n"
63     coll = Arv::Collection.new(m_text.dup)
64     coll.normalize
65     assert_equal(m_text, coll.manifest_text)
66   end
67
68   def test_escaping_through_normalization
69     coll = Arv::Collection.new(MANY_ESCAPES_MANIFEST)
70     coll.normalize
71     # The result should simply duplicate the file spec.
72     # The source file spec has an unescaped backslash in it.
73     # It's OK for the Collection class to properly escape that.
74     expect_text = MANY_ESCAPES_MANIFEST.sub(/ \d+:\d+:\S+/) do |file_spec|
75       file_spec.gsub(/([^\\])(\\[^\\\d])/, '\1\\\\\2')
76     end
77     assert_equal(expect_text, coll.manifest_text)
78   end
79
80   def test_concatenation_with_locator_overlap(over_index=0)
81     blocks = random_blocks(4, 2)
82     blocks_s = blocks.join(" ")
83     coll = Arv::Collection.new(". %s 0:8:file\n. %s 0:4:file\n" %
84                                [blocks_s, blocks[over_index, 2].join(" ")])
85     coll.normalize
86     assert_equal(". #{blocks_s} 0:8:file #{over_index * 2}:4:file\n",
87                  coll.manifest_text)
88   end
89
90   def test_concatenation_with_middle_locator_overlap
91     test_concatenation_with_locator_overlap(1)
92   end
93
94   def test_concatenation_with_end_locator_overlap
95     test_concatenation_with_locator_overlap(2)
96   end
97
98   def test_concatenation_with_partial_locator_overlap
99     blocks = random_blocks(3, 3)
100     coll = Arv::Collection
101       .new(". %s 0:6:overlap\n. %s 0:6:overlap\n" %
102            [blocks[0, 2].join(" "), blocks[1, 2].join(" ")])
103     coll.normalize
104     assert_equal(". #{blocks.join(' ')} 0:6:overlap 3:6:overlap\n",
105                  coll.manifest_text)
106   end
107
108   def test_normalize
109     block = random_block
110     coll = Arv::Collection.new(". #{block} 0:0:f2 0:0:f1\n")
111     coll.normalize
112     assert_equal(". #{block} 0:0:f1 0:0:f2\n", coll.manifest_text)
113   end
114
115   def test_normalization_file_spans_two_whole_blocks(file_specs="0:10:f1",
116                                                      num_blocks=2)
117     blocks = random_blocks(num_blocks, 5)
118     m_text = ". #{blocks.join(' ')} #{file_specs}\n"
119     coll = Arv::Collection.new(m_text.dup)
120     coll.normalize
121     assert_equal(m_text, coll.manifest_text)
122   end
123
124   def test_normalization_file_fits_beginning_block
125     test_normalization_file_spans_two_whole_blocks("0:7:f1")
126   end
127
128   def test_normalization_file_fits_end_block
129     test_normalization_file_spans_two_whole_blocks("3:7:f1")
130   end
131
132   def test_normalization_file_spans_middle
133     test_normalization_file_spans_two_whole_blocks("3:5:f1")
134   end
135
136   def test_normalization_file_spans_three_whole_blocks
137     test_normalization_file_spans_two_whole_blocks("0:15:f1", 3)
138   end
139
140   def test_normalization_file_skips_bytes
141     test_normalization_file_spans_two_whole_blocks("0:3:f1 5:5:f1")
142   end
143
144   def test_normalization_file_inserts_bytes
145     test_normalization_file_spans_two_whole_blocks("0:3:f1 5:3:f1 3:2:f1")
146   end
147
148   def test_normalization_file_duplicates_bytes
149     test_normalization_file_spans_two_whole_blocks("2:3:f1 2:3:f1", 1)
150   end
151
152   def test_normalization_handles_duplicate_locator
153     blocks = random_blocks(2, 5)
154     coll = Arv::Collection.new(". %s %s 1:8:f1 11:8:f1\n" %
155                                [blocks.join(" "), blocks.reverse.join(" ")])
156     coll.normalize
157     assert_equal(". #{blocks.join(' ')} #{blocks[0]} 1:8:f1 6:8:f1\n",
158                  coll.manifest_text)
159   end
160
161   ### .cp_r
162
163   def test_simple_file_copy
164     coll = Arv::Collection.new(SIMPLEST_MANIFEST)
165     coll.cp_r("./simple.txt", "./new")
166     assert_equal(SIMPLEST_MANIFEST.sub(" 0:9:", " 0:9:new 0:9:"),
167                  coll.manifest_text)
168   end
169
170   def test_copy_file_into_other_stream(target="./s1/f2", basename="f2")
171     coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
172     coll.cp_r("./f2", target)
173     expected = "%s./s1 %s 0:5:f1 14:4:%s 5:4:f3\n" %
174       [TWO_BY_TWO_MANIFEST_A.first,
175        TWO_BY_TWO_BLOCKS.reverse.join(" "), basename]
176     assert_equal(expected, coll.manifest_text)
177   end
178
179   def test_implicit_copy_file_into_other_stream
180     test_copy_file_into_other_stream("./s1")
181   end
182
183   def test_copy_file_into_other_stream_with_new_name
184     test_copy_file_into_other_stream("./s1/f2a", "f2a")
185   end
186
187   def test_copy_file_over_in_other_stream(target="./s1/f1")
188     coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
189     coll.cp_r("./f1", target)
190     expected = "%s./s1 %s 0:5:f1 14:4:f3\n" %
191       [TWO_BY_TWO_MANIFEST_A.first, TWO_BY_TWO_BLOCKS.join(" ")]
192     assert_equal(expected, coll.manifest_text)
193   end
194
195   def test_implicit_copy_file_over_in_other_stream
196     test_copy_file_over_in_other_stream("./s1")
197   end
198
199   def test_simple_stream_copy
200     coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
201     coll.cp_r("./s1", "./sNew")
202     new_line = TWO_BY_TWO_MANIFEST_A.last.sub("./s1 ", "./sNew ")
203     assert_equal(TWO_BY_TWO_MANIFEST_S + new_line, coll.manifest_text)
204   end
205
206   def test_copy_stream_into_other_stream(target="./dir2/subdir",
207                                          basename="subdir")
208     coll = Arv::Collection.new(MULTILEVEL_MANIFEST)
209     coll.cp_r("./dir1/subdir", target)
210     new_line = MULTILEVEL_MANIFEST.lines[4].sub("./dir1/subdir ",
211                                                 "./dir2/#{basename} ")
212     assert_equal(MULTILEVEL_MANIFEST + new_line, coll.manifest_text)
213   end
214
215   def test_implicit_copy_stream_into_other_stream
216     test_copy_stream_into_other_stream("./dir2")
217   end
218
219   def test_copy_stream_into_other_stream_with_new_name
220     test_copy_stream_into_other_stream("./dir2/newsub", "newsub")
221   end
222
223   def test_copy_stream_over_empty_stream
224     coll = Arv::Collection.new(MULTILEVEL_MANIFEST)
225     (1..3).each do |file_num|
226       coll.rm("./dir0/subdir/file#{file_num}")
227     end
228     coll.cp_r("./dir1/subdir", "./dir0")
229     expected = MULTILEVEL_MANIFEST.lines
230     expected[2] = expected[4].sub("./dir1/", "./dir0/")
231     assert_equal(expected.join(""), coll.manifest_text)
232   end
233
234   def test_copy_stream_over_file_raises_ENOTDIR(source="./s1", target="./f2")
235     coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
236     assert_raises(Errno::ENOTDIR) do
237       coll.cp_r(source, target)
238     end
239   end
240
241   def test_copy_file_under_file_raises_ENOTDIR
242     test_copy_stream_over_file_raises_ENOTDIR("./f1", "./f2/newfile")
243   end
244
245   def test_copy_stream_over_nonempty_stream_merges_and_overwrites
246     blocks = random_blocks(3, 9)
247     manifest_a =
248       ["./subdir #{blocks[0]} 0:1:s1 1:2:zero\n",
249        "./zdir #{blocks[1]} 0:9:zfile\n",
250        "./zdir/subdir #{blocks[2]} 0:1:s2 1:2:zero\n"]
251     coll = Arv::Collection.new(manifest_a.join(""))
252     coll.cp_r("./subdir", "./zdir")
253     manifest_a[2] = "./zdir/subdir %s %s 0:1:s1 9:1:s2 1:2:zero\n" %
254       [blocks[0], blocks[2]]
255     assert_equal(manifest_a.join(""), coll.manifest_text)
256   end
257
258   def test_copy_stream_into_substream(source="./dir1",
259                                       target="./dir1/subdir/dir1")
260     coll = Arv::Collection.new(MULTILEVEL_MANIFEST)
261     coll.cp_r(source, target)
262     expected = MULTILEVEL_MANIFEST.lines.flat_map do |line|
263       [line, line.gsub(/^#{Regexp.escape(source)}([\/ ])/, "#{target}\\1")].uniq
264     end
265     assert_equal(expected.sort.join(""), coll.manifest_text)
266   end
267
268   def test_copy_root
269     test_copy_stream_into_substream(".", "./root")
270   end
271
272   def test_adding_to_root_after_copy
273     coll = Arv::Collection.new(SIMPLEST_MANIFEST)
274     coll.cp_r(".", "./root")
275     src_coll = Arv::Collection.new(COLON_FILENAME_MANIFEST)
276     coll.cp_r("./file:test.txt", ".", src_coll)
277     got_lines = coll.manifest_text.lines
278     assert_equal(2, got_lines.size)
279     assert_match(/^\. \S{33,} \S{33,} 0:9:file:test\.txt 9:9:simple\.txt\n/,
280                  got_lines.first)
281     assert_equal(SIMPLEST_MANIFEST.sub(". ", "./root "), got_lines.last)
282   end
283
284   def test_copy_chaining
285     coll = Arv::Collection.new(SIMPLEST_MANIFEST)
286     coll.cp_r("./simple.txt", "./a").cp_r("./a", "./b")
287     assert_equal(SIMPLEST_MANIFEST.sub(" 0:9:", " 0:9:a 0:9:b 0:9:"),
288                  coll.manifest_text)
289   end
290
291   def prep_two_collections_for_copy(src_stream, dst_stream)
292     blocks = random_blocks(2, 8)
293     src_text = "#{src_stream} #{blocks.first} 0:8:f1\n"
294     dst_text = "#{dst_stream} #{blocks.last} 0:8:f2\n"
295     return [blocks, src_text, dst_text,
296             Arv::Collection.new(src_text.dup),
297             Arv::Collection.new(dst_text.dup)]
298   end
299
300   def test_copy_file_from_other_collection(src_stream=".", dst_stream="./s1")
301     blocks, src_text, dst_text, src_coll, dst_coll =
302       prep_two_collections_for_copy(src_stream, dst_stream)
303     dst_coll.cp_r("#{src_stream}/f1", dst_stream, src_coll)
304     assert_equal("#{dst_stream} #{blocks.join(' ')} 0:8:f1 8:8:f2\n",
305                  dst_coll.manifest_text)
306     assert_equal(src_text, src_coll.manifest_text)
307   end
308
309   def test_copy_file_from_other_collection_to_root
310     test_copy_file_from_other_collection("./s1", ".")
311   end
312
313   def test_copy_stream_from_other_collection
314     blocks, src_text, dst_text, src_coll, dst_coll =
315       prep_two_collections_for_copy("./s2", "./s1")
316     dst_coll.cp_r("./s2", "./s1", src_coll)
317     assert_equal(dst_text + src_text.sub("./s2 ", "./s1/s2 "),
318                  dst_coll.manifest_text)
319     assert_equal(src_text, src_coll.manifest_text)
320   end
321
322   def test_copy_stream_from_other_collection_to_root
323     blocks, src_text, dst_text, src_coll, dst_coll =
324       prep_two_collections_for_copy("./s1", ".")
325     dst_coll.cp_r("./s1", ".", src_coll)
326     assert_equal(dst_text + src_text, dst_coll.manifest_text)
327     assert_equal(src_text, src_coll.manifest_text)
328   end
329
330   def test_copy_stream_contents
331     coll = Arv::Collection.new(MULTILEVEL_MANIFEST)
332     coll.cp_r("./dir0/subdir/", "./dir1/subdir")
333     expect_lines = MULTILEVEL_MANIFEST.lines
334     expect_lines[4] = expect_lines[2].sub("./dir0/", "./dir1/")
335     assert_equal(expect_lines.join(""), coll.manifest_text)
336   end
337
338   def test_copy_file_into_new_stream_with_implicit_filename
339     coll = Arv::Collection.new(SIMPLEST_MANIFEST)
340     coll.cp_r("./simple.txt", "./new/")
341     assert_equal(SIMPLEST_MANIFEST + SIMPLEST_MANIFEST.sub(". ", "./new "),
342                  coll.manifest_text)
343   end
344
345   def test_copy_file_into_new_stream_with_explicit_filename
346     coll = Arv::Collection.new(SIMPLEST_MANIFEST)
347     coll.cp_r("./simple.txt", "./new/newfile.txt")
348     new_line = SIMPLEST_MANIFEST.sub(". ", "./new ").sub(":simple", ":newfile")
349     assert_equal(SIMPLEST_MANIFEST + new_line, coll.manifest_text)
350   end
351
352   def test_copy_stream_contents_into_root
353     coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
354     coll.cp_r("./s1/", ".")
355     assert_equal(". %s 0:5:f1 14:4:f2 5:4:f3\n%s" %
356                  [TWO_BY_TWO_BLOCKS.reverse.join(" "),
357                   TWO_BY_TWO_MANIFEST_A.last],
358                  coll.manifest_text)
359   end
360
361   def test_copy_root_contents_into_stream
362     # This is especially fun, because we're copying a parent into its child.
363     # Make sure that happens depth-first.
364     coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
365     coll.cp_r("./", "./s1")
366     assert_equal("%s./s1 %s 0:5:f1 5:4:f2 14:4:f3\n%s" %
367                  [TWO_BY_TWO_MANIFEST_A.first, TWO_BY_TWO_BLOCKS.join(" "),
368                   TWO_BY_TWO_MANIFEST_A.last.sub("./s1 ", "./s1/s1 ")],
369                  coll.manifest_text)
370   end
371
372   def test_copy_stream_contents_across_collections
373     block = random_block(8)
374     src_coll = Arv::Collection.new("./s1 #{block} 0:8:f1\n")
375     dst_coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
376     dst_coll.cp_r("./s1/", "./s1", src_coll)
377     assert_equal("%s./s1 %s %s 0:8:f1 13:4:f3\n" %
378                  [TWO_BY_TWO_MANIFEST_A.first, block, TWO_BY_TWO_BLOCKS.last],
379                  dst_coll.manifest_text)
380   end
381
382   def test_copy_root_contents_across_collections
383     block = random_block(8)
384     src_coll = Arv::Collection.new(". #{block} 0:8:f1\n")
385     dst_coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
386     dst_coll.cp_r("./", ".", src_coll)
387     assert_equal(". %s %s 0:8:f1 13:4:f2\n%s" %
388                  [block, TWO_BY_TWO_BLOCKS.first, TWO_BY_TWO_MANIFEST_A.last],
389                  dst_coll.manifest_text)
390   end
391
392   def test_copy_root_into_empty_collection
393     block = random_block(8)
394     src_coll = Arv::Collection.new(". #{block} 0:8:f1\n")
395     dst_coll = Arv::Collection.new()
396     dst_coll.cp_r("./", ".", src_coll)
397     assert_equal(". %s 0:8:f1\n" %
398                  [block],
399                  dst_coll.manifest_text)
400   end
401
402   def test_copy_with_repeated_blocks
403     blocks = abcde_blocks
404     src_coll = Arv::Collection.new(". #{blocks[0]} #{blocks[1]} #{blocks[2]} #{blocks[0]} #{blocks[1]} #{blocks[2]} #{blocks[3]} #{blocks[4]} 27:27:f1\n")
405     dst_coll = Arv::Collection.new()
406     dst_coll.cp_r("f1", "./", src_coll)
407     assert_equal(". #{blocks[0]} #{blocks[1]} #{blocks[2]} 0:27:f1\n", dst_coll.manifest_text, "mangled by cp_r")
408   end
409
410   def test_copy_with_repeated_split_blocks
411     blocks = abcde_blocks
412     src_coll = Arv::Collection.new(". #{blocks[0]} #{blocks[1]} #{blocks[2]} #{blocks[0]} #{blocks[1]} #{blocks[2]} #{blocks[3]} #{blocks[4]} 20:27:f1\n")
413     dst_coll = Arv::Collection.new()
414     src_coll.normalize
415     assert_equal(". #{blocks[2]} #{blocks[0]} #{blocks[1]} #{blocks[2]} 2:27:f1\n", src_coll.manifest_text, "mangled by normalize()")
416     dst_coll.cp_r("f1", "./", src_coll)
417     assert_equal(". #{blocks[2]} #{blocks[0]} #{blocks[1]} #{blocks[2]} 2:27:f1\n", dst_coll.manifest_text, "mangled by cp_r")
418   end
419
420   def test_copy_empty_source_path_raises_ArgumentError(src="", dst="./s1")
421     coll = Arv::Collection.new(SIMPLEST_MANIFEST)
422     assert_raises(ArgumentError) do
423       coll.cp_r(src, dst)
424     end
425   end
426
427   def test_copy_empty_destination_path_raises_ArgumentError
428     test_copy_empty_source_path_raises_ArgumentError(".", "")
429   end
430
431   ### .each_file_path
432
433   def test_each_file_path
434     coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
435     if block_given?
436       result = yield(coll)
437     else
438       result = []
439       coll.each_file_path { |path| result << path }
440     end
441     assert_equal(["./f1", "./f2", "./s1/f1", "./s1/f3"], result.sort)
442   end
443
444   def test_each_file_path_without_block
445     test_each_file_path { |coll| coll.each_file_path.to_a }
446   end
447
448   def test_each_file_path_empty_collection
449     assert_empty(Arv::Collection.new.each_file_path.to_a)
450   end
451
452   def test_each_file_path_after_collection_emptied
453     coll = Arv::Collection.new(SIMPLEST_MANIFEST)
454     coll.rm("simple.txt")
455     assert_empty(coll.each_file_path.to_a)
456   end
457
458   def test_each_file_path_deduplicates_manifest_listings
459     coll = Arv::Collection.new(MULTIBLOCK_FILE_MANIFEST)
460     assert_equal(["./repfile", "./s1/repfile", "./s1/uniqfile",
461                   "./uniqfile", "./uniqfile2"],
462                  coll.each_file_path.to_a.sort)
463   end
464
465   ### .exist?
466
467   def test_exist(test_method=:assert, path="f2")
468     coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
469     send(test_method, coll.exist?(path))
470   end
471
472   def test_file_not_exist
473     test_exist(:refute, "f3")
474   end
475
476   def test_stream_exist
477     test_exist(:assert, "s1")
478   end
479
480   def test_file_inside_stream_exist
481     test_exist(:assert, "s1/f1")
482   end
483
484   def test_path_inside_stream_not_exist
485     test_exist(:refute, "s1/f2")
486   end
487
488   def test_path_under_file_not_exist
489     test_exist(:refute, "f2/nonexistent")
490   end
491
492   def test_deep_substreams_not_exist
493     test_exist(:refute, "a/b/c/d/e/f/g")
494   end
495
496   ### .rename
497
498   def test_simple_file_rename
499     coll = Arv::Collection.new(SIMPLEST_MANIFEST)
500     coll.rename("./simple.txt", "./new")
501     assert_equal(SIMPLEST_MANIFEST.sub(":simple.txt", ":new"),
502                  coll.manifest_text)
503   end
504
505   def test_rename_file_into_other_stream(target="./s1/f2", basename="f2")
506     coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
507     coll.rename("./f2", target)
508     expected = ". %s 0:5:f1\n./s1 %s 0:5:f1 14:4:%s 5:4:f3\n" %
509       [TWO_BY_TWO_BLOCKS.first,
510        TWO_BY_TWO_BLOCKS.reverse.join(" "), basename]
511     assert_equal(expected, coll.manifest_text)
512   end
513
514   def test_implicit_rename_file_into_other_stream
515     test_rename_file_into_other_stream("./s1")
516   end
517
518   def test_rename_file_into_other_stream_with_new_name
519     test_rename_file_into_other_stream("./s1/f2a", "f2a")
520   end
521
522   def test_rename_file_over_in_other_stream(target="./s1/f1")
523     coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
524     coll.rename("./f1", target)
525     expected = ". %s 5:4:f2\n./s1 %s 0:5:f1 14:4:f3\n" %
526       [TWO_BY_TWO_BLOCKS.first, TWO_BY_TWO_BLOCKS.join(" ")]
527     assert_equal(expected, coll.manifest_text)
528   end
529
530   def test_implicit_rename_file_over_in_other_stream
531     test_rename_file_over_in_other_stream("./s1")
532   end
533
534   def test_simple_stream_rename
535     coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
536     coll.rename("./s1", "./newS")
537     assert_equal(TWO_BY_TWO_MANIFEST_S.sub("\n./s1 ", "\n./newS "),
538                  coll.manifest_text)
539   end
540
541   def test_rename_stream_into_other_stream(target="./dir2/subdir",
542                                            basename="subdir")
543     coll = Arv::Collection.new(MULTILEVEL_MANIFEST)
544     coll.rename("./dir1/subdir", target)
545     expected = MULTILEVEL_MANIFEST.lines
546     replaced_line = expected.delete_at(4)
547     expected << replaced_line.sub("./dir1/subdir ", "./dir2/#{basename} ")
548     assert_equal(expected.join(""), coll.manifest_text)
549   end
550
551   def test_implicit_rename_stream_into_other_stream
552     test_rename_stream_into_other_stream("./dir2")
553   end
554
555   def test_rename_stream_into_other_stream_with_new_name
556     test_rename_stream_into_other_stream("./dir2/newsub", "newsub")
557   end
558
559   def test_rename_stream_over_empty_stream
560     coll = Arv::Collection.new(MULTILEVEL_MANIFEST)
561     (1..3).each do |file_num|
562       coll.rm("./dir0/subdir/file#{file_num}")
563     end
564     coll.rename("./dir1/subdir", "./dir0")
565     expected = MULTILEVEL_MANIFEST.lines
566     expected[2] = expected.delete_at(4).sub("./dir1/", "./dir0/")
567     assert_equal(expected.sort.join(""), coll.manifest_text)
568   end
569
570   def test_rename_stream_over_file_raises_ENOTDIR
571     coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
572     assert_raises(Errno::ENOTDIR) do
573       coll.rename("./s1", "./f2")
574     end
575   end
576
577   def test_rename_stream_over_nonempty_stream_raises_ENOTEMPTY
578     coll = Arv::Collection.new(MULTILEVEL_MANIFEST)
579     assert_raises(Errno::ENOTEMPTY) do
580       coll.rename("./dir1/subdir", "./dir0")
581     end
582   end
583
584   def test_rename_stream_into_substream(source="./dir1",
585                                         target="./dir1/subdir/dir1")
586     coll = Arv::Collection.new(MULTILEVEL_MANIFEST)
587     coll.rename(source, target)
588     assert_equal(MULTILEVEL_MANIFEST.gsub(/^#{Regexp.escape(source)}([\/ ])/m,
589                                           "#{target}\\1"),
590                  coll.manifest_text)
591   end
592
593   def test_rename_root
594     test_rename_stream_into_substream(".", "./root")
595   end
596
597   def test_adding_to_root_after_rename
598     coll = Arv::Collection.new(SIMPLEST_MANIFEST)
599     coll.rename(".", "./root")
600     src_coll = Arv::Collection.new(SIMPLEST_MANIFEST)
601     coll.cp_r("./simple.txt", ".", src_coll)
602     assert_equal(SIMPLEST_MANIFEST + SIMPLEST_MANIFEST.sub(". ", "./root "),
603                  coll.manifest_text)
604   end
605
606   def test_rename_chaining
607     coll = Arv::Collection.new(SIMPLEST_MANIFEST)
608     coll.rename("./simple.txt", "./x").rename("./x", "./simple.txt")
609     assert_equal(SIMPLEST_MANIFEST, coll.manifest_text)
610   end
611
612   ### .rm
613
614   def test_simple_remove
615     coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S.dup)
616     coll.rm("./f2")
617     assert_equal(TWO_BY_TWO_MANIFEST_S.sub(" 5:4:f2", ""), coll.manifest_text)
618   end
619
620   def empty_stream_and_assert(expect_index=0)
621     coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
622     yield coll
623     assert_equal(TWO_BY_TWO_MANIFEST_A[expect_index], coll.manifest_text)
624   end
625
626   def test_remove_all_files_in_substream
627     empty_stream_and_assert do |coll|
628       coll.rm("./s1/f1")
629       coll.rm("./s1/f3")
630     end
631   end
632
633   def test_remove_all_files_in_root_stream
634     empty_stream_and_assert(1) do |coll|
635       coll.rm("./f1")
636       coll.rm("./f2")
637     end
638   end
639
640   def test_chaining_removes
641     empty_stream_and_assert do |coll|
642       coll.rm("./s1/f1").rm("./s1/f3")
643     end
644   end
645
646   def test_remove_last_file
647     coll = Arv::Collection.new(SIMPLEST_MANIFEST)
648     coll.rm("./simple.txt")
649     assert_equal("", coll.manifest_text)
650   end
651
652   def test_remove_nonexistent_file_raises_ENOENT(path="./NoSuchFile",
653                                                  method=:rm)
654     coll = Arv::Collection.new(SIMPLEST_MANIFEST)
655     assert_raises(Errno::ENOENT) do
656       coll.send(method, path)
657     end
658   end
659
660   def test_remove_from_nonexistent_stream_raises_ENOENT
661     test_remove_nonexistent_file_raises_ENOENT("./NoSuchStream/simple.txt")
662   end
663
664   def test_remove_stream_raises_EISDIR(path="./s1")
665     coll = Arv::Collection.new(TWO_BY_TWO_MANIFEST_S)
666     assert_raises(Errno::EISDIR) do
667       coll.rm(path)
668     end
669   end
670
671   def test_remove_root_raises_EISDIR
672     test_remove_stream_raises_EISDIR(".")
673   end
674
675   def test_remove_empty_string_raises_ArgumentError
676     coll = Arv::Collection.new(SIMPLEST_MANIFEST)
677     assert_raises(ArgumentError) do
678       coll.rm("")
679     end
680   end
681
682   ### rm_r
683
684   def test_recursive_remove
685     empty_stream_and_assert do |coll|
686       coll.rm_r("./s1")
687     end
688   end
689
690   def test_recursive_remove_on_files
691     empty_stream_and_assert do |coll|
692       coll.rm_r("./s1/f1")
693       coll.rm_r("./s1/f3")
694     end
695   end
696
697   def test_recursive_remove_root
698     coll = Arv::Collection.new(MULTILEVEL_MANIFEST)
699     coll.rm_r(".")
700     assert_equal("", coll.manifest_text)
701   end
702
703   def test_rm_r_nonexistent_file_raises_ENOENT(path="./NoSuchFile")
704     test_remove_nonexistent_file_raises_ENOENT("./NoSuchFile", :rm_r)
705   end
706
707   def test_rm_r_from_nonexistent_stream_raises_ENOENT
708     test_remove_nonexistent_file_raises_ENOENT("./NoSuchStream/file", :rm_r)
709   end
710
711   def test_rm_r_empty_string_raises_ArgumentError
712     coll = Arv::Collection.new(SIMPLEST_MANIFEST)
713     assert_raises(ArgumentError) do
714       coll.rm_r("")
715     end
716   end
717
718   ### .modified?
719
720   def test_new_collection_unmodified(*args)
721     coll = Arv::Collection.new(*args)
722     yield coll if block_given?
723     refute(coll.modified?)
724   end
725
726   def test_collection_unmodified_after_instantiation
727     test_new_collection_unmodified(SIMPLEST_MANIFEST)
728   end
729
730   def test_collection_unmodified_after_mark
731     test_new_collection_unmodified(SIMPLEST_MANIFEST) do |coll|
732       coll.cp_r("./simple.txt", "./copy")
733       coll.unmodified
734     end
735   end
736
737   def check_collection_modified
738     coll = Arv::Collection.new(SIMPLEST_MANIFEST)
739     yield coll
740     assert(coll.modified?)
741   end
742
743   def test_collection_modified_after_copy
744     check_collection_modified do |coll|
745       coll.cp_r("./simple.txt", "./copy")
746     end
747   end
748
749   def test_collection_modified_after_remove
750     check_collection_modified do |coll|
751       coll.rm("./simple.txt")
752     end
753   end
754
755   def test_collection_modified_after_rename
756     check_collection_modified do |coll|
757       coll.rename("./simple.txt", "./newname")
758     end
759   end
760 end