-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We need to be able to decode string representations of final sources, and the existing ParseSource function doesn't handle versioned registry source addresses. This commit introduces ParseFileSource and supporting functions for decoding final registry sources.
- Loading branch information
Showing
3 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,3 +106,78 @@ func TestResolveRelativeFinalSource(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestParseFinalSource(t *testing.T) { | ||
onePointOh := versions.MustParseVersion("1.0.0") | ||
|
||
tests := []struct { | ||
Addr string | ||
Want FinalSource | ||
WantErr string | ||
}{ | ||
{ | ||
Addr: "./a/b", | ||
Want: MustParseSource("./a/b").(FinalSource), | ||
}, | ||
{ | ||
Addr: "git::https://github.com/hashicorp/go-slug.git//beep/boop", | ||
Want: MustParseSource("git::https://github.com/hashicorp/go-slug.git//beep/boop").(FinalSource), | ||
}, | ||
{ | ||
Addr: "example.com/foo/bar/[email protected]//beep", | ||
Want: MustParseSource("example.com/foo/bar/baz//beep").(RegistrySource).Versioned(onePointOh), | ||
}, | ||
{ | ||
Addr: "example.com/foo/bar/[email protected]", | ||
Want: MustParseSource("example.com/foo/bar/baz").(RegistrySource).Versioned(onePointOh), | ||
}, | ||
{ | ||
Addr: "./a/[email protected]", | ||
Want: MustParseSource("./a/[email protected]").(FinalSource), | ||
}, | ||
{ | ||
Addr: " ./a/b", | ||
WantErr: "source address must not have leading or trailing spaces", | ||
}, | ||
{ | ||
Addr: "", | ||
WantErr: "a valid source address is required", | ||
}, | ||
{ | ||
Addr: "example.com/foo/bar/[email protected]//beep", | ||
WantErr: `invalid module registry source address "example.com/foo/bar/[email protected]//beep": invalid version: can't use wildcard for patch number; an exact version is required`, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.Addr, func(t *testing.T) { | ||
got, gotErr := ParseFinalSource(test.Addr) | ||
|
||
if test.WantErr != "" { | ||
if gotErr == nil { | ||
t.Fatalf("unexpected success\ngot result: %#v (%T)\nwant error: %s", got, got, test.WantErr) | ||
} | ||
if got, want := gotErr.Error(), test.WantErr; got != want { | ||
t.Fatalf("wrong error\ngot error: %s\nwant error: %s", got, want) | ||
} | ||
return | ||
} | ||
|
||
if gotErr != nil { | ||
t.Fatalf("unexpected error: %s", gotErr) | ||
} | ||
|
||
// Two addresses are equal if they have the same string representation | ||
// and the same dynamic type. | ||
gotStr := got.String() | ||
wantStr := test.Want.String() | ||
if gotStr != wantStr { | ||
t.Errorf("wrong result\ngot: %s\nwant: %s", gotStr, wantStr) | ||
} | ||
|
||
if gotType, wantType := reflect.TypeOf(got), reflect.TypeOf(test.Want); gotType != wantType { | ||
t.Errorf("wrong result type\ngot: %s\nwant: %s", gotType, wantType) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters