File: ParseSpec.hs

package info (click to toggle)
haskell-load-env 0.2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 92 kB
  • sloc: haskell: 249; makefile: 5
file content (115 lines) | stat: -rw-r--r-- 4,449 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
module LoadEnv.ParseSpec
    ( spec
    ) where

import LoadEnv.Parse
import Test.Hspec
import Text.Parsec (parse)

spec :: Spec
spec = do
    describe "parseEnvironment" $ do
        it "parses variable declarations among comments and blank lines" $ do
            let env = unlines
                    [ "# An environment file"
                    , "FOO=bar"
                    , "BAZ=\"bat\""
                    , "BAT=\"multi-"
                    , "pass"
                    , "\""
                    , ""
                    , "# vim ft:sh:"
                    ]

            parse parseEnvironment "" env `shouldBe` Right
                [ ("FOO", "bar")
                , ("BAZ", "bat")
                , ("BAT", "multi-\npass\n")
                ]

        it "parses an empty file into an empty list of variables" $ do
            parse parseEnvironment "" "" `shouldBe` Right []


    describe "parseVariable" $ do
        it "reads unquoted variables" $
            parse parseVariable "" "FOO=bar\n" `shouldBe` Right ("FOO", "bar")

        it "reads quoted variables" $ do
            parse parseVariable "" "FOO=\"bar\"\n"
                `shouldBe` Right ("FOO", "bar")
            parse parseVariable "" "FOO='bar'\n"
                `shouldBe` Right ("FOO", "bar")

        it "allows newlines in quoted variables" $ do
            parse parseVariable "" "FOO=\"foo\nbar\"\n"
                `shouldBe` Right ("FOO", "foo\nbar")

        it "handles empty values" $
            parse parseVariable "" "FOO=\n" `shouldBe` Right ("FOO", "")

        it "handles empty quoted values" $ do
            parse parseVariable "" "FOO=\"\"\n" `shouldBe` Right ("FOO", "")
            parse parseVariable "" "FOO=''\n" `shouldBe` Right ("FOO", "")

        it "handles underscored variables" $
            parse parseVariable "" "FOO_BAR=baz\n"
                `shouldBe` Right ("FOO_BAR", "baz")

        it "treats leading spaces as invalid" $
            parse parseVariable "" "  FOO=bar\n"
                `shouldContainError` "unexpected \"F\""

        it "treats spaces around equals as invalid" $
            parse parseVariable "" "FOO = bar\n"
                `shouldContainError` "unexpected \" \""

        it "treats unquoted spaces as invalid" $
            parse parseVariable "" "FOO=bar baz\n"
                `shouldContainError` "unexpected \"b\""

        it "treats unbalanced quotes as invalid" $ do
            parse parseVariable "" "FOO=\"bar\n"
                `shouldContainError` "unexpected end of input"
            parse parseVariable "" "FOO='bar\n"
                `shouldContainError` "unexpected end of input"
            parse parseVariable "" "FOO=bar\"\n"
                `shouldContainError` "unexpected \"\\\"\""
            parse parseVariable "" "FOO=bar'\n"
                `shouldContainError` "unexpected \"\'\""

        it "handles escaped quotes" $ do
            parse parseVariable "" "FOO=\"bar\\\"baz\"\n"
                `shouldBe` Right ("FOO", "bar\"baz")
            parse parseVariable "" "FOO='bar\\'baz'\n"
                `shouldBe` Right ("FOO", "bar'baz")

        it "handles escaped spaces" $
            parse parseVariable "" "FOO=bar\\ baz\n"
                `shouldBe` Right ("FOO", "bar baz")

        it "discards any lines using `export'" $
            parse parseVariable "" "export FOO=bar\n"
                `shouldBe` Right ("FOO", "bar")

        it "requires valid environment variable identifies" $ do
            parse parseVariable "" "S3_KEY=abc123\n"
                `shouldBe` Right ("S3_KEY", "abc123")
            parse parseVariable "" "_S3_KEY=abc123\n"
                `shouldBe` Right ("_S3_KEY", "abc123")
            parse parseVariable "" "S3_key=abc123\n"
                `shouldBe` Right ("S3_key", "abc123")
            parse parseVariable "" "s3_key=abc123\n"
                `shouldBe` Right ("s3_key", "abc123")

            parse parseVariable "" "S3~KEY=abc123\n"
                `shouldContainError` "unexpected \"~\""
            parse parseVariable "" "S3-KEY=abc123\n"
                `shouldContainError` "unexpected \"-\""
            parse parseVariable "" "3_KEY=abc123\n"
                `shouldContainError` "unexpected \"3\""

shouldContainError :: Show a => Either a b -> String -> Expectation
v `shouldContainError` msg = either
    (\e -> show e `shouldContain` msg)
    (\_ -> expectationFailure "Expected no parse") v