-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #26: reorganize test modules for better decoupling
- Loading branch information
Marko Dimjašević
committed
Nov 25, 2019
1 parent
2b5c8c0
commit f182af2
Showing
5 changed files
with
139 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE OverloadedLabels #-} | ||
|
||
-- | Module with helper functions used in rules and other testing. | ||
module Fencer.Rules.Test.Helpers | ||
( toErrorList | ||
, writeContentsToFile | ||
, writeAndLoadRules | ||
, expectLoadRules | ||
) | ||
where | ||
|
||
import BasePrelude | ||
|
||
import qualified Data.Text.IO as TIO | ||
import Named ((:!), arg) | ||
import qualified System.Directory as Dir | ||
import System.FilePath (FilePath, takeDirectory, (</>)) | ||
import qualified System.IO.Temp as Temp | ||
import Test.Tasty.HUnit (assertBool, assertEqual, Assertion) | ||
|
||
import Fencer.Rules (LoadRulesError(..), loadRulesFromDirectory) | ||
import Fencer.Rules.Test.Types (RuleFile(..)) | ||
import Fencer.Types (DomainDefinition(..)) | ||
|
||
-- | 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. | ||
writeContentsToFile | ||
:: "root" :! FilePath | ||
-> "file" :! RuleFile | ||
-> IO () | ||
writeContentsToFile | ||
(arg #root -> root) | ||
(arg #file -> file) = do | ||
|
||
let | ||
dir = takeDirectory (ruleFilePath file) | ||
fullPath = root </> (ruleFilePath file) | ||
Dir.createDirectoryIfMissing True (root </> dir) | ||
TIO.writeFile fullPath (ruleFileContents file) | ||
perms <- Dir.getPermissions fullPath | ||
Dir.setPermissions fullPath (ruleFileModifyPermissions file perms) | ||
|
||
-- | Write the content of files at the given root and load the files. | ||
writeAndLoadRules | ||
:: "ignoreDotFiles" :! Bool | ||
-> "root" :! FilePath | ||
-> "files" :! [RuleFile] | ||
-> IO (Either [LoadRulesError] [DomainDefinition]) | ||
writeAndLoadRules | ||
(arg #ignoreDotFiles -> ignoreDotFiles) | ||
(arg #root -> root) | ||
(arg #files -> files) = do | ||
|
||
forM_ files $ \file -> writeContentsToFile | ||
(#root root) | ||
(#file file) | ||
loadRulesFromDirectory | ||
(#rootDirectory root) | ||
(#subDirectory ".") | ||
(#ignoreDotFiles ignoreDotFiles) | ||
|
||
-- | Create given directory structure and check that | ||
-- 'loadRulesFromDirectory' produces expected result such that file | ||
-- permissions are configurable. | ||
expectLoadRules | ||
:: "ignoreDotFiles" :! Bool | ||
-> "files" :! [RuleFile] | ||
-> "result" :! Either [LoadRulesError] [DomainDefinition] | ||
-> Assertion | ||
expectLoadRules | ||
(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)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-- | Types useful for rule testing. | ||
module Fencer.Rules.Test.Types | ||
( RuleFile(..) | ||
, simpleRuleFile) | ||
where | ||
|
||
import BasePrelude | ||
|
||
import Data.Text (Text) | ||
import qualified System.Directory as Dir | ||
import System.FilePath (FilePath) | ||
|
||
-- | A record useful in testing, which groups together a file path, | ||
-- its contents and file permissions. | ||
data RuleFile = MkRuleFile | ||
{ -- | The path to the file | ||
ruleFilePath :: FilePath | ||
-- | The contents of the file in plain text | ||
, ruleFileContents :: Text | ||
-- | A function specifying how the file permissions should be | ||
-- changed, i.e., what they should be once the file is written to | ||
-- disk. | ||
, ruleFileModifyPermissions :: Dir.Permissions -> Dir.Permissions | ||
} | ||
|
||
simpleRuleFile :: FilePath -> Text -> RuleFile | ||
simpleRuleFile p c = MkRuleFile p c id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters