| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package shell
- import (
- "context"
- "path/filepath"
- "runtime"
- "strings"
- "testing"
- "time"
- )
- // Benchmark to measure CPU efficiency
- func BenchmarkShellQuickCommands(b *testing.B) {
- shell := NewShell(&Options{WorkingDir: b.TempDir()})
- b.ReportAllocs()
- for b.Loop() {
- _, _, err := shell.Exec(b.Context(), "echo test")
- exitCode := ExitCode(err)
- if err != nil || exitCode != 0 {
- b.Fatalf("Command failed: %v, exit code: %d", err, exitCode)
- }
- }
- }
- func TestTestTimeout(t *testing.T) {
- // XXX(@andreynering): This fails on Windows. Address once possible.
- if runtime.GOOS == "windows" {
- t.Skip("Skipping test on Windows")
- }
- ctx, cancel := context.WithTimeout(t.Context(), time.Millisecond)
- t.Cleanup(cancel)
- shell := NewShell(&Options{WorkingDir: t.TempDir()})
- _, _, err := shell.Exec(ctx, "sleep 10")
- if status := ExitCode(err); status == 0 {
- t.Fatalf("Expected non-zero exit status, got %d", status)
- }
- if !IsInterrupt(err) {
- t.Fatalf("Expected command to be interrupted, but it was not")
- }
- if err == nil {
- t.Fatalf("Expected an error due to timeout, but got none")
- }
- }
- func TestTestCancel(t *testing.T) {
- ctx, cancel := context.WithCancel(t.Context())
- cancel() // immediately cancel the context
- shell := NewShell(&Options{WorkingDir: t.TempDir()})
- _, _, err := shell.Exec(ctx, "sleep 10")
- if status := ExitCode(err); status == 0 {
- t.Fatalf("Expected non-zero exit status, got %d", status)
- }
- if !IsInterrupt(err) {
- t.Fatalf("Expected command to be interrupted, but it was not")
- }
- if err == nil {
- t.Fatalf("Expected an error due to cancel, but got none")
- }
- }
- func TestRunCommandError(t *testing.T) {
- shell := NewShell(&Options{WorkingDir: t.TempDir()})
- _, _, err := shell.Exec(t.Context(), "nopenopenope")
- if status := ExitCode(err); status == 0 {
- t.Fatalf("Expected non-zero exit status, got %d", status)
- }
- if IsInterrupt(err) {
- t.Fatalf("Expected command to not be interrupted, but it was")
- }
- if err == nil {
- t.Fatalf("Expected an error, got nil")
- }
- }
- func TestRunContinuity(t *testing.T) {
- tempDir1 := t.TempDir()
- tempDir2 := t.TempDir()
- shell := NewShell(&Options{WorkingDir: tempDir1})
- if _, _, err := shell.Exec(t.Context(), "export FOO=bar"); err != nil {
- t.Fatalf("failed to set env: %v", err)
- }
- if _, _, err := shell.Exec(t.Context(), "cd "+filepath.ToSlash(tempDir2)); err != nil {
- t.Fatalf("failed to change directory: %v", err)
- }
- out, _, err := shell.Exec(t.Context(), "echo $FOO ; pwd")
- if err != nil {
- t.Fatalf("failed to echo: %v", err)
- }
- expect := "bar\n" + tempDir2 + "\n"
- if out != expect {
- t.Fatalf("expected output %q, got %q", expect, out)
- }
- }
- func TestCrossPlatformExecution(t *testing.T) {
- shell := NewShell(&Options{WorkingDir: "."})
- ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
- defer cancel()
- // Test a simple command that should work on all platforms
- stdout, stderr, err := shell.Exec(ctx, "echo hello")
- if err != nil {
- t.Fatalf("Echo command failed: %v, stderr: %s", err, stderr)
- }
- if stdout == "" {
- t.Error("Echo command produced no output")
- }
- // The output should contain "hello" regardless of platform
- if !strings.Contains(strings.ToLower(stdout), "hello") {
- t.Errorf("Echo output should contain 'hello', got: %q", stdout)
- }
- }
|