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

     1  // Copyright 2022 Edward McFarlane. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package starlib
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestResolve(t *testing.T) {
    13  
    14  	for _, tt := range []struct {
    15  		name       string
    16  		threadName string
    17  		module     string
    18  		wantBkt    string
    19  		wantPath   string
    20  	}{{
    21  		name:       "local",
    22  		threadName: "file://?prefix=a%2Fsubfolder%2F",
    23  		module:     "./mod.star",
    24  		wantBkt:    "file://?prefix=a/subfolder/",
    25  		wantPath:   "mod.star",
    26  	}, {
    27  		name:       "localKey",
    28  		threadName: "file://?prefix=a/subfolder/&key=b/nested/file.star",
    29  		module:     "./mod.star",
    30  		wantBkt:    "file://?prefix=a/subfolder/",
    31  		wantPath:   "b/nested/mod.star",
    32  	}, {
    33  		name:       "parentKey",
    34  		threadName: "file://?prefix=a/subfolder/&key=b/nested/file.star",
    35  		module:     "../mod.star",
    36  		wantBkt:    "file://?prefix=a/subfolder/",
    37  		wantPath:   "b/mod.star",
    38  	}} {
    39  		t.Run(tt.name, func(t *testing.T) {
    40  			gotBkt, gotPath, err := resolveModuleURL(tt.threadName, tt.module)
    41  			if err != nil {
    42  				t.Fatal(err)
    43  			}
    44  			gotBkt = strings.ReplaceAll(gotBkt, "%2F", "/") // url encoding
    45  			if gotBkt != tt.wantBkt {
    46  				t.Errorf("invalid bkt: %s, want %s", gotBkt, tt.wantBkt)
    47  			}
    48  			if gotPath != tt.wantPath {
    49  				t.Errorf("invalid path: %s, want %s", gotPath, tt.wantPath)
    50  			}
    51  
    52  		})
    53  	}
    54  }