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