packages feed

envy 0.2.0.0 → 0.3.0.0

raw patch · 3 files changed

+42/−37 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ System.Envy: setEnvironment' :: ToEnv a => a -> IO (Either String ())
- System.Envy: toEnv :: ToEnv a => EnvList a
+ System.Envy: toEnv :: ToEnv a => a -> EnvList a

Files

envy.cabal view
@@ -1,9 +1,9 @@ name:                envy-version:             0.2.0.0+version:             0.3.0.0 synopsis:            An environmentally friendly way to deal with environment variables license:             BSD3 license-file:        LICENSE-author:              David Johnson+author:              David Johnson, Tim Adams maintainer:          djohnson.m@gmail.com copyright:           David Johnson (c) 2015 category:            System
src/System/Envy.hs view
@@ -2,7 +2,6 @@ {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE FlexibleInstances          #-} {-# LANGUAGE OverloadedStrings          #-}-{-# LANGUAGE RecordWildCards            #-} {-# LANGUAGE RankNTypes                 #-} ------------------------------------------------------------------------------ -- |@@ -24,6 +23,7 @@        , decode        , showEnv        , setEnvironment+       , setEnvironment'        , unsetEnvironment        , makeEnv         , env@@ -34,19 +34,14 @@ ------------------------------------------------------------------------------ import           Control.Applicative import           Control.Monad.Except-import           Control.Monad.Identity import           Control.Exception import           Data.Maybe import           Data.Time-import           Data.Monoid-import           Control.Monad import           Data.Typeable-import           Data.String import           System.Environment import           Text.Read (readMaybe) import qualified Data.Text as T import qualified Data.Text.Lazy as TL-import qualified Data.Map as M import           Data.Text (Text) import           Data.Word import           Data.Int@@ -137,7 +132,7 @@ ------------------------------------------------------------------------------ -- | ToEnv Typeclass class Show a => ToEnv a where-  toEnv :: EnvList a+  toEnv :: a -> EnvList a  ------------------------------------------------------------------------------ -- | EnvList type w/ phanton@@ -247,16 +242,21 @@ -- | Set environment via a ToEnv constrained type setEnvironment :: EnvList a -> IO (Either String ()) setEnvironment (EnvList xs) = do-  result <- try $ mapM_ (uncurry setEnv) (map getEnvVar xs)+  result <- try $ mapM_ (uncurry setEnv . getEnvVar) xs   return $ case result of    Left (ex :: IOException) -> Left (show ex)    Right () -> Right ()  ------------------------------------------------------------------------------+-- | Set environment directly using a value of class ToEnv+setEnvironment' :: ToEnv a => a -> IO (Either String ())+setEnvironment' = setEnvironment . toEnv++------------------------------------------------------------------------------ -- | Unset Environment from a ToEnv constrained type unsetEnvironment :: EnvList a -> IO (Either String ()) unsetEnvironment (EnvList xs) = do-  result <- try $ mapM_ unsetEnv $ map fst (map getEnvVar xs)+  result <- try $ mapM_ (unsetEnv . fst . getEnvVar) xs   return $ case result of    Left (ex :: IOException) -> Left (show ex)    Right () -> Right ()
tests/Main.hs view
@@ -1,32 +1,22 @@ {-# LANGUAGE ScopedTypeVariables        #-} {-# LANGUAGE RecordWildCards            #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE OverloadedStrings          #-}-{-# LANGUAGE DeriveDataTypeable         #-} ------------------------------------------------------------------------------ module Main ( main ) where ------------------------------------------------------------------------------ import           Control.Applicative-import           Control.Exception-import           Control.Monad-import           Control.Monad.IO.Class (liftIO) import qualified Data.ByteString.Char8 as B8 import qualified Data.ByteString.Lazy.Char8 as BL8-import           Data.Either import           Data.Int-import           Data.String-import           Data.Text (Text)-import           Data.Text (Text) import qualified Data.Text as T import qualified Data.Text.Lazy as LT import           Data.Time-import           Data.Typeable import           Data.Word-import           System.Environment import           System.Envy import           Test.Hspec import           Test.QuickCheck-import           Test.QuickCheck.Instances+import           Test.QuickCheck.Instances ()+import           Test.QuickCheck.Monadic ------------------------------------------------------------------------------ data ConnectInfo = ConnectInfo {       pgHost :: String@@ -34,14 +24,26 @@     , pgUser :: String     , pgPass :: String     , pgDB   :: String-  } deriving (Show)+  } deriving (Show, Eq) +instance Arbitrary ConnectInfo where+    arbitrary = ConnectInfo <$> nonulls+                            <*> arbitrary+                            <*> nonulls+                            <*> nonulls+                            <*> nonulls+      where nonempty = getNonEmpty <$> arbitrary+            nonulls = nonempty `suchThat` (not . ('\NUL' `elem`))+ ------------------------------------------------------------------------------ -- | Posgtres config data PGConfig = PGConfig {     pgConnectInfo :: ConnectInfo -- ^ Connnection Info-  } +  } deriving (Eq) +instance Arbitrary PGConfig where+    arbitrary = PGConfig <$> arbitrary+ ------------------------------------------------------------------------------ -- | Custom show instance instance Show PGConfig where@@ -60,13 +62,14 @@ ------------------------------------------------------------------------------ -- | To Environment Instances instance ToEnv PGConfig where-  toEnv = makeEnv -       [ "PG_HOST" .= ("localhost" :: String)-       , "PG_PORT" .= (5432        :: Word16)-       , "PG_USER" .= ("user"      :: String)-       , "PG_PASS" .= ("pass"      :: String)-       , "PG_DB"   .= ("db"        :: String)-       ]+  toEnv PGConfig{..} = let ConnectInfo{..} = pgConnectInfo+                       in+                       makeEnv [ "PG_HOST" .= pgHost+                               , "PG_PORT" .= pgPort+                               , "PG_USER" .= pgUser+                               , "PG_PASS" .= pgPass+                               , "PG_DB"   .= pgDB+                               ]  ------------------------------------------------------------------------------ -- | Start tests@@ -103,8 +106,10 @@      \(x :: LT.Text) -> Just x == fromVar (toVar x)     it "Text Var isomorphism" $ property $       \(x :: T.Text) -> Just x == fromVar (toVar x)-  describe "Can set to and from environment" $ do-    it "Should set environment" $ do-      setEnvironment (toEnv :: EnvList PGConfig)-      result <- decodeEnv :: IO (Either String PGConfig)-      result `shouldSatisfy` isRight+  describe "Can set to and from environment" $+    it "Isomorphism through setEnvironment['] and decodeEnv" $ property $+      \(pgConf::PGConfig) -> monadicIO $ do+        res <- run $ do+                 _ <- setEnvironment' pgConf+                 decodeEnv+        assert $ res == Right pgConf