-
Notifications
You must be signed in to change notification settings - Fork 14
/
ParseSpec.hs
283 lines (242 loc) · 10.7 KB
/
ParseSpec.hs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Configuration.Dotenv.ParseSpec (main, spec) where
import Configuration.Dotenv.Internal
( ParsedVariable (..),
VarFragment (..),
VarValue (..),
configParser
)
import Data.Void (Void)
import Test.Hspec (Spec, context, describe, hspec, it)
import Test.Hspec.Megaparsec (shouldFailOn, shouldParse, shouldSucceedOn)
import Test.QuickCheck
( Arbitrary,
Gen,
arbitrary,
arbitraryPrintableChar,
choose,
elements,
forAll,
listOf,
listOf1,
property,
resize,
sized,
suchThat,
)
import Text.Megaparsec (ParseErrorBundle, parse)
main :: IO ()
main = hspec $ do
spec
specProperty
spec :: Spec
spec = describe "parse" $ do
it "supports CRLF line breaks" $
parseConfig "FOO=bar\r\nbaz=fbb"
`shouldParse` [ ParsedVariable "FOO" (Unquoted [VarLiteral "bar"]),
ParsedVariable "baz" (Unquoted [VarLiteral "fbb"])
]
it "parses empty values" $
parseConfig "FOO="
`shouldParse` [ParsedVariable "FOO" (Unquoted [])]
it "parses unquoted interpolated values" $ do
parseConfig "FOO=$HOME"
`shouldParse` [ParsedVariable "FOO" (Unquoted [VarInterpolation "HOME"])]
parseConfig "FOO=abc_$HOME"
`shouldParse` [ ParsedVariable
"FOO"
( Unquoted
[ VarLiteral "abc_",
VarInterpolation "HOME"
]
)
]
parseConfig "FOO=${HOME}"
`shouldParse` [ParsedVariable "FOO" (Unquoted [VarInterpolation "HOME"])]
parseConfig "FOO=abc_${HOME}"
`shouldParse` [ ParsedVariable
"FOO"
( Unquoted
[ VarLiteral "abc_",
VarInterpolation "HOME"
]
)
]
it "parses double-quoted interpolated values" $ do
parseConfig "FOO=\"$HOME\""
`shouldParse` [ParsedVariable "FOO" (DoubleQuoted [VarInterpolation "HOME"])]
parseConfig "FOO=\"abc_$HOME\""
`shouldParse` [ ParsedVariable
"FOO"
( DoubleQuoted
[ VarLiteral "abc_",
VarInterpolation "HOME"
]
)
]
parseConfig "FOO=\"${HOME}\""
`shouldParse` [ParsedVariable "FOO" (DoubleQuoted [VarInterpolation "HOME"])]
parseConfig "FOO=\"abc_${HOME}\""
`shouldParse` [ ParsedVariable
"FOO"
( DoubleQuoted
[ VarLiteral "abc_",
VarInterpolation "HOME"
]
)
]
it "parses single-quoted interpolated values as literals" $ do
parseConfig "FOO='$HOME'"
`shouldParse` [ParsedVariable "FOO" (SingleQuoted [VarLiteral "$HOME"])]
parseConfig "FOO='abc_$HOME'"
`shouldParse` [ParsedVariable "FOO" (SingleQuoted [VarLiteral "abc_$HOME"])]
parseConfig "FOO='${HOME}'"
`shouldParse` [ParsedVariable "FOO" (SingleQuoted [VarLiteral "${HOME}"])]
parseConfig "FOO='abc_${HOME}'"
`shouldParse` [ParsedVariable "FOO" (SingleQuoted [VarLiteral "abc_${HOME}"])]
it "does not parse if line format is incorrect" $ do
parseConfig `shouldFailOn` "lol$wut"
parseConfig `shouldFailOn` "KEY=\nVALUE"
parseConfig `shouldFailOn` "KEY\n=VALUE"
it "expands newlines in quoted strings" $
parseConfig "FOO=\"bar\nbaz\""
`shouldParse` [ParsedVariable "FOO" (DoubleQuoted [VarLiteral "bar\nbaz"])]
it "does not parse variables with hyphens in the name" $
parseConfig `shouldFailOn` "FOO-BAR=foobar"
it "parses variables with \"_\" in the name" $
parseConfig "FOO_BAR=foobar"
`shouldParse` [ParsedVariable "FOO_BAR" (Unquoted [VarLiteral "foobar"])]
it "parses variables with digits after the first character" $
parseConfig "FOO_BAR_12=foobar"
`shouldParse` [ParsedVariable "FOO_BAR_12" (Unquoted [VarLiteral "foobar"])]
it "allows vertical spaces after a quoted variable" $
parseConfig "foo='bar' "
`shouldParse` [ParsedVariable "foo" (SingleQuoted [VarLiteral "bar"])]
it "does not parse variable names beginning with a digit" $
parseConfig `shouldFailOn` "45FOO_BAR=foobar"
it "strips unquoted values" $
parseConfig "foo=bar "
`shouldParse` [ParsedVariable "foo" (Unquoted [VarLiteral "bar"])]
it "ignores empty lines" $
parseConfig "\n \t \nfoo=bar\n \nfizz=buzz"
`shouldParse` [ ParsedVariable "foo" (Unquoted [VarLiteral "bar"]),
ParsedVariable "fizz" (Unquoted [VarLiteral "buzz"])
]
it "ignores comment lines" $
parseConfig "\n\t \n\n # HERE GOES FOO \nfoo=bar"
`shouldParse` [ParsedVariable "foo" (Unquoted [VarLiteral "bar"])]
it "doesn't allow more configuration options after a quoted value" $
parseConfig `shouldFailOn` "foo='bar'baz='buz'"
context "$(command) interpolation" $ do
it "parses a simple command" $ do
parseConfig "FOO=$(command)"
`shouldParse` [ParsedVariable "FOO" (Unquoted [CommandInterpolation "command" []])]
it "parses a command anywhere in the value" $ do
parseConfig "FOO=asdf_$(command)"
`shouldParse` [ParsedVariable "FOO" (Unquoted [VarLiteral "asdf_", CommandInterpolation "command" []])]
it "parses a command with arguments" $ do
parseConfig "FOO=$(foo-bar arg1 arg2)"
`shouldParse` [ParsedVariable "FOO" (Unquoted [CommandInterpolation "foo-bar" ["arg1", "arg2"]])]
it "parses a command with quoted arguments" $ do
parseConfig "FOO=$(bin/foo \"arg 1\" arg2)"
`shouldParse` [ParsedVariable "FOO" (Unquoted [CommandInterpolation "bin/foo" ["arg 1", "arg2"]])]
it "parses a command with arguments separated by newlines" $ do
parseConfig "FOO=$(foo.sh \"arg 1\"\narg2\n)"
`shouldParse` [ParsedVariable "FOO" (Unquoted [CommandInterpolation "foo.sh" ["arg 1", "arg2"]])]
it "parses empty content (when the file is empty)" $
parseConfig `shouldSucceedOn` ""
specProperty :: Spec
specProperty = do
it "parses unquoted values as literals" $
property $
forAll (generateInput `suchThat` validChars) $ \input -> do
let value = "FOO=" ++ input
parseConfig value `shouldParse` [ParsedVariable "FOO" (Unquoted [VarLiteral input])]
it "parses single-quoted values as literals" $
property $
forAll (generateInput `suchThat` validChars) $ \input -> do
let quotedInput = "'" ++ input ++ "'"
let value = "FOO=" ++ quotedInput
parseConfig value `shouldParse` [ParsedVariable "FOO" (SingleQuoted [VarLiteral input])]
it "parses double-quoted values as literals" $
property $
forAll (generateInput `suchThat` validChars) $ \input -> do
let quotedInput = "\"" ++ input ++ "\""
let value = "FOO=" ++ quotedInput
parseConfig value `shouldParse` [ParsedVariable "FOO" (DoubleQuoted [VarLiteral input])]
it "parses escaped values as literals" $
property $
forAll (generateTextWithChar "\\\"" "\"") $ \input -> do
let quotedInput = "\"" ++ fst input ++ "\""
let value = "FOO=" ++ quotedInput
parseConfig value `shouldParse` [ParsedVariable "FOO" (DoubleQuoted [VarLiteral $ snd input])]
it "parses # in quoted values" $
property $
forAll (generateTextWithChar "#" "#") $ \input -> do
let quotedInput = "\"" ++ fst input ++ "\""
let value = "FOO=" ++ quotedInput
parseConfig value `shouldParse` [ParsedVariable "FOO" (DoubleQuoted [VarLiteral $ snd input])]
it "parses values with spaces around equal signs" $
property $
forAll generateValidInputWithSpaces $ \input -> do
parseConfig (fst input) `shouldParse` [ParsedVariable "FOO" (Unquoted [VarLiteral $ snd input])]
it "ignores inline comments after unquoted arguments" $
property $
forAll (generateInput `suchThat` validChars) $ \input -> do
let value = "FOO=" ++ input ++ " # comment" ++ input
parseConfig value `shouldParse` [ParsedVariable "FOO" (Unquoted [VarLiteral input])]
it "ignores inline comments after quoted arguments" $
property $
forAll (generateInput `suchThat` validChars) $ \input -> do
let value = "FOO=" ++ "\"" ++ input ++ "\"" ++ " # comment" ++ input
parseConfig value `shouldParse` [ParsedVariable "FOO" (DoubleQuoted [VarLiteral input])]
---------------------------------------------------------------------------
-- Helpers
validChars :: String -> Bool
validChars str = not (null str) && all (`notElem` ['\\', '$', '\'', '"', '\"', ' ', '#']) str
-- | Generate random text with a specific character and expected output.
--
-- This function generates random text that includes a specific character and
-- produces both the input string and the expected output string. The generated
-- text is constructed by repeating a non-empty string multiple times and
-- appending it with the specified character. The size of the generated text is
-- limited by the 'maxSize' parameter.
generateTextWithChar
-- | The character to include in the generated text
:: String
-- | The expected output character to include in the generated text
-> String
-> Gen (String, String)
generateTextWithChar input expected = do
let maxSize = 5
nonEmptyString <- resize maxSize $ generateInput `suchThat` validChars
strForQuoted <- resize maxSize $ generateInput `suchThat` validChars
numRepeat <- choose (1, maxSize)
let quotedStr = input ++ strForQuoted
let quotedStrForInput = expected ++ strForQuoted
return
( concat (replicate numRepeat (nonEmptyString ++ quotedStr)),
concat (replicate numRepeat (nonEmptyString ++ quotedStrForInput))
)
generateValidInputWithSpaces :: Gen (String, String)
generateValidInputWithSpaces = do
input <- generateInput `suchThat` validChars
spacesBefore <- listOf (elements " \t")
spacesAfter <- listOf (elements " \t")
return ("FOO" ++ spacesBefore ++ "=" ++ spacesAfter ++ input, input)
generateInput :: Gen String
generateInput = do
nonEmptyString <- arbitrary :: Gen NonEmptyPrintableString
return (getNonEmptyPrintableString nonEmptyString)
-- Non-empty version of 'PrintableString'
newtype NonEmptyPrintableString = NonEmptyPrintableString
{ getNonEmptyPrintableString :: String
}
deriving (Show)
instance Arbitrary NonEmptyPrintableString where
arbitrary = sized $ \s -> do
someText <- resize (s * 10) $ listOf1 arbitraryPrintableChar
return $ NonEmptyPrintableString someText
parseConfig :: String -> Either (ParseErrorBundle String Void) [ParsedVariable]
parseConfig = parse configParser ""