Skip to content

Commit

Permalink
Issue #26: resolve a merge conflict and fix a test description
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko Dimjašević committed Oct 29, 2019
2 parents 4df9ea8 + c518b0b commit 80e0f8c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/Fencer/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ reloadRules logger settings appState = do
-- Read and parse the rules
ruleDefinitions :: [DomainDefinition] <-
loadRulesFromDirectory
(#directory configDir)
(#rootDirectory $ settingsRoot settings)
(#subDirectory $ settingsSubdirectory settings </> "config")
(#ignoreDotFiles (settingsIgnoreDotFiles settings))
Logger.info logger $
Logger.msg ("Parsed rules for domains: " ++
Expand Down
14 changes: 10 additions & 4 deletions lib/Fencer/Rules.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Control.Monad.Extra (partitionM, concatMapM)
import qualified Data.HashMap.Strict as HM
import Named ((:!), arg)
import System.Directory (listDirectory, doesFileExist, doesDirectoryExist, pathIsSymbolicLink)
import System.FilePath ((</>), takeFileName)
import System.FilePath ((</>), makeRelative, normalise, splitDirectories)
import qualified Data.Yaml as Yaml

import Fencer.Types
Expand All @@ -26,22 +26,28 @@ import Fencer.Types
--
-- Throws an exception for unparseable or unreadable files.
loadRulesFromDirectory
:: "directory" :! FilePath
:: "rootDirectory" :! FilePath
-> "subDirectory" :! FilePath
-> "ignoreDotFiles" :! Bool
-> IO [DomainDefinition]
loadRulesFromDirectory
(arg #directory -> directory)
(arg #rootDirectory -> rootDirectory)
(arg #subDirectory -> subDirectory)
(arg #ignoreDotFiles -> ignoreDotFiles)
=
do
let directory = rootDirectory </> subDirectory
files <- listAllFiles directory
mapM Yaml.decodeFileThrow $
if ignoreDotFiles
then filter (not . isDotFile) files
else files
where
isDotFile :: FilePath -> Bool
isDotFile file = "." `isPrefixOf` takeFileName file
isDotFile file =
let
normRelPath = normalise $ makeRelative rootDirectory file
in any ("." `isPrefixOf`) $ splitDirectories normRelPath

-- | Is the path a true directory (not a symlink)?
isDirectory :: FilePath -> IO Bool
Expand Down
13 changes: 7 additions & 6 deletions test/Fencer/Rules/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ expectLoadRules
createDirectoryIfMissing True (tempDir </> dir)
TIO.writeFile (tempDir </> dir </> file) txt
definitions <- loadRulesFromDirectory
(#directory tempDir)
(#rootDirectory tempDir)
(#subDirectory ".")
(#ignoreDotFiles ignoreDotFiles)
assertEqual "unexpected definitions"
(sortOn domainDefinitionId result)
Expand All @@ -68,18 +69,18 @@ test_rulesLoadRulesYaml =
)
(#result [domain1, domain2])

-- | test that 'loadRulesFromDirectory' loads rules from a
-- | test that 'loadRulesFromDirectory' does not load rules from a
-- dot-directory when dot-files should be ignored.
test_rulesLoadRulesDotDirectory :: TestTree
test_rulesLoadRulesDotDirectory =
testCase "Rules are loaded from a dot-directory" $
testCase "Rules are not loaded from a dot-directory" $
expectLoadRules
(#ignoreDotFiles True)
(#files
[ (".domain1" </> "config1.yml", domain1Text)
, (".domain2" </> "config2.yaml", domain2Text) ]
, ("domain2" </> "config2.yaml", domain2Text) ]
)
(#result [domain1, domain2])
(#result [domain2])

-- | test that 'loadRulesFromDirectory' correctly implements the case
-- RUNTIME_IGNOREDOTFILES=true.
Expand All @@ -98,7 +99,7 @@ test_rulesLoadRulesRUNTIME_IGNOREDOTFILEStrue =
-- RUNTIME_IGNOREDOTFILES=false.
test_rulesLoadRulesRUNTIME_IGNOREDOTFILESfalse :: TestTree
test_rulesLoadRulesRUNTIME_IGNOREDOTFILESfalse =
testCase "Rules are not loaded from a dot-file" $
testCase "Rules are loaded from a dot-file" $
expectLoadRules
(#ignoreDotFiles False)
(#files
Expand Down

0 comments on commit 80e0f8c

Please sign in to comment.