|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package config |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/google/go-cmp/cmp" |
| 21 | +) |
| 22 | + |
| 23 | +func TestDetermineTagFormat(t *testing.T) { |
| 24 | + for _, test := range []struct { |
| 25 | + name string |
| 26 | + libraryState *LibraryState |
| 27 | + librarianConfig *LibrarianConfig |
| 28 | + want string |
| 29 | + wantErrMsg string |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "uses_default", |
| 33 | + libraryState: &LibraryState{ |
| 34 | + ID: "example-library", |
| 35 | + }, |
| 36 | + librarianConfig: &LibrarianConfig{}, |
| 37 | + want: defaultTagFormat, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "prefers_per-library_from_config", |
| 41 | + libraryState: &LibraryState{ |
| 42 | + ID: "example-library", |
| 43 | + TagFormat: "per-library-tag-format-from-state", |
| 44 | + }, |
| 45 | + librarianConfig: &LibrarianConfig{ |
| 46 | + TagFormat: "from-config", |
| 47 | + Libraries: []*LibraryConfig{ |
| 48 | + { |
| 49 | + LibraryID: "example-library", |
| 50 | + TagFormat: "per-library-tag-format-from-config", |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + want: "per-library-tag-format-from-config", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "prefers_from_config", |
| 58 | + libraryState: &LibraryState{ |
| 59 | + ID: "example-library", |
| 60 | + TagFormat: "per-library-tag-format-from-state", |
| 61 | + }, |
| 62 | + librarianConfig: &LibrarianConfig{ |
| 63 | + TagFormat: "from-config", |
| 64 | + Libraries: []*LibraryConfig{ |
| 65 | + { |
| 66 | + LibraryID: "example-library", |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + want: "from-config", |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "falls_back_to_per-library_from_state", |
| 74 | + libraryState: &LibraryState{ |
| 75 | + ID: "example-library", |
| 76 | + TagFormat: "per-library-tag-format-from-state", |
| 77 | + }, |
| 78 | + librarianConfig: &LibrarianConfig{}, |
| 79 | + want: "per-library-tag-format-from-state", |
| 80 | + }, |
| 81 | + } { |
| 82 | + t.Run(test.name, func(t *testing.T) { |
| 83 | + got := DetermineTagFormat("example-library", test.libraryState, test.librarianConfig) |
| 84 | + |
| 85 | + if diff := cmp.Diff(test.want, got); diff != "" { |
| 86 | + t.Errorf("determineTagFormat() mismatch (-want +got):\n%s", diff) |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestFormatTag(t *testing.T) { |
| 93 | + t.Parallel() |
| 94 | + for _, test := range []struct { |
| 95 | + name string |
| 96 | + library *LibraryState |
| 97 | + want string |
| 98 | + }{ |
| 99 | + { |
| 100 | + name: "default_format", |
| 101 | + library: &LibraryState{ |
| 102 | + ID: "google.cloud.foo.v1", |
| 103 | + Version: "1.2.3", |
| 104 | + }, |
| 105 | + want: "google.cloud.foo.v1-1.2.3", |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "custom_format", |
| 109 | + library: &LibraryState{ |
| 110 | + ID: "google.cloud.foo.v1", |
| 111 | + Version: "1.2.3", |
| 112 | + TagFormat: "v{version}-{id}", |
| 113 | + }, |
| 114 | + want: "v1.2.3-google.cloud.foo.v1", |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "custom_format_version_only", |
| 118 | + library: &LibraryState{ |
| 119 | + ID: "google.cloud.foo.v1", |
| 120 | + Version: "1.2.3", |
| 121 | + TagFormat: "v{version}", |
| 122 | + }, |
| 123 | + want: "v1.2.3", |
| 124 | + }, |
| 125 | + } { |
| 126 | + t.Run(test.name, func(t *testing.T) { |
| 127 | + got := FormatTag(test.library.TagFormat, test.library.ID, test.library.Version) |
| 128 | + if got != test.want { |
| 129 | + t.Errorf("FormatTag() = %q, want %q", got, test.want) |
| 130 | + } |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
0 commit comments