|
| 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 |
0 commit comments