Skip to content

Commit

Permalink
Load rules from non-yaml files
Browse files Browse the repository at this point in the history
  • Loading branch information
neongreen committed Oct 4, 2019
1 parent 9279eaa commit 41ad292
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
14 changes: 5 additions & 9 deletions lib/Fencer/Rules.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
17 changes: 17 additions & 0 deletions test/Fencer/Rules/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
-- | Tests for "Fencer.Rules".
module Fencer.Rules.Test
( test_loadRulesYaml
, test_loadRulesNonYaml
)
where

Expand Down Expand Up @@ -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
----------------------------------------------------------------------------
Expand Down

0 comments on commit 41ad292

Please sign in to comment.