Skip to content

Commit

Permalink
Add basic ConfigSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrisbin committed Oct 18, 2024
1 parent 9e15db2 commit bd87b90
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 15 deletions.
2 changes: 2 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,7 @@ tests:
- hspec-expectations-lifted
- lens-aeson
- load-env
- opt-env-conf
- restyler
- text
- unliftio
3 changes: 3 additions & 0 deletions restyler.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ test-suite test
Restyler.Config.CommitTemplateSpec
Restyler.Config.IncludeSpec
Restyler.Config.InterpreterSpec
Restyler.ConfigSpec
Restyler.DelimitedSpec
Restyler.IgnoreSpec
Restyler.RestrictionsSpec
Expand Down Expand Up @@ -187,6 +188,8 @@ test-suite test
, hspec-expectations-lifted
, lens-aeson
, load-env
, opt-env-conf
, restyler
, text
, unliftio
default-language: GHC2021
28 changes: 13 additions & 15 deletions src/Restyler/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
-- Portability : POSIX
module Restyler.Config
( Config (..)
, configParser
, parseConfig

-- * Individual configuration points
Expand Down Expand Up @@ -74,16 +75,23 @@ instance HasRestylerOverrides Config where

parseConfig :: IO Config
parseConfig = do
withSystemTempFile "restyler-default-config.yaml" $ \tmp h -> do
withSystemTempFile "restyler-default-config.yaml" $ \defaults h -> do
BS.hPutStr h defaultConfigContent >> hClose h
runParser Pkg.version "Restyle local file" $ configParser tmp
runParser Pkg.version "Restyle local file"
$ configParser
[ ".github/restyled.yml"
, ".github/restyled.yaml"
, ".restyled.yml"
, ".restyled.yaml"
, defaults
]

defaultConfigContent :: ByteString
defaultConfigContent = $(embedFile "config/default.yaml")

configParser :: FilePath -> Parser Config
configParser defaults =
withCombinedYamlConfigs (configSources defaults)
configParser :: [FilePath] -> Parser Config
configParser paths =
withCombinedYamlConfigs (traverse hiddenPath paths)
$ Config
<$> setting
[ help "Do anything at all"
Expand All @@ -105,16 +113,6 @@ configParser defaults =
<*> restylerOverridesParser
<*> subConfig_ "cli" optionsParser

configSources :: FilePath -> Parser [Path Abs File]
configSources defaults =
sequenceA
[ hiddenPath ".github/restyled.yml"
, hiddenPath ".github/restyled.yaml"
, hiddenPath ".restyled.yml"
, hiddenPath ".restyled.yaml"
, hiddenPath defaults
]

hiddenPath :: FilePath -> Parser (Path Abs File)
hiddenPath x = filePathSetting [value x, hidden]

Expand Down
52 changes: 52 additions & 0 deletions test/Restyler/ConfigSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Restyler.ConfigSpec
( spec
) where

import Restyler.Prelude

import Data.Text.IO (hPutStr)
import OptEnvConf.Args (parseArgs)
import OptEnvConf.EnvMap qualified as EnvMap
import OptEnvConf.Run (runParserOn)
import Restyler.Config
import System.IO (hClose)
import Test.Hspec
import UnliftIO.Temporary (withSystemTempFile)

spec :: Spec
spec = do
it "uses defined defaults" $ do
config <- loadTestConfig [] [] ["Foo.hs"]
config.enabled `shouldBe` True

loadTestConfig
:: HasCallStack
=> [Text]
-- ^ Lines of Yaml
--
-- Empty means to behave as if no file at all.
-> [(String, String)]
-- ^ ENV
-> [String]
-- ^ Args
-> IO Config
loadTestConfig yaml env args = do
result <- case yaml of
[] -> runParser ["config/default.yaml"]
ls -> withSystemTempFile "restyler-test-config.yaml" $ \path h -> do
hPutStr h (unlines ls) >> hClose h
runParser [path, "config/default.yaml"]

case result of
Left errs -> do
expectationFailure $ show errs
error "unreachable"
Right config -> pure config
where
runParser paths =
runParserOn
Nothing
(configParser paths)
(parseArgs args)
(EnvMap.parse env)
Nothing

0 comments on commit bd87b90

Please sign in to comment.