From dedf6c118630acb3365a7509890692981a7f5ff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Dimja=C5=A1evi=C4=87?= Date: Thu, 21 Nov 2019 09:49:57 +0100 Subject: [PATCH] Issue #26: reorganise helper functions in rule tests --- test/Fencer/Rules/Test.hs | 197 ++++++++++++++++++++------------------ 1 file changed, 102 insertions(+), 95 deletions(-) diff --git a/test/Fencer/Rules/Test.hs b/test/Fencer/Rules/Test.hs index 57c1418..76b2531 100644 --- a/test/Fencer/Rules/Test.hs +++ b/test/Fencer/Rules/Test.hs @@ -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 = @@ -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 ----------------------------------------------------------------------------