packages feed

dotenv 0.1.0.8 → 0.1.0.9

raw patch · 3 files changed

+149/−2 lines, 3 filesdep ~optparse-applicative

Dependency ranges changed: optparse-applicative

Files

dotenv.cabal view
@@ -1,5 +1,5 @@ name:                dotenv-version:             0.1.0.8+version:             0.1.0.9 synopsis:            Loads environment variables from dotenv files homepage:            https://github.com/stackbuilders/dotenv-hs description:@@ -48,7 +48,7 @@   -- other-extensions:   build-depends:         base >=4.5 && <4.9                        , base-compat >= 0.4-                       , optparse-applicative >=0.11 && <0.12+                       , optparse-applicative >=0.12 && < 0.13                        , parsec >= 3.1.0 && <= 3.2                        , process @@ -72,6 +72,9 @@   type: exitcode-stdio-1.0   hs-source-dirs: spec, src   main-is: Spec.hs+  other-modules:         Configuration.DotenvSpec+                       , Configuration.Dotenv.ParseSpec+   build-depends:       base >=4.5 && <4.9                        , base-compat >= 0.4                        , parsec >= 3.1.0 && <= 3.2
+ spec/Configuration/Dotenv/ParseSpec.hs view
@@ -0,0 +1,89 @@+{-# OPTIONS_GHC -fno-warn-orphans #-}++module Configuration.Dotenv.ParseSpec (spec) where++import Configuration.Dotenv.Parse (configParser)++import Test.Hspec (it, describe, shouldBe, Spec)++import Text.Parsec (ParseError, parse)++import Text.ParseErrorEq ()++spec :: Spec+spec = describe "parse" $ do+  it "parses unquoted values" $+    parseConfig "FOO=bar" `shouldBe` Right [("FOO", "bar")]++  it "parses values with spaces around equal signs" $ do+    parseConfig "FOO =bar" `shouldBe` Right [("FOO", "bar")]+    parseConfig "FOO= bar" `shouldBe` Right [("FOO", "bar")]+    parseConfig "FOO =\t bar" `shouldBe` Right [("FOO", "bar")]++  it "parses double-quoted values" $+    parseConfig "FOO=\"bar\"" `shouldBe` Right [("FOO", "bar")]++  it "parses single-quoted values" $+    parseConfig "FOO='bar'" `shouldBe` Right [("FOO", "bar")]++  it "parses escaped double quotes" $+    parseConfig "FOO=\"escaped\\\"bar\"" `shouldBe`+    Right [("FOO", "escaped\"bar")]++  it "parses empty values" $+    parseConfig "FOO=" `shouldBe` Right [("FOO", "")]++  it "does not parse if line format is incorrect" $ do+    isLeft (parseConfig "lol$wut") `shouldBe` True+    isLeft (parseConfig "KEY=\nVALUE") `shouldBe` True+    isLeft (parseConfig "KEY\n=VALUE") `shouldBe` True++  it "expands newlines in quoted strings" $+    parseConfig "FOO=\"bar\nbaz\"" `shouldBe` Right [("FOO", "bar\nbaz")]++  it "does not parse variables with hyphens in the name" $+    isLeft (parseConfig "FOO-BAR=foobar") `shouldBe` True++  it "parses variables with '_' in the name" $+    parseConfig "FOO_BAR=foobar" `shouldBe` Right [("FOO_BAR", "foobar")]++  it "parses variables with digits after the first character" $+    parseConfig "FOO_BAR_12=foobar" `shouldBe` Right [("FOO_BAR_12", "foobar")]++  it "allows vertical spaces after a quoted variable" $+    parseConfig "foo='bar' " `shouldBe` Right [("foo", "bar")]++  it "does not parse variable names beginning with a digit" $+    isLeft (parse configParser "null" "45FOO_BAR=foobar") `shouldBe` True++  it "strips unquoted values" $+    parseConfig "foo=bar " `shouldBe` Right [("foo", "bar")]++  it "ignores empty lines" $+    parseConfig "\n \t  \nfoo=bar\n \nfizz=buzz" `shouldBe`+    Right [("foo", "bar"), ("fizz", "buzz")]++  it "ignores inline comments after unquoted arguments" $+    parseConfig "FOO=bar # this is foo" `shouldBe` Right [("FOO", "bar")]++  it "ignores inline comments after quoted arguments" $+    parseConfig "FOO=\"bar\" # this is foo" `shouldBe` Right [("FOO", "bar")]++  it "allows # in quoted values" $+    parseConfig "foo=\"bar#baz\" # comment" `shouldBe`+    Right [("foo", "bar#baz")]++  it "ignores comment lines" $+    parseConfig "\n\t \n\n # HERE GOES FOO \nfoo=bar" `shouldBe`+    Right [("foo", "bar")]++  it "doesn't allow more configuration options after a quoted value" $+    isLeft (parse configParser "null" "foo='bar'baz='buz'") `shouldBe` True+++isLeft :: Either a b -> Bool+isLeft ( Left _ ) = True+isLeft _          = False++parseConfig :: String -> Either ParseError [(String, String)]+parseConfig = parse configParser "(unknown)"
+ spec/Configuration/DotenvSpec.hs view
@@ -0,0 +1,55 @@+module Configuration.DotenvSpec (spec) where++import Configuration.Dotenv (load, loadFile)++import Test.Hspec++import System.Environment.Compat (lookupEnv, setEnv, unsetEnv)++{-# ANN module "HLint: ignore Reduce duplication" #-}++spec :: Spec+spec = do+  describe "load" $ after_ (unsetEnv "foo") $ do+    it "loads the given list of configuration options to the environment" $ do+      lookupEnv "foo" `shouldReturn` Nothing++      load False [("foo", "bar")]++      lookupEnv "foo" `shouldReturn` Just "bar"++    it "preserves existing settings when overload is false" $ do+      setEnv "foo" "preset"++      load False [("foo", "new setting")]++      lookupEnv "foo" `shouldReturn` Just "preset"++    it "overrides existing settings when overload is true" $ do+      setEnv "foo" "preset"++      load True [("foo", "new setting")]++      lookupEnv "foo" `shouldReturn` Just "new setting"++  describe "loadFile" $ after_ (unsetEnv "DOTENV") $ do+    it "loads the configuration options to the environment from a file" $ do+      lookupEnv "DOTENV" `shouldReturn` Nothing++      loadFile False "spec/fixtures/.dotenv"++      lookupEnv "DOTENV" `shouldReturn` Just "true"++    it "respects predefined settings when overload is false" $ do+      setEnv "DOTENV" "preset"++      loadFile False "spec/fixtures/.dotenv"++      lookupEnv "DOTENV" `shouldReturn` Just "preset"++    it "overrides predefined settings when overload is true" $ do+      setEnv "DOTENV" "preset"++      loadFile True "spec/fixtures/.dotenv"++      lookupEnv "DOTENV" `shouldReturn` Just "true"