|
| 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 automation |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/googleapis/librarian/internal/config" |
| 23 | +) |
| 24 | + |
| 25 | +func TestNewPublishRunner(t *testing.T) { |
| 26 | + t.Parallel() |
| 27 | + for _, test := range []struct { |
| 28 | + name string |
| 29 | + cfg *config.Config |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "create_a_runner", |
| 33 | + cfg: &config.Config{ |
| 34 | + Project: "example-project", |
| 35 | + }, |
| 36 | + }, |
| 37 | + } { |
| 38 | + t.Run(test.name, func(t *testing.T) { |
| 39 | + t.Parallel() |
| 40 | + runner := newPublishRunner(test.cfg) |
| 41 | + if runner.projectID != test.cfg.Project { |
| 42 | + t.Errorf("newPublishRunner() projectID is not set") |
| 43 | + } |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestPublishRunnerRun(t *testing.T) { |
| 49 | + t.Parallel() |
| 50 | + tests := []struct { |
| 51 | + name string |
| 52 | + args []string |
| 53 | + runCommandErr error |
| 54 | + wantErr bool |
| 55 | + }{ |
| 56 | + { |
| 57 | + name: "error from RunCommand", |
| 58 | + runCommandErr: errors.New("run command failed"), |
| 59 | + wantErr: true, |
| 60 | + }, |
| 61 | + } |
| 62 | + for _, test := range tests { |
| 63 | + t.Run(test.name, func(t *testing.T) { |
| 64 | + t.Parallel() |
| 65 | + runCommandFn = func(ctx context.Context, command string, projectId string, push bool, build bool) error { |
| 66 | + return test.runCommandErr |
| 67 | + } |
| 68 | + runner := &publishRunner{} |
| 69 | + if err := runner.run(t.Context()); (err != nil) != test.wantErr { |
| 70 | + t.Errorf("run() error = %v, wantErr %v", err, test.wantErr) |
| 71 | + } |
| 72 | + }) |
| 73 | + } |
| 74 | +} |
0 commit comments