viye/internal/plumbing/shell/shell_test.go (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com resturcture + tests + expand shell, 2 months ago
olexsmir@gmail.com resturcture + tests + expand shell, 2 months ago
| 1 | package shell |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/olexsmir/viye/internal/viye" |
| 9 | ) |
| 10 | |
| 11 | func TestMatch(t *testing.T) { |
| 12 | tests := []struct { |
| 13 | path string |
| 14 | cmd string |
| 15 | args []string |
| 16 | want bool |
| 17 | }{ |
| 18 | {"$", "$", nil, true}, |
| 19 | {"$ go run .", "$", []string{"go", "run", "."}, true}, |
| 20 | {"$$", "$$", nil, true}, |
| 21 | {"$$ sleep 10", "$$", []string{"sleep", "10"}, true}, |
| 22 | {"kill 1234", "kill", []string{"1234"}, true}, |
| 23 | {"kill abc", "kill", []string{"abc"}, true}, |
| 24 | {"mkdir dir", "mkdir", []string{"dir"}, true}, |
| 25 | {"mkdir dir1 dir2", "mkdir", []string{"dir1", "dir2"}, true}, |
| 26 | {"", "", nil, false}, |
| 27 | {"ip", "ip", nil, false}, |
| 28 | {"kill", "kill", nil, false}, |
| 29 | {"kill 1 2", "kill", []string{"1", "2"}, false}, |
| 30 | {"mkdir", "mkdir", nil, false}, |
| 31 | } |
| 32 | for _, tt := range tests { |
| 33 | got := (&Tool{}).Match(&viye.Context{ |
| 34 | Path: []string{tt.path}, |
| 35 | Cmd: tt.cmd, |
| 36 | Args: tt.args, |
| 37 | }) |
| 38 | if got != tt.want { |
| 39 | t.Errorf("Match(%q) = %v; want %v", tt.path, got, tt.want) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestExecute(t *testing.T) { |
| 45 | t.Run("cmd", func(t *testing.T) { |
| 46 | got, err := (&Tool{}).Execute(&viye.Context{ |
| 47 | Path: []string{"$ echo hello"}, |
| 48 | Cmd: "$", |
| 49 | Args: []string{"echo", "hello"}, |
| 50 | Dir: ".", |
| 51 | }) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | if got != "| hello\n" { |
| 56 | t.Errorf("Execute($ echo hello) = %q; want %q", got, "| hello\\n") |
| 57 | } |
| 58 | }) |
| 59 | |
| 60 | t.Run("bg cmd", func(t *testing.T) { |
| 61 | got, err := (&Tool{}).Execute(&viye.Context{ |
| 62 | Path: []string{"$$ sleep 0"}, |
| 63 | Cmd: "$$", |
| 64 | Args: []string{"sleep", "0"}, |
| 65 | Dir: ".", |
| 66 | }) |
| 67 | if err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | if got != "| pid: " && len(got) < 10 { |
| 71 | t.Errorf("Execute($$ sleep 0) = %q; want pid string", got) |
| 72 | } |
| 73 | }) |
| 74 | |
| 75 | t.Run("mkdir", func(t *testing.T) { |
| 76 | dir := t.TempDir() |
| 77 | got, err := (&Tool{}).Execute(&viye.Context{ |
| 78 | Path: []string{"mkdir sub"}, |
| 79 | Cmd: "mkdir", |
| 80 | Args: []string{"sub"}, |
| 81 | Dir: dir, |
| 82 | }) |
| 83 | if err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | if _, err := os.Stat(filepath.Join(dir, "sub")); err != nil { |
| 87 | t.Errorf("dir not created: %v", err) |
| 88 | } |
| 89 | if got != "| created "+filepath.Join(dir, "sub")+"\n" { |
| 90 | t.Errorf("output = %q; want %q", got, "| created "+filepath.Join(dir, "sub")+"\n") |
| 91 | } |
| 92 | }) |
| 93 | |
| 94 | t.Run("kill", func(t *testing.T) { |
| 95 | // no reliable way to test kill without a real process, |
| 96 | // just verify it doesn't panic with valid args structure |
| 97 | if _, err := (&Tool{}).Execute(&viye.Context{ |
| 98 | Path: []string{"kill 999999"}, |
| 99 | Cmd: "kill", |
| 100 | Args: []string{"999999"}, |
| 101 | Dir: ".", |
| 102 | }); err == nil { |
| 103 | t.Log("kill on bogus pid returned nil (expected error or success)") |
| 104 | } |
| 105 | }) |
| 106 | } |