| 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 |
// http://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 execv |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"os/exec" |
| 21 |
"strings" |
| 22 |
"testing" |
| 23 |
) |
| 24 |
|
| 25 |
func TestRun(t *testing.T) { |
| 26 |
tests := []struct { |
| 27 |
name string |
| 28 |
args []string |
| 29 |
wantErr bool |
| 30 |
wantExit int |
| 31 |
wantInStderr string |
| 32 |
}{ |
| 33 |
{ |
| 34 |
name: "valid command", |
| 35 |
args: []string{"echo", "hello"}, |
| 36 |
wantErr: false, |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "invalid command", |
| 40 |
args: []string{"command-that-does-not-exist"}, |
| 41 |
wantErr: true, |
| 42 |
}, |
| 43 |
{ |
| 44 |
name: "command with non-zero exit", |
| 45 |
args: []string{"sh", "-c", "exit 1"}, |
| 46 |
wantErr: true, |
| 47 |
wantExit: 1, |
| 48 |
}, |
| 49 |
{ |
| 50 |
name: "command with stderr output", |
| 51 |
args: []string{"sh", "-c", "echo 'test error' >&2; exit 1"}, |
| 52 |
wantErr: true, |
| 53 |
wantExit: 1, |
| 54 |
wantInStderr: "test error", |
| 55 |
}, |
| 56 |
} |
| 57 |
for _, tt := range tests { |
| 58 |
t.Run(tt.name, func(t *testing.T) { |
| 59 |
err := Run(context.Background(), tt.args, ".") |
| 60 |
if (err != nil) != tt.wantErr { |
| 61 |
t.Fatalf("Run() error = %v, wantErr %v", err, tt.wantErr) |
| 62 |
} |
| 63 |
|
| 64 |
if !tt.wantErr { |
| 65 |
return |
| 66 |
} |
| 67 |
|
| 68 |
var exitErr *exec.ExitError |
| 69 |
if errors.As(err, &exitErr) { |
| 70 |
if tt.wantExit != 0 && exitErr.ExitCode() != tt.wantExit { |
| 71 |
t.Errorf("Run() exit code = %d, want %d", exitErr.ExitCode(), tt.wantExit) |
| 72 |
} |
| 73 |
if tt.wantInStderr != "" && !strings.Contains(string(exitErr.Stderr), tt.wantInStderr) { |
| 74 |
t.Errorf("Run() stderr = %q, want contains %q", string(exitErr.Stderr), tt.wantInStderr) |
| 75 |
} |
| 76 |
} |
| 77 |
}) |
| 78 |
} |
| 79 |
} |