Skip to content

Commit a6b4c1c

Browse files
committed
Adding regressions tests for #153
1 parent 8facf74 commit a6b4c1c

4 files changed

Lines changed: 107 additions & 0 deletions

File tree

cmd/html_report_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"testing"
1313

14+
whatChangedModel "github.com/pb33f/libopenapi/what-changed/model"
1415
htmlReport "github.com/pb33f/openapi-changes/html-report"
1516
"github.com/pb33f/openapi-changes/model"
1617
"github.com/stretchr/testify/assert"
@@ -273,6 +274,27 @@ func TestBuildHTMLReportItems_SplitsStandardAndChangeExplorerGraphs(t *testing.T
273274
assert.Contains(t, explorerNodeWithChildChanges, "childChanges", "change explorer nodes render change summaries")
274275
}
275276

277+
func TestBuildHTMLReportItems_ComposedSchemaTitleRemovalPreservesPropertyLabel(t *testing.T) {
278+
leftPath, rightPath := createComposedSchemaTitleRemovalSpecPair(t, "allOf")
279+
280+
commits, err := loadLeftRightCommits(leftPath, rightPath, summaryOpts{noColor: true})
281+
require.NoError(t, err)
282+
require.Len(t, commits, 1)
283+
284+
items, err := buildHTMLReportItems(commits, nil)
285+
require.NoError(t, err)
286+
require.Len(t, items, 1)
287+
288+
var changes []*whatChangedModel.Change
289+
require.NoError(t, json.Unmarshal(items[0].Graph.Changes, &changes))
290+
require.Len(t, changes, 1)
291+
292+
assert.Equal(t, "title", changes[0].Property)
293+
assert.Equal(t, "schema", changes[0].Type)
294+
assert.Equal(t, "$.components.schemas['Contract']", changes[0].Path)
295+
assert.NotContains(t, string(items[0].Graph.Changes), `"property":"anyOf"`)
296+
}
297+
276298
type newHtmlNodeGraph struct {
277299
Nodes []map[string]any
278300
Root map[string]any

cmd/report_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,25 @@ func TestRunLeftRightReport_NormalizesParameterPaths(t *testing.T) {
264264
assert.Contains(t, content, `"type":"parameter"`)
265265
}
266266

267+
func TestRunLeftRightReport_ComposedSchemaTitleRemovalDoesNotMislabelAsAnyOf(t *testing.T) {
268+
leftPath, rightPath := createComposedSchemaTitleRemovalSpecPair(t, "allOf")
269+
270+
report, err := runLeftRightReport(leftPath, rightPath, summaryOpts{noColor: true}, nil)
271+
272+
require.NoError(t, err)
273+
require.NotNil(t, report)
274+
require.Len(t, report.Changes, 1)
275+
276+
change := report.Changes[0]
277+
assert.Equal(t, "title", change.Property)
278+
assert.Equal(t, "schema", change.Type)
279+
assert.Equal(t, "$.components.schemas['Contract']", change.Path)
280+
281+
encoded, err := json.Marshal(report)
282+
require.NoError(t, err)
283+
assert.NotContains(t, string(encoded), `"property":"anyOf"`)
284+
}
285+
267286
func TestReportCommand_GitRefUsesLeftRightMode(t *testing.T) {
268287
wd, err := os.Getwd()
269288
require.NoError(t, err)

cmd/summary_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,29 @@ func TestRenderSummary_GitRefExplodedSpecDetectsSiblingChanges(t *testing.T) {
404404
assert.Contains(t, output, "Breaking Highlights")
405405
}
406406

407+
func TestRenderSummary_ComposedSchemaTitleRemovalDoesNotMislabelAsAnyOf(t *testing.T) {
408+
leftPath, rightPath := createComposedSchemaTitleRemovalSpecPair(t, "allOf")
409+
410+
commits, err := loadLeftRightCommits(leftPath, rightPath, summaryOpts{noColor: true})
411+
require.NoError(t, err)
412+
require.Len(t, commits, 1)
413+
414+
output, hasBreaking, hasChanges, err := renderSummary(
415+
commits,
416+
nil,
417+
false,
418+
true,
419+
false,
420+
summaryStyles{},
421+
)
422+
423+
require.NoError(t, err)
424+
assert.True(t, hasChanges)
425+
assert.False(t, hasBreaking)
426+
assert.Contains(t, output, "title")
427+
assert.NotContains(t, output, "anyOf")
428+
}
429+
407430
func TestLoadLeftRightCommits_ReturnsHTTPStatusErrors(t *testing.T) {
408431
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
409432
http.Error(w, "missing", http.StatusNotFound)

cmd/test_helpers_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package cmd
55

66
import (
7+
"fmt"
78
"os"
89
"os/exec"
910
"path/filepath"
@@ -225,6 +226,48 @@ paths:
225226
return leftPath, rightPath
226227
}
227228

229+
func createComposedSchemaTitleRemovalSpecPair(t *testing.T, keyword string) (string, string) {
230+
t.Helper()
231+
232+
left := fmt.Sprintf(`openapi: 3.1.0
233+
info:
234+
title: Contracts API
235+
version: "1.0"
236+
paths: {}
237+
components:
238+
schemas:
239+
Contract:
240+
title: Contract
241+
%s:
242+
- type: object
243+
properties:
244+
id:
245+
type: string
246+
`, keyword)
247+
248+
right := fmt.Sprintf(`openapi: 3.1.0
249+
info:
250+
title: Contracts API
251+
version: "1.0"
252+
paths: {}
253+
components:
254+
schemas:
255+
Contract:
256+
%s:
257+
- type: object
258+
properties:
259+
id:
260+
type: string
261+
`, keyword)
262+
263+
baseDir := t.TempDir()
264+
leftPath := filepath.Join(baseDir, "left.yaml")
265+
rightPath := filepath.Join(baseDir, "right.yaml")
266+
require.NoError(t, os.WriteFile(leftPath, []byte(left), 0o644))
267+
require.NoError(t, os.WriteFile(rightPath, []byte(right), 0o644))
268+
return leftPath, rightPath
269+
}
270+
228271
func runGitInDir(t *testing.T, dir string, args ...string) {
229272
t.Helper()
230273

0 commit comments

Comments
 (0)