|
| 1 | +//go:build freebsd || linux || netbsd || openbsd || solaris |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright The ORAS Authors. |
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package option |
| 19 | + |
| 20 | +import ( |
| 21 | + "os" |
| 22 | + "reflect" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/spf13/cobra" |
| 26 | + "oras.land/oras/internal/testutils" |
| 27 | +) |
| 28 | + |
| 29 | +func TestTerminal_Parse(t *testing.T) { |
| 30 | + _, device, err := testutils.NewPty() |
| 31 | + if err != nil { |
| 32 | + t.Fatal(err) |
| 33 | + } |
| 34 | + defer device.Close() |
| 35 | + opts := Terminal{ |
| 36 | + noTTY: false, |
| 37 | + TTY: device, |
| 38 | + } |
| 39 | + // TTY output |
| 40 | + if err := opts.Parse(&cobra.Command{}); err != nil { |
| 41 | + t.Errorf("unexpected error with TTY output: %v", err) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestTerminal_DisableTTY(t *testing.T) { |
| 46 | + testTTY := &os.File{} |
| 47 | + tests := []struct { |
| 48 | + name string |
| 49 | + debug bool |
| 50 | + toSTDOUT bool |
| 51 | + noTTY bool |
| 52 | + ttyEnforced bool |
| 53 | + expectedTTY *os.File |
| 54 | + }{ |
| 55 | + { |
| 56 | + "output to STDOUT, --no-tty flag not used, reset TTY", false, false, true, false, nil, |
| 57 | + }, |
| 58 | + { |
| 59 | + "output to STDOUT, --no-tty set to true, reset TTY", false, true, true, true, nil, |
| 60 | + }, |
| 61 | + { |
| 62 | + "output to STDOUT, --no-tty set to false", false, true, false, true, testTTY, |
| 63 | + }, |
| 64 | + { |
| 65 | + "not output to STDOUT, --no-tty flag not used", false, false, false, false, testTTY, |
| 66 | + }, |
| 67 | + { |
| 68 | + "not output to STDOUT, --no-tty set to true, reset TTY", false, false, true, false, nil, |
| 69 | + }, |
| 70 | + { |
| 71 | + "not output to STDOUT, --no-tty set to false", false, false, false, true, testTTY, |
| 72 | + }, |
| 73 | + { |
| 74 | + "debug enabled, --no-tty flag is not used", true, false, false, false, nil, |
| 75 | + }, |
| 76 | + } |
| 77 | + for _, tt := range tests { |
| 78 | + t.Run(tt.name, func(t *testing.T) { |
| 79 | + opts := &Terminal{ |
| 80 | + TTY: testTTY, |
| 81 | + noTTY: tt.noTTY, |
| 82 | + ttyEnforced: tt.ttyEnforced, |
| 83 | + } |
| 84 | + opts.DisableTTY(tt.debug, tt.toSTDOUT) |
| 85 | + if !reflect.DeepEqual(opts.TTY, tt.expectedTTY) { |
| 86 | + t.Fatalf("tt.TTY got %v, want %v", opts.TTY, tt.expectedTTY) |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
0 commit comments