-
Notifications
You must be signed in to change notification settings - Fork 17
/
Main.purs
416 lines (333 loc) · 14.4 KB
/
Main.purs
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
module Test.Main where
import Prelude
import Data.Maybe (Maybe(..), maybe)
import Data.Newtype (un)
import Data.Array.NonEmpty (cons')
import Data.String as Str
import Data.String.NonEmpty (NonEmptyString)
import Data.String.NonEmpty (fromString) as NES
import Data.String.NonEmpty.CodeUnits (singleton) as NES
import Data.Tuple (Tuple(..))
import Effect (Effect)
import Effect.Console (info)
import Effect.Exception (throw)
import Pathy (class IsDirOrFile, class IsRelOrAbs, Abs, Dir, Name(..), Path, Rel, alterExtension, currentDir, debugPrintPath, dir, extension, file, in', joinName, parentOf, parseAbsDir, parseAbsFile, parseRelDir, parseRelFile, peel, posixParser, posixPrinter, printPath, relativeTo, rename, rootDir, sandbox, sandboxAny, splitName, unsandbox, windowsPrinter, (<..>), (<.>), (</>))
import Pathy.Gen as PG
import Pathy.Name (reflectName)
import Type.Proxy (Proxy(..))
import Test.QuickCheck ((===))
import Test.QuickCheck as QC
import Test.QuickCheck.Gen as Gen
import Unsafe.Coerce (unsafeCoerce)
test :: forall a. Show a => Eq a => String -> a -> a -> Effect Unit
test name actual expected = do
info $ "Test: " <> name
if expected == actual then info $ "Passed: " <> (show expected)
else throw $ "Failed:\n Expected: " <> (show expected) <> "\n Actual: " <> (show actual)
test' :: forall a b. IsRelOrAbs a => IsDirOrFile b => String -> Path a b -> String -> Effect Unit
test' n p s = test n (printTestPath p) s
pathPart :: Gen.Gen NonEmptyString
pathPart = asNonEmptyString <$> Gen.suchThat QC.arbitrary (not <<< Str.null)
where
asNonEmptyString :: String -> NonEmptyString
asNonEmptyString = unsafeCoerce
dirFoo :: Path Rel Dir
dirFoo = dir (Proxy :: Proxy "foo")
dirBar :: Path Rel Dir
dirBar = dir (Proxy :: Proxy "bar")
dirBaz :: Path Rel Dir
dirBaz = dir (Proxy :: Proxy "baz")
parsePrintCheck :: forall a b. IsRelOrAbs a => IsDirOrFile b => Path a b -> Maybe (Path a b) -> QC.Result
parsePrintCheck input parsed =
if parsed == Just input then QC.Success
else QC.Failed
$ "`parse (print path) != Just path` for path: `" <> show input <> "` which was re-parsed into `" <> show parsed <> "`"
<> "\n\tPrinted path: "
<> show (printTestPath input)
<> "\n\tPrinted path': `"
<> show (map (printTestPath) parsed)
<> "`"
parsePrintAbsDirPath :: Gen.Gen QC.Result
parsePrintAbsDirPath = PG.genAbsDirPath <#> \path ->
parsePrintCheck path (parseAbsDir posixParser $ printTestPath path)
parsePrintAbsFilePath :: Gen.Gen QC.Result
parsePrintAbsFilePath = PG.genAbsFilePath <#> \path ->
parsePrintCheck path (parseAbsFile posixParser $ printTestPath path)
parsePrintRelDirPath :: Gen.Gen QC.Result
parsePrintRelDirPath = PG.genRelDirPath <#> \path ->
parsePrintCheck path (parseRelDir posixParser $ printTestPath path)
parsePrintRelFilePath :: Gen.Gen QC.Result
parsePrintRelFilePath = PG.genRelFilePath <#> \path ->
parsePrintCheck path (parseRelFile posixParser $ printTestPath path)
genAmbigiousName :: forall a. Gen.Gen (Name a)
genAmbigiousName =
let
genNES = PG.genName <#> un Name
in
map Name $ Gen.oneOf $ cons' genNES
[ genNES <#> \a -> a <> (NES.singleton '.')
, genNES <#> \a -> (NES.singleton '.') <> a
, pure (NES.singleton '.')
, do
a <- genNES
b <- genNES
pure $ a <> (NES.singleton '.') <> b
]
checkAlterExtensionId :: Gen.Gen QC.Result
checkAlterExtensionId = do
n <- genAmbigiousName
pure $ alterExtension identity n === identity n
checkJoinSplitNameId :: Gen.Gen QC.Result
checkJoinSplitNameId = do
n <- genAmbigiousName
pure $ joinName (splitName n) === identity n
checkPeelIn :: forall b. IsDirOrFile b => Gen.Gen (Path Abs b) -> Gen.Gen QC.Result
checkPeelIn gen = do
p <- gen
pure $ p === maybe p (\(Tuple r n) -> r </> in' n) (peel p)
checkRelative :: forall b. IsDirOrFile b => Gen.Gen (Path Abs b) -> Gen.Gen QC.Result
checkRelative gen = do
p1 <- gen
p2 <- PG.genAbsDirPath
let rel = p1 `relativeTo` p2
let p1' = p2 </> rel
pure
if p1 == p1' then QC.Success
else
QC.Failed
$ "`relativeTo` property did not hold:"
<> "\n\tp1: "
<> printTestPath p1
<> "\n\tp2: "
<> printTestPath p2
<> "\n\trel: "
<> printTestPath rel
<> "\n\tp1': "
<> printTestPath p1'
main :: Effect Unit
main = do
info "checking `parse <<< print` for `AbsDir`" *> QC.quickCheck parsePrintAbsDirPath
info "checking `parse <<< print` for `AbsFile`" *> QC.quickCheck parsePrintAbsFilePath
info "checking `parse <<< print` for `RelDir`" *> QC.quickCheck parsePrintRelDirPath
info "checking `parse <<< print` for `RelFile`" *> QC.quickCheck parsePrintRelFilePath
info "checking `relativeTo` for `AbsDir`" *> QC.quickCheck (checkRelative PG.genAbsDirPath)
info "checking `relativeTo` for `AbsFile`" *> QC.quickCheck (checkRelative PG.genAbsFilePath)
info "checking `p === maybe p (\\(Tuple r n) -> r </> in' n) (peel p)` for `AbsDir`" *> QC.quickCheck (checkPeelIn PG.genAbsDirPath)
info "checking `p === maybe p (\\(Tuple r n) -> r </> in' n) (peel p)` for `AbsFile`" *> QC.quickCheck (checkPeelIn PG.genAbsFilePath)
info "checking `joinName <<< splitName === id`" *> QC.quickCheck checkJoinSplitNameId
info "checking `alterExtension id === id`" *> QC.quickCheck checkAlterExtensionId
-- Should not compile:
-- test
-- "(</>) - file in dir"
-- (printPath (file "image.png" </> dirFoo))
-- "./image.png/foo"
-- Should not compile:
-- test
-- "(</>) - absolute dir in absolute dir"
-- (printPath (rootDir </> rootDir))
-- "/"
-- Should not compile:
-- test
-- "(</>) - absolute dir in relative dir"
-- (printPath (currentDir </> rootDir))
-- "/"
test' "(</>) - two directories"
(dirFoo </> dirBar)
"./foo/bar/"
test "windowsPrinter"
(printWindowsPath $ rootDir </> dir (Proxy :: Proxy "C") </> dirBar)
"C:\\bar\\"
test' "(</>) - file with two parents"
( dirFoo
</> dirBar
</> file (Proxy :: Proxy "image.png")
)
"./foo/bar/image.png"
test' "(<.>) - file without extension"
( file (Proxy :: Proxy "image")
<.> "png"
)
"./image.png"
test' "(<.>) - file with extension"
( file (Proxy :: Proxy "image.jpg")
<.> "png"
)
"./image.png"
test' "printPath - ./../"
(parentOf currentDir)
"./../"
test """printPath windowsPrinter - C:\Users\Default\"""
(printPath windowsPrinter $ sandboxAny $ rootDir </> dir (Proxy :: Proxy "C") </> dir (Proxy :: Proxy "Users") </> dir (Proxy :: Proxy "Default"))
"""C:\Users\Default\"""
test """printPath posixPrinter - /C/Users/Default/"""
(printPath posixPrinter $ sandboxAny $ rootDir </> dir (Proxy :: Proxy "C") </> dir (Proxy :: Proxy "Users") </> dir (Proxy :: Proxy "Default"))
"""/C/Users/Default/"""
test """printPath windowsPrinter - \"""
(printPath windowsPrinter $ sandboxAny rootDir)
"""\"""
test """printPath posixPrinter - /"""
(printPath posixPrinter $ sandboxAny rootDir)
"""/"""
test' "(</>) - ./../foo/"
(parentOf currentDir </> dirFoo)
"./../foo/"
test' "parentOf - ./../foo/../"
((parentOf currentDir </> dirFoo) </> (parentOf currentDir))
"./../"
test' "(<..>) - ./../"
(currentDir <..> currentDir)
"./../"
test' "(<..>) - ./../foo/"
(currentDir <..> dirFoo)
"./../foo/"
test' "(<..>) - ./../foo/../"
((currentDir <..> dirFoo) <..> currentDir)
"./../"
test' "./foo/../ = ./"
(parentOf dirFoo)
"./"
test' "./foo/bar/../../ = ./"
((parentOf (parentOf (dirFoo </> dirBar))))
"./"
test' "/../../ = /"
((parentOf (parentOf rootDir)))
"/"
test "/foo/../bar/ = /bar"
((rootDir </> dirFoo <..> dirBar))
(rootDir </> dirBar)
test "/foo/bar/ </> ../bar/ = /foo/bar/"
((rootDir </> dirFoo </> dirBar) </> (currentDir <..> dirBar))
(rootDir </> dirFoo </> dirBar)
test "/foo/bar/ </> ../../bar/ = /bar/"
((rootDir </> dirFoo </> dirBar) </> (currentDir <..> currentDir <..> currentDir </> dirBar))
(rootDir </> dirBar)
test "relativeTo rootDir rootDir = currentDir"
(relativeTo rootDir rootDir)
(currentDir)
test' "(rootDir </> dirFoo) `relativeTo` (rootDir </> dirFoo) = ./"
((rootDir </> dirFoo) `relativeTo` (rootDir </> dirFoo))
"./"
test' "(rootDir </> dirFoo) `relativeTo` rootDir = currentDir </> dirFoo"
((rootDir </> dirFoo) `relativeTo` rootDir)
"./foo/"
test' "(rootDir </> dirFoo) `relativeTo` (rootDir </> dirBar) = currentDir <..> dirFoo"
((rootDir </> dirFoo) `relativeTo` (rootDir </> dirBar))
"./../foo/"
test' "(rootDir </> dirBar) `relativeTo` (rootDir </> dirFoo) = ./../bar/"
((rootDir </> dirBar) `relativeTo` (rootDir </> dirFoo))
"./../bar/"
test' "(rootDir </> dirBar) `relativeTo` (rootDir </> dirFoo </> dirFoo) = ./../../bar/"
((rootDir </> dirBar) `relativeTo` (rootDir </> dirFoo </> dirFoo))
"./../../bar/"
test' "(rootDir </> dirBar) `relativeTo` (rootDir </> dirFoo </> dirFoo </> dirFoo) = ./../../../bar/"
((rootDir </> dirBar) `relativeTo` (rootDir </> dirFoo </> dirFoo </> dirFoo))
"./../../../bar/"
test' "(rootDir </> dirBar </> dirBar) `relativeTo` (rootDir </> dirFoo) = ./../bar/bar/"
((rootDir </> dirBar </> dirBar) `relativeTo` (rootDir </> dirFoo))
"./../bar/bar/"
test' "(rootDir </> dirBar </> dirBar) `relativeTo` (rootDir </> dirFoo </> dirFoo) = ./../../bar/bar/"
((rootDir </> dirBar </> dirBar) `relativeTo` (rootDir </> dirFoo </> dirFoo))
"./../../bar/bar/"
test' "(rootDir </> dirBar </> dirFoo </> dirFoo) `relativeTo` (rootDir </> dirFoo </> dirFoo </> dirFoo) = ./../../../bar/foo/foo"
((rootDir </> dirBar </> dirFoo </> dirFoo) `relativeTo` (rootDir </> dirFoo </> dirFoo </> dirFoo))
"./../../../bar/foo/foo/"
test' "(rootDir </> dirFoo </> dirBar </> dirBaz) `relativeTo` rootDir = ./foo/bar/baz/"
((rootDir </> dirFoo </> dirBar </> dirBaz) `relativeTo` rootDir)
"./foo/bar/baz/"
test' "(rootDir </> dirFoo </> dirBar </> dirBaz) `relativeTo` (rootDir </> dirFoo) = ./bar/baz/"
((rootDir </> dirFoo </> dirBar </> dirBaz) `relativeTo` (rootDir </> dirFoo))
"./bar/baz/"
test' "(rootDir </> dirFoo </> dirBar </> dirBaz) `relativeTo` (rootDir </> dirBaz) = ./../foo/bar/baz/"
((rootDir </> dirFoo </> dirBar </> dirBaz) `relativeTo` (rootDir </> dirBaz))
"./../foo/bar/baz/"
test' "(rootDir </> dirBar </> dirFoo) `relativeTo` (rootDir </> dirBar) = ./foo/"
((rootDir </> dirBar </> dirFoo) `relativeTo` (rootDir </> dirBar))
"./foo/"
test "rename - single level deep"
(rename (alterExtension (const Nothing)) (file (Proxy :: Proxy "image.png")))
(file (Proxy :: Proxy "image"))
test """extension (Name ".foo") == Nothing"""
(extension (reflectName (Proxy :: Proxy ".foo")))
(Nothing)
test """extension (Name "foo.") == Nothing"""
(extension (reflectName (Proxy :: Proxy "foo.")))
(Nothing)
test """extension (Name "foo") == Nothing"""
(extension (reflectName (Proxy :: Proxy "foo")))
(Nothing)
test """extension (Name ".") == Nothing"""
(extension (reflectName (Proxy :: Proxy ".")))
(Nothing)
test """extension (Name "foo.baz") == (Just "baz")"""
(extension (reflectName (Proxy :: Proxy "foo.baz")))
(NES.fromString "baz")
test "sandbox - fail when relative path lies outside sandbox (above)"
(sandbox (rootDir </> dirBar) (parentOf currentDir))
Nothing
test "sandbox - fail when relative path lies outside sandbox (neigbouring)"
(sandbox (rootDir </> dirBar) (parentOf currentDir </> dirFoo))
Nothing
test "sandbox - fail when absolute path lies outside sandbox"
(sandbox (rootDir </> dirBar) (rootDir </> dirFoo </> dirBar))
Nothing
test "sandbox - succeed when relative path goes above sandbox but returns to it"
(unsandbox <$> sandbox (rootDir </> dirBar) (parentOf currentDir </> dirBar))
(Just (parentOf currentDir </> dirBar))
test "sandbox - succeed when absolute path lies inside sandbox"
(unsandbox <$> sandbox (rootDir </> dirBar) (rootDir </> dirBar </> dirFoo))
(Just (rootDir </> dirBar </> dirFoo))
test "sandbox - print relative path that goes above sandbox but returns to it"
(printPath posixPrinter <$> sandbox (rootDir </> dirBar) (parentOf currentDir </> dirBar))
(Just "/bar/")
test "sandbox - print absolute path that lies inside sandbox"
(printPath posixPrinter <$> sandbox (rootDir </> dirBar) (rootDir </> dirBar </> dirFoo))
(Just "/bar/foo/")
test "parseRelFile - image.png"
(parseRelFile posixParser "image.png")
(Just $ file (Proxy :: Proxy "image.png"))
test "parseRelFile - ./image.png"
(parseRelFile posixParser "./image.png")
(Just $ file (Proxy :: Proxy "image.png"))
test "parseRelFile - foo/image.png"
(parseRelFile posixParser "foo/image.png")
(Just $ dirFoo </> file (Proxy :: Proxy "image.png"))
test "parseRelFile - ../foo/image.png"
(parseRelFile posixParser "../foo/image.png")
(Just $ currentDir <..> dirFoo </> file (Proxy :: Proxy "image.png"))
test "parseAbsFile - /image.png"
(parseAbsFile posixParser "/image.png")
(Just $ rootDir </> file (Proxy :: Proxy "image.png"))
test "parseAbsFile - /foo/image.png"
(parseAbsFile posixParser "/foo/image.png")
(Just $ rootDir </> dirFoo </> file (Proxy :: Proxy "image.png"))
test "parseRelDir - empty string"
(parseRelDir posixParser "")
Nothing
test "parseRelDir - ./../"
(parseRelDir posixParser "./../")
(Just $ currentDir <..> currentDir)
test "parseRelDir - foo/"
(parseRelDir posixParser "foo/")
(Just dirFoo)
test "parseRelDir - foo/bar"
(parseRelDir posixParser "foo/bar/")
(Just $ dirFoo </> dirBar)
test "parseRelDir - ./foo/bar"
(parseRelDir posixParser "./foo/bar/")
(Just $ dirFoo </> dirBar)
test "parseAbsDir - /"
(parseAbsDir posixParser "/")
(Just $ rootDir)
test "parseAbsDir - /foo/"
(parseAbsDir posixParser "/foo/")
(Just $ rootDir </> dirFoo)
test "parseAbsDir - /foo/bar"
(parseAbsDir posixParser "/foo/bar/")
(Just $ rootDir </> dirFoo </> dirBar)
test "parseRelDir - /foo/././//bar/"
(parseAbsDir posixParser "/foo/././//bar/")
(Just $ rootDir </> dirFoo </> dirBar)
printTestPath :: forall a b. IsRelOrAbs a => IsDirOrFile b => Path a b -> String
printTestPath p = debugPrintPath posixPrinter p
printWindowsPath :: forall a b. IsRelOrAbs a => IsDirOrFile b => Path a b -> String
printWindowsPath p = debugPrintPath windowsPrinter p