|
| 1 | +{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 2 | + |
| 3 | +module Configuration.Dotenv.ParseSpec (spec) where |
| 4 | + |
| 5 | +import Configuration.Dotenv.Parse (configParser) |
| 6 | + |
| 7 | +import Test.Hspec (it, describe, shouldBe, Spec) |
| 8 | + |
| 9 | +import Text.Parsec (ParseError, parse) |
| 10 | +import Text.ParserCombinators.Parsec.Error(errorMessages) |
| 11 | + |
| 12 | + |
| 13 | +spec :: Spec |
| 14 | +spec = describe "parse" $ do |
| 15 | + it "parses unquoted values" $ |
| 16 | + parseConfig "FOO=bar" `shouldBe` Right [("FOO", "bar")] |
| 17 | + |
| 18 | + it "parses values with spaces around equal signs" $ do |
| 19 | + parseConfig "FOO =bar" `shouldBe` Right [("FOO", "bar")] |
| 20 | + parseConfig "FOO= bar" `shouldBe` Right [("FOO", "bar")] |
| 21 | + parseConfig "FOO = bar" `shouldBe` Right [("FOO", "bar")] |
| 22 | + |
| 23 | + it "parses double-quoted values" $ |
| 24 | + parseConfig "FOO=\"bar\"" `shouldBe` Right [("FOO", "bar")] |
| 25 | + |
| 26 | + it "parses single-quoted values" $ |
| 27 | + parseConfig "FOO='bar'" `shouldBe` Right [("FOO", "bar")] |
| 28 | + |
| 29 | + it "parses escaped double quotes" $ |
| 30 | + parseConfig "FOO=\"escaped\\\"bar\"" `shouldBe` Right [("FOO", "escaped\"bar")] |
| 31 | + |
| 32 | + it "parses empty values" $ |
| 33 | + parseConfig "FOO=" `shouldBe` Right [("FOO", "")] |
| 34 | + |
| 35 | + it "does not parse if line format is incorrect" $ |
| 36 | + isLeft (parseConfig "lol$wut") `shouldBe` True |
| 37 | + |
| 38 | + it "expands newlines in quoted strings" $ |
| 39 | + parseConfig "FOO=\"bar\nbaz\"" `shouldBe` Right [("FOO", "bar\nbaz")] |
| 40 | + |
| 41 | + it "parses variables with '.' in the name" $ |
| 42 | + parseConfig "FOO.BAR=foobar" `shouldBe` Right [("FOO.BAR", "foobar")] |
| 43 | + |
| 44 | + it "strips unquoted values" $ |
| 45 | + parseConfig "foo=bar " `shouldBe` Right [("foo", "bar")] |
| 46 | + |
| 47 | + it "ignores empty lines" $ |
| 48 | + parseConfig "\n \t \nfoo=bar\n \nfizz=buzz" `shouldBe` |
| 49 | + Right [("foo", "bar"), ("fizz", "buzz")] |
| 50 | + |
| 51 | + it "ignores inline comments" $ |
| 52 | + parseConfig "FOO=bar # this is foo" `shouldBe` Right [("FOO", "bar")] |
| 53 | + |
| 54 | + it "allows # in quoted values" $ |
| 55 | + parseConfig "foo=\"bar#baz\" # comment" `shouldBe` |
| 56 | + Right [("foo", "bar#baz")] |
| 57 | + |
| 58 | + it "ignores comment lines" $ |
| 59 | + parseConfig "\n\n\n # HERE GOES FOO \nfoo=bar" `shouldBe` |
| 60 | + Right [("foo", "bar")] |
| 61 | + |
| 62 | + |
| 63 | +isLeft :: Either a b -> Bool |
| 64 | +isLeft ( Left _ ) = True |
| 65 | +isLeft _ = False |
| 66 | + |
| 67 | + |
| 68 | +instance Eq ParseError where |
| 69 | + a == b = errorMessages a == errorMessages b |
| 70 | + |
| 71 | +parseConfig :: String -> Either ParseError [(String, String)] |
| 72 | +parseConfig = parse configParser "(unknown)" |
0 commit comments