Skip to content

Commit 110bdb7

Browse files
committed
Add tests for SpecialOutput.
1 parent 71b2506 commit 110bdb7

File tree

2 files changed

+169
-12
lines changed

2 files changed

+169
-12
lines changed

src/SpecialOutput.hs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ toAnnotation v = do
140140
ruleId _ = Nothing
141141

142142
-- | Returns the annotations for the location in a result object.
143-
-- If there are any location annotations, the return value will end with @", "@,
143+
-- If there are any location annotations, the return value will end with @","@,
144144
-- otherwise the return value will be empty.
145145
locationAnnotation :: Object -> Text
146146
locationAnnotation v =
@@ -153,23 +153,23 @@ locationAnnotation v =
153153
]
154154
where
155155
fileAnnotation
156-
| (Just s) <- filename = "file=" <> escapeSpecial s <> ", "
156+
| (Just s) <- filename = "file=" <> escapeSpecial s <> ","
157157
| otherwise = ""
158158

159159
colAnnotation
160-
| (Just n) <- col = "col=" <> pack (show n) <> ", "
160+
| (Just n) <- col = "col=" <> pack (show n) <> ","
161161
| otherwise = ""
162162

163163
endColumnAnnotation
164-
| (Just n) <- endColumn = "endColumn=" <> pack (show n) <> ", "
164+
| (Just n) <- endColumn = "endColumn=" <> pack (show n) <> ","
165165
| otherwise = ""
166166

167167
lineAnnotation
168-
| (Just n) <- line = "line=" <> pack (show n) <> ", "
168+
| (Just n) <- line = "line=" <> pack (show n) <> ","
169169
| otherwise = ""
170170

171171
endLineAnnotation
172-
| (Just n) <- endLine = "endLine=" <> pack (show n) <> ", "
172+
| (Just n) <- endLine = "endLine=" <> pack (show n) <> ","
173173
| otherwise = ""
174174

175175
locations
@@ -193,19 +193,19 @@ locationAnnotation v =
193193
| otherwise = Nothing
194194

195195
col
196-
| Just (Number n) <- lookup "startColumn" =<< region = Just n
196+
| Just (Number n) <- lookup "startColumn" =<< region = Just (round n :: Int)
197197
| otherwise = Nothing
198198

199199
endColumn
200-
| Just (Number n) <- lookup "endColumn" =<< region = Just n
200+
| Just (Number n) <- lookup "endColumn" =<< region = Just (round n :: Int)
201201
| otherwise = Nothing
202202

203203
line
204-
| Just (Number n) <- lookup "startLine" =<< region = Just n
204+
| Just (Number n) <- lookup "startLine" =<< region = Just (round n :: Int)
205205
| otherwise = Nothing
206206

207207
endLine
208-
| Just (Number n) <- lookup "endLine" =<< region = Just n
208+
| Just (Number n) <- lookup "endLine" =<< region = Just (round n :: Int)
209209
| otherwise = Nothing
210210

211211
-- | Replace newlines in output so that they can be treated as newlines

test/SpecialOutputSpec.hs

Lines changed: 159 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,167 @@ limitations under the License.
1919
-- Copyright: Copyright 2025 Google LLC
2020
-- License: Apache-2.0
2121
-- Maintainer: [email protected]
22-
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
2429
import Test.Hspec
2530

2631
spec :: Spec
2732
spec = 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\ncomment: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

Comments
 (0)