github.com/anchore/syft@v1.38.2/internal/licenses/context_test.go (about)

     1  package licenses
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestSetContextLicenseScanner(t *testing.T) {
    11  	scanner := testScanner()
    12  	ctx := context.Background()
    13  	ctx = SetContextLicenseScanner(ctx, scanner)
    14  
    15  	val := ctx.Value(ctxKey)
    16  	require.NotNil(t, val)
    17  	s, ok := val.(Scanner)
    18  	require.True(t, ok)
    19  	require.Equal(t, scanner, s)
    20  }
    21  
    22  func TestIsContextLicenseScannerSet(t *testing.T) {
    23  	scanner := testScanner()
    24  	ctx := context.Background()
    25  	require.False(t, IsContextLicenseScannerSet(ctx))
    26  
    27  	ctx = SetContextLicenseScanner(ctx, scanner)
    28  	require.True(t, IsContextLicenseScannerSet(ctx))
    29  }
    30  
    31  func TestContextLicenseScanner(t *testing.T) {
    32  	t.Run("with scanner", func(t *testing.T) {
    33  		scanner := testScanner()
    34  		ctx := SetContextLicenseScanner(context.Background(), scanner)
    35  		s, err := ContextLicenseScanner(ctx)
    36  		if err != nil || s != scanner {
    37  			t.Fatal("expected scanner from context")
    38  		}
    39  	})
    40  
    41  	t.Run("without scanner", func(t *testing.T) {
    42  		ctx := context.Background()
    43  		s, err := ContextLicenseScanner(ctx)
    44  		if err != nil || s == nil {
    45  			t.Fatal("expected default scanner")
    46  		}
    47  	})
    48  }