Skip to content

Commit

Permalink
make sure TOML.parsefile error when used on a directory (#38078)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Oct 18, 2020
1 parent 444aa87 commit 44b55d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 7 additions & 4 deletions stdlib/TOML/src/TOML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ module Internals
end
end

# https://github.com/JuliaLang/julia/issues/36605
readstring(f::AbstractString) = isfile(f) ? read(f, String) : error(repr(f), ": No such file")

"""
Parser()
Expand All @@ -38,9 +41,9 @@ Parse file `f` and return the resulting table (dictionary). Throw a
See also: [`TOML.tryparsefile`](@ref)
"""
parsefile(f::AbstractString) =
Internals.parse(Parser(read(f, String); filepath=abspath(f)))
Internals.parse(Parser(readstring(f); filepath=abspath(f)))
parsefile(p::Parser, f::AbstractString) =
Internals.parse(Internals.reinit!(p, read(f, String); filepath=abspath(f)))
Internals.parse(Internals.reinit!(p, readstring(f); filepath=abspath(f)))

"""
tryparsefile(f::AbstractString)
Expand All @@ -52,9 +55,9 @@ Parse file `f` and return the resulting table (dictionary). Return a
See also: [`TOML.parsefile`](@ref)
"""
tryparsefile(f::AbstractString) =
Internals.tryparse(Parser(read(f, String); filepath=abspath(f)))
Internals.tryparse(Parser(readstring(f); filepath=abspath(f)))
tryparsefile(p::Parser, f::AbstractString) =
Internals.tryparse(Internals.reinit!(p, read(f, String); filepath=abspath(f)))
Internals.tryparse(Internals.reinit!(p, readstring(f); filepath=abspath(f)))

"""
parse(x::Union{AbstractString, IO})
Expand Down
4 changes: 4 additions & 0 deletions stdlib/TOML/test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ using TOML: ParserError
@test_throws ParserError TOML.parsefile(SubString(invalid_path))
@test_throws ParserError TOML.parsefile(p, invalid_path)
@test_throws ParserError TOML.parsefile(p, SubString(invalid_path))
@test_throws ErrorException TOML.parsefile(homedir())
@test_throws ErrorException TOML.parsefile(p, homedir())
# TOML.tryparsefile
@test TOML.tryparsefile(path) == TOML.tryparsefile(SubString(path)) ==
TOML.tryparsefile(p, path) == TOML.tryparsefile(p, SubString(path)) == dict
@test TOML.tryparsefile(invalid_path) isa ParserError
@test TOML.tryparsefile(SubString(invalid_path)) isa ParserError
@test TOML.tryparsefile(p, invalid_path) isa ParserError
@test TOML.tryparsefile(p, SubString(invalid_path)) isa ParserError
@test_throws ErrorException TOML.tryparsefile(homedir())
@test_throws ErrorException TOML.tryparsefile(p, homedir())
end

0 comments on commit 44b55d1

Please sign in to comment.