lib: add tests and code for nonexistant files and malformed globs

This commit is contained in:
Joseph Weston 2018-07-23 14:36:45 +02:00 committed by Simon Michael
parent 2c4d0c0acd
commit 5077443a84
2 changed files with 23 additions and 5 deletions

View File

@ -204,11 +204,17 @@ includedirectivep = do
void newline
where
getFilePaths curdir fileglob =
if isLiteral (compile fileglob)
-- </> and globDir1 correctly handle case when 'fileglob' is absolute
then pure [curdir </> fileglob]
else liftIO $ globDir1 (compile fileglob) curdir
getFilePaths curdir filename = do
-- Compiling filename as a glob pattern works even if it is a literal
fileglob <- case tryCompileWith compDefault{errorRecovery=False} filename of
Right x -> pure x
Left e -> fail $ "Invalid glob pattern: " ++ e
-- Get all matching files in the current working directory
filepaths <- liftIO $ globDir1 fileglob curdir
if (not . null) filepaths
then pure filepaths
else fail $ "No existing files match pattern: " ++ filename
parseChild parentpos filepath = do
childInput <- lift $ readFilePortably filepath
`orRethrowIOError` (show parentpos ++ " reading " ++ filepath)

View File

@ -48,3 +48,15 @@ include *y.journal
(B) 1
>>>=0
# 4. include invalid glob patterns
hledger -f - print
<<<
include [.journal
>>>=1
# 5. include nonexsitant file
hledger -f - print
<<<
include doesnotexist.journal
>>>=1