Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <wangxiaoxuan119@gmail.com>
  • Loading branch information
wangxiaoxuan273 committed Mar 14, 2024
1 parent a090126 commit d29dbab
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/oras/internal/option/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (opts *Common) parseTTY(f *os.File) error {
// path value.
func (opts *Common) UpdateTTY(flagPresent bool, toSTDOUT bool) {
ttyEnforced := flagPresent && !opts.noTTY
if toSTDOUT && !ttyEnforced {
if opts.noTTY || (toSTDOUT && !ttyEnforced) {
opts.TTY = nil
}
}
44 changes: 44 additions & 0 deletions cmd/oras/internal/option/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.
package option

import (
"os"
"reflect"
"testing"

"github.com/spf13/pflag"
Expand All @@ -28,3 +30,45 @@ func TestCommon_FlagsInit(t *testing.T) {

ApplyFlags(&test, pflag.NewFlagSet("oras-test", pflag.ExitOnError))
}

func TestCommon_UpdateTTY(t *testing.T) {
testTTY := &os.File{}
tests := []struct {
name string
flagPresent bool
toSTDOUT bool
noTTY bool
expectedTTY *os.File
}{
{
"output path == -, --no-tty flag not used", false, true, false, nil,
},
{
"output path == -, --no-tty", true, true, true, nil,
},
{
"output path == -, --no-tty=false", true, true, false, testTTY,
},
{
"output path != -, --no-tty flag not used", false, false, false, testTTY,
},
{
"output path != -, --no-tty", true, false, true, nil,
},
{
"output path != -, --no-tty=false", true, false, false, testTTY,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := &Common{
noTTY: tt.noTTY,
TTY: testTTY,
}
opts.UpdateTTY(tt.flagPresent, tt.toSTDOUT)
if !reflect.DeepEqual(opts.TTY, tt.expectedTTY) {
t.Fatalf("tt.TTY got %v, want %v", opts.TTY, tt.expectedTTY)
}
})
}
}

0 comments on commit d29dbab

Please sign in to comment.