...

Source file src/github.com/sigstore/cosign/v2/internal/ui/log_test.go

Documentation: github.com/sigstore/cosign/v2/internal/ui

     1  // Copyright 2023 The Sigstore Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package ui_test
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  
    20  	"github.com/sigstore/cosign/v2/internal/ui"
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  type testCase struct {
    25  	name     string
    26  	input    string
    27  	args     []any
    28  	expected string
    29  }
    30  
    31  func TestInfof(t *testing.T) {
    32  	cases := []testCase{
    33  		{"basic", "foo", nil, "foo\n"},
    34  		{"multiline", "foo\nbar", nil, "foo\nbar\n"},
    35  		{"fmt", "foo: %v", []any{"bar"}, "foo: bar\n"},
    36  	}
    37  	for _, tc := range cases {
    38  		stderr := ui.RunWithTestCtx(func(ctx context.Context, _ ui.WriteFunc) {
    39  			ui.Infof(ctx, tc.input, tc.args...)
    40  		})
    41  		assert.Equal(t, tc.expected, stderr, "Bad output to STDERR")
    42  	}
    43  }
    44  
    45  func TestWarnf(t *testing.T) {
    46  	cases := []testCase{
    47  		{"basic", "foo", nil, "WARNING: foo\n"},
    48  		{"multiline", "foo\nbar", nil, "WARNING: foo\nbar\n"},
    49  		{"fmt", "bar: %v", []any{"baz"}, "WARNING: bar: baz\n"},
    50  	}
    51  	for _, tc := range cases {
    52  		stderr := ui.RunWithTestCtx(func(ctx context.Context, _ ui.WriteFunc) {
    53  			ui.Warnf(ctx, tc.input, tc.args...)
    54  		})
    55  		assert.Equal(t, tc.expected, stderr, "Bad output to STDERR")
    56  	}
    57  }
    58  

View as plain text