Skip to content

Commit

Permalink
Issue #26: reorganise helper functions in rule tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko Dimjašević committed Nov 21, 2019
1 parent aa3188e commit dedf6c1
Showing 1 changed file with 102 additions and 95 deletions.
197 changes: 102 additions & 95 deletions test/Fencer/Rules/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,101 +43,6 @@ tests = testGroup "Rule tests"
, test_rulesLoadRulesReadPermissions
]

-- | Write contents to a path in the given root and modify file
-- permissions.
writeFile
:: "root" :! FilePath
-> "path" :! FilePath
-> "content" :! Text
-> "modifyPerms" :! (Dir.Permissions -> Dir.Permissions)
-> IO ()
writeFile
(arg #root -> root)
(arg #path -> path)
(arg #content -> content)
(arg #modifyPerms -> modifyPerms) = do

let
dir = takeDirectory path
fullPath = root </> path
Dir.createDirectoryIfMissing True (root </> dir)
TIO.writeFile fullPath content
perms <- Dir.getPermissions fullPath
Dir.setPermissions fullPath (modifyPerms perms)

writeAndLoadRules
:: "ignoreDotFiles" :! Bool
-> "root" :! FilePath
-> "files" :! [(FilePath, Text, Dir.Permissions -> Dir.Permissions)]
-> IO (Either [LoadRulesError] [DomainDefinition])
writeAndLoadRules
(arg #ignoreDotFiles -> ignoreDotFiles)
(arg #root -> root)
(arg #files -> files) = do

forM_ files $ \(path, txt, permUpdate) -> Fencer.Rules.Test.writeFile
(#root root)
(#path path)
(#content txt)
(#modifyPerms permUpdate)
loadRulesFromDirectory
(#rootDirectory root)
(#subDirectory ".")
(#ignoreDotFiles ignoreDotFiles)

-- | Create given directory structure and check that
-- 'loadRulesFromDirectory' produces expected result such that file
-- permissions are configurable.
expectLoadRulesWithPermissions
:: "ignoreDotFiles" :! Bool
-> "files" :! [(FilePath, Text, Dir.Permissions -> Dir.Permissions)]
-> "result" :! Either [LoadRulesError] [DomainDefinition]
-> Assertion
expectLoadRulesWithPermissions
(arg #ignoreDotFiles -> ignoreDotFiles)
(arg #files -> files)
(arg #result -> result) =
Temp.withSystemTempDirectory "fencer-config" $ \tempDir ->
writeAndLoadRules
(#ignoreDotFiles ignoreDotFiles)
(#root tempDir)
(#files files)
>>= \case
f@(Left _) ->
-- Paths to temporary files vary and there is not much point
-- in writing down exact expected exception messages so the
-- only assertion made is that the number of exceptions is the
-- same.
assertEqual
"unexpected failure"
(length . toErrorList $ result)
(length . toErrorList $ f)
Right definitions -> assertBool "unexpected definitions"
(((==) `on` show)
(sortOn domainDefinitionId <$> result)
(Right $ sortOn domainDefinitionId definitions))
where
toErrorList :: Either [LoadRulesError] [DomainDefinition] -> [LoadRulesError]
toErrorList (Right _) = []
toErrorList (Left fs) = fs

-- | Create given directory structure and check that 'loadRulesFromDirectory'
-- produces expected result.
expectLoadRules
:: "ignoreDotFiles" :! Bool
-> "files" :! [(FilePath, Text)]
-> "result" :! Either [LoadRulesError] [DomainDefinition]
-> Assertion
expectLoadRules
(arg #ignoreDotFiles -> ignoreDotFiles)
(arg #files -> files)
(arg #result -> result) =

expectLoadRulesWithPermissions
(#ignoreDotFiles ignoreDotFiles)
(#files (map (\(path, txt) -> (path, txt, id)) files))
(#result result)

-- | test that 'loadRulesFromDirectory' loads rules from YAML files.
test_rulesLoadRulesYaml :: TestTree
test_rulesLoadRulesYaml =
Expand Down Expand Up @@ -259,6 +164,108 @@ test_rulesLoadRulesReadPermissions =
)
(#result $ Right [domain2])

----------------------------------------------------------------------------
-- Helpers
----------------------------------------------------------------------------

-- | Get a list of values on the Left or an empty list if it is a
-- Right value.
toErrorList :: Either [a] [b] -> [a]
toErrorList (Right _) = []
toErrorList (Left xs) = xs

-- | Write contents to a path in the given root and modify file
-- permissions.
writeFile
:: "root" :! FilePath
-> "path" :! FilePath
-> "content" :! Text
-> "modifyPerms" :! (Dir.Permissions -> Dir.Permissions)
-> IO ()
writeFile
(arg #root -> root)
(arg #path -> path)
(arg #content -> content)
(arg #modifyPerms -> modifyPerms) = do

let
dir = takeDirectory path
fullPath = root </> path
Dir.createDirectoryIfMissing True (root </> dir)
TIO.writeFile fullPath content
perms <- Dir.getPermissions fullPath
Dir.setPermissions fullPath (modifyPerms perms)

-- | Write the content of files at the given root and load the files.
writeAndLoadRules
:: "ignoreDotFiles" :! Bool
-> "root" :! FilePath
-> "files" :! [(FilePath, Text, Dir.Permissions -> Dir.Permissions)]
-> IO (Either [LoadRulesError] [DomainDefinition])
writeAndLoadRules
(arg #ignoreDotFiles -> ignoreDotFiles)
(arg #root -> root)
(arg #files -> files) = do

forM_ files $ \(path, txt, permUpdate) -> Fencer.Rules.Test.writeFile
(#root root)
(#path path)
(#content txt)
(#modifyPerms permUpdate)
loadRulesFromDirectory
(#rootDirectory root)
(#subDirectory ".")
(#ignoreDotFiles ignoreDotFiles)

-- | Create given directory structure and check that
-- 'loadRulesFromDirectory' produces expected result such that file
-- permissions are configurable.
expectLoadRulesWithPermissions
:: "ignoreDotFiles" :! Bool
-> "files" :! [(FilePath, Text, Dir.Permissions -> Dir.Permissions)]
-> "result" :! Either [LoadRulesError] [DomainDefinition]
-> Assertion
expectLoadRulesWithPermissions
(arg #ignoreDotFiles -> ignoreDotFiles)
(arg #files -> files)
(arg #result -> result) =
Temp.withSystemTempDirectory "fencer-config" $ \tempDir ->
writeAndLoadRules
(#ignoreDotFiles ignoreDotFiles)
(#root tempDir)
(#files files)
>>= \case
f@(Left _) ->
-- Paths to temporary files vary and there is not much point
-- in writing down exact expected exception messages so the
-- only assertion made is that the number of exceptions is the
-- same.
assertEqual
"unexpected failure"
(length . toErrorList $ result)
(length . toErrorList $ f)
Right definitions -> assertBool "unexpected definitions"
(((==) `on` show)
(sortOn domainDefinitionId <$> result)
(Right $ sortOn domainDefinitionId definitions))

-- | Create given directory structure and check that 'loadRulesFromDirectory'
-- produces expected result.
expectLoadRules
:: "ignoreDotFiles" :! Bool
-> "files" :! [(FilePath, Text)]
-> "result" :! Either [LoadRulesError] [DomainDefinition]
-> Assertion
expectLoadRules
(arg #ignoreDotFiles -> ignoreDotFiles)
(arg #files -> files)
(arg #result -> result) =

expectLoadRulesWithPermissions
(#ignoreDotFiles ignoreDotFiles)
(#files (map (\(path, txt) -> (path, txt, id)) files))
(#result result)

----------------------------------------------------------------------------
-- Sample definitions
----------------------------------------------------------------------------
Expand Down

0 comments on commit dedf6c1

Please sign in to comment.