@@ -153,3 +153,81 @@ func TestEscaping(t *testing.T) {
153153 t .Errorf ("escapeProperty = %q" , got )
154154 }
155155}
156+
157+ const specFixture = `openapi: 3.0.0
158+ info:
159+ title: X
160+ version: 1.0.0
161+ paths:
162+ /products:
163+ get:
164+ operationId: getProducts
165+ responses:
166+ "200":
167+ description: ok
168+ /orders:
169+ post:
170+ operationId: placeOrder
171+ responses:
172+ "201":
173+ description: created
174+ `
175+
176+ func writeSpec (t * testing.T ) string {
177+ t .Helper ()
178+ p := filepath .Join (t .TempDir (), "spec.yaml" )
179+ if err := os .WriteFile (p , []byte (specFixture ), 0o644 ); err != nil {
180+ t .Fatal (err )
181+ }
182+ return p
183+ }
184+
185+ func TestOperationLine (t * testing.T ) {
186+ spec := writeSpec (t )
187+ cases := map [string ]int {
188+ "GET /products" : 7 , // line of "get:" under /products
189+ "POST /orders" : 13 , // line of "post:" under /orders
190+ "GET /nonexistent" : 0 ,
191+ "weird" : 0 , // no method/path split
192+ }
193+ for op , want := range cases {
194+ if got := operationLine (spec , op ); got != want {
195+ t .Errorf ("operationLine(%q) = %d, want %d" , op , got , want )
196+ }
197+ }
198+ if got := operationLine ("/no/such/file.yaml" , "GET /products" ); got != 0 {
199+ t .Errorf ("missing file = %d, want 0" , got )
200+ }
201+ }
202+
203+ func TestGitHubActionsFileLineAnnotation (t * testing.T ) {
204+ spec := writeSpec (t )
205+ result := & connectors.TestResult {
206+ Success : false ,
207+ TestCaseResults : []connectors.TestCaseResult {
208+ {Success : false , OperationName : "GET /products" , TestStepResults : []connectors.TestStepResult {
209+ {Success : false , RequestName : "r" , Message : "boom" },
210+ }},
211+ },
212+ }
213+ formatter , err := NewFormatter (FormatGitHubActions , WithArtifactPath (spec ))
214+ if err != nil {
215+ t .Fatal (err )
216+ }
217+ out , err := formatter .Format (result )
218+ if err != nil {
219+ t .Fatal (err )
220+ }
221+ for _ , want := range []string {"title=GET /products" , "file=" + spec , "line=7" } {
222+ if ! strings .Contains (out , want ) {
223+ t .Errorf ("annotation missing %q\n %s" , want , out )
224+ }
225+ }
226+
227+ // Without an artifact path, no file/line properties.
228+ plain , _ := NewFormatter (FormatGitHubActions )
229+ out2 , _ := plain .Format (result )
230+ if strings .Contains (out2 , "file=" ) || strings .Contains (out2 , "line=" ) {
231+ t .Errorf ("did not expect file/line without artifact path:\n %s" , out2 )
232+ }
233+ }
0 commit comments