github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/cmd/syft/internal/ui/post_ui_event_writer_test.go (about)

     1  package ui
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/gkampitakis/go-snaps/snaps"
     8  	"github.com/stretchr/testify/require"
     9  	"github.com/wagoodman/go-partybus"
    10  
    11  	"github.com/anchore/syft/syft/event"
    12  	"github.com/anchore/syft/syft/event/parsers"
    13  )
    14  
    15  func Test_postUIEventWriter_write(t *testing.T) {
    16  
    17  	tests := []struct {
    18  		name    string
    19  		quiet   bool
    20  		events  []partybus.Event
    21  		wantErr require.ErrorAssertionFunc
    22  	}{
    23  		{
    24  			name: "no events",
    25  		},
    26  		{
    27  			name: "all events",
    28  			events: []partybus.Event{
    29  				{
    30  					Type:  event.CLINotification,
    31  					Value: "\n\n<my notification 1!!\n...still notifying>\n\n",
    32  				},
    33  				{
    34  					Type:  event.CLINotification,
    35  					Value: "<notification 2>",
    36  				},
    37  				{
    38  					Type: event.CLIAppUpdateAvailable,
    39  					Value: parsers.UpdateCheck{
    40  						New:     "v0.33.0",
    41  						Current: "[not provided]",
    42  					},
    43  				},
    44  				{
    45  					Type:  event.CLINotification,
    46  					Value: "<notification 3>",
    47  				},
    48  				{
    49  					Type:  event.CLIReport,
    50  					Value: "\n\n<my --\n-\n-\nreport 1!!>\n\n",
    51  				},
    52  				{
    53  					Type:  event.CLIReport,
    54  					Value: "<report 2>",
    55  				},
    56  			},
    57  		},
    58  		{
    59  			name:  "quiet only shows report",
    60  			quiet: true,
    61  			events: []partybus.Event{
    62  
    63  				{
    64  					Type:  event.CLINotification,
    65  					Value: "<notification 1>",
    66  				},
    67  				{
    68  					Type: event.CLIAppUpdateAvailable,
    69  					Value: parsers.UpdateCheck{
    70  						New:     "<new version>",
    71  						Current: "<current version>",
    72  					},
    73  				},
    74  				{
    75  					Type:  event.CLIReport,
    76  					Value: "<report 1>",
    77  				},
    78  			},
    79  		},
    80  	}
    81  	for _, tt := range tests {
    82  		t.Run(tt.name, func(t *testing.T) {
    83  			if tt.wantErr == nil {
    84  				tt.wantErr = require.NoError
    85  			}
    86  
    87  			stdout := &bytes.Buffer{}
    88  			stderr := &bytes.Buffer{}
    89  			w := newPostUIEventWriter(stdout, stderr)
    90  
    91  			tt.wantErr(t, w.write(tt.quiet, tt.events...))
    92  
    93  			t.Run("stdout", func(t *testing.T) {
    94  				snaps.MatchSnapshot(t, stdout.String())
    95  			})
    96  
    97  			t.Run("stderr", func(t *testing.T) {
    98  				snaps.MatchSnapshot(t, stderr.String())
    99  			})
   100  		})
   101  	}
   102  }