From 30196c056d04725868a90133b2a9a77b55d98142 Mon Sep 17 00:00:00 2001 From: Reuven Harrison Date: Tue, 27 Aug 2024 13:18:23 +0300 Subject: [PATCH] fix 'p' shortcut for match-path (#596) --- data/path-filter/base.yaml | 15 +++++++++++++++ data/path-filter/revision.yaml | 19 +++++++++++++++++++ internal/cmd_flags.go | 6 +++--- internal/run_test.go | 18 +++++++++++++++++- 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 data/path-filter/base.yaml create mode 100644 data/path-filter/revision.yaml diff --git a/data/path-filter/base.yaml b/data/path-filter/base.yaml new file mode 100644 index 00000000..ca6bef8c --- /dev/null +++ b/data/path-filter/base.yaml @@ -0,0 +1,15 @@ +openapi: 3.0.1 +info: + title: Test API + version: v1 +paths: + /a: + get: + responses: + "200": + description: OK + /b: + get: + responses: + "200": + description: OK diff --git a/data/path-filter/revision.yaml b/data/path-filter/revision.yaml new file mode 100644 index 00000000..5a929d9b --- /dev/null +++ b/data/path-filter/revision.yaml @@ -0,0 +1,19 @@ +openapi: 3.0.1 +info: + title: Test API + version: v1 +paths: + /a: + get: + tags: + - Test + responses: + "200": + description: OK + /b: + get: + tags: + - Test + responses: + "200": + description: OK diff --git a/internal/cmd_flags.go b/internal/cmd_flags.go index f8a81d30..0909bb11 100644 --- a/internal/cmd_flags.go +++ b/internal/cmd_flags.go @@ -9,7 +9,7 @@ import ( func addCommonDiffFlags(cmd *cobra.Command) { cmd.PersistentFlags().BoolP("composed", "c", false, "work in 'composed' mode, compare paths in all specs matching base and revision globs") - cmd.PersistentFlags().String("match-path", "p", "include only paths that match this regular expression") + cmd.PersistentFlags().StringP("match-path", "p", "", "include only paths that match this regular expression") cmd.PersistentFlags().String("filter-extension", "", "exclude paths and operations with an OpenAPI Extension matching this regular expression") cmd.PersistentFlags().String("prefix-base", "", "add this prefix to paths in base-spec before comparison") cmd.PersistentFlags().String("prefix-revision", "", "add this prefix to paths in revised-spec before comparison") @@ -28,7 +28,7 @@ func addCommonDiffFlags(cmd *cobra.Command) { // --flatten was replaced by --flatten-allof // we still accept --flatten as a synonym for --flatten-allof to avoid breaking existing scripts func addHiddenFlattenFlag(cmd *cobra.Command) { - cmd.PersistentFlags().BoolP("flatten", "", false, "merge subschemas under allOf before diff") + cmd.PersistentFlags().Bool("flatten", false, "merge subschemas under allOf before diff") hideFlag(cmd, "flatten") } @@ -36,7 +36,7 @@ func addHiddenFlattenFlag(cmd *cobra.Command) { // --max-circular-dep is no longer needed because kin-openapi3 handles circular references automatically since https://github.com/getkin/kin-openapi/pull/970 // we still accept --max-circular-dep to avoid breaking existing scripts, but we ignore this flag func addHiddenCircularDepFlag(cmd *cobra.Command) { - cmd.PersistentFlags().IntP("max-circular-dep", "", 5, "maximum allowed number of circular dependencies between objects in OpenAPI specs") + cmd.PersistentFlags().Int("max-circular-dep", 5, "maximum allowed number of circular dependencies between objects in OpenAPI specs") hideFlag(cmd, "max-circular-dep") } diff --git a/internal/run_test.go b/internal/run_test.go index 11e7177c..18667987 100644 --- a/internal/run_test.go +++ b/internal/run_test.go @@ -252,7 +252,7 @@ func Test_ChangelogWithAttributes(t *testing.T) { require.Zero(t, internal.Run(cmdToArgs("oasdiff changelog ../data/openapi-test1.yaml ../data/openapi-test3.yaml --attributes x-beta,x-extension-test -f yaml"), &stdout, io.Discard)) cl := formatters.Changes{} require.NoError(t, yaml.Unmarshal(stdout.Bytes(), &cl)) - require.Len(t, cl, 19) + require.Len(t, cl, 21) require.Equal(t, map[string]interface{}{"x-beta": true, "x-extension-test": interface{}(nil)}, cl[12].Attributes) } @@ -343,3 +343,19 @@ func Test_CustomSeverityLevels(t *testing.T) { func Test_CustomSeverityLevelsInvalidFile(t *testing.T) { require.Equal(t, 106, internal.Run(cmdToArgs("oasdiff changelog ../data/openapi-test1.yaml ../data/openapi-test3.yaml --severity-levels ../data/invalid.txt"), io.Discard, io.Discard)) } + +func Test_Changelog_WithoutPathFilter(t *testing.T) { + var stdout bytes.Buffer + require.Zero(t, internal.Run(cmdToArgs("oasdiff changelog ../data/path-filter/base.yaml ../data/path-filter/revision.yaml --format json"), &stdout, io.Discard)) + bc := formatters.Changes{} + require.NoError(t, json.Unmarshal(stdout.Bytes(), &bc)) + require.Len(t, bc, 2) +} + +func Test_Changelog_WithPathFilter(t *testing.T) { + var stdout bytes.Buffer + require.Zero(t, internal.Run(cmdToArgs("oasdiff changelog ../data/path-filter/base.yaml ../data/path-filter/revision.yaml --format json -p a"), &stdout, io.Discard)) + bc := formatters.Changes{} + require.NoError(t, json.Unmarshal(stdout.Bytes(), &bc)) + require.Len(t, bc, 1) +}