github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/builder/build_test.go (about) 1 package builder 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/emcfarlane/larking/starlib/starlarkrule" 8 "github.com/go-logr/logr" 9 "github.com/go-logr/logr/testr" 10 "go.starlark.net/starlark" 11 _ "gocloud.dev/blob/fileblob" 12 ) 13 14 func TestRun(t *testing.T) { 15 16 src := "file://./?metadata=skip" 17 makeLabel := func(name string) *starlarkrule.Label { 18 l, err := starlarkrule.ParseRelativeLabel(src, name) 19 if err != nil { 20 t.Fatal(err) 21 } 22 return l 23 } 24 srcLabel := makeLabel(src) 25 26 makeDefaultInfoFiles := func(modSrc string, executable string, filenames []string) *starlarkrule.AttrArgs { 27 var files []starlark.Value 28 for _, filename := range filenames { 29 files = append(files, makeLabel(filename)) 30 } 31 var kwargs []starlark.Tuple 32 if len(files) > 0 { 33 kwargs = append(kwargs, starlark.Tuple{ 34 starlark.String("files"), 35 starlark.NewList(files), 36 }) 37 } 38 39 if len(executable) > 0 { 40 kwargs = append(kwargs, starlark.Tuple{ 41 starlark.String("executable"), 42 makeLabel(executable), 43 }) 44 } 45 46 srcLabel, err := srcLabel.Parse(modSrc) 47 if err != nil { 48 t.Fatal(err) 49 } 50 51 args, err := starlarkrule.DefaultInfo.MakeArgs(srcLabel, kwargs) 52 if err != nil { 53 t.Fatal(err) 54 } 55 return args 56 } 57 58 tests := []struct { 59 name string 60 label string 61 wants []*starlarkrule.AttrArgs // starlark.Value 62 wantErr error 63 }{{ 64 name: "cgo", 65 label: "testdata/cgo/helloc", 66 wants: []*starlarkrule.AttrArgs{ 67 makeDefaultInfoFiles( 68 "testdata/cgo/BUILD.star", 69 "testdata/cgo/helloc", 70 []string{ 71 "testdata/cgo/helloc", 72 }, 73 ), 74 }, 75 }, { 76 name: "xcgo", 77 label: "testdata/cgo/helloc?goarch=amd64&goos=linux", 78 wants: []*starlarkrule.AttrArgs{ 79 makeDefaultInfoFiles( 80 "testdata/cgo/BUILD.star", 81 "testdata/cgo/helloc?goarch=amd64&goos=linux", 82 []string{ 83 "testdata/cgo/helloc?goarch=amd64&goos=linux", 84 }, 85 ), 86 }, 87 }, { 88 name: "tar", 89 label: "testdata/archive/hello.tar", 90 wants: []*starlarkrule.AttrArgs{ 91 makeDefaultInfoFiles( 92 "testdata/archive/BUILD.star", "", 93 []string{"testdata/archive/hello.tar"}, 94 ), 95 }, 96 }, { 97 name: "tar.gz", 98 label: "testdata/archive/helloc.tar.gz", 99 wants: []*starlarkrule.AttrArgs{ 100 makeDefaultInfoFiles( 101 "testdata/archive/BUILD.star", "", 102 []string{"testdata/archive/helloc.tar.gz"}, 103 ), 104 }, 105 }, { 106 // TODO: PULL LOCALLY 107 name: "containerPull", 108 label: "testdata/container/distroless.tar", 109 wants: []*starlarkrule.AttrArgs{ 110 makeDefaultInfoFiles( 111 "testdata/container/BUILD.star", "", 112 []string{"testdata/container/distroless.tar"}, 113 ), 114 }, 115 //wantConstructor: imageConstructor, 116 117 // name: "tarxcgo", 118 // label: "testdata/archive/helloc.tar.gz", 119 // wantConstructor: FileConstructor, 120 //}, { 121 // name: "containerPull", 122 // label: "testdata/container/distroless.tar", 123 // wantConstructor: imageConstructor, 124 //}, { 125 // name: "containerBuild", 126 // label: "testdata/container/helloc.tar", 127 // wantConstructor: imageConstructor, 128 }} 129 130 for _, tt := range tests { 131 t.Run(tt.name, func(t *testing.T) { 132 ctx := logr.NewContext(context.Background(), testr.New(t)) 133 got, err := Build(ctx, tt.label) 134 if err != nil { 135 t.Fatal(err) 136 } 137 if got.Failed && got.Error != tt.wantErr { 138 t.Fatalf("error got: %v, want: %v", got.Error, tt.wantErr) 139 } 140 if got.Failed { 141 t.Fatal("error failed: ", got) 142 } 143 144 for _, want := range tt.wants { 145 attrs := want.Attrs() 146 v, ok, err := got.Get(attrs) 147 if err != nil { 148 t.Fatal(err) 149 } 150 if !ok { 151 t.Errorf("missing attrs: %v", attrs) 152 continue 153 } 154 155 t.Logf("GOT!\t\t %v %T", v, v) 156 t.Logf("WANT\t\t %v %T", want, want) 157 ok, err = starlark.Equal(v, want) 158 if err != nil { 159 t.Fatal(err) 160 } 161 if !ok { 162 t.Errorf("not equal, got %v, want %v", v, want) 163 } 164 t.Logf("%v", want) 165 } 166 }) 167 } 168 }