-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
66 lines (59 loc) · 1.87 KB
/
Copy pathmain_test.go
File metadata and controls
66 lines (59 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestReorderArgs(t *testing.T) {
cases := []struct {
in []string
want string
}{
// flag after the positional binary path
{[]string{"/bin/ls", "-s", "Main window"}, "-s|Main window|/bin/ls"},
// flag value kept attached, positionals preserved in order
{[]string{"bin", "-debug", "d.dSYM", "0x1000"}, "-debug|d.dSYM|bin|0x1000"},
// -s=value form is self-contained
{[]string{"bin", "-s=foo"}, "-s=foo|bin"},
// already flags-first is unchanged
{[]string{"-s", "foo", "bin"}, "-s|foo|bin"},
// everything after -- is positional
{[]string{"-s", "x", "--", "-weirdname"}, "-s|x|-weirdname"},
}
for _, c := range cases {
if got := strings.Join(reorderArgs(c.in), "|"); got != c.want {
t.Errorf("reorderArgs(%v) = %q, want %q", c.in, got, c.want)
}
}
}
func TestResolveTargetKeepsExistingFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "app")
if err := os.WriteFile(path, []byte("binary"), 0o644); err != nil {
t.Fatal(err)
}
if got := resolveTarget(path); got != path {
t.Fatalf("resolveTarget(existing file) = %q, want %q", got, path)
}
}
func TestResolveTargetLooksUpPathCommand(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "exex-test-command")
if err := os.WriteFile(path, []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", dir)
if got := resolveTarget("exex-test-command"); got != path {
t.Fatalf("resolveTarget(PATH command) = %q, want %q", got, path)
}
}
func TestResolveTargetPassesThroughUnresolvedPath(t *testing.T) {
dir := t.TempDir()
missing := filepath.Join(dir, "missing")
if got := resolveTarget(missing); got != missing {
t.Fatalf("resolveTarget(missing path) = %q, want %q", got, missing)
}
if got := resolveTarget(dir); got != dir {
t.Fatalf("resolveTarget(directory) = %q, want %q", got, dir)
}
}