]> jaekl.net Git - quanlib.git/commitdiff
Refactor walk_dir_test to reduce complexity
authorChris Jaekl <chris@jaekl.net>
Thu, 27 Jun 2024 02:54:15 +0000 (22:54 -0400)
committerChris Jaekl <chris@jaekl.net>
Thu, 27 Jun 2024 02:54:15 +0000 (22:54 -0400)
test/walk_dir_test.rb

index fc068d15a86d5ca8cd0adc7e15604de982ce1d46..943d5daf68693d94a2758f3496ac63dad3c28945 100644 (file)
@@ -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