From a405041619df2505c5652c30f39b9866ed67d622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Dimja=C5=A1evi=C4=87?= Date: Mon, 28 Oct 2019 10:24:28 +0100 Subject: [PATCH 01/11] Issue #26: add a test for rule loading from a dot-directory --- test/Fencer/Rules/Test.hs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/Fencer/Rules/Test.hs b/test/Fencer/Rules/Test.hs index b92cfb6..7d9363c 100644 --- a/test/Fencer/Rules/Test.hs +++ b/test/Fencer/Rules/Test.hs @@ -26,6 +26,7 @@ tests = testGroup "Rule tests" [ test_rulesLoadRulesYaml , test_rulesLoadRulesNonYaml , test_rulesLoadRulesRecursively + , test_rulesLoadRulesDotDirectory ] -- | test that 'loadRulesFromDirectory' loads rules from YAML files. @@ -75,6 +76,21 @@ test_rulesLoadRulesRecursively = (sortOn domainDefinitionId definitions) +-- | test that 'loadRulesFromDirectory' loads rules from a +-- dot-directory when dot-files should be ignored. +test_rulesLoadRulesDotDirectory :: TestTree +test_rulesLoadRulesDotDirectory = + testCase "Rules are loaded from a dot-directory" $ + Temp.withSystemTempDirectory ".fencer-config" $ \tempDir -> do + putStrLn $ show $tempDir + TIO.writeFile (tempDir "config1.yml") domain1Text + TIO.writeFile (tempDir "config2.yaml") domain2Text + definitions <- + loadRulesFromDirectory (#directory tempDir) (#ignoreDotFiles True) + assertEqual "unexpected definitions" + (sortOn domainDefinitionId [domain1, domain2]) + (sortOn domainDefinitionId definitions) + ---------------------------------------------------------------------------- -- Sample definitions ---------------------------------------------------------------------------- From 0b1bc80719df911bb8931cb6e8ea5533c427e920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Dimja=C5=A1evi=C4=87?= Date: Mon, 28 Oct 2019 10:37:03 +0100 Subject: [PATCH 02/11] Issue #26: fix a hlint warning about test code duplication --- test/Fencer/Rules/Test.hs | 49 ++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/test/Fencer/Rules/Test.hs b/test/Fencer/Rules/Test.hs index 7d9363c..be12fb9 100644 --- a/test/Fencer/Rules/Test.hs +++ b/test/Fencer/Rules/Test.hs @@ -29,18 +29,35 @@ tests = testGroup "Rule tests" , test_rulesLoadRulesDotDirectory ] +-- | A helper function for loading rules and making sure they are as +-- expected. +loadRules :: String -> Bool -> IO () +loadRules dirTemplate ignoreDotFiles = + Temp.withSystemTempDirectory dirTemplate $ \tempDir -> do + TIO.writeFile (tempDir "config1.yml") domain1Text + TIO.writeFile (tempDir "config2.yaml") domain2Text + definitions <- + loadRulesFromDirectory + (#directory tempDir) + (#ignoreDotFiles ignoreDotFiles) + assertEqual "unexpected definitions" + (sortOn domainDefinitionId [domain1, domain2]) + (sortOn domainDefinitionId definitions) + + -- | test that 'loadRulesFromDirectory' loads rules from YAML files. test_rulesLoadRulesYaml :: TestTree test_rulesLoadRulesYaml = testCase "Rules are loaded from YAML files" $ - Temp.withSystemTempDirectory "fencer-config" $ \tempDir -> do - TIO.writeFile (tempDir "config1.yml") domain1Text - TIO.writeFile (tempDir "config2.yaml") domain2Text - definitions <- - loadRulesFromDirectory (#directory tempDir) (#ignoreDotFiles True) - assertEqual "unexpected definitions" - (sortOn domainDefinitionId [domain1, domain2]) - (sortOn domainDefinitionId definitions) + loadRules "fencer-config" True + +-- | test that 'loadRulesFromDirectory' loads rules from a +-- dot-directory when dot-files should be ignored. +test_rulesLoadRulesDotDirectory :: TestTree +test_rulesLoadRulesDotDirectory = + testCase "Rules are loaded from a dot-directory" $ + loadRules ".fencer-config" True + -- | Test that 'loadRulesFromDirectory' loads rules from all files, not just -- YAML files. @@ -75,22 +92,6 @@ test_rulesLoadRulesRecursively = (sortOn domainDefinitionId [domain1, domain2]) (sortOn domainDefinitionId definitions) - --- | test that 'loadRulesFromDirectory' loads rules from a --- dot-directory when dot-files should be ignored. -test_rulesLoadRulesDotDirectory :: TestTree -test_rulesLoadRulesDotDirectory = - testCase "Rules are loaded from a dot-directory" $ - Temp.withSystemTempDirectory ".fencer-config" $ \tempDir -> do - putStrLn $ show $tempDir - TIO.writeFile (tempDir "config1.yml") domain1Text - TIO.writeFile (tempDir "config2.yaml") domain2Text - definitions <- - loadRulesFromDirectory (#directory tempDir) (#ignoreDotFiles True) - assertEqual "unexpected definitions" - (sortOn domainDefinitionId [domain1, domain2]) - (sortOn domainDefinitionId definitions) - ---------------------------------------------------------------------------- -- Sample definitions ---------------------------------------------------------------------------- From aab53c0d9a70c25b9c2f884c652ca11fd11bd480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Dimja=C5=A1evi=C4=87?= Date: Mon, 28 Oct 2019 11:18:44 +0100 Subject: [PATCH 03/11] Issue #26: test that RUNTIME_IGNOREDOTFILES is respected --- test/Fencer/Rules/Test.hs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/Fencer/Rules/Test.hs b/test/Fencer/Rules/Test.hs index be12fb9..7fa1313 100644 --- a/test/Fencer/Rules/Test.hs +++ b/test/Fencer/Rules/Test.hs @@ -18,6 +18,7 @@ import Test.Tasty (TestTree, testGroup) import Test.Tasty.HUnit (assertEqual, testCase) import Fencer.Rules +import Fencer.Settings (getSettingsFromEnvironment, settingsIgnoreDotFiles) import Fencer.Types @@ -27,6 +28,7 @@ tests = testGroup "Rule tests" , test_rulesLoadRulesNonYaml , test_rulesLoadRulesRecursively , test_rulesLoadRulesDotDirectory + , test_rulesLoadRulesRUNTIME_IGNOREDOTFILES ] -- | A helper function for loading rules and making sure they are as @@ -59,6 +61,25 @@ test_rulesLoadRulesDotDirectory = loadRules ".fencer-config" True +-- | test that 'loadRulesFromDirectory' respects the +-- RUNTIME_IGNOREDOTFILES environment variable. +test_rulesLoadRulesRUNTIME_IGNOREDOTFILES :: TestTree +test_rulesLoadRulesRUNTIME_IGNOREDOTFILES = + testCase "Rules are not loaded from a dot-file" $ do + setEnv "RUNTIME_IGNOREDOTFILES" "true" + setEnv "RUNTIME_SUBDIRECTORY" "sub" -- this value will not be used anyway + settings <- getSettingsFromEnvironment + Temp.withSystemTempDirectory "fencer-config" $ \tempDir -> do + TIO.writeFile (tempDir "config1.yml") domain1Text + TIO.writeFile (tempDir ".config2.yaml") domain2Text + definitions <- + loadRulesFromDirectory + (#directory tempDir) + (#ignoreDotFiles $ settingsIgnoreDotFiles settings) + assertEqual "RUNTIME_IGNOREDOTFILES not respected!" + [domain1] + definitions + -- | Test that 'loadRulesFromDirectory' loads rules from all files, not just -- YAML files. -- From 0ce515ec4fb409e2b124306fe723ce8f14131b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Dimja=C5=A1evi=C4=87?= Date: Mon, 28 Oct 2019 11:24:06 +0100 Subject: [PATCH 04/11] Issue #26: add a clarification in the README about dot-directories --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2ee5a97..5f152b9 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ Fencer-specific environment variables are: - `RUNTIME_IGNOREDOTFILES` - A flag indicating whether to ignore files with names starting with a dot (hidden files on Linux-based systems and macOS). It can be `True` or `False`. The default value is - `False`. + `False`. Directories with names starting in a dot are not ignored. - `GRPC_PORT` - The port to run the gRPC server on. Default is 8081. ## Developing From 908d62d3565e0af9ac5448a77c1f328625867521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Dimja=C5=A1evi=C4=87?= Date: Mon, 28 Oct 2019 16:12:32 +0100 Subject: [PATCH 05/11] Issue #26: implement Artyom's feedback on generalizing the rule tests --- fencer.cabal | 1 + test/Fencer/Rules/Test.hs | 92 ++++++++++++++++++++++++--------------- 2 files changed, 58 insertions(+), 35 deletions(-) diff --git a/fencer.cabal b/fencer.cabal index e6c5f97..cc872a5 100644 --- a/fencer.cabal +++ b/fencer.cabal @@ -124,6 +124,7 @@ test-suite test-fencer , directory , filepath , grpc-haskell + , named , neat-interpolation , proto3-wire , proto3-suite diff --git a/test/Fencer/Rules/Test.hs b/test/Fencer/Rules/Test.hs index be12fb9..c27ee39 100644 --- a/test/Fencer/Rules/Test.hs +++ b/test/Fencer/Rules/Test.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE DataKinds #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedLabels #-} @@ -10,12 +11,13 @@ import BasePrelude import Data.List (sortOn) import Data.Text (Text) import qualified Data.Text.IO as TIO +import Named ((:!), arg) import NeatInterpolation (text) import qualified System.IO.Temp as Temp -import System.FilePath (()) +import System.FilePath (splitFileName, ()) import System.Directory (createDirectoryIfMissing) import Test.Tasty (TestTree, testGroup) -import Test.Tasty.HUnit (assertEqual, testCase) +import Test.Tasty.HUnit (assertEqual, Assertion, testCase) import Fencer.Rules import Fencer.Types @@ -29,35 +31,57 @@ tests = testGroup "Rule tests" , test_rulesLoadRulesDotDirectory ] --- | A helper function for loading rules and making sure they are as --- expected. -loadRules :: String -> Bool -> IO () -loadRules dirTemplate ignoreDotFiles = +-- | Create given directory structure and check that 'loadRulesFromDirectory' +-- produces expected result. +expectLoadRules + :: "ignoreDotFiles" :! Bool + -> "dirTemplate" :! String + -> "files" :! [(FilePath, Text)] + -> "result" :! [DomainDefinition] + -> Assertion +expectLoadRules + (arg #ignoreDotFiles -> ignoreDotFiles) + (arg #dirTemplate -> dirTemplate) + (arg #files -> files) + (arg #result -> result) = Temp.withSystemTempDirectory dirTemplate $ \tempDir -> do - TIO.writeFile (tempDir "config1.yml") domain1Text - TIO.writeFile (tempDir "config2.yaml") domain2Text - definitions <- - loadRulesFromDirectory - (#directory tempDir) - (#ignoreDotFiles ignoreDotFiles) + forM_ files $ \(path, txt) -> do + let (dir, file) = splitFileName path + createDirectoryIfMissing True (tempDir dir) + TIO.writeFile (tempDir dir file) txt + definitions <- loadRulesFromDirectory + (#directory tempDir) + (#ignoreDotFiles ignoreDotFiles) assertEqual "unexpected definitions" - (sortOn domainDefinitionId [domain1, domain2]) + (sortOn domainDefinitionId result) (sortOn domainDefinitionId definitions) - -- | test that 'loadRulesFromDirectory' loads rules from YAML files. test_rulesLoadRulesYaml :: TestTree test_rulesLoadRulesYaml = testCase "Rules are loaded from YAML files" $ - loadRules "fencer-config" True + expectLoadRules + (#ignoreDotFiles True) + (#dirTemplate "fencer-config") + (#files + [ ("config1.yml", domain1Text) + , ("config2.yaml", domain2Text) ] + ) + (#result [domain1, domain2]) -- | test that 'loadRulesFromDirectory' loads rules from a -- dot-directory when dot-files should be ignored. test_rulesLoadRulesDotDirectory :: TestTree test_rulesLoadRulesDotDirectory = testCase "Rules are loaded from a dot-directory" $ - loadRules ".fencer-config" True - + expectLoadRules + (#ignoreDotFiles True) + (#dirTemplate ".fencer-config") + (#files + [ ("config1.yml", domain1Text) + , ("config2.yaml", domain2Text) ] + ) + (#result [domain1, domain2]) -- | Test that 'loadRulesFromDirectory' loads rules from all files, not just -- YAML files. @@ -66,14 +90,14 @@ test_rulesLoadRulesDotDirectory = test_rulesLoadRulesNonYaml :: TestTree test_rulesLoadRulesNonYaml = testCase "Rules are loaded from non-YAML files" $ - 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) + expectLoadRules + (#ignoreDotFiles True) + (#dirTemplate "fencer-config") + (#files + [ ("config1.bin", domain1Text) + , ("config2", domain2Text) ] + ) + (#result [domain1, domain2]) -- | Test that 'loadRulesFromDirectory' loads rules recursively. -- @@ -81,16 +105,14 @@ test_rulesLoadRulesNonYaml = test_rulesLoadRulesRecursively :: TestTree test_rulesLoadRulesRecursively = testCase "Rules are loaded recursively" $ - Temp.withSystemTempDirectory "fencer-config" $ \tempDir -> do - createDirectoryIfMissing True (tempDir "domain1") - TIO.writeFile (tempDir "domain1/config.yml") domain1Text - createDirectoryIfMissing True (tempDir "domain2/config") - TIO.writeFile (tempDir "domain2/config/config.yml") domain2Text - definitions <- - loadRulesFromDirectory (#directory tempDir) (#ignoreDotFiles True) - assertEqual "unexpected definitions" - (sortOn domainDefinitionId [domain1, domain2]) - (sortOn domainDefinitionId definitions) + expectLoadRules + (#ignoreDotFiles True) + (#dirTemplate "fencer-config") + (#files + [ ("domain1/config.yml", domain1Text) + , ("domain2/config/config.yml", domain2Text) ] + ) + (#result [domain1, domain2]) ---------------------------------------------------------------------------- -- Sample definitions From 066f499a28168b5212dae47b0ac4c0bf0acd122f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Dimja=C5=A1evi=C4=87?= Date: Mon, 28 Oct 2019 16:18:22 +0100 Subject: [PATCH 06/11] Issue #26: changes the dot-directory test per Artyom's feedback --- test/Fencer/Rules/Test.hs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/test/Fencer/Rules/Test.hs b/test/Fencer/Rules/Test.hs index c27ee39..d99e305 100644 --- a/test/Fencer/Rules/Test.hs +++ b/test/Fencer/Rules/Test.hs @@ -35,16 +35,14 @@ tests = testGroup "Rule tests" -- produces expected result. expectLoadRules :: "ignoreDotFiles" :! Bool - -> "dirTemplate" :! String -> "files" :! [(FilePath, Text)] -> "result" :! [DomainDefinition] -> Assertion expectLoadRules (arg #ignoreDotFiles -> ignoreDotFiles) - (arg #dirTemplate -> dirTemplate) (arg #files -> files) (arg #result -> result) = - Temp.withSystemTempDirectory dirTemplate $ \tempDir -> do + Temp.withSystemTempDirectory "fencer-config" $ \tempDir -> do forM_ files $ \(path, txt) -> do let (dir, file) = splitFileName path createDirectoryIfMissing True (tempDir dir) @@ -62,7 +60,6 @@ test_rulesLoadRulesYaml = testCase "Rules are loaded from YAML files" $ expectLoadRules (#ignoreDotFiles True) - (#dirTemplate "fencer-config") (#files [ ("config1.yml", domain1Text) , ("config2.yaml", domain2Text) ] @@ -76,10 +73,9 @@ test_rulesLoadRulesDotDirectory = testCase "Rules are loaded from a dot-directory" $ expectLoadRules (#ignoreDotFiles True) - (#dirTemplate ".fencer-config") (#files - [ ("config1.yml", domain1Text) - , ("config2.yaml", domain2Text) ] + [ (".domain1/config1.yml", domain1Text) + , (".domain2/config2.yaml", domain2Text) ] ) (#result [domain1, domain2]) @@ -92,7 +88,6 @@ test_rulesLoadRulesNonYaml = testCase "Rules are loaded from non-YAML files" $ expectLoadRules (#ignoreDotFiles True) - (#dirTemplate "fencer-config") (#files [ ("config1.bin", domain1Text) , ("config2", domain2Text) ] @@ -107,7 +102,6 @@ test_rulesLoadRulesRecursively = testCase "Rules are loaded recursively" $ expectLoadRules (#ignoreDotFiles True) - (#dirTemplate "fencer-config") (#files [ ("domain1/config.yml", domain1Text) , ("domain2/config/config.yml", domain2Text) ] From e58f63a425b8da925acd3e356da4f1fe2aeaa877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Dimja=C5=A1evi=C4=87?= Date: Mon, 28 Oct 2019 16:38:32 +0100 Subject: [PATCH 07/11] Issue #26: implement Artyom's feedback about RUNTIME_IGNOREDOTFILES --- test/Fencer/Rules/Test.hs | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/test/Fencer/Rules/Test.hs b/test/Fencer/Rules/Test.hs index 61d226e..961e093 100644 --- a/test/Fencer/Rules/Test.hs +++ b/test/Fencer/Rules/Test.hs @@ -20,7 +20,6 @@ import Test.Tasty (TestTree, testGroup) import Test.Tasty.HUnit (assertEqual, Assertion, testCase) import Fencer.Rules -import Fencer.Settings (getSettingsFromEnvironment, settingsIgnoreDotFiles) import Fencer.Types @@ -86,19 +85,13 @@ test_rulesLoadRulesDotDirectory = test_rulesLoadRulesRUNTIME_IGNOREDOTFILES :: TestTree test_rulesLoadRulesRUNTIME_IGNOREDOTFILES = testCase "Rules are not loaded from a dot-file" $ do - setEnv "RUNTIME_IGNOREDOTFILES" "true" - setEnv "RUNTIME_SUBDIRECTORY" "sub" -- this value will not be used anyway - settings <- getSettingsFromEnvironment - Temp.withSystemTempDirectory "fencer-config" $ \tempDir -> do - TIO.writeFile (tempDir "config1.yml") domain1Text - TIO.writeFile (tempDir ".config2.yaml") domain2Text - definitions <- - loadRulesFromDirectory - (#directory tempDir) - (#ignoreDotFiles $ settingsIgnoreDotFiles settings) - assertEqual "RUNTIME_IGNOREDOTFILES not respected!" - [domain1] - definitions + expectLoadRules + (#ignoreDotFiles True) + (#files + [ ("config1.yml", domain1Text) + , ("dir/.config2.yaml", domain2Text) ] + ) + (#result [domain1]) -- | Test that 'loadRulesFromDirectory' loads rules from all files, not just -- YAML files. From 067be4fc93ba9c9806ea5c5e363c13fb9a6a1ade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Dimja=C5=A1evi=C4=87?= Date: Tue, 29 Oct 2019 09:38:23 +0100 Subject: [PATCH 08/11] Issue #26: implements Artyom's feedback for RUNTIME_IGNOREDOTFILES tests --- test/Fencer/Rules/Test.hs | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/test/Fencer/Rules/Test.hs b/test/Fencer/Rules/Test.hs index 961e093..003e33d 100644 --- a/test/Fencer/Rules/Test.hs +++ b/test/Fencer/Rules/Test.hs @@ -29,7 +29,8 @@ tests = testGroup "Rule tests" , test_rulesLoadRulesNonYaml , test_rulesLoadRulesRecursively , test_rulesLoadRulesDotDirectory - , test_rulesLoadRulesRUNTIME_IGNOREDOTFILES + , test_rulesLoadRulesRUNTIME_IGNOREDOTFILEStrue + , test_rulesLoadRulesRUNTIME_IGNOREDOTFILESfalse ] -- | Create given directory structure and check that 'loadRulesFromDirectory' @@ -75,24 +76,37 @@ test_rulesLoadRulesDotDirectory = expectLoadRules (#ignoreDotFiles True) (#files - [ (".domain1/config1.yml", domain1Text) - , (".domain2/config2.yaml", domain2Text) ] + [ (".domain1" "config1.yml", domain1Text) + , (".domain2" "config2.yaml", domain2Text) ] ) (#result [domain1, domain2]) --- | test that 'loadRulesFromDirectory' respects the --- RUNTIME_IGNOREDOTFILES environment variable. -test_rulesLoadRulesRUNTIME_IGNOREDOTFILES :: TestTree -test_rulesLoadRulesRUNTIME_IGNOREDOTFILES = +-- | test that 'loadRulesFromDirectory' correctly implements the case +-- RUNTIME_IGNOREDOTFILES=true. +test_rulesLoadRulesRUNTIME_IGNOREDOTFILEStrue :: TestTree +test_rulesLoadRulesRUNTIME_IGNOREDOTFILEStrue = testCase "Rules are not loaded from a dot-file" $ do expectLoadRules (#ignoreDotFiles True) (#files [ ("config1.yml", domain1Text) - , ("dir/.config2.yaml", domain2Text) ] + , ("dir" ".config2.yaml", domain2Text) ] ) (#result [domain1]) +-- | test that 'loadRulesFromDirectory' correctly implements the case +-- RUNTIME_IGNOREDOTFILES=false. +test_rulesLoadRulesRUNTIME_IGNOREDOTFILESfalse :: TestTree +test_rulesLoadRulesRUNTIME_IGNOREDOTFILESfalse = + testCase "Rules are not loaded from a dot-file" $ do + expectLoadRules + (#ignoreDotFiles False) + (#files + [ ("config1.yml", domain1Text) + , ("dir" ".config2.yaml", domain2Text) ] + ) + (#result [domain1, domain2]) + -- | Test that 'loadRulesFromDirectory' loads rules from all files, not just -- YAML files. -- @@ -117,8 +131,8 @@ test_rulesLoadRulesRecursively = expectLoadRules (#ignoreDotFiles True) (#files - [ ("domain1/config.yml", domain1Text) - , ("domain2/config/config.yml", domain2Text) ] + [ ("domain1" "config.yml", domain1Text) + , ("domain2" "config" "config.yml", domain2Text) ] ) (#result [domain1, domain2]) From 4df9ea811cc0375c8fd12c3748d0cd6776bafef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Dimja=C5=A1evi=C4=87?= Date: Tue, 29 Oct 2019 09:41:51 +0100 Subject: [PATCH 09/11] Issue #26: fixes hlint warnings --- test/Fencer/Rules/Test.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Fencer/Rules/Test.hs b/test/Fencer/Rules/Test.hs index 003e33d..b108978 100644 --- a/test/Fencer/Rules/Test.hs +++ b/test/Fencer/Rules/Test.hs @@ -85,7 +85,7 @@ test_rulesLoadRulesDotDirectory = -- RUNTIME_IGNOREDOTFILES=true. test_rulesLoadRulesRUNTIME_IGNOREDOTFILEStrue :: TestTree test_rulesLoadRulesRUNTIME_IGNOREDOTFILEStrue = - testCase "Rules are not loaded from a dot-file" $ do + testCase "Rules are not loaded from a dot-file" $ expectLoadRules (#ignoreDotFiles True) (#files @@ -98,7 +98,7 @@ test_rulesLoadRulesRUNTIME_IGNOREDOTFILEStrue = -- RUNTIME_IGNOREDOTFILES=false. test_rulesLoadRulesRUNTIME_IGNOREDOTFILESfalse :: TestTree test_rulesLoadRulesRUNTIME_IGNOREDOTFILESfalse = - testCase "Rules are not loaded from a dot-file" $ do + testCase "Rules are not loaded from a dot-file" $ expectLoadRules (#ignoreDotFiles False) (#files From c518b0be535fed12c59627a217a7f398d4c37fe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Dimja=C5=A1evi=C4=87?= Date: Tue, 29 Oct 2019 11:19:05 +0100 Subject: [PATCH 10/11] Issue #26: fix how dot-directories are handled wrt RUNTIME_IGNOREDOTFILES --- lib/Fencer/Main.hs | 3 ++- lib/Fencer/Rules.hs | 14 ++++++++++---- test/Fencer/Rules/Test.hs | 15 ++++++++------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/Fencer/Main.hs b/lib/Fencer/Main.hs index f163aac..b0a0f21 100644 --- a/lib/Fencer/Main.hs +++ b/lib/Fencer/Main.hs @@ -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: " ++ diff --git a/lib/Fencer/Rules.hs b/lib/Fencer/Rules.hs index cf7a1b7..0a81ef9 100644 --- a/lib/Fencer/Rules.hs +++ b/lib/Fencer/Rules.hs @@ -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 @@ -26,14 +26,17 @@ 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 @@ -41,7 +44,10 @@ loadRulesFromDirectory 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 diff --git a/test/Fencer/Rules/Test.hs b/test/Fencer/Rules/Test.hs index d99e305..5074f31 100644 --- a/test/Fencer/Rules/Test.hs +++ b/test/Fencer/Rules/Test.hs @@ -48,7 +48,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) @@ -66,7 +67,7 @@ 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 = @@ -74,10 +75,10 @@ test_rulesLoadRulesDotDirectory = expectLoadRules (#ignoreDotFiles True) (#files - [ (".domain1/config1.yml", domain1Text) - , (".domain2/config2.yaml", domain2Text) ] + [ (".domain1" "config1.yml", domain1Text) + , ("domain2" "config2.yaml", domain2Text) ] ) - (#result [domain1, domain2]) + (#result [domain2]) -- | Test that 'loadRulesFromDirectory' loads rules from all files, not just -- YAML files. @@ -103,8 +104,8 @@ test_rulesLoadRulesRecursively = expectLoadRules (#ignoreDotFiles True) (#files - [ ("domain1/config.yml", domain1Text) - , ("domain2/config/config.yml", domain2Text) ] + [ ("domain1" "config.yml", domain1Text) + , ("domain2" "config" "config.yml", domain2Text) ] ) (#result [domain1, domain2]) From 192ec247cb14afdba0a5edfb895dd5c134fe9946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Dimja=C5=A1evi=C4=87?= Date: Wed, 30 Oct 2019 14:02:44 +0100 Subject: [PATCH 11/11] [skip ci] Issue #26: fix the README on ignoring dot-directories --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 35abf21..b38c542 100644 --- a/README.md +++ b/README.md @@ -135,9 +135,9 @@ Fencer-specific environment variables are: subdirectory. The default value is `/srv/runtime_data/current`. - `RUNTIME_SUBDIRECTORY` - The directory with Fencer settings. - `RUNTIME_IGNOREDOTFILES` - A flag indicating whether to ignore files - with names starting with a dot (hidden files on Linux-based systems - and macOS). It can be `True` or `False`. The default value is - `False`. Directories with names starting in a dot are not ignored. + and directories with names starting with a dot (hidden files on + Linux-based systems and macOS). It can be `True` or `False`. The + default value is `False`. - `GRPC_PORT` - The port to run the gRPC server on. Default is 8081. ## Developing