add tests for path and config functions
This commit is contained in:
+163
@@ -0,0 +1,163 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExpandHome(t *testing.T) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
t.Skipf("cannot get home dir: %v", err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"~", home},
|
||||
{"~/foo", filepath.Join(home, "foo")},
|
||||
{"~/foo/bar", filepath.Join(home, "foo", "bar")},
|
||||
{"/absolute/path", "/absolute/path"},
|
||||
{"relative/path", "relative/path"},
|
||||
{"", ""},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
got := expandHome(tt.input)
|
||||
if got != tt.expected {
|
||||
t.Errorf("expandHome(%q) = %q, want %q", tt.input, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompressHome(t *testing.T) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
t.Skipf("cannot get home dir: %v", err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{home, "~"},
|
||||
{filepath.Join(home, "foo"), "~/foo"},
|
||||
{filepath.Join(home, "foo", "bar"), "~/foo/bar"},
|
||||
{"/other/path", "/other/path"},
|
||||
{"relative/path", "relative/path"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
got := compressHome(tt.input)
|
||||
if got != tt.expected {
|
||||
t.Errorf("compressHome(%q) = %q, want %q", tt.input, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitPackageSpec(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
wantPkg string
|
||||
wantRel string
|
||||
wantErr bool
|
||||
}{
|
||||
{"pkg", "pkg", "", false},
|
||||
{"pkg:path", "pkg", "path", false},
|
||||
{"pkg:path/to/file", "pkg", "path/to/file", false},
|
||||
{"pkg:/leading", "pkg", "leading", false},
|
||||
{"/leading/pkg", "leading/pkg", "", false}, // leading slash trimmed
|
||||
{"", "", "", true},
|
||||
{":nope", "", "", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
pkg, rel, err := splitPackageSpec(tt.input)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("splitPackageSpec(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if pkg != tt.wantPkg || rel != tt.wantRel {
|
||||
t.Errorf("splitPackageSpec(%q) = (%q, %q), want (%q, %q)", tt.input, pkg, rel, tt.wantPkg, tt.wantRel)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectTarget(t *testing.T) {
|
||||
goos := runtime.GOOS
|
||||
osKey := goos
|
||||
if osKey == "darwin" {
|
||||
osKey = "macos"
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg *packageConfig
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "matching os",
|
||||
cfg: &packageConfig{
|
||||
targets: map[string]string{osKey: "/foo"},
|
||||
},
|
||||
want: filepath.Join("/foo"),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "fallback to default",
|
||||
cfg: &packageConfig{
|
||||
targets: map[string]string{"default": "/bar"},
|
||||
},
|
||||
want: filepath.Join("/bar"),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "os overrides default",
|
||||
cfg: &packageConfig{
|
||||
targets: map[string]string{osKey: "/os", "default": "/default"},
|
||||
},
|
||||
want: filepath.Join("/os"),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "disabled target",
|
||||
cfg: &packageConfig{
|
||||
targets: map[string]string{},
|
||||
disabled: map[string]bool{osKey: true},
|
||||
},
|
||||
want: "",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "missing target",
|
||||
cfg: &packageConfig{
|
||||
targets: map[string]string{"other": "/other"},
|
||||
},
|
||||
want: "",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := selectTarget(tt.cfg)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("selectTarget() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("selectTarget() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user