-
Notifications
You must be signed in to change notification settings - Fork 5
/
Main.hs
1719 lines (1427 loc) · 55.9 KB
/
Main.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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import qualified CabalGild.Unstable.Class.MonadLog as MonadLog
import qualified CabalGild.Unstable.Class.MonadRead as MonadRead
import qualified CabalGild.Unstable.Class.MonadWalk as MonadWalk
import qualified CabalGild.Unstable.Class.MonadWrite as MonadWrite
import qualified CabalGild.Unstable.Exception.CheckFailure as CheckFailure
import qualified CabalGild.Unstable.Exception.DuplicateOption as DuplicateOption
import qualified CabalGild.Unstable.Exception.InvalidOption as InvalidOption
import qualified CabalGild.Unstable.Exception.SpecifiedOutputWithCheckMode as SpecifiedOutputWithCheckMode
import qualified CabalGild.Unstable.Exception.SpecifiedStdinWithFileInput as SpecifiedStdinWithFileInput
import qualified CabalGild.Unstable.Exception.UnexpectedArgument as UnexpectedArgument
import qualified CabalGild.Unstable.Exception.UnknownOption as UnknownOption
import qualified CabalGild.Unstable.Extra.String as String
import qualified CabalGild.Unstable.Main as Gild
import qualified CabalGild.Unstable.Type.Input as Input
import qualified CabalGild.Unstable.Type.Output as Output
import qualified Control.Monad.Catch as Exception
import qualified Control.Monad.Trans.Class as Trans
import qualified Control.Monad.Trans.Except as ExceptT
import qualified Control.Monad.Trans.RWS as RWST
import qualified Data.ByteString as ByteString
import qualified Data.Either as Either
import qualified Data.Functor.Identity as Identity
import qualified Data.Map as Map
import qualified Data.Maybe as Maybe
import qualified GHC.Stack as Stack
import qualified System.Directory as Directory
import qualified System.Exit as Exit
import qualified System.FilePath as FilePath
import qualified System.FilePattern as FilePattern
import qualified System.IO.Temp as Temp
import qualified Test.Hspec as Hspec
main :: IO ()
main = Hspec.hspec . Hspec.parallel . Hspec.describe "cabal-gild" $ do
Hspec.it "shows the help" $ do
let (a, s, w) = runGild ["--help"] [] (".", [])
a `shouldBeFailure` Exit.ExitSuccess
w `Hspec.shouldNotBe` []
s `Hspec.shouldBe` Map.empty
Hspec.it "shows the version" $ do
let (a, s, w) = runGild ["--version"] [] (".", [])
a `shouldBeFailure` Exit.ExitSuccess
w `Hspec.shouldNotBe` []
s `Hspec.shouldBe` Map.empty
Hspec.it "fails with an unknown option" $ do
expectException ["--unknown"] $
UnknownOption.fromString "--unknown"
Hspec.it "fails with an invalid option" $ do
expectException ["--help=invalid"] $
InvalidOption.fromString "option `--help' doesn't allow an argument"
Hspec.it "fails with an unexpected argument" $ do
expectException ["unexpected"] $
UnexpectedArgument.fromString "unexpected"
Hspec.it "fails when --crlf is given twice" $ do
expectException ["--crlf=strict", "--crlf=lenient"] $
DuplicateOption.DuplicateOption "crlf" "strict" "lenient"
Hspec.it "does not fail when a flag is given twice with the same value" $ do
let (a, s, w) =
runGild
["--crlf=strict", "--crlf=strict"]
[(Input.Stdin, String.toUtf8 "")]
(".", [])
a `Hspec.shouldSatisfy` Either.isRight
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.singleton Output.Stdout (String.toUtf8 "")
Hspec.it "fails when a flag is repeatedly overriden" $ do
expectException ["-ia", "-ib", "-i-"] $
DuplicateOption.DuplicateOption "input" "a" "b"
Hspec.it "fails when --input is given twice" $ do
expectException
["--input=f", "--input=-"]
$ DuplicateOption.DuplicateOption "input" "f" "-"
Hspec.it "fails when --mode is given twice" $ do
expectException
["--mode=check", "--mode=format"]
$ DuplicateOption.DuplicateOption "mode" "check" "format"
Hspec.it "fails when --output is given twice" $ do
expectException
["--output=f", "--output=-"]
$ DuplicateOption.DuplicateOption "output" "f" "-"
Hspec.it "fails when --stdin is given twice" $ do
expectException
["--stdin=f", "--stdin=g"]
$ DuplicateOption.DuplicateOption "stdin" "f" "g"
Hspec.it "reads from an input file" $ do
let (a, s, w) =
runGild
["--input", "input.cabal"]
[(Input.File "input.cabal", String.toUtf8 "")]
(".", [])
a `Hspec.shouldSatisfy` Either.isRight
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.singleton Output.Stdout (String.toUtf8 "")
Hspec.it "writes to an output file" $ do
let (a, s, w) =
runGild
["--output", "output.cabal"]
[(Input.Stdin, String.toUtf8 "")]
(".", [])
a `Hspec.shouldSatisfy` Either.isRight
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.singleton (Output.File "output.cabal") (String.toUtf8 "")
Hspec.it "succeeds when checking formatted input" $ do
let (a, s, w) =
runGild
["--mode", "check"]
[(Input.Stdin, String.toUtf8 "pass: yes\n")]
(".", [])
a `Hspec.shouldSatisfy` Either.isRight
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.empty
Hspec.it "fails when checking unformatted input" $ do
let (a, s, w) =
runGild
["--mode", "check"]
[(Input.Stdin, String.toUtf8 "pass: no")]
(".", [])
a `shouldBeFailure` CheckFailure.CheckFailure
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.empty
Hspec.it "succeeds when checking CRLF input leniently" $ do
let (a, s, w) =
runGild
["--mode", "check"]
[(Input.Stdin, String.toUtf8 "pass: yes\r\n")]
(".", [])
a `Hspec.shouldSatisfy` Either.isRight
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.empty
Hspec.it "fails when checking CRLF input strictly" $ do
let (a, s, w) =
runGild
["--crlf", "strict", "--mode", "check"]
[(Input.Stdin, String.toUtf8 "pass: no\r\n")]
(".", [])
a `shouldBeFailure` CheckFailure.CheckFailure
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.empty
Hspec.it "fails when --stdin is given with an input file" $ do
let (a, s, w) =
runGild
["--input", "f", "--stdin", "g"]
[]
(".", [])
a `shouldBeFailure` SpecifiedStdinWithFileInput.SpecifiedStdinWithFileInput
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.empty
Hspec.it "fails when --output is given with check mode" $ do
let (a, s, w) =
runGild
["--mode", "check", "--output", "-"]
[]
(".", [])
a `shouldBeFailure` SpecifiedOutputWithCheckMode.SpecifiedOutputWithCheckMode
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.empty
Hspec.it "does not overwrite output when input is formatted" $ do
let (a, s, w) =
runGild
["--io", "io.cabal"]
[(Input.File "io.cabal", String.toUtf8 "")]
(".", [])
a `Hspec.shouldSatisfy` Either.isRight
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.empty
Hspec.it "writes to stdout when input is formatted" $ do
let (a, s, w) =
runGild
["--input", "p.cabal"]
[(Input.File "p.cabal", String.toUtf8 "")]
(".", [])
a `Hspec.shouldSatisfy` Either.isRight
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.singleton Output.Stdout (String.toUtf8 "")
Hspec.it "writes to output when input is formatted" $ do
let (a, s, w) =
runGild
["--input", "p.cabal", "--output", "q.cabal"]
[(Input.File "p.cabal", String.toUtf8 "")]
(".", [])
a `Hspec.shouldSatisfy` Either.isRight
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.singleton (Output.File "q.cabal") (String.toUtf8 "")
Hspec.it "writes to output when stdin is formatted" $ do
let (a, s, w) =
runGild
["--output", "q.cabal"]
[(Input.Stdin, String.toUtf8 "")]
(".", [])
a `Hspec.shouldSatisfy` Either.isRight
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.singleton (Output.File "q.cabal") (String.toUtf8 "")
Hspec.it "sets input and output simultaneously" $ do
let (a, s, w) =
runGild
["--io", "io.cabal"]
[(Input.File "io.cabal", String.toUtf8 "f:a")]
(".", [])
a `Hspec.shouldSatisfy` Either.isRight
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.singleton (Output.File "io.cabal") (String.toUtf8 "f: a\n")
Hspec.it "does not overwrite CRLF file when lenient" $ do
let (a, s, w) =
runGild
["--io", "p.cabal"]
[(Input.File "p.cabal", String.toUtf8 "s\r\n")]
(".", [])
a `Hspec.shouldSatisfy` Either.isRight
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.empty
Hspec.it "overwrites CRLF file when strict" $ do
let (a, s, w) =
runGild
["--crlf", "strict", "--io", "p.cabal"]
[(Input.File "p.cabal", String.toUtf8 "s\r\n")]
(".", [])
a `Hspec.shouldSatisfy` Either.isRight
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.singleton (Output.File "p.cabal") (String.toUtf8 "s\n")
Hspec.it "uses --stdin for discovery" $ do
let (a, s, w) =
runGild
["--stdin", "d/p.cabal"]
[(Input.Stdin, String.toUtf8 "library\n -- cabal-gild: discover\n exposed-modules:")]
("d", [["M.hs"]])
a `Hspec.shouldSatisfy` Either.isRight
w `Hspec.shouldBe` []
s `Hspec.shouldBe` Map.singleton Output.Stdout (String.toUtf8 "library\n -- cabal-gild: discover\n exposed-modules: M\n")
Hspec.it "succeeds with empty input" $ do
expectGilded
""
""
Hspec.it "removes extra blank space" $ do
expectGilded
"\t\r\n \r\n"
""
Hspec.it "formats a comment" $ do
expectGilded
"-- c"
"-- c\n"
Hspec.it "keeps a blank comment" $ do
expectGilded
"--"
"--\n"
Hspec.it "formats multiple comments" $ do
expectGilded
"-- c\n-- d"
"-- c\n-- d\n"
Hspec.it "removes blank lines between comments" $ do
expectGilded
"-- c\n\n-- d"
"-- c\n-- d\n"
Hspec.it "does not require a space after the comment start" $ do
expectGilded
"--c"
"--c\n"
Hspec.it "leaves leading blank space in comments" $ do
expectGilded
"--\t c"
"--\t c\n"
Hspec.it "trims trailing blank space from comments" $ do
expectGilded
"-- c\t \n"
"-- c\n"
Hspec.it "normalizes Windows line endings in comments" $ do
expectGilded
"-- c\r\n"
"-- c\n"
Hspec.it "formats a field without a value" $ do
expectGilded
"f:"
"f:\n"
Hspec.it "formats a field with a value" $ do
expectGilded
"f: 1"
"f: 1\n"
Hspec.it "does not require spaces before a field's value" $ do
expectGilded
"f:1"
"f: 1\n"
Hspec.it "allows spaces after a field's name" $ do
expectGilded
"f :1"
"f: 1\n"
Hspec.it "formats multiple fields" $ do
expectGilded
"f: 1\ng: 2"
"f: 1\ng: 2\n"
Hspec.it "formats a field with hung values" $ do
expectGilded
"f: 1\n 2"
"f:\n 1\n 2\n"
Hspec.it "formats a field with nested values" $ do
expectGilded
"f:\n 1\n 2"
"f:\n 1\n 2\n"
Hspec.it "puts a line after a field with multiple values" $ do
expectGilded
"f: 1\n 2\ng: 3"
"f:\n 1\n 2\n\ng: 3\n"
Hspec.it "does not pu a line before a field with multiple values" $ do
expectGilded
"f: 1\ng: 2\n 3"
"f: 1\ng:\n 2\n 3\n"
Hspec.it "converts field names to lowercase" $ do
expectGilded
"F: 1"
"f: 1\n"
Hspec.it "does not convert field values to lowercase" $ do
expectGilded
"f: A"
"f: A\n"
Hspec.it "normalizes spaces within field values" $ do
expectGilded
"f:\n a\n\n b"
"f:\n a\n b\n"
Hspec.it "formats a section" $ do
expectGilded
"s"
"s\n"
Hspec.it "puts a line after a section" $ do
expectGilded
"s\nf: 1"
"s\n\nf: 1\n"
Hspec.it "puts a line before a section" $ do
expectGilded
"f: 1\ns"
"f: 1\n\ns\n"
Hspec.it "formats a section with an argument" $ do
expectGilded
"s a"
"s a\n"
Hspec.it "formats a section with multiple arguments" $ do
expectGilded
"s a b"
"s a b\n"
Hspec.it "formats a section with a quoted argument" $ do
expectGilded
"s \"a\""
"s \"a\"\n"
Hspec.it "formats a section with an operator argument" $ do
expectGilded
"s !"
"s !\n"
Hspec.it "converts section names to lowercase" $ do
expectGilded
"S"
"s\n"
Hspec.it "does not convert section arguments to lowercase" $ do
expectGilded
"s A"
"s A\n"
Hspec.it "formats nested sections" $ do
-- Note that the nested section does not have an extra blank line before
-- it.
expectGilded
"s\n t"
"s\n t\n"
Hspec.it "formats a section with a field" $ do
expectGilded
"s\n f: 1"
"s\n f: 1\n"
Hspec.it "formats a section with multiple fields" $ do
expectGilded
"s\n f: 1\n g: 2"
"s\n f: 1\n g: 2\n"
Hspec.it "correctly indents a section containing a field with multiple values" $ do
expectGilded
"s\n f:\n 1\n 2"
"s\n f:\n 1\n 2\n"
Hspec.it "formats a comment before a field" $ do
expectGilded
"-- c\nf: 1"
"-- c\nf: 1\n"
Hspec.it "formats a comment after a field" $ do
expectGilded
"f: 1\n-- c"
"f: 1\n-- c\n"
Hspec.it "formats a comment before a field's value" $ do
expectGilded
"f:\n -- c\n 1"
"f:\n -- c\n 1\n"
Hspec.it "formats a comment in a field's value" $ do
expectGilded
"f:\n 1\n -- c\n 2"
"f:\n -- c\n 1\n 2\n"
Hspec.it "formats a comment after a field's value" $ do
expectGilded
"f:\n 1\n -- c"
"f:\n 1\n\n-- c\n"
Hspec.it "formats a comment after a field with multiple values" $ do
expectGilded
"f: 1\n 2\n-- c"
"f:\n 1\n 2\n\n-- c\n"
Hspec.it "formats a comment before a section" $ do
expectGilded
"-- c\ns"
"-- c\ns\n"
Hspec.it "formats a comment after a section" $ do
expectGilded
"s\n-- c"
"s\n\n-- c\n"
Hspec.it "correctly indents a comment in a section" $ do
expectGilded
"s\n -- c\n f: 1"
"s\n -- c\n f: 1\n"
Hspec.describe "description" $ do
-- These tests apply to other "free text" fields as well. The description
-- field is just a representative example.
Hspec.describe "< 3.0" $ do
-- These may look surprising, but they're the expected behavior.
-- <https://github.com/haskell/cabal/issues/5938>
Hspec.it "collapses extra blank lines" $ do
expectGilded
"description:\n 1\n\n 2"
"description:\n 1\n 2\n"
Hspec.it "trims leading blank space" $ do
expectGilded
"description:\n 1\n 2"
"description:\n 1\n 2\n"
Hspec.describe ">= 3.0" $ do
Hspec.it "keeps one extra blank line" $ do
expectGilded
"cabal-version: 3.0\ndescription:\n 1\n\n 2"
"cabal-version: 3.0\ndescription:\n 1\n\n 2\n"
Hspec.it "keeps two extra blank lines" $ do
-- Although Haddock will collapse these, Cabal itself keeps them.
expectGilded
"cabal-version: 3.0\ndescription:\n 1\n\n\n 2"
"cabal-version: 3.0\ndescription:\n 1\n\n\n 2\n"
Hspec.it "indents second line past first when nested" $ do
expectGilded
"cabal-version: 3.0\ndescription:\n 1\n 2"
"cabal-version: 3.0\ndescription:\n 1\n 2\n"
Hspec.it "indents first line past second when nested" $ do
expectGilded
"cabal-version: 3.0\ndescription:\n 1\n 2"
"cabal-version: 3.0\ndescription:\n 1\n 2\n"
Hspec.it "does not indent first line when hung" $ do
expectGilded
"cabal-version: 3.0\ndescription: 1\n 2"
"cabal-version: 3.0\ndescription:\n 1\n 2\n"
Hspec.it "indents second line past first when hung" $ do
expectGilded
"cabal-version: 3.0\ndescription: 1\n 2"
"cabal-version: 3.0\ndescription:\n 1\n 2\n"
Hspec.it "indents third line (but not first) past second when hung" $ do
-- If the first line of the value is on the same line as the name, then
-- it should never be indented.
expectGilded
"cabal-version: 3.0\ndescription: 1\n 2\n 3"
"cabal-version: 3.0\ndescription:\n 1\n 2\n 3\n"
Hspec.it "treats tabs the same as spaces" $ do
-- Yes, each tab only counts as one space.
expectGilded
"cabal-version: 3.0\ndescription:\n 1\n\t2"
"cabal-version: 3.0\ndescription:\n 1\n 2\n"
Hspec.it "does not insert extra blank lines before comments" $ do
expectGilded
"cabal-version: 3.0\ndescription:\n -- c\n 1\n -- d\n 2"
"cabal-version: 3.0\ndescription:\n -- c\n -- d\n 1\n 2\n"
Hspec.it "does not consider comments for indentation" $ do
expectGilded
"cabal-version: 3.0\ndescription:\n 1\n -- c\n 2"
"cabal-version: 3.0\ndescription:\n -- c\n 1\n 2\n"
Hspec.it "properly formats conditionals" $ do
expectGilded
"if ! impl ( ghc >= 9.8 )"
"if !impl(ghc >=9.8)\n"
Hspec.describe "conditionals" $ do
Hspec.it "formats variable" $ do
expectGilded
"if flag ( x )"
"if flag(x)\n"
Hspec.it "formats false" $ do
expectGilded
"if false"
"if false\n"
Hspec.it "formats upper false" $ do
expectGilded
"if False"
"if false\n"
Hspec.it "formats true" $ do
expectGilded
"if true"
"if true\n"
Hspec.it "formats upper true" $ do
expectGilded
"if True"
"if true\n"
Hspec.it "formats not" $ do
expectGilded
"if ! flag ( x )"
"if !flag(x)\n"
Hspec.it "formats or" $ do
expectGilded
"if flag ( x )||flag ( y )"
"if flag(x) || flag(y)\n"
Hspec.it "formats and" $ do
expectGilded
"if flag ( x )&&flag ( y )"
"if flag(x) && flag(y)\n"
Hspec.it "formats arch" $ do
expectGilded
"if arch ( aarch64 )"
"if arch(aarch64)\n"
Hspec.it "formats upper arch" $ do
expectGilded
"if arch ( AARCH64 )"
"if arch(aarch64)\n"
Hspec.it "formats arch alias" $ do
expectGilded
"if arch ( arm64 )"
"if arch(aarch64)\n"
Hspec.it "formats flag" $ do
expectGilded
"if flag ( x )"
"if flag(x)\n"
Hspec.it "formats upper flag" $ do
expectGilded
"if flag ( X )"
"if flag(x)\n"
Hspec.it "formats impl" $ do
expectGilded
"if impl ( ghc > 0 )"
"if impl(ghc >0)\n"
Hspec.it "formats upper impl" $ do
expectGilded
"if impl ( GHC > 0 )"
"if impl(ghc >0)\n"
Hspec.it "formats impl without version range" $ do
expectGilded
"if impl ( ghc )"
"if impl(ghc >=0)\n"
Hspec.it "formats os" $ do
expectGilded
"if os ( osx )"
"if os(osx)\n"
Hspec.it "formats upper os" $ do
expectGilded
"if os ( OSX )"
"if os(osx)\n"
Hspec.it "formats os alias" $ do
expectGilded
"if os ( darwin )"
"if os(osx)\n"
Hspec.it "does not format old elif" $ do
expectGilded
"elif flag ( x )"
"elif flag ( x )\n"
Hspec.it "formats new elif" $ do
expectGilded
"cabal-version: 2.2\nelif flag ( x )"
"cabal-version: 2.2\nelif flag(x)\n"
Hspec.it "keeps explicit parentheses" $ do
expectGilded
"if ( true )"
"if (true)\n"
Hspec.it "formats multiple nots" $ do
expectGilded
"if ! ! flag ( a )"
"if !!flag(a)\n"
Hspec.it "formats multiple ands" $ do
expectGilded
"if flag ( a ) && flag ( b ) && flag ( c )"
"if flag(a) && flag(b) && flag(c)\n"
Hspec.it "formats multiple ors" $ do
expectGilded
"if flag ( a ) || flag ( b ) || flag ( c )"
"if flag(a) || flag(b) || flag(c)\n"
Hspec.it "formats mixed operators" $ do
expectGilded
"if ! flag ( a ) && flag ( b ) || flag ( c )"
"if !flag(a) && flag(b) || flag(c)\n"
Hspec.describe "license-files" $ do
-- These tests apply to other "list" fields as well. The license-files
-- field is just a representative example.
Hspec.it "removes duplicates" $ do
expectGilded
"license-files: f f"
"license-files: f\n"
Hspec.it "sorts unquoted" $ do
expectGilded
"license-files: g f"
"license-files:\n f\n g\n"
Hspec.it "sorts mixed" $ do
expectGilded
"license-files: \"g\" f"
"license-files:\n f\n g\n"
Hspec.it "sorts quoted" $ do
expectGilded
"license-files: \"g\" \"f\""
"license-files:\n f\n g\n"
Hspec.it "sorts comma-separated" $ do
expectGilded
"license-files: g, f"
"license-files:\n f\n g\n"
Hspec.it "collects comments at the top" $ do
-- This isn't ideal, but keeping comments "close" to their original
-- identifier is very difficult.
expectGilded
"license-files:\n -- c\n g\n -- d\n f"
"license-files:\n -- c\n -- d\n f\n g\n"
Hspec.it "leaves invalid fields alone" $ do
-- This field value is valid in general. Cabal will reject it because the
-- license-files field has special rules. Perhaps the user is trying to
-- format something other than a package description. Or maybe they just
-- made a typo. Either way, we can leave it alone.
expectGilded
"license-files: , f ,"
"license-files: , f ,\n"
Hspec.it "sorts tested-with" $ do
expectGilded
"tested-with: GHC == 9.8.1 , GHC == 9.6.4"
"tested-with:\n ghc ==9.6.4\n ghc ==9.8.1\n"
Hspec.it "sorts data-files" $ do
expectGilded
"data-files: g f"
"data-files:\n f\n g\n"
Hspec.it "sorts extra-source-files" $ do
expectGilded
"extra-source-files: g f"
"extra-source-files:\n f\n g\n"
Hspec.it "sorts extra-tmp-files" $ do
expectGilded
"extra-tmp-files: g f"
"extra-tmp-files:\n f\n g\n"
Hspec.it "sorts extra-doc-files" $ do
expectGilded
"extra-doc-files: g f"
"extra-doc-files:\n f\n g\n"
Hspec.it "sorts exposed-modules" $ do
expectGilded
"library\n exposed-modules: N M"
"library\n exposed-modules:\n M\n N\n"
Hspec.it "sorts reexported-modules" $ do
expectGilded
"library\n reexported-modules: p:M4 as M5, M2 as M3, q:M6, M1"
"library\n reexported-modules:\n M1,\n M2 as M3,\n p:M4 as M5,\n q:M6\n"
Hspec.it "sorts signatures" $ do
expectGilded
"library\n signatures: N M"
"library\n signatures:\n M\n N\n"
Hspec.it "sorts build-tools" $ do
expectGilded
"library\n build-tools: q , p == 1"
"library\n build-tools:\n p ==1,\n q >=0\n"
Hspec.it "adds a trailing comma to build-tools when possible" $ do
expectGilded
"cabal-version: 2.2\nlibrary\n build-tools: p == 1, q == 2"
"cabal-version: 2.2\n\nlibrary\n build-tools:\n p ==1,\n q ==2,\n"
Hspec.it "does not add a trailing comma to build-tools with one element" $ do
expectGilded
"cabal-version: 2.2\nlibrary\n build-tools: p"
"cabal-version: 2.2\n\nlibrary\n build-tools: p >=0\n"
Hspec.it "sorts build-tool-depends" $ do
expectGilded
"library\n build-tool-depends: q:d , p:c == 1"
"library\n build-tool-depends:\n p:c ==1,\n q:d\n"
Hspec.it "sorts pkgconfig-depends" $ do
-- This unusual version range format appears to be a quirk of Cabal's
-- parser specifically for pkg-config.
expectGilded
"library\n pkgconfig-depends: q , p == 1"
"library\n pkgconfig-depends:\n p >=1 && <=1,\n q\n"
Hspec.it "sorts frameworks" $ do
expectGilded
"library\n frameworks: b a"
"library\n frameworks:\n a\n b\n"
Hspec.it "sorts extra-framework-dirs" $ do
expectGilded
"library\n extra-framework-dirs: e d"
"library\n extra-framework-dirs:\n d\n e\n"
Hspec.it "sorts asm-sources" $ do
expectGilded
"library\n asm-sources: g f"
"library\n asm-sources:\n f\n g\n"
Hspec.it "sorts cmm-sources" $ do
expectGilded
"library\n cmm-sources: g f"
"library\n cmm-sources:\n f\n g\n"
Hspec.it "sorts c-sources" $ do
expectGilded
"library\n c-sources: g f"
"library\n c-sources:\n f\n g\n"
Hspec.it "sorts cxx-sources" $ do
expectGilded
"library\n cxx-sources: g f"
"library\n cxx-sources:\n f\n g\n"
Hspec.it "sorts js-sources" $ do
expectGilded
"library\n js-sources: g f"
"library\n js-sources:\n f\n g\n"
Hspec.it "sorts other-modules" $ do
expectGilded
"library\n other-modules: N M"
"library\n other-modules:\n M\n N\n"
Hspec.it "sorts virtual-modules" $ do
expectGilded
"library\n virtual-modules: N M"
"library\n virtual-modules:\n M\n N\n"
Hspec.it "sorts autogen-modules" $ do
expectGilded
"library\n autogen-modules: N M"
"library\n autogen-modules:\n M\n N\n"
Hspec.it "sorts other-languages" $ do
expectGilded
"library\n other-languages: b a"
"library\n other-languages:\n a\n b\n"
Hspec.it "sorts default-extensions" $ do
expectGilded
"library\n default-extensions: b a"
"library\n default-extensions:\n a\n b\n"
Hspec.it "sorts known extensions by name" $ do
expectGilded
"library\n default-extensions: DerivingVia BlockArguments"
"library\n default-extensions:\n BlockArguments\n DerivingVia\n"
Hspec.it "does not sort disabling extensions after enabling ones" $ do
expectGilded
"library\n default-extensions: StrictData NoStrictData"
"library\n default-extensions:\n NoStrictData\n StrictData\n"
Hspec.it "sorts unknown extensions with known ones" $ do
expectGilded
"library\n default-extensions: LambdaCase Imaginary"
"library\n default-extensions:\n Imaginary\n LambdaCase\n"
Hspec.it "sorts unknown disabling extensions with known ones" $ do
expectGilded
"library\n default-extensions: NoLambdaCase NoImaginary"
"library\n default-extensions:\n NoImaginary\n NoLambdaCase\n"
Hspec.it "sorts extensions (issue 29)" $ do
expectGilded
"library\n default-extensions: Arrows Imaginary1 NoCPP NoImaginary2 NoUnboxedTuples UnicodeSyntax"
"library\n default-extensions:\n Arrows\n Imaginary1\n NoCPP\n NoImaginary2\n NoUnboxedTuples\n UnicodeSyntax\n"
Hspec.it "sorts other-extensions" $ do
expectGilded
"library\n other-extensions: b a"
"library\n other-extensions:\n a\n b\n"
Hspec.it "sorts extensions" $ do
expectGilded
"library\n extensions: b a"
"library\n extensions:\n a\n b\n"
Hspec.it "sorts build-depends" $ do
expectGilded
"library\n build-depends: q , p == 1"
"library\n build-depends:\n p ==1,\n q\n"
Hspec.it "adds a trailing comma to build-depends when possible" $ do
expectGilded
"cabal-version: 2.2\nlibrary\n build-depends: p == 1, q == 2"
"cabal-version: 2.2\n\nlibrary\n build-depends:\n p ==1,\n q ==2,\n"
Hspec.it "does not add a trailing comma to build-depends with one element" $ do
expectGilded
"cabal-version: 2.2\nlibrary\n build-depends: p"
"cabal-version: 2.2\n\nlibrary\n build-depends: p\n"
Hspec.it "sorts sub-libraries in build-depends" $ do
expectGilded
"cabal-version: 3.0\nlibrary\n build-depends: p:{b,a}"
"cabal-version: 3.0\n\nlibrary\n build-depends: p:{a, b}\n"
Hspec.it "removes duplicate options" $ do
-- This is kind of silly because there's only one possible foreign library
-- option.
expectGilded
"foreign-library x\n options: standalone standalone"
"foreign-library x\n options: standalone\n"
Hspec.it "does not sort hs-source-dirs" $ do
expectGilded
"library\n hs-source-dirs: b a"
"library\n hs-source-dirs:\n b\n a\n"
Hspec.it "does not sort code-generators" $ do
expectGilded
"test-suite x\n code-generators: b, a"
"test-suite x\n code-generators:\n b,\n a\n"
Hspec.it "adds a trailing comma to code-generators when possible" $ do
expectGilded
"cabal-version: 2.2\ntest-suite x\n code-generators: a, b"
"cabal-version: 2.2\n\ntest-suite x\n code-generators:\n a,\n b,\n"
Hspec.it "does not add a trailing comma to code-generators with one element" $ do
expectGilded
"cabal-version: 2.2\ntest-suite x\n code-generators: a"
"cabal-version: 2.2\n\ntest-suite x\n code-generators: a\n"
Hspec.it "does not sort cpp-options" $ do
expectGilded
"library\n cpp-options: b a"
"library\n cpp-options:\n b\n a\n"
Hspec.it "does not sort asm-options" $ do
expectGilded
"library\n asm-options: b a"
"library\n asm-options:\n b\n a\n"
Hspec.it "does not sort cmm-options" $ do
expectGilded
"library\n cmm-options: b a"
"library\n cmm-options:\n b\n a\n"
Hspec.it "does not sort cc-options" $ do
expectGilded
"library\n cc-options: b a"
"library\n cc-options:\n b\n a\n"
Hspec.it "does not sort cxx-options" $ do
expectGilded
"library\n cxx-options: b a"
"library\n cxx-options:\n b\n a\n"
Hspec.it "does not sort ld-options" $ do
expectGilded
"library\n ld-options: b a"
"library\n ld-options:\n b\n a\n"
Hspec.it "does not sort hsc2hs-options" $ do
expectGilded
"library\n hsc2hs-options: b a"
"library\n hsc2hs-options:\n b\n a\n"
Hspec.it "sorts extra-libraries" $ do
expectGilded
"library\n extra-libraries: b a"
"library\n extra-libraries:\n a\n b\n"
Hspec.it "sorts extra-libraries-static" $ do
expectGilded
"library\n extra-libraries-static: b a"
"library\n extra-libraries-static:\n a\n b\n"
Hspec.it "sorts extra-ghci-libraries" $ do
expectGilded
"library\n extra-ghci-libraries: b a"
"library\n extra-ghci-libraries:\n a\n b\n"
Hspec.it "sorts extra-bundled-libraries" $ do
expectGilded
"library\n extra-bundled-libraries: b a"
"library\n extra-bundled-libraries:\n a\n b\n"
Hspec.it "sorts extra-library-flavours" $ do
expectGilded