github.com/magodo/terraform@v0.11.12-beta1/config/module/detector_test.go (about)

     1  package module
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/registry/regsrc"
     8  )
     9  
    10  func TestParseRegistrySource(t *testing.T) {
    11  	for _, tc := range []struct {
    12  		source      string
    13  		host        string
    14  		id          string
    15  		err         bool
    16  		notRegistry bool
    17  	}{
    18  		{ // simple source id
    19  			source: "namespace/id/provider",
    20  			id:     "namespace/id/provider",
    21  		},
    22  		{ // source with hostname
    23  			source: "registry.com/namespace/id/provider",
    24  			host:   "registry.com",
    25  			id:     "namespace/id/provider",
    26  		},
    27  		{ // source with hostname and port
    28  			source: "registry.com:4443/namespace/id/provider",
    29  			host:   "registry.com:4443",
    30  			id:     "namespace/id/provider",
    31  		},
    32  		{ // too many parts
    33  			source: "registry.com/namespace/id/provider/extra",
    34  			err:    true,
    35  		},
    36  		{ // local path
    37  			source:      "./local/file/path",
    38  			notRegistry: true,
    39  		},
    40  		{ // local path with hostname
    41  			source:      "./registry.com/namespace/id/provider",
    42  			notRegistry: true,
    43  		},
    44  		{ // full URL
    45  			source:      "https://example.com/foo/bar/baz",
    46  			notRegistry: true,
    47  		},
    48  		{ // punycode host not allowed in source
    49  			source: "xn--80akhbyknj4f.com/namespace/id/provider",
    50  			err:    true,
    51  		},
    52  		{ // simple source id with subdir
    53  			source: "namespace/id/provider//subdir",
    54  			id:     "namespace/id/provider",
    55  		},
    56  		{ // source with hostname and subdir
    57  			source: "registry.com/namespace/id/provider//subdir",
    58  			host:   "registry.com",
    59  			id:     "namespace/id/provider",
    60  		},
    61  		{ // source with hostname
    62  			source: "registry.com/namespace/id/provider",
    63  			host:   "registry.com",
    64  			id:     "namespace/id/provider",
    65  		},
    66  		{ // we special case github
    67  			source:      "github.com/namespace/id/provider",
    68  			notRegistry: true,
    69  		},
    70  		{ // we special case github ssh
    71  			source:      "git@github.com:namespace/id/provider",
    72  			notRegistry: true,
    73  		},
    74  		{ // we special case bitbucket
    75  			source:      "bitbucket.org/namespace/id/provider",
    76  			notRegistry: true,
    77  		},
    78  	} {
    79  		t.Run(tc.source, func(t *testing.T) {
    80  			mod, err := regsrc.ParseModuleSource(tc.source)
    81  			if tc.notRegistry {
    82  				if err != regsrc.ErrInvalidModuleSource {
    83  					t.Fatalf("%q should not be a registry source, got err %v", tc.source, err)
    84  				}
    85  				return
    86  			}
    87  
    88  			if tc.err {
    89  				if err == nil {
    90  					t.Fatal("expected error")
    91  				}
    92  				return
    93  			}
    94  
    95  			if err != nil {
    96  				t.Fatal(err)
    97  			}
    98  
    99  			id := fmt.Sprintf("%s/%s/%s", mod.RawNamespace, mod.RawName, mod.RawProvider)
   100  
   101  			if tc.host != "" {
   102  				if mod.RawHost.Normalized() != tc.host {
   103  					t.Fatalf("expected host %q, got %q", tc.host, mod.RawHost)
   104  				}
   105  			}
   106  
   107  			if tc.id != id {
   108  				t.Fatalf("expected id %q, got %q", tc.id, id)
   109  			}
   110  		})
   111  	}
   112  }