From a86a656ed0983891170edcef5a20457d116e75c2 Mon Sep 17 00:00:00 2001 From: Nolan J Tait Date: Mon, 31 Jul 2023 00:32:03 -0700 Subject: [PATCH] Fixes #key missing for NilClass errors on empty folders --- lib/dry/files/memory_file_system.rb | 2 +- spec/unit/dry/files/file_system_spec.rb | 6 ++++++ spec/unit/dry/files/memory_file_system_spec.rb | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/dry/files/memory_file_system.rb b/lib/dry/files/memory_file_system.rb index 335ec5d..9c8cade 100644 --- a/lib/dry/files/memory_file_system.rb +++ b/lib/dry/files/memory_file_system.rb @@ -401,7 +401,7 @@ def entries(path) raise IOError, Errno::ENOENT.new(path.to_s) if node.nil? raise IOError, Errno::ENOTDIR.new(path.to_s) unless node.directory? - [".", ".."] + node.children.keys + [".", ".."] + Array(node.children&.keys) end private diff --git a/spec/unit/dry/files/file_system_spec.rb b/spec/unit/dry/files/file_system_spec.rb index 37e9ff8..aa7091d 100644 --- a/spec/unit/dry/files/file_system_spec.rb +++ b/spec/unit/dry/files/file_system_spec.rb @@ -609,6 +609,12 @@ expect(subject.entries(root)).to eq [".", "..", "file-2.txt", "file-1.txt"] end + it "returns an array with only relative paths on an empty directory" do + subject.mkdir(root.join("empty")) + + expect(subject.entries(root.join("empty"))).to eq [".", ".."] + end + it "raises error if directory doesn't exist" do path = root.join("non-existent") diff --git a/spec/unit/dry/files/memory_file_system_spec.rb b/spec/unit/dry/files/memory_file_system_spec.rb index c58f173..29c8591 100644 --- a/spec/unit/dry/files/memory_file_system_spec.rb +++ b/spec/unit/dry/files/memory_file_system_spec.rb @@ -660,5 +660,11 @@ def call(*) "file-2.txt" ] end + + it "returns an array with only relative paths on an empty directory" do + subject.mkdir("empty") + + expect(subject.entries(subject.join("empty"))).to eq [".", ".."] + end end end