Skip to content

Commit

Permalink
bugfix: fix ignore logic (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
blva authored May 9, 2023
1 parent 1a9dc73 commit c33aec3
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 3 deletions.
2 changes: 1 addition & 1 deletion BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Sometimes, you may want to ignore certain breaking changes.
The new Breaking Changes method allows you define breaking changes that you want to ignore in a configuration file.
You can specify the configuration file name in the oasdiff command-line with the `-warn-ignore` flag for WARNINGS or the `-err-ignore` flag for ERRORS.
Each line in the configuration file should contain two parts:
1. method and path
1. method and path (the first field in the line beginning with slash)
2. description of the breaking change

For example:
Expand Down
28 changes: 26 additions & 2 deletions checker/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ import (
"github.com/TwiN/go-color"
)

func ignoreLinePath(ignoreLine string) string {
ignoreComponents := strings.Fields(ignoreLine)
pathIndex := -1

for i, component := range ignoreComponents {
if strings.HasPrefix(component, "/") {
pathIndex = i
break
}
}

if pathIndex == -1 {
return ""
}

return ignoreComponents[pathIndex]
}

func ProcessIgnoredBackwardCompatibilityErrors(level int, errs []BackwardCompatibilityError, ignoreFile string) ([]BackwardCompatibilityError, error) {
result := make([]BackwardCompatibilityError, 0)

Expand All @@ -21,6 +39,12 @@ func ProcessIgnoredBackwardCompatibilityErrors(level int, errs []BackwardCompati
ignoredErrs := make([]bool, len(errs))
for ignoreScanner.Scan() {
ignoreLine := strings.ToLower(ignoreScanner.Text())

ignorePath := ignoreLinePath(ignoreLine)
if ignorePath == "" {
continue
}

for errIndex, err := range errs {
if err.Level != level {
continue
Expand All @@ -29,10 +53,10 @@ func ProcessIgnoredBackwardCompatibilityErrors(level int, errs []BackwardCompati
uncolorizedText := strings.ReplaceAll(err.Text, color.Bold, "")
uncolorizedText = strings.ReplaceAll(uncolorizedText, color.Reset, "")

if strings.Contains(ignoreLine, strings.ToLower(err.Operation+" "+err.Path)) &&
if ignorePath == strings.ToLower(err.Path) &&
strings.Contains(ignoreLine, strings.ToLower(err.Operation+" "+err.Path)) &&
strings.Contains(ignoreLine, strings.ToLower(uncolorizedText)) {
ignoredErrs[errIndex] = true
break
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions checker/ignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,32 @@ func TestIgnore(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 5, len(errs))
}

func TestIgnoreSubpath(t *testing.T) {
s1 := l(t, 6)
s2 := l(t, 7)

d, osm, err := diff.GetWithOperationsSourcesMap(&diff.Config{}, &s1, &s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(checker.GetDefaultChecks(), d, osm)
require.Equal(t, 3, len(errs))

errs, err = checker.ProcessIgnoredBackwardCompatibilityErrors(checker.ERR, errs, "../data/ignore-err-example-2.txt")
require.NoError(t, err)
require.Equal(t, 0, len(errs))
}

func TestIgnoreOnlyIncludedSubpaths(t *testing.T) {
s1 := l(t, 8)
s2 := l(t, 7)

d, osm, err := diff.GetWithOperationsSourcesMap(&diff.Config{}, &s1, &s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(checker.GetDefaultChecks(), d, osm)
require.Equal(t, 2, len(errs)) // detect new and newest were deleted

errs, err = checker.ProcessIgnoredBackwardCompatibilityErrors(checker.ERR, errs, "../data/ignore-err-example-3.txt")
require.NoError(t, err)
require.Equal(t, 1, len(errs))
require.Contains(t, errs[0].Path, "/resource/new") //see that new breaking change was kept even though it is a substring of newest
}
3 changes: 3 additions & 0 deletions data/ignore-err-example-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
API get /health api path removed without deprecation
get /health/live api path removed without deprecation
get /health/ready api path removed without deprecation
1 change: 1 addition & 0 deletions data/ignore-err-example-3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
get /resource/newest api path removed without deprecation
23 changes: 23 additions & 0 deletions data/openapi-test6.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
openapi: "3.0.0"
info:
title: "Test API"
version: "1.0.0"
paths:
/health:
get:
summary: "Get health status"
responses:
200:
description: "Success"
/health/live:
get:
summary: "Get live health status"
responses:
200:
description: "Success"
/health/ready:
get:
summary: "Get readiness status"
responses:
200:
description: "Success"
5 changes: 5 additions & 0 deletions data/openapi-test7.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
openapi: "3.0.0"
info:
title: "Test API"
version: "2.0.0"
paths: {}
17 changes: 17 additions & 0 deletions data/openapi-test8.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
openapi: "3.0.0"
info:
title: "Test API"
version: "1.0.0"
paths:
/resource/new:
get:
summary: "Get new resource"
responses:
200:
description: "Success"
/resource/newest:
get:
summary: "Get newest resource"
responses:
200:
description: "Success"

0 comments on commit c33aec3

Please sign in to comment.