From 9ae4db6228a90bf10ad7e3d3aeb8eece3b119b5c Mon Sep 17 00:00:00 2001 From: Chris Jaekl Date: Wed, 26 Jun 2024 22:54:15 -0400 Subject: [PATCH] Refactor walk_dir_test to reduce complexity --- test/walk_dir_test.rb | 71 +++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/test/walk_dir_test.rb b/test/walk_dir_test.rb index fc068d1..943d5da 100644 --- a/test/walk_dir_test.rb +++ b/test/walk_dir_test.rb @@ -81,46 +81,43 @@ class WalkDirTest < Minitest::Test WalkDir.new(config_file, root_path) end - def set_sample_dir_expectations - root_entries = %w[ - 00_nonFiction - LouisaAlcott - FranklinDixon - ] - - Dir.expects(:entries).with("./00_nonFiction").returns(%w[BenjaminFranklin]) - Dir.expects(:entries).with("./00_nonFiction/BenjaminFranklin").returns( - %w[ - The_Autobiography_of_Benjamin_Franklin.epub - ] - ) - Dir.expects(:entries).with("./LouisaAlcott").returns(["LW01_Little_Women.epub"]) - Dir.expects(:entries).with("./FranklinDixon").returns( - %w[ - HB001_The_Tower_Treasure.epub - HB002_The_House_on_the_Cliff.epub - ] - ) - - %w[ - ./00_nonFiction - ./00_nonFiction/BenjaminFranklin - ./LouisaAlcott - ./FranklinDixon - ].each do |path| - File.expects(:directory?).with(path).returns(true).at_least_once - File.expects(:symlink?).with(path).returns(false).at_least_once + SAMPLE_DIR = { + "00_nonFiction" => { + "BenjaminFranklin" => ["The_Autobiography_of_Benjamin_Franklin.epub"], + }, + "LouisaAlcott" => ["LW01_Little_Women.epub"], + "FranklinDixon" => %w[ + HB001_The_Tower_Treasure.epub + HB002_The_House_on_the_Cliff.epub + ], + }.freeze + private_constant :SAMPLE_DIR + + def set_dir_expectations(base_path, content) + Dir.expects(:entries).with(base_path).returns(content.keys) unless base_path == "." + + content.each do |child, grandchildren| + File.expects(:directory?).with("#{base_path}/#{child}").returns(true).at_least_once + File.expects(:symlink?).with("#{base_path}/#{child}").returns(false).at_least_once + + if grandchildren.is_a?(Array) + set_file_expectations("#{base_path}/#{child}", grandchildren) + else # grandchildren.is_a?(Hash), so they have children of their own + set_dir_expectations("#{base_path}/#{child}", grandchildren) + end end + end - %w[ - ./00_nonFiction/BenjaminFranklin/The_Autobiography_of_Benjamin_Franklin.epub - ./LouisaAlcott/LW01_Little_Women.epub - ./FranklinDixon/HB001_The_Tower_Treasure.epub - ./FranklinDixon/HB002_The_House_on_the_Cliff.epub - ].each do |path| - File.expects(:directory?).with(path).returns(false).at_least_once + def set_file_expectations(base_path, filenames) + Dir.expects(:entries).with(base_path).returns(filenames).at_least_once + filenames.each do |filename| + File.expects(:directory?).with("#{base_path}/#{filename}").returns(false).at_least_once end + end + + def set_sample_dir_expectations + set_dir_expectations(".", SAMPLE_DIR) - root_entries + SAMPLE_DIR.keys end end -- 2.39.2