From: Chris Jaekl Date: Thu, 11 Jul 2024 01:22:41 +0000 (-0400) Subject: Reduce complexity of book_loader_test and walk_dir X-Git-Url: http://jaekl.net/gitweb/?a=commitdiff_plain;h=8070fa0df296ddbfc6c8dea488f4d10a258904a5;p=quanlib.git Reduce complexity of book_loader_test and walk_dir --- diff --git a/app/walk_dir.rb b/app/walk_dir.rb index d23de80..6049468 100644 --- a/app/walk_dir.rb +++ b/app/walk_dir.rb @@ -75,19 +75,12 @@ class WalkDir next unless Book.can_handle?(file) key = "#{File.dirname(file)}/#{File.basename(file, ".*")}" - if unique.key?(key) - new_ext = File.extname(file) - old_ext = File.extname(unique[key]) - if old_ext == ".pdf" && new_ext == ".epub" - # Prefer EPUB over PDF - puts "REPLACED #{unique[key]} with #{file}" - unique[key] = file + unique[key] = + if unique.key?(key) + preferred_format(old_file: unique[key], new_file: file) else - puts "DROPPED #{file} because it's superceded by #{unique[key]}" + file end - else - unique[key] = file - end end unique.values @@ -115,6 +108,19 @@ class WalkDir 12 end + def preferred_format(old_file:, new_file:) + new_ext = File.extname(new_file) + old_ext = File.extname(old_file) + if old_ext == ".pdf" && new_ext == ".epub" + # Prefer EPUB over PDF + puts "REPLACED #{old_file} with #{new_file}" + new_file + else + puts "DROPPED #{new_file} because it's superseded by #{old_file}" + old_file + end + end + def should_descend_into_directory?(path) child = File.basename(path) File.directory?(path) && child != "." && child != ".." && !File.symlink?(path) diff --git a/test/book_loader_test.rb b/test/book_loader_test.rb index fd0adb5..1bf165f 100644 --- a/test/book_loader_test.rb +++ b/test/book_loader_test.rb @@ -24,6 +24,7 @@ class BookLoaderTest < Minitest::Test end end + # rubocop:disable Metrics/AbcSize def test_load_from_file @store.expects(:get_series).returns(mock_series_lw) @store.connect @@ -36,15 +37,18 @@ class BookLoaderTest < Minitest::Test assert_equal "Louisa May Alcott", author.reading_order assert_equal "Alcott, Louisa May", author.sort_order - expected_descr = "This story follows the lives of the four March sisters&emdash;Meg, Jo, Beth, and Amy&emdash;" \ - "and details their coming of age." + assert_equal( + "This story follows the lives of the four March sisters&emdash;Meg, Jo, Beth, and Amy&emdash;" \ + "and details their coming of age.", + book.description + ) - assert_equal expected_descr, book.description assert_equal "en", book.language assert_equal "Little Women: Or, Meg, Jo, Beth and Amy", book.title assert_equal mock_series_lw.to_s, book.series_id.to_s assert_equal 1, book.volume.to_i end + # rubocop:enable Metrics/AbcSize def test_heading @store.expects(:get_series).returns(mock_series_lw)