Skip to content

Commit 4612877

Browse files
author
José Valim
committed
Improvements to File.ls implementation
1 parent c0fbea8 commit 4612877

File tree

2 files changed

+16
-39
lines changed

2 files changed

+16
-39
lines changed

lib/elixir/lib/file.ex

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -897,44 +897,30 @@ defmodule File do
897897
end
898898
end
899899

900+
@doc """
901+
Returns list of files in the given directory.
900902
901-
@doc """
902-
Returns list of files in the current working directory. In rare circumstances, this function can
903-
fail on Unix. It may happen if read permission does not exist for the parent
904-
directories of the current directory. For this reason, returns `{ :ok, [files] }`
905-
in case of success, `{ :error, reason }` otherwise.
906-
"""
907-
def ls() do
908-
ls(".")
909-
end
910-
911-
@doc """
912-
Returns list of files in the given directory. In rare circumstances, this function can
913-
fail on Unix. It may happen if read permission does not exist for the parent
914-
directories of the current directory. For this reason, returns `{ :ok, [files] }`
915-
in case of success, `{ :error, reason }` otherwise.
903+
It returns `{ :ok, [files] }` in case of success,
904+
`{ :error, reason }` otherwise.
916905
"""
917-
def ls(path) do
906+
def ls(path // ".") do
918907
case F.list_dir(path) do
919908
{ :ok, file_list } -> { :ok, Enum.map file_list, :unicode.characters_to_binary(&1) }
920909
{ :error, _ } = error -> error
921910
end
922911
end
923912

924-
925913
@doc """
926-
Get list of files in directories in `dir`.
927-
928-
Raises File.Error in case of an error.
914+
The same as `ls/1` but raises `File.Error`
915+
in case of an error.
929916
"""
930-
def ls!(dir) do
931-
case F.list_dir(dir) do
932-
{ :ok, file_list } -> Enum.map file_list, :unicode.characters_to_binary(&1)
917+
def ls!(dir // ".") do
918+
case ls(dir) do
919+
{ :ok, value } -> value
933920
{ :error, reason } ->
934921
raise File.Error, reason: reason, action: "list directory", path: :unicode.characters_to_binary(dir)
935922
end
936923
end
937-
938924

939925
@doc """
940926
Closes the file referenced by `io_device`. It mostly returns `:ok`, except

lib/elixir/test/elixir/file_test.exs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -398,32 +398,23 @@ defmodule FileTest do
398398
end
399399

400400
test :ls do
401-
{code, value} = File.ls(fixture_path)
402-
assert code == :ok
403-
refute code == :error
404-
assert is_list value
405-
assert 0 < Enum.count(value)
401+
{ :ok, value } = File.ls(fixture_path)
406402
assert List.member?(value, "code_sample.exs")
407403
assert List.member?(value, "file.txt")
408-
{code2, reason} = File.ls(fixture_path("non-existent-subdirectory"))
409-
refute code2 == :ok
410-
assert code2 == :error
411-
assert reason != nil
404+
405+
{ :error, :enoent } = File.ls(fixture_path("non-existent-subdirectory"))
412406
end
413407

414-
test :ls2 do
408+
test :ls! do
415409
value = File.ls!(fixture_path)
416-
assert is_list value
417-
assert 0 < Enum.count(value)
418410
assert List.member?(value, "code_sample.exs")
419411
assert List.member?(value, "file.txt")
412+
420413
assert_raise File.Error, fn ->
421-
File.ls!(fixture_path("non-existent-subdirectory"))
414+
File.ls!(fixture_path("non-existent-subdirectory"))
422415
end
423416
end
424417

425-
426-
427418
defmodule OpenReadWrite do
428419
use ExUnit.Case
429420

0 commit comments

Comments
 (0)