diff --git a/lib/Fencer/Rules.hs b/lib/Fencer/Rules.hs index c149167..430a498 100644 --- a/lib/Fencer/Rules.hs +++ b/lib/Fencer/Rules.hs @@ -15,7 +15,7 @@ import BasePrelude import qualified Data.HashMap.Strict as HM import Named ((:!), arg) import System.Directory (listDirectory, doesFileExist) -import System.FilePath ((), takeExtension, takeFileName) +import System.FilePath ((), takeFileName) import qualified Data.Yaml as Yaml import Fencer.Types @@ -36,15 +36,11 @@ loadRulesFromDirectory files <- filterM doesFileExist . map (directory ) =<< listDirectory directory - let ruleFiles = - (if ignoreDotFiles then filter (not . isDotFile) else id) $ - filter isYaml files - mapM Yaml.decodeFileThrow ruleFiles - -- TODO: what does lyft/ratelimit do with unparseable files? + mapM Yaml.decodeFileThrow $ + if ignoreDotFiles + then filter (not . isDotFile) files + else files where - isYaml :: FilePath -> Bool - isYaml file = takeExtension file `elem` [".yml", ".yaml"] - isDotFile :: FilePath -> Bool isDotFile file = "." `isPrefixOf` takeFileName file diff --git a/test/Fencer/Rules/Test.hs b/test/Fencer/Rules/Test.hs index 446ac99..f97e52a 100644 --- a/test/Fencer/Rules/Test.hs +++ b/test/Fencer/Rules/Test.hs @@ -5,6 +5,7 @@ -- | Tests for "Fencer.Rules". module Fencer.Rules.Test ( test_loadRulesYaml + , test_loadRulesNonYaml ) where @@ -35,6 +36,22 @@ test_loadRulesYaml = (sortOn domainDefinitionId [domain1, domain2]) (sortOn domainDefinitionId definitions) +-- | Test that 'loadRulesFromDirectory' loads rules from all files, not just +-- YAML files. +-- +-- This counterintuitive behavior matches the behavior of @lyft/ratelimit@. +test_loadRulesNonYaml :: TestTree +test_loadRulesNonYaml = + testCase "Rules are loaded from non-YAML files" $ do + Temp.withSystemTempDirectory "fencer-config" $ \tempDir -> do + TIO.writeFile (tempDir "config1.bin") domain1Text + TIO.writeFile (tempDir "config2") domain2Text + definitions <- + loadRulesFromDirectory (#directory tempDir) (#ignoreDotFiles True) + assertEqual "unexpected definitions" + (sortOn domainDefinitionId [domain1, domain2]) + (sortOn domainDefinitionId definitions) + ---------------------------------------------------------------------------- -- Sample definitions ----------------------------------------------------------------------------