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 1/2] 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 2/2] 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) ]