|
| 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 rustrelease |
| 16 | + |
| 17 | +import ( |
| 18 | + "os/exec" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/googleapis/librarian/internal/sidekick/internal/config" |
| 22 | +) |
| 23 | + |
| 24 | +func TestPreflightSuccess(t *testing.T) { |
| 25 | + const echo = "/bin/echo" |
| 26 | + requireCommand(t, echo) |
| 27 | + release := config.Release{ |
| 28 | + Preinstalled: map[string]string{ |
| 29 | + "git": echo, |
| 30 | + "cargo": echo, |
| 31 | + }, |
| 32 | + } |
| 33 | + if err := PreFlight(&release); err != nil { |
| 34 | + t.Fatal(err) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func TestPreflightMissingGit(t *testing.T) { |
| 39 | + release := config.Release{ |
| 40 | + Preinstalled: map[string]string{ |
| 41 | + "git": "git-is-not-installed", |
| 42 | + }, |
| 43 | + } |
| 44 | + if err := PreFlight(&release); err == nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestPreflightMissingCargo(t *testing.T) { |
| 50 | + requireCommand(t, "git") |
| 51 | + release := config.Release{ |
| 52 | + Preinstalled: map[string]string{ |
| 53 | + "cargo": "cargo-is-not-installed", |
| 54 | + }, |
| 55 | + } |
| 56 | + if err := PreFlight(&release); err == nil { |
| 57 | + t.Fatal(err) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestGitExe(t *testing.T) { |
| 62 | + release := config.Release{} |
| 63 | + if got := gitExe(&release); got != "git" { |
| 64 | + t.Errorf("mismatch in gitExe(), want=git, got=%s", got) |
| 65 | + } |
| 66 | + release = config.Release{ |
| 67 | + Preinstalled: map[string]string{ |
| 68 | + "git": "alternative", |
| 69 | + }, |
| 70 | + } |
| 71 | + if got := gitExe(&release); got != "alternative" { |
| 72 | + t.Errorf("mismatch in gitExe(), want=alternative, got=%s", got) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestCargoExe(t *testing.T) { |
| 77 | + release := config.Release{} |
| 78 | + if got := cargoExe(&release); got != "cargo" { |
| 79 | + t.Errorf("mismatch in cargoExe(), want=cargo, got=%s", got) |
| 80 | + } |
| 81 | + release = config.Release{ |
| 82 | + Preinstalled: map[string]string{ |
| 83 | + "cargo": "alternative", |
| 84 | + }, |
| 85 | + } |
| 86 | + if got := cargoExe(&release); got != "alternative" { |
| 87 | + t.Errorf("mismatch in cargoExe(), want=alternative, got=%s", got) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func requireCommand(t *testing.T, command string) { |
| 92 | + t.Helper() |
| 93 | + if _, err := exec.LookPath(command); err != nil { |
| 94 | + t.Skipf("skipping test because %s is not installed", command) |
| 95 | + } |
| 96 | +} |
0 commit comments