|
| 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 librarian |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/googleapis/librarian/internal/config" |
| 25 | + "gopkg.in/yaml.v3" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + libExists = "library-one" |
| 30 | + libExistsOutput = "output1" |
| 31 | + newLib = "library-two" |
| 32 | + newLibOutput = "output2" |
| 33 | + newLibSpec = "google/cloud/storage/v1" |
| 34 | + newLibSC = "google/cloud/storage/v1/storage_v1.yaml" |
| 35 | +) |
| 36 | + |
| 37 | +type mockGenerator struct { |
| 38 | + called bool |
| 39 | + callArgs struct { |
| 40 | + all bool |
| 41 | + libraryName string |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func (m *mockGenerator) Run(ctx context.Context, all bool, libraryName string) error { |
| 46 | + m.called = true |
| 47 | + m.callArgs.all = all |
| 48 | + m.callArgs.libraryName = libraryName |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func TestCreateForExistingLib(t *testing.T) { |
| 53 | + |
| 54 | + for _, test := range []struct { |
| 55 | + name string |
| 56 | + libName string |
| 57 | + output string |
| 58 | + language string |
| 59 | + }{ |
| 60 | + { |
| 61 | + name: "run create for existing library", |
| 62 | + libName: libExists, |
| 63 | + output: libExistsOutput, |
| 64 | + language: "rust", |
| 65 | + }, |
| 66 | + } { |
| 67 | + t.Run(test.name, func(t *testing.T) { |
| 68 | + |
| 69 | + createLibrarianYaml(t, test.libName, test.output, test.language, "") |
| 70 | + var gen Generator = &mockGenerator{} |
| 71 | + |
| 72 | + err := runCreateWithGenerator(context.Background(), test.libName, "", "", test.output, "protobuf", gen) |
| 73 | + if err != nil { |
| 74 | + t.Fatal(err) |
| 75 | + } |
| 76 | + |
| 77 | + mock := gen.(*mockGenerator) |
| 78 | + if !mock.called { |
| 79 | + t.Error("expected mockGenerator.Run to be called") |
| 80 | + } |
| 81 | + if mock.callArgs.libraryName != test.libName { |
| 82 | + t.Errorf("expected libraryName %s, got %s", test.libName, mock.callArgs.libraryName) |
| 83 | + } |
| 84 | + |
| 85 | + }) |
| 86 | + } |
| 87 | + |
| 88 | +} |
| 89 | + |
| 90 | +func TestCreateCommand(t *testing.T) { |
| 91 | + |
| 92 | + for _, test := range []struct { |
| 93 | + name string |
| 94 | + args []string |
| 95 | + language string |
| 96 | + skipCreatingYaml bool |
| 97 | + wantErr error |
| 98 | + defaultOutput string |
| 99 | + libOutputFolder string |
| 100 | + }{ |
| 101 | + { |
| 102 | + name: "no args", |
| 103 | + args: []string{"librarian", "create"}, |
| 104 | + wantErr: errMissingNameFlag, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "missing service-config", |
| 108 | + args: []string{"librarian", "create", "--name", newLib, "--output", newLibOutput, "--specification-source", newLibSpec}, |
| 109 | + language: "rust", |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "missing specification-source", |
| 113 | + args: []string{"librarian", "create", "--name", newLib, "--output", newLibOutput, "--service-config", newLibSC}, |
| 114 | + language: "rust", |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "missing specification-source and service-config", |
| 118 | + args: []string{"librarian", "create", "--name", newLib, "--output", newLibOutput}, |
| 119 | + language: "rust", |
| 120 | + wantErr: errServiceConfigOrSpecRequired, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "create new library", |
| 124 | + args: []string{"librarian", "create", "--name", newLib, "--output", newLibOutput, "--service-config", newLibSC, "--specification-source", newLibSpec}, |
| 125 | + language: "rust", |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "no yaml", |
| 129 | + args: []string{"librarian", "create", "--name", newLib}, |
| 130 | + skipCreatingYaml: true, |
| 131 | + wantErr: errNoYaml, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "unsupported language", |
| 135 | + args: []string{"librarian", "create", "--name", newLib}, |
| 136 | + language: "unsupported-lang", |
| 137 | + wantErr: errUnsupportedLanguage, |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "output flag required", |
| 141 | + args: []string{"librarian", "create", "--name", newLib, "--service-config", newLibSC, "--specification-source", newLibSpec}, |
| 142 | + language: "rust", |
| 143 | + wantErr: errOutputFlagRequired, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "default output directory used", |
| 147 | + args: []string{"librarian", "create", "--name", newLib, "--service-config", newLibSC, "--specification-source", newLibSpec}, |
| 148 | + language: "rust", |
| 149 | + defaultOutput: "default", |
| 150 | + }, |
| 151 | + } { |
| 152 | + t.Run(test.name, func(t *testing.T) { |
| 153 | + if !test.skipCreatingYaml { |
| 154 | + createLibrarianYaml(t, libExists, libExistsOutput, test.language, test.defaultOutput) |
| 155 | + } |
| 156 | + err := Run(t.Context(), test.args...) |
| 157 | + if test.wantErr != nil { |
| 158 | + if !errors.Is(err, test.wantErr) { |
| 159 | + t.Errorf("want error %v, got %v", test.wantErr, err) |
| 160 | + } |
| 161 | + return |
| 162 | + } |
| 163 | + if err != nil { |
| 164 | + t.Fatal(err) |
| 165 | + } |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func createLibrarianYaml(t *testing.T, libName string, libOutput string, language string, defaultOutput string) { |
| 171 | + tempDir := t.TempDir() |
| 172 | + t.Chdir(tempDir) |
| 173 | + configPath := filepath.Join(tempDir, librarianConfigPath) |
| 174 | + config := config.Config{ |
| 175 | + Language: language, |
| 176 | + Sources: &config.Sources{ |
| 177 | + Googleapis: &config.Source{ |
| 178 | + Dir: "/googleapis/testdata", |
| 179 | + }, |
| 180 | + }, |
| 181 | + Default: &config.Default{ |
| 182 | + Output: defaultOutput, |
| 183 | + }, |
| 184 | + Libraries: []*config.Library{ |
| 185 | + { |
| 186 | + Name: libName, |
| 187 | + Output: libOutput, |
| 188 | + }, |
| 189 | + }, |
| 190 | + } |
| 191 | + |
| 192 | + configBytes, err := yaml.Marshal(&config) |
| 193 | + if err != nil { |
| 194 | + t.Fatalf("Failed to marshal YAML: %v", err) |
| 195 | + } |
| 196 | + |
| 197 | + if err := os.WriteFile(configPath, configBytes, 0644); err != nil { |
| 198 | + t.Fatal(err) |
| 199 | + } |
| 200 | + |
| 201 | + if err := os.MkdirAll(filepath.Join(tempDir, libOutput), 0755); err != nil { |
| 202 | + t.Fatal(err) |
| 203 | + } |
| 204 | +} |
0 commit comments