| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package main |
| |
| import ( |
| "net/url" |
| "testing" |
| |
| buildbucketpb "go.chromium.org/luci/buildbucket/proto" |
| ) |
| |
| func TestResolveIntegrationURL(t *testing.T) { |
| t.Parallel() |
| |
| var tests = []struct { |
| gerritHost string |
| gitilesHost string |
| expectedURL string |
| expectedErr bool |
| }{ |
| { |
| "https://fuchsia-review.googlesource.com", |
| "", |
| "https://fuchsia.googlesource.com/integration", |
| false, |
| }, |
| { |
| "", |
| "https://fuchsia.googlesource.com", |
| "https://fuchsia.googlesource.com/integration", |
| false, |
| }, |
| { |
| "", |
| "", |
| "", |
| true, |
| }, |
| } |
| |
| for _, test := range tests { |
| buildInput := buildbucketpb.Build_Input{} |
| if test.gerritHost != "" { |
| buildInput.GerritChanges = []*buildbucketpb.GerritChange{{Host: test.gerritHost}} |
| } else if test.gitilesHost != "" { |
| buildInput.GitilesCommit = &buildbucketpb.GitilesCommit{Host: test.gitilesHost} |
| } |
| expectedURL, err := url.Parse(test.expectedURL) |
| if err != nil { |
| t.Fatalf("invalid test input URL %q", test.expectedURL) |
| } |
| integrationURL, err := resolveIntegrationURL(&buildInput) |
| if err == nil { |
| if test.expectedErr { |
| t.Fatalf("expected error, got nil") |
| } |
| if integrationURL.String() != expectedURL.String() { |
| t.Fatalf("integration URL %q does not match expected %q", integrationURL, expectedURL) |
| } |
| } else if !test.expectedErr { |
| t.Fatalf("got unexpected error %v", err) |
| } |
| } |
| } |