@@ -19,10 +19,167 @@ limitations under the License.
1919-- Copyright: Copyright 2025 Google LLC
2020-- License: Apache-2.0
212122- module SpecialOutputSpec ( spec ) where
22+ module SpecialOutputSpec where
2323
24+ import Data.Aeson hiding (Error )
25+ import Data.Aeson.KeyMap qualified as KeyMap
26+ import Data.Vector qualified as Vector
27+ import SpecialOutput
28+ import System.Exit
2429import Test.Hspec
2530
2631spec :: Spec
2732spec = parallel $ do
28- it " is pending" pending
33+ it " outputs minimal annotation" $
34+ let v = encode $ Object $ KeyMap. singleton " runs" runs
35+ runs = Array $ Vector. singleton run
36+ run = Object $ KeyMap. singleton " results" results
37+ results = Array $ Vector. singleton result
38+ result =
39+ Object $
40+ KeyMap. fromList
41+ [ (" level" , " error" ),
42+ (" ruleId" , " redundant entity" ),
43+ (" message" , Object $ KeyMap. singleton " text" " random comment" )
44+ ]
45+ in output Never v `shouldBe` (" ::error title=redundant entity::random comment\n " , ExitSuccess )
46+
47+ it " outputs annotation with some location information" $
48+ let v = encode $ Object $ KeyMap. singleton " runs" runs
49+ runs = Array $ Vector. singleton run
50+ run = Object $ KeyMap. singleton " results" results
51+ results = Array $ Vector. singleton result
52+ result =
53+ Object $
54+ KeyMap. fromList
55+ [ (" level" , " error" ),
56+ (" ruleId" , " redundant entity" ),
57+ (" message" , Object $ KeyMap. singleton " text" " random comment" ),
58+ (" locations" , Array $ Vector. singleton location)
59+ ]
60+ location = Object $ KeyMap. singleton " physicalLocation" physicalLocation
61+ physicalLocation = Object $ KeyMap. singleton " artifactLocation" artifactLocation
62+ artifactLocation = Object $ KeyMap. singleton " uri" " SpecialOutput.hs"
63+ in output Never v
64+ `shouldBe` (" ::error file=SpecialOutput.hs,title=redundant entity::random comment\n " , ExitSuccess )
65+
66+ it " outputs annotation with full location information" $
67+ let v = encode $ Object $ KeyMap. singleton " runs" runs
68+ runs = Array $ Vector. singleton run
69+ run = Object $ KeyMap. singleton " results" results
70+ results = Array $ Vector. singleton result
71+ result =
72+ Object $
73+ KeyMap. fromList
74+ [ (" level" , " error" ),
75+ (" ruleId" , " redundant entity" ),
76+ (" message" , Object $ KeyMap. singleton " text" " random comment" ),
77+ (" locations" , Array $ Vector. singleton location)
78+ ]
79+ location = Object $ KeyMap. singleton " physicalLocation" physicalLocation
80+ physicalLocation =
81+ Object $
82+ KeyMap. fromList
83+ [ (" artifactLocation" , artifactLocation),
84+ ( " region" ,
85+ Object $
86+ KeyMap. fromList
87+ [ (" startColumn" , Number 12 ),
88+ (" endColumn" , Number 20 ),
89+ (" startLine" , Number 1020 ),
90+ (" endLine" , Number 1025 )
91+ ]
92+ )
93+ ]
94+ artifactLocation = Object $ KeyMap. singleton " uri" " ./SpecialOutput.hs"
95+ in output Never v
96+ `shouldBe` ( mconcat
97+ [ " ::error " ,
98+ " file=SpecialOutput.hs," ,
99+ " col=12," ,
100+ " endColumn=20," ,
101+ " line=1020," ,
102+ " endLine=1025," ,
103+ " title=redundant entity::" ,
104+ " random comment\n "
105+ ],
106+ ExitSuccess
107+ )
108+
109+ it " escapes newlines in messages" $
110+ let v = encode $ Object $ KeyMap. singleton " runs" runs
111+ runs = Array $ Vector. singleton run
112+ run = Object $ KeyMap. singleton " results" results
113+ results = Array $ Vector. singleton result
114+ result =
115+ Object $
116+ KeyMap. fromList
117+ [ (" level" , " error" ),
118+ (" ruleId" , " redundant entity" ),
119+ (" message" , Object $ KeyMap. singleton " text" " random\n comment:2=2" )
120+ ]
121+ in output Never v
122+ `shouldBe` (" ::error title=redundant entity::random%0Acomment:2=2\n " , ExitSuccess )
123+
124+ it " escapes special characters" $
125+ let v = encode $ Object $ KeyMap. singleton " runs" runs
126+ runs = Array $ Vector. singleton run
127+ run = Object $ KeyMap. singleton " results" results
128+ results = Array $ Vector. singleton result
129+ result =
130+ Object $
131+ KeyMap. fromList
132+ [ (" level" , " error" ),
133+ (" ruleId" , " redundant entity\n :=" ),
134+ (" message" , Object $ KeyMap. singleton " text" " random comment" ),
135+ (" locations" , Array $ Vector. singleton location)
136+ ]
137+ location = Object $ KeyMap. singleton " physicalLocation" physicalLocation
138+ physicalLocation = Object $ KeyMap. singleton " artifactLocation" artifactLocation
139+ artifactLocation = Object $ KeyMap. singleton " uri" " ./SpecialOutput.hs\n :="
140+ in output Never v
141+ `shouldBe` ( mconcat
142+ [ " ::error " ,
143+ " file=SpecialOutput.hs%0A%3A%3D," ,
144+ " title=redundant entity%0A%3A%3D::" ,
145+ " random comment\n "
146+ ],
147+ ExitSuccess
148+ )
149+
150+ let sarif levels = encode $ Object $ KeyMap. singleton " runs" runs
151+ where
152+ runs = Array $ Vector. singleton run
153+ run = Object $ KeyMap. singleton " results" $ results levels
154+ results levels = Array $ Vector. fromList $ map result levels
155+ result level =
156+ Object $
157+ KeyMap. fromList
158+ [ (" level" , level),
159+ (" ruleId" , " a" ),
160+ (" message" , Object $ KeyMap. singleton " text" " b" )
161+ ]
162+ in describe " exit code" $ do
163+ it " never : [error]" $
164+ snd (output Never $ sarif [" error" ]) `shouldBe` ExitSuccess
165+
166+ it " error : [note, warning]" $
167+ snd (output Error $ sarif [" note" , " warning" ]) `shouldBe` ExitSuccess
168+
169+ it " error : [note, warning, error]" $
170+ snd (output Error $ sarif [" note" , " warning" , " error" ]) `shouldBe` ExitFailure 1
171+
172+ it " warning : [note, note]" $
173+ snd (output Warning $ sarif [" note" , " note" ]) `shouldBe` ExitSuccess
174+
175+ it " warning : [note, warning]" $
176+ snd (output Warning $ sarif [" note" , " warning" ]) `shouldBe` ExitFailure 1
177+
178+ it " note : []" $
179+ snd (output Note $ sarif [] ) `shouldBe` ExitSuccess
180+
181+ it " note : [note]" $
182+ snd (output Note $ sarif [" note" ]) `shouldBe` ExitFailure 1
183+
184+ it " note : [warning]" $
185+ snd (output Note $ sarif [" warning" ]) `shouldBe` ExitFailure 1
0 commit comments