github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/starlib/starlarkrule/rule_test.go (about)

     1  package starlarkrule_test
     2  
     3  import (
     4  	"context"
     5  	"net/url"
     6  	"testing"
     7  
     8  	"github.com/emcfarlane/larking/starlib"
     9  	"github.com/emcfarlane/larking/starlib/starlarkrule"
    10  	"gocloud.dev/blob"
    11  	_ "gocloud.dev/blob/fileblob"
    12  )
    13  
    14  func TestExecFile(t *testing.T) {
    15  	starlib.RunTests(t, "testdata/*.star", nil)
    16  }
    17  
    18  func TestLabels(t *testing.T) {
    19  	tests := []struct {
    20  		name    string
    21  		label   string
    22  		source  string
    23  		want    string
    24  		wantErr error
    25  	}{{
    26  		name:   "full",
    27  		label:  "testdata/go/hello",
    28  		source: "file:///", // root
    29  		want: "file:///?" + url.Values{
    30  			"key": []string{"testdata/go/hello"},
    31  		}.Encode(),
    32  	}, {
    33  		name:   "relative",
    34  		label:  "hello",
    35  		source: "file://./",
    36  		want: "file://./?" + url.Values{
    37  			"key": []string{"hello"},
    38  		}.Encode(),
    39  	}, {
    40  		name:  "dotRelative",
    41  		label: "./hello",
    42  		source: "file://./?" + url.Values{
    43  			"key": []string{"testdata/go/file.star"},
    44  		}.Encode(),
    45  		want: "file://./?" + url.Values{
    46  			"key": []string{"testdata/go/hello"},
    47  		}.Encode(),
    48  	}, {
    49  		name:  "dotdotRelative",
    50  		label: "../hello",
    51  		source: "file://./?" + url.Values{
    52  			"key": []string{"testdata/go/file.star"},
    53  		}.Encode(),
    54  		want: "file://./?" + url.Values{
    55  			"key": []string{"testdata/hello"},
    56  		}.Encode(),
    57  	}, {
    58  		name:  "dotdotdotdotRelative",
    59  		label: "../../hello",
    60  		source: "file://./?" + url.Values{
    61  			"key": []string{"testdata/go/file.star"},
    62  		}.Encode(),
    63  		want: "file://./?" + url.Values{
    64  			"key": []string{"hello"},
    65  		}.Encode(),
    66  	}, {
    67  		name:  "dotdotCD",
    68  		label: "../packaging/file.star",
    69  		source: "file://./?" + url.Values{
    70  			"key": []string{"testdata/go/file.star"},
    71  		}.Encode(),
    72  		want: "file://./?" + url.Values{
    73  			"key": []string{"testdata/packaging/file.star"},
    74  		}.Encode(),
    75  	}, {
    76  		name:  "other",
    77  		label: "file:///usr/local/bin?key=go",
    78  		source: "file://./?" + url.Values{
    79  			"key": []string{"testdata/go/file.star"},
    80  		}.Encode(),
    81  		want: "file:///usr/local/bin?key=go",
    82  		/*}, {
    83  			name:  "absolute",
    84  			label: "/users/edward/Downloads/file.txt",
    85  			dir:   "",
    86  			want:  "file:///users/edward/Downloads/file.txt",
    87  		}, {
    88  			name:  "fileLabel",
    89  			label: "file://rules/go/zxx",
    90  			dir:   "testdata/cgo",
    91  			want:  "file://rules/go/zxx",
    92  		*/
    93  		// TODO: query parameters as part of the binary?
    94  		/*}, {
    95  			name:  "queryRelative",
    96  			label: "helloc?goarch=amd64&goos=linux",
    97  			source: "file://./?" + url.Values{
    98  				"key": []string{"testdata/go/file.star"},
    99  			}.Encode(),
   100  			want: "file://./?" + url.Values{
   101  				"key":    []string{"testdata/go/helloc"},
   102  				"goarch": []string{"amd64"},
   103  				"goos":   []string{"linux"},
   104  			}.Encode(),
   105  		}, {
   106  			name: "queryAbsoluteURL",
   107  			label: "file://./?" + url.Values{
   108  				"key":    []string{"testdata/cgo/helloc"},
   109  				"goarch": []string{"amd64"},
   110  				"goos":   []string{"linux"},
   111  			}.Encode(),
   112  			source: "file://./?" + url.Values{
   113  				"key": []string{"testdata/go/file.star"},
   114  			}.Encode(),
   115  			want: "file://./?" + url.Values{
   116  				"key":    []string{"testdata/go/helloc"},
   117  				"goarch": []string{"amd64"},
   118  				"goos":   []string{"linux"},
   119  			}.Encode(),*/
   120  	}}
   121  
   122  	for _, tt := range tests {
   123  		t.Run(tt.name, func(t *testing.T) {
   124  			l, err := starlarkrule.ParseRelativeLabel(tt.source, tt.label)
   125  			if err != tt.wantErr {
   126  				t.Fatalf("error got: %v, want: %v", err, tt.wantErr)
   127  			}
   128  			if l.String() != tt.want {
   129  				t.Fatalf("%s != %s", l, tt.want)
   130  			}
   131  
   132  			// Check valid bucket.
   133  			ctx := context.Background()
   134  			bkt, err := blob.OpenBucket(ctx, l.String())
   135  			if err != nil {
   136  				t.Fatal(err)
   137  			}
   138  			defer bkt.Close()
   139  		})
   140  	}
   141  }