Skip to content

Commit d24709d

Browse files
committed
Tweak bag_adapter; update/expand tests
1 parent 85ff984 commit d24709d

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

lib/bag_adapter.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ class BagAdapter
88

99
attr_reader :additional_tag_files
1010

11-
def initialize(target_dir, detect_hidden)
12-
@detect_hidden = detect_hidden
13-
@bag = BagIt::Bag.new(target_dir, {}, false, @detect_hidden)
11+
def initialize(target_dir:, detect_hidden: true)
12+
@bag = BagIt::Bag.new(target_dir, {}, false, detect_hidden)
1413
@additional_tag_files = []
1514
end
1615

test/test_bag_adapter.rb

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,32 @@ def setup
1111
@test_tag_file_name = "some-tag-file.txt"
1212
@test_tag_file_text = "TAG FILE TEXT GOES HERE"
1313

14+
@regular_data_file_name = "something.txt"
15+
@hidden_data_file_name = ".hidden"
16+
1417
# Reset test directory
1518
FileUtils.rm_r(@test_dir_path) if File.exist?(@test_dir_path)
1619
Dir.mkdir @test_dir_path
17-
@detect_hidden_yes = true
18-
@detect_hidden_no = false
1920
end
2021

21-
def add_data_file
22+
def add_data_files
2223
File.write(
23-
File.join(@data_dir_path, "something.txt"),
24+
File.join(@data_dir_path, @regular_data_file_name),
2425
"Something to be preserved"
2526
)
27+
File.write(File.join(@data_dir_path, @hidden_data_file_name), "")
2628
end
2729

2830
def test_bag_dir
2931
assert_equal(
30-
BagAdapter::BagAdapter.new(@test_dir_path, @detect_hidden_no).bag_dir,
32+
BagAdapter::BagAdapter.new(target_dir: @test_dir_path).bag_dir,
3133
@test_dir_path
3234
)
3335
end
3436

3537
def test_data_dir
3638
assert_equal(
37-
BagAdapter::BagAdapter.new(@test_dir_path, @detect_hidden_no).data_dir,
39+
BagAdapter::BagAdapter.new(target_dir: @test_dir_path).data_dir,
3840
@data_dir_path
3941
)
4042
end
@@ -43,13 +45,13 @@ def test_add_bag_info
4345
expected_text = <<~TEXT
4446
Bag-Software-Agent: BagIt Ruby Gem (https://github.com/tipr/bagit)
4547
Bagging-Date: 2023-12-22
46-
Payload-Oxum: 25.1
48+
Payload-Oxum: 25.2
4749
Some-Custom-Key: Some Value
4850
TEXT
4951

5052
Date.stub :today, Date.new(2023, 12, 22) do
51-
bag = BagAdapter::BagAdapter.new(@test_dir_path, @detect_hidden_no)
52-
add_data_file
53+
bag = BagAdapter::BagAdapter.new(target_dir: @test_dir_path)
54+
add_data_files
5355
bag.add_bag_info(@test_bag_info_data)
5456
end
5557

@@ -58,7 +60,7 @@ def test_add_bag_info
5860
end
5961

6062
def test_add_tag_file
61-
bag = BagAdapter::BagAdapter.new(@test_dir_path, @detect_hidden_no)
63+
bag = BagAdapter::BagAdapter.new(target_dir: @test_dir_path)
6264
bag.add_tag_file!(tag_file_text: @test_tag_file_text, file_name: @test_tag_file_name)
6365

6466
expected_file_path = File.join(@test_dir_path, @test_tag_file_name)
@@ -70,12 +72,18 @@ def test_add_tag_file
7072
end
7173

7274
def test_add_manifests
73-
bag = BagAdapter::BagAdapter.new(@test_dir_path, @detect_hidden_no)
74-
add_data_file
75+
bag = BagAdapter::BagAdapter.new(target_dir: @test_dir_path)
76+
add_data_files
7577
bag.add_tag_file!(tag_file_text: @test_tag_file_text, file_name: @test_tag_file_name)
7678
bag.add_manifests
7779

78-
assert File.exist?(File.join(@test_dir_path, "manifest-md5.txt"))
80+
expected_manifest_file = File.join(@test_dir_path, "manifest-md5.txt")
81+
assert File.exist?(expected_manifest_file)
82+
if File.exist?(expected_manifest_file)
83+
file_text = File.read(expected_manifest_file)
84+
assert file_text.include?(@regular_data_file_name)
85+
assert file_text.include?(@hidden_data_file_name)
86+
end
7987

8088
expected_tagmanifest_path = File.join(@test_dir_path, "tagmanifest-md5.txt")
8189
assert File.exist?(expected_tagmanifest_path)
@@ -87,4 +95,18 @@ def test_add_manifests
8795

8896
assert !File.exist?(File.join(@test_dir_path, "tagmanifest-sha1.txt"))
8997
end
98+
99+
def test_add_manifests_when_detect_hidden_false
100+
bag = BagAdapter::BagAdapter.new(target_dir: @test_dir_path, detect_hidden: false)
101+
add_data_files
102+
bag.add_manifests
103+
104+
expected_manifest_file = File.join(@test_dir_path, "manifest-md5.txt")
105+
assert File.exist?(expected_manifest_file)
106+
if File.exist?(expected_manifest_file)
107+
file_text = File.read(expected_manifest_file)
108+
assert file_text.include?(@regular_data_file_name)
109+
refute file_text.include?(@hidden_data_file_name)
110+
end
111+
end
90112
end

0 commit comments

Comments
 (0)