3
3
module Configuration.Dotenv.ParseSpec (main , spec ) where
4
4
5
5
import Configuration.Dotenv.Parse (configParser )
6
+ import Configuration.Dotenv.ParsedVariable (ParsedVariable (.. ),
7
+ VarValue (.. ),
8
+ VarFragment (.. ))
6
9
import Test.Hspec (it , describe , Spec , hspec )
7
10
import Test.Hspec.Megaparsec (shouldParse , shouldFailOn )
8
11
import Text.Megaparsec (ParseError , Dec , parse )
@@ -13,76 +16,130 @@ main = hspec spec
13
16
spec :: Spec
14
17
spec = describe " parse" $ do
15
18
it " parses unquoted values" $
16
- parseConfig " FOO=bar" `shouldParse` [(" FOO" , " bar" )]
19
+ parseConfig " FOO=bar"
20
+ `shouldParse` [ParsedVariable " FOO" (Unquoted [VarLiteral " bar" ])]
17
21
18
22
it " parses values with spaces around equal signs" $ do
19
- parseConfig " FOO =bar" `shouldParse` [(" FOO" , " bar" )]
20
- parseConfig " FOO= bar" `shouldParse` [(" FOO" , " bar" )]
21
- parseConfig " FOO =\t bar" `shouldParse` [(" FOO" , " bar" )]
23
+ parseConfig " FOO =bar"
24
+ `shouldParse` [ParsedVariable " FOO" (Unquoted [VarLiteral " bar" ])]
25
+ parseConfig " FOO= bar"
26
+ `shouldParse` [ParsedVariable " FOO" (Unquoted [VarLiteral " bar" ])]
27
+ parseConfig " FOO =\t bar"
28
+ `shouldParse` [ParsedVariable " FOO" (Unquoted [VarLiteral " bar" ])]
22
29
23
30
it " parses double-quoted values" $
24
- parseConfig " FOO=\" bar\" " `shouldParse` [(" FOO" , " bar" )]
31
+ parseConfig " FOO=\" bar\" "
32
+ `shouldParse` [ParsedVariable " FOO" (DoubleQuoted [VarLiteral " bar" ])]
25
33
26
34
it " parses single-quoted values" $
27
- parseConfig " FOO='bar'" `shouldParse` [(" FOO" , " bar" )]
35
+ parseConfig " FOO='bar'"
36
+ `shouldParse` [ParsedVariable " FOO" (SingleQuoted [VarLiteral " bar" ])]
28
37
29
38
it " parses escaped double quotes" $
30
39
parseConfig " FOO=\" escaped\\\" bar\" "
31
- `shouldParse` [( " FOO" , " escaped\" bar" )]
40
+ `shouldParse` [ParsedVariable " FOO" ( DoubleQuoted [ VarLiteral " escaped\" bar" ] )]
32
41
33
42
it " supports CRLF line breaks" $
34
43
parseConfig " FOO=bar\r\n baz=fbb"
35
- `shouldParse` [(" FOO" , " bar" ), (" baz" , " fbb" )]
44
+ `shouldParse` [ParsedVariable " FOO" (Unquoted [VarLiteral " bar" ]),
45
+ ParsedVariable " baz" (Unquoted [VarLiteral " fbb" ])]
36
46
37
47
it " parses empty values" $
38
- parseConfig " FOO=" `shouldParse` [(" FOO" , " " )]
48
+ parseConfig " FOO="
49
+ `shouldParse` [ParsedVariable " FOO" (Unquoted [] )]
50
+
51
+ it " parses unquoted interpolated values" $ do
52
+ parseConfig " FOO=$HOME"
53
+ `shouldParse` [ParsedVariable " FOO" (Unquoted [VarInterpolation " HOME" ])]
54
+ parseConfig " FOO=abc_$HOME"
55
+ `shouldParse` [ParsedVariable " FOO" (Unquoted [VarLiteral " abc_" ,
56
+ VarInterpolation " HOME" ])
57
+ ]
58
+ parseConfig " FOO=${HOME}"
59
+ `shouldParse` [ParsedVariable " FOO" (Unquoted [VarInterpolation " HOME" ])]
60
+ parseConfig " FOO=abc_${HOME}"
61
+ `shouldParse` [ParsedVariable " FOO" (Unquoted [VarLiteral " abc_" ,
62
+ VarInterpolation " HOME" ])
63
+ ]
64
+
65
+ it " parses double-quoted interpolated values" $ do
66
+ parseConfig " FOO=\" $HOME\" "
67
+ `shouldParse` [ParsedVariable " FOO" (DoubleQuoted [VarInterpolation " HOME" ])]
68
+ parseConfig " FOO=\" abc_$HOME\" "
69
+ `shouldParse` [ParsedVariable " FOO" (DoubleQuoted [VarLiteral " abc_" ,
70
+ VarInterpolation " HOME" ])
71
+ ]
72
+ parseConfig " FOO=\" ${HOME}\" "
73
+ `shouldParse` [ParsedVariable " FOO" (DoubleQuoted [VarInterpolation " HOME" ])]
74
+ parseConfig " FOO=\" abc_${HOME}\" "
75
+ `shouldParse` [ParsedVariable " FOO" (DoubleQuoted [VarLiteral " abc_" ,
76
+ VarInterpolation " HOME" ])
77
+ ]
78
+
79
+ it " parses single-quoted interpolated values as literals" $ do
80
+ parseConfig " FOO='$HOME'"
81
+ `shouldParse` [ParsedVariable " FOO" (SingleQuoted [VarLiteral " $HOME" ])]
82
+ parseConfig " FOO='abc_$HOME'"
83
+ `shouldParse` [ParsedVariable " FOO" (SingleQuoted [VarLiteral " abc_$HOME" ])]
84
+ parseConfig " FOO='${HOME}'"
85
+ `shouldParse` [ParsedVariable " FOO" (SingleQuoted [VarLiteral " ${HOME}" ])]
86
+ parseConfig " FOO='abc_${HOME}'"
87
+ `shouldParse` [ParsedVariable " FOO" (SingleQuoted [VarLiteral " abc_${HOME}" ])]
39
88
40
89
it " does not parse if line format is incorrect" $ do
41
90
parseConfig `shouldFailOn` " lol$wut"
42
91
parseConfig `shouldFailOn` " KEY=\n VALUE"
43
92
parseConfig `shouldFailOn` " KEY\n =VALUE"
44
93
45
94
it " expands newlines in quoted strings" $
46
- parseConfig " FOO=\" bar\n baz\" " `shouldParse` [(" FOO" , " bar\n baz" )]
95
+ parseConfig " FOO=\" bar\n baz\" "
96
+ `shouldParse` [ParsedVariable " FOO" (DoubleQuoted [VarLiteral " bar\n baz" ])]
47
97
48
98
it " does not parse variables with hyphens in the name" $
49
99
parseConfig `shouldFailOn` " FOO-BAR=foobar"
50
100
51
101
it " parses variables with \" _\" in the name" $
52
- parseConfig " FOO_BAR=foobar" `shouldParse` [(" FOO_BAR" , " foobar" )]
102
+ parseConfig " FOO_BAR=foobar"
103
+ `shouldParse` [ParsedVariable " FOO_BAR" (Unquoted [VarLiteral " foobar" ])]
53
104
54
105
it " parses variables with digits after the first character" $
55
- parseConfig " FOO_BAR_12=foobar" `shouldParse` [(" FOO_BAR_12" , " foobar" )]
106
+ parseConfig " FOO_BAR_12=foobar"
107
+ `shouldParse` [ParsedVariable " FOO_BAR_12" (Unquoted [VarLiteral " foobar" ])]
56
108
57
109
it " allows vertical spaces after a quoted variable" $
58
- parseConfig " foo='bar' " `shouldParse` [(" foo" , " bar" )]
110
+ parseConfig " foo='bar' "
111
+ `shouldParse` [ParsedVariable " foo" (SingleQuoted [VarLiteral " bar" ])]
59
112
60
113
it " does not parse variable names beginning with a digit" $
61
114
parseConfig `shouldFailOn` " 45FOO_BAR=foobar"
62
115
63
116
it " strips unquoted values" $
64
- parseConfig " foo=bar " `shouldParse` [(" foo" , " bar" )]
117
+ parseConfig " foo=bar "
118
+ `shouldParse` [ParsedVariable " foo" (Unquoted [VarLiteral " bar" ])]
65
119
66
120
it " ignores empty lines" $
67
121
parseConfig " \n \t \n foo=bar\n \n fizz=buzz"
68
- `shouldParse` [(" foo" , " bar" ), (" fizz" , " buzz" )]
122
+ `shouldParse` [ParsedVariable " foo" (Unquoted [VarLiteral " bar" ]),
123
+ ParsedVariable " fizz" (Unquoted [VarLiteral " buzz" ])]
69
124
70
125
it " ignores inline comments after unquoted arguments" $
71
- parseConfig " FOO=bar # this is foo" `shouldParse` [(" FOO" , " bar" )]
126
+ parseConfig " FOO=bar # this is foo"
127
+ `shouldParse` [ParsedVariable " FOO" (Unquoted [VarLiteral " bar" ])]
72
128
73
129
it " ignores inline comments after quoted arguments" $
74
- parseConfig " FOO=\" bar\" # this is foo" `shouldParse` [(" FOO" , " bar" )]
130
+ parseConfig " FOO=\" bar\" # this is foo"
131
+ `shouldParse` [ParsedVariable " FOO" (DoubleQuoted [VarLiteral " bar" ])]
75
132
76
133
it " allows \" #\" in quoted values" $
77
134
parseConfig " foo=\" bar#baz\" # comment"
78
- `shouldParse` [( " foo" , " bar#baz" )]
135
+ `shouldParse` [ParsedVariable " foo" ( DoubleQuoted [ VarLiteral " bar#baz" ] )]
79
136
80
137
it " ignores comment lines" $
81
138
parseConfig " \n\t \n\n # HERE GOES FOO \n foo=bar"
82
- `shouldParse` [( " foo" , " bar" )]
139
+ `shouldParse` [ParsedVariable " foo" ( Unquoted [ VarLiteral " bar" ] )]
83
140
84
141
it " doesn't allow more configuration options after a quoted value" $
85
142
parseConfig `shouldFailOn` " foo='bar'baz='buz'"
86
143
87
- parseConfig :: String -> Either (ParseError Char Dec ) [( String , String ) ]
144
+ parseConfig :: String -> Either (ParseError Char Dec ) [ParsedVariable ]
88
145
parseConfig = parse configParser " "
0 commit comments