Skip to content

Commit

Permalink
Tweak bag_adapter; update/expand tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ssciolla committed Aug 5, 2024
1 parent 85ff984 commit d24709d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
5 changes: 2 additions & 3 deletions lib/bag_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ class BagAdapter

attr_reader :additional_tag_files

def initialize(target_dir, detect_hidden)
@detect_hidden = detect_hidden
@bag = BagIt::Bag.new(target_dir, {}, false, @detect_hidden)
def initialize(target_dir:, detect_hidden: true)
@bag = BagIt::Bag.new(target_dir, {}, false, detect_hidden)
@additional_tag_files = []
end

Expand Down
48 changes: 35 additions & 13 deletions test/test_bag_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,32 @@ def setup
@test_tag_file_name = "some-tag-file.txt"
@test_tag_file_text = "TAG FILE TEXT GOES HERE"

@regular_data_file_name = "something.txt"
@hidden_data_file_name = ".hidden"

# Reset test directory
FileUtils.rm_r(@test_dir_path) if File.exist?(@test_dir_path)
Dir.mkdir @test_dir_path
@detect_hidden_yes = true
@detect_hidden_no = false
end

def add_data_file
def add_data_files
File.write(
File.join(@data_dir_path, "something.txt"),
File.join(@data_dir_path, @regular_data_file_name),
"Something to be preserved"
)
File.write(File.join(@data_dir_path, @hidden_data_file_name), "")
end

def test_bag_dir
assert_equal(
BagAdapter::BagAdapter.new(@test_dir_path, @detect_hidden_no).bag_dir,
BagAdapter::BagAdapter.new(target_dir: @test_dir_path).bag_dir,
@test_dir_path
)
end

def test_data_dir
assert_equal(
BagAdapter::BagAdapter.new(@test_dir_path, @detect_hidden_no).data_dir,
BagAdapter::BagAdapter.new(target_dir: @test_dir_path).data_dir,
@data_dir_path
)
end
Expand All @@ -43,13 +45,13 @@ def test_add_bag_info
expected_text = <<~TEXT
Bag-Software-Agent: BagIt Ruby Gem (https://github.com/tipr/bagit)
Bagging-Date: 2023-12-22
Payload-Oxum: 25.1
Payload-Oxum: 25.2
Some-Custom-Key: Some Value
TEXT

Date.stub :today, Date.new(2023, 12, 22) do
bag = BagAdapter::BagAdapter.new(@test_dir_path, @detect_hidden_no)
add_data_file
bag = BagAdapter::BagAdapter.new(target_dir: @test_dir_path)
add_data_files
bag.add_bag_info(@test_bag_info_data)
end

Expand All @@ -58,7 +60,7 @@ def test_add_bag_info
end

def test_add_tag_file
bag = BagAdapter::BagAdapter.new(@test_dir_path, @detect_hidden_no)
bag = BagAdapter::BagAdapter.new(target_dir: @test_dir_path)
bag.add_tag_file!(tag_file_text: @test_tag_file_text, file_name: @test_tag_file_name)

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

def test_add_manifests
bag = BagAdapter::BagAdapter.new(@test_dir_path, @detect_hidden_no)
add_data_file
bag = BagAdapter::BagAdapter.new(target_dir: @test_dir_path)
add_data_files
bag.add_tag_file!(tag_file_text: @test_tag_file_text, file_name: @test_tag_file_name)
bag.add_manifests

assert File.exist?(File.join(@test_dir_path, "manifest-md5.txt"))
expected_manifest_file = File.join(@test_dir_path, "manifest-md5.txt")
assert File.exist?(expected_manifest_file)
if File.exist?(expected_manifest_file)
file_text = File.read(expected_manifest_file)
assert file_text.include?(@regular_data_file_name)
assert file_text.include?(@hidden_data_file_name)
end

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

assert !File.exist?(File.join(@test_dir_path, "tagmanifest-sha1.txt"))
end

def test_add_manifests_when_detect_hidden_false
bag = BagAdapter::BagAdapter.new(target_dir: @test_dir_path, detect_hidden: false)
add_data_files
bag.add_manifests

expected_manifest_file = File.join(@test_dir_path, "manifest-md5.txt")
assert File.exist?(expected_manifest_file)
if File.exist?(expected_manifest_file)
file_text = File.read(expected_manifest_file)
assert file_text.include?(@regular_data_file_name)
refute file_text.include?(@hidden_data_file_name)
end
end
end

0 comments on commit d24709d

Please sign in to comment.