|
| 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 TestNewStageRunner(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 := newStageRunner(test.cfg) |
| 41 | + if runner.projectID != test.cfg.Project { |
| 42 | + t.Errorf("newStageRunner() projectID is not set") |
| 43 | + } |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestStageRunnerRun(t *testing.T) { |
| 49 | + tests := []struct { |
| 50 | + name string |
| 51 | + runner *stageRunner |
| 52 | + runCommandErr error |
| 53 | + wantErr bool |
| 54 | + wantCmd string |
| 55 | + wantProjectID string |
| 56 | + wantPush bool |
| 57 | + }{ |
| 58 | + { |
| 59 | + name: "success", |
| 60 | + runner: &stageRunner{ |
| 61 | + projectID: "test-project", |
| 62 | + push: true, |
| 63 | + }, |
| 64 | + wantCmd: stageCmdName, |
| 65 | + wantProjectID: "test-project", |
| 66 | + wantPush: true, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "error from RunCommand", |
| 70 | + runner: &stageRunner{}, |
| 71 | + runCommandErr: errors.New("run command failed"), |
| 72 | + wantErr: true, |
| 73 | + wantCmd: stageCmdName, |
| 74 | + }, |
| 75 | + } |
| 76 | + for _, test := range tests { |
| 77 | + t.Run(test.name, func(t *testing.T) { |
| 78 | + runCommandFn = func(ctx context.Context, command string, projectId string, push bool, build bool) error { |
| 79 | + if command != test.wantCmd { |
| 80 | + t.Errorf("runCommandFn() command = %v, want %v", command, test.wantCmd) |
| 81 | + } |
| 82 | + // Only check other args on success case to avoid nil pointer with empty runner |
| 83 | + if test.runCommandErr == nil { |
| 84 | + if projectId != test.wantProjectID { |
| 85 | + t.Errorf("runCommandFn() projectId = %v, want %v", projectId, test.wantProjectID) |
| 86 | + } |
| 87 | + if push != test.wantPush { |
| 88 | + t.Errorf("runCommandFn() push = %v, want %v", push, test.wantPush) |
| 89 | + } |
| 90 | + } |
| 91 | + return test.runCommandErr |
| 92 | + } |
| 93 | + |
| 94 | + if err := test.runner.run(t.Context()); (err != nil) != test.wantErr { |
| 95 | + t.Errorf("run() error = %v, wantErr %v", err, test.wantErr) |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
0 commit comments