github.com/quay/claircore@v1.5.28/rhel/dockerfile/dockerfile_test.go (about)

     1  package dockerfile
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"io/fs"
     8  	"os"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/google/go-cmp/cmp"
    13  )
    14  
    15  func TestGetLabels(t *testing.T) {
    16  	ctx := context.Background()
    17  	td := os.DirFS("testdata")
    18  	de, err := fs.ReadDir(td, ".")
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	for _, de := range de {
    23  		n := de.Name()
    24  		if !strings.HasPrefix(n, "Dockerfile") ||
    25  			strings.HasSuffix(n, ".want") ||
    26  			strings.HasSuffix(n, ".want.err") {
    27  			continue
    28  		}
    29  		t.Run(n, func(t *testing.T) {
    30  			f, err := td.Open(n)
    31  			if err != nil {
    32  				t.Fatal(err)
    33  			}
    34  			defer f.Close()
    35  			w, err := td.Open(n + ".want")
    36  			if err != nil {
    37  				t.Fatal(err)
    38  			}
    39  			defer w.Close()
    40  			wantErr, _ := fs.ReadFile(td, n+".want.err")
    41  
    42  			want := make(map[string]string)
    43  			if err := json.NewDecoder(w).Decode(&want); err != nil {
    44  				t.Error(err)
    45  			}
    46  			got, err := GetLabels(ctx, f)
    47  			if len(wantErr) == 0 {
    48  				if err != nil {
    49  					t.Error(err)
    50  				}
    51  			} else {
    52  				if err == nil {
    53  					t.Error("got nil, wanted error")
    54  				} else {
    55  					if got, want := err.Error(), string(bytes.TrimSpace(wantErr)); got != want {
    56  						t.Errorf("got: %+#q, want: %+#q", got, want)
    57  					}
    58  				}
    59  			}
    60  
    61  			if !cmp.Equal(got, want) {
    62  				t.Error(cmp.Diff(got, want))
    63  			}
    64  		})
    65  	}
    66  }
    67  
    68  func TestSplit(t *testing.T) {
    69  	for _, p := range []struct {
    70  		In   string
    71  		Want []string
    72  	}{
    73  		{
    74  			In:   "",
    75  			Want: nil,
    76  		},
    77  		{
    78  			In:   "k=v",
    79  			Want: []string{"k=v"},
    80  		},
    81  		{
    82  			In:   `k=v\ v`,
    83  			Want: []string{`k=v\ v`},
    84  		},
    85  		{
    86  			In:   `k=v k=v k=v`,
    87  			Want: []string{`k=v`, `k=v`, `k=v`},
    88  		},
    89  		{
    90  			In:   `k=" v "`,
    91  			Want: []string{`k=" v "`},
    92  		},
    93  		{
    94  			In:   `k=' v '`,
    95  			Want: []string{`k=' v '`},
    96  		},
    97  		{
    98  			In:   `k=' v ' k="   "`,
    99  			Want: []string{`k=' v '`, `k="   "`},
   100  		},
   101  		{
   102  			In:   "k=' v '	\v k=\"   \"",
   103  			Want: []string{`k=' v '`, `k="   "`},
   104  		},
   105  		{
   106  			In:   "k=' v ' \t\"k\"=\"   \"",
   107  			Want: []string{`k=' v '`, `"k"="   "`},
   108  		},
   109  	} {
   110  		t.Logf("input: %#q", p.In)
   111  		got, err := splitKV('\\', p.In)
   112  		if err != nil {
   113  			t.Error(err)
   114  		}
   115  		if want := p.Want; !cmp.Equal(got, want) {
   116  			t.Error(cmp.Diff(got, want))
   117  		}
   118  	}
   119  }