Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <wangxiaoxuan119@gmail.com>
  • Loading branch information
wangxiaoxuan273 committed Apr 18, 2024
1 parent 245ac1e commit c91ec41
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
7 changes: 4 additions & 3 deletions cmd/oras/internal/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,17 @@ func CheckMutuallyExclusiveFlags(fs *pflag.FlagSet, exclusiveFlagSets ...[]strin
return nil
}

// CheckRequiredTogetherFlags checks if any flags required together are all set,
// returns an error when detecting any of the required flags not set.
// CheckRequiredTogetherFlags checks if any flags required together are all used,
// returns an error when detecting any flags not used while other flags have been used.
func CheckRequiredTogetherFlags(fs *pflag.FlagSet, requiredTogetherFlags ...string) error {
var unchangedFlags []string
for _, flagName := range requiredTogetherFlags {
if !fs.Changed(flagName) {
unchangedFlags = append(unchangedFlags, fmt.Sprintf("--%s", flagName))
}
}
if len(unchangedFlags) > 0 {
nUnchangedFlags := len(unchangedFlags)
if nUnchangedFlags > 0 && nUnchangedFlags != len(requiredTogetherFlags) {
flags := strings.Join(unchangedFlags, ", ")
return fmt.Errorf("%s required but not provided", flags)
}
Expand Down
55 changes: 55 additions & 0 deletions cmd/oras/internal/errors/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package errors

import (
"testing"

"github.com/spf13/pflag"
)

func TestCheckRequiredTogetherFlags(t *testing.T) {
fs := &pflag.FlagSet{}
var foo, bar, hello bool
fs.BoolVar(&foo, "foo", false, "foo test")
fs.BoolVar(&bar, "bar", false, "bar test")
fs.BoolVar(&hello, "hello", false, "hello test")
fs.Lookup("foo").Changed = true
fs.Lookup("bar").Changed = true
tests := []struct {
name string
requiredTogetherFlags []string
wantErr bool
}{
{
"--foo and --bar are both used, no error is returned",
[]string{"foo", "bar"},
false,
},
{
"--foo and --hello are not both used, an error is returned",
[]string{"foo", "hello"},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CheckRequiredTogetherFlags(fs, tt.requiredTogetherFlags...); (err != nil) != tt.wantErr {
t.Errorf("CheckRequiredTogetherFlags() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit c91ec41

Please sign in to comment.