| package main |
| |
| import ( |
| "testing" |
| |
| "github.com/google/go-cmp/cmp" |
| "go.fuchsia.dev/infra/cmd/jiri2repo/jiri" |
| "go.fuchsia.dev/infra/cmd/jiri2repo/repo" |
| ) |
| |
| func TestSSOCodeReviewURL(t *testing.T) { |
| tests := []struct { |
| input string |
| want string |
| }{ |
| { |
| input: "https://host.googlesource.com", |
| want: "sso://host-review/", |
| }, |
| { |
| input: "https://host-with-dash.googlesource.com", |
| want: "sso://host-with-dash-review/", |
| }, |
| { |
| input: "https://host.with.dot.googlesource.com", |
| want: "sso://host.with.dot-review/", |
| }, |
| { |
| input: "sso://host.googlesource.com", |
| want: "sso://host-review/", |
| }, |
| } |
| |
| for _, tt := range tests { |
| t.Run(tt.input, func(t *testing.T) { |
| url, err := ssoCodeReviewURL(tt.input) |
| if err != nil { |
| t.Errorf("unexpected error: %v", err.Error()) |
| return |
| } |
| got := url.String() |
| if tt.want != got { |
| t.Errorf("got %q but wanted %q", got, tt.want) |
| } |
| }) |
| } |
| |
| errTests := []string{ |
| "host", |
| "host.com", |
| "host.googlesource.com", |
| } |
| for _, input := range errTests { |
| t.Run(input, func(t *testing.T) { |
| url, err := ssoCodeReviewURL(input) |
| if err == nil { |
| t.Errorf("got %s but wanted an error", url.String()) |
| } |
| }) |
| } |
| } |
| |
| func TestConvert(t *testing.T) { |
| input := jiri.SourceManifest{ |
| Directories: map[string]*jiri.SourceManifest_Directory{ |
| // Ensure convert handles cwd. |
| ".": &jiri.SourceManifest_Directory{ |
| GitCheckout: &jiri.SourceManifest_GitCheckout{ |
| FetchRef: "refs/heads/master", |
| RepoUrl: "https://fuchsia.googlesource.com/fuchsia", |
| Revision: "fuchsia_revision", |
| }, |
| }, |
| "code": &jiri.SourceManifest_Directory{ |
| GitCheckout: &jiri.SourceManifest_GitCheckout{ |
| FetchRef: "refs/heads/dev", |
| RepoUrl: "https://code.googlesource.com/code", |
| Revision: "code_revision", |
| }, |
| }, |
| }, |
| } |
| |
| defaults := repo.Default{ |
| Branch: "default_branch", |
| // DefaultRemote should not explicitly appear as any project's remote. |
| // This should also be recorded as just "fuchsia" in the output <default> |
| Remote: "fuchsia.googlesource.com", |
| } |
| |
| want := repo.Manifest{ |
| Remote: []repo.Remote{ |
| { |
| Name: "code", |
| Review: "sso://code-review/", |
| Fetch: "refs/heads/dev", |
| }, |
| { |
| Name: "fuchsia", |
| Review: "sso://fuchsia-review/", |
| Fetch: "refs/heads/master", |
| }, |
| }, |
| Default: repo.Default{ |
| Branch: "default_branch", |
| Remote: "fuchsia", |
| }, |
| Project: []repo.Project{ |
| { |
| Name: "code", |
| Path: "code", |
| Remote: "code", |
| Revision: "code_revision", |
| }, |
| { |
| Name: "fuchsia", |
| Path: "fuchsia", |
| Revision: "fuchsia_revision", |
| }, |
| }, |
| } |
| |
| var got repo.Manifest |
| if err := convert(input, &got, defaults); err != nil { |
| t.Fatal(err) |
| } |
| |
| // No point in testing the comment |
| got.Comment = "" |
| |
| if diff := cmp.Diff(want, got); diff != "" { |
| t.Errorf("convert() mismatch (-want +got):\n%s", diff) |
| } |
| } |