Skip to content

Commit 5df497f

Browse files
committed
Adds unit tests for Zeitwerk::Loader::FileSystem
1 parent 0a7021a commit 5df497f

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# frozen_string_literal: true
2+
3+
require "fileutils"
4+
require "test_helper"
5+
6+
class TestFileSystemUtilities < LoaderTest
7+
ABSPATHS_WITHOUT_RB_EXTENSION = %W(/tmp/Rakefile /tmp/README.md /tmp/template.rb.erb).freeze
8+
9+
def setup
10+
super
11+
@fs = loader.instance_variable_get(:@fs)
12+
end
13+
14+
# --- supported_ftype? ---
15+
16+
test "supported_ftype? return :file is the extension is .rb" do
17+
assert_equal :file, @fs.supported_ftype?(__FILE__)
18+
end
19+
20+
test "supported_ftype? return :directory if it's a directory" do
21+
assert_equal :directory, @fs.supported_ftype?(__dir__)
22+
end
23+
24+
test "supported_ftype? return nil for anything else" do
25+
ABSPATHS_WITHOUT_RB_EXTENSION.each do |file_name|
26+
assert_nil @fs.supported_ftype?(file_name)
27+
end
28+
end
29+
30+
# --- rb_extension? ---
31+
32+
test "rb_extension? returns true if the argument has extension .rb" do
33+
assert @fs.rb_extension?(__FILE__)
34+
end
35+
36+
test "rb_extension? return false otherwise" do
37+
ABSPATHS_WITHOUT_RB_EXTENSION.each do |file_name|
38+
assert !@fs.rb_extension?(file_name)
39+
end
40+
end
41+
42+
# --- dir? ---
43+
44+
test "dir? returns true if the argument is the name of an existing directory" do
45+
assert @fs.dir?(__dir__)
46+
end
47+
48+
test "dir? returns true if the argument is the name of a symlink of an existing directory" do
49+
with_files do
50+
original = File.expand_path("original")
51+
FileUtils.mkdir('original')
52+
assert @fs.dir?(original) # precondition
53+
54+
symlink = File.expand_path("symlink")
55+
FileUtils.ln_s(original, symlink)
56+
assert @fs.dir?(symlink)
57+
end
58+
rescue NotImplementedError
59+
skip "Symlinks are not supported on this platform, it is OK"
60+
end
61+
62+
test "dir? return false otherwise" do
63+
assert !@fs.dir?(__FILE__)
64+
end
65+
66+
# --- walk_up ---
67+
68+
test "walk_up yields the given directory and its ancestors" do
69+
dir = Pathname.new(__dir__)
70+
71+
expected = []
72+
dir.ascend { expected << _1.to_path }
73+
74+
yielded = []
75+
@fs.walk_up(dir.to_path) { yielded << _1 }
76+
77+
assert_equal expected, yielded
78+
end
79+
80+
test "walk_up does not assume the directory exists" do
81+
dir = "/foo/bar/baz"
82+
83+
yielded = []
84+
@fs.walk_up(dir) { yielded << _1 }
85+
86+
assert_equal ["/foo/bar/baz", "/foo/bar", "/foo", "/"], yielded
87+
end
88+
end
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# frozen_string_literal: true
2+
3+
require "fileutils"
4+
require "test_helper"
5+
6+
class TestFileSystemLS < LoaderTest
7+
def setup
8+
super
9+
@fs = loader.instance_variable_get(:@fs)
10+
end
11+
12+
def yielded(dir)
13+
yielded = []
14+
@fs.ls(dir) { |*entry| yielded << entry }
15+
yielded
16+
end
17+
18+
test "ls yields Ruby files" do
19+
with_files(["user.rb"]) do |cwd|
20+
assert_equal [["user.rb", "#{cwd}/user.rb", :file]], yielded(cwd)
21+
end
22+
end
23+
24+
test "ls yields symlinks with .rb extension and the extension of the original file is irrelevant" do
25+
with_files(["original.whatever"]) do |cwd|
26+
original = File.expand_path("original.whatever")
27+
FileUtils.ln_s(original, "#{cwd}/symlink.rb")
28+
assert_equal [["symlink.rb", "#{cwd}/symlink.rb", :file]], yielded(cwd)
29+
end
30+
rescue NotImplementedError
31+
skip "Symlinks are not supported on this platform, it is OK"
32+
end
33+
34+
test "ls yields directories with Ruby files" do
35+
with_files(["admin/users_controller.rb"]) do |cwd|
36+
assert_equal [["admin", "#{cwd}/admin", :directory]], yielded(cwd)
37+
end
38+
end
39+
40+
test "ls yields symlinks to directories" do
41+
with_files(["out/original/users_controller.rb"]) do |cwd|
42+
FileUtils.mkdir("project")
43+
FileUtils.ln_s("#{cwd}/out/original", "#{cwd}/project/symlink")
44+
assert_equal [["symlink", "#{cwd}/project/symlink", :directory]], yielded("#{cwd}/project")
45+
end
46+
end
47+
48+
test "ls yields entries in sorted order" do
49+
with_files(["b.rb", "c/foo.rb", "a.rb"]) do |cwd|
50+
assert_equal ["a.rb", "b.rb", "c"], yielded(cwd).map(&:first)
51+
end
52+
end
53+
54+
test "ls ignores hidden files and directories" do
55+
with_files([".hidden"]) do |cwd|
56+
assert_empty yielded(cwd)
57+
end
58+
end
59+
60+
test "ls ignores files without .rb extension" do
61+
with_files(["main.js", "Gemfile"]) do |cwd|
62+
assert_empty yielded(cwd)
63+
end
64+
end
65+
66+
test "ls ignores directories without Ruby files" do
67+
with_files(["assets/home.css"]) do |cwd|
68+
assert_empty yielded(cwd)
69+
end
70+
end
71+
72+
test "ls ignores ignored Ruby files" do
73+
with_setup(["ignored.rb"]) do |cwd|
74+
assert_empty yielded(cwd)
75+
end
76+
end
77+
78+
test "ls ignores ignored directories" do
79+
with_setup(["ignored/users_controller.rb"]) do |cwd|
80+
assert_empty yielded(cwd)
81+
end
82+
end
83+
84+
test "ls ignores directories with only ignored Ruby files" do
85+
with_setup(["admin/ignored.rb"]) do |cwd|
86+
assert_empty yielded(cwd)
87+
end
88+
end
89+
90+
test "ls ignores nested root directories" do
91+
with_setup(["rd/user.rb"], dirs: %w(. rd)) do |cwd|
92+
assert_empty yielded(cwd)
93+
end
94+
end
95+
end

test/lib/zeitwerk/test_file_system.rb

Whitespace-only changes.

0 commit comments

Comments
 (0)