-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tags to rules and the "checks" cmd (#586)
- Loading branch information
1 parent
6e06613
commit d2e44ea
Showing
6 changed files
with
449 additions
and
288 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package internal | ||
|
||
import "github.com/tufin/oasdiff/checker" | ||
|
||
func getAllTags() []string { | ||
return []string{"request", "response", "add", "remove", "change", "generalize", "specialize", "increase", "decrease", "set", "body", "parameters", "properties", "headers", "security", "components"} | ||
} | ||
|
||
// matchTags returns true if the rule matches all the tags | ||
func matchTags(tags []string, rule checker.BackwardCompatibilityRule) bool { | ||
if len(tags) == 0 { | ||
return true | ||
} | ||
|
||
for _, tag := range tags { | ||
if !matchTag(tag, rule) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
func matchTag(tag string, rule checker.BackwardCompatibilityRule) bool { | ||
if matchLocationTag(tag, rule.Location) { | ||
return true | ||
} | ||
|
||
if matchActionTag(tag, rule.Action) { | ||
return true | ||
} | ||
|
||
if matchDirectionTag(tag, rule.Direction) { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
func matchDirectionTag(tag string, direction checker.Direction) bool { | ||
switch tag { | ||
case "request": | ||
return direction == checker.DirectionRequest | ||
case "response": | ||
return direction == checker.DirectionResponse | ||
} | ||
|
||
return false | ||
} | ||
|
||
func matchActionTag(tag string, action checker.Action) bool { | ||
switch tag { | ||
case "add": | ||
return action == checker.ActionAdd | ||
case "remove": | ||
return action == checker.ActionRemove | ||
case "change": | ||
return action == checker.ActionChange | ||
case "generalize": | ||
return action == checker.ActionGeneralize | ||
case "specialize": | ||
return action == checker.ActionSpecialize | ||
case "increase": | ||
return action == checker.ActionIncrease | ||
case "decrease": | ||
return action == checker.ActionDecrease | ||
case "set": | ||
return action == checker.ActionSet | ||
} | ||
|
||
return false | ||
} | ||
|
||
func matchLocationTag(tag string, location checker.Location) bool { | ||
switch tag { | ||
case "body": | ||
return location == checker.LocationBody | ||
case "parameters": | ||
return location == checker.LocationParameters | ||
case "properties": | ||
return location == checker.LocationProperties | ||
case "headers": | ||
return location == checker.LocationHeaders | ||
case "security": | ||
return location == checker.LocationSecurity | ||
case "components": | ||
return location == checker.LocationComponents | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package internal_test | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/tufin/oasdiff/internal" | ||
) | ||
|
||
func Test_ChecksNoTags(t *testing.T) { | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru"), io.Discard, io.Discard)) | ||
} | ||
|
||
func Test_ChecksTagsDirection(t *testing.T) { | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags request"), io.Discard, io.Discard)) | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags response"), io.Discard, io.Discard)) | ||
} | ||
|
||
func Test_ChecksTagsAction(t *testing.T) { | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags add"), io.Discard, io.Discard)) | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags remove"), io.Discard, io.Discard)) | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags change"), io.Discard, io.Discard)) | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags generalize"), io.Discard, io.Discard)) | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags specialize"), io.Discard, io.Discard)) | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags increase"), io.Discard, io.Discard)) | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags decrease"), io.Discard, io.Discard)) | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags set"), io.Discard, io.Discard)) | ||
} | ||
|
||
func Test_ChecksTagsLocation(t *testing.T) { | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags body"), io.Discard, io.Discard)) | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags parameters"), io.Discard, io.Discard)) | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags properties"), io.Discard, io.Discard)) | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags headers"), io.Discard, io.Discard)) | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags security"), io.Discard, io.Discard)) | ||
require.Zero(t, internal.Run(cmdToArgs("oasdiff checks -l ru --tags components"), io.Discard, io.Discard)) | ||
} |