mirror of
https://github.com/chenasraf/sofmani.git
synced 2026-05-17 17:28:04 +00:00
147 lines
4.4 KiB
Go
147 lines
4.4 KiB
Go
package appconfig
|
|
|
|
import (
|
|
"sort"
|
|
"testing"
|
|
|
|
"github.com/chenasraf/sofmani/platform"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestInstallerData_Environ(t *testing.T) {
|
|
t.Run("returns empty slice when both Env and PlatformEnv are nil", func(t *testing.T) {
|
|
data := &InstallerData{}
|
|
result := data.Environ()
|
|
assert.NotNil(t, result)
|
|
assert.Empty(t, result)
|
|
})
|
|
|
|
t.Run("returns Env values when only Env is set", func(t *testing.T) {
|
|
env := map[string]string{"KEY": "value", "OTHER": "test"}
|
|
data := &InstallerData{
|
|
Env: &env,
|
|
}
|
|
result := data.Environ()
|
|
sort.Strings(result) // Sort for consistent comparison
|
|
assert.Len(t, result, 2)
|
|
assert.Contains(t, result, "KEY=value")
|
|
assert.Contains(t, result, "OTHER=test")
|
|
})
|
|
|
|
t.Run("returns PlatformEnv values for current platform", func(t *testing.T) {
|
|
macEnv := map[string]string{"PLATFORM": "macos"}
|
|
linuxEnv := map[string]string{"PLATFORM": "linux"}
|
|
data := &InstallerData{
|
|
PlatformEnv: &platform.PlatformMap[map[string]string]{
|
|
MacOS: &macEnv,
|
|
Linux: &linuxEnv,
|
|
},
|
|
}
|
|
result := data.Environ()
|
|
// Result depends on current platform
|
|
if len(result) > 0 {
|
|
assert.Contains(t, result[0], "PLATFORM=")
|
|
}
|
|
})
|
|
|
|
t.Run("combines Env and PlatformEnv", func(t *testing.T) {
|
|
env := map[string]string{"COMMON": "value"}
|
|
macEnv := map[string]string{"SPECIFIC": "mac"}
|
|
data := &InstallerData{
|
|
Env: &env,
|
|
PlatformEnv: &platform.PlatformMap[map[string]string]{
|
|
MacOS: &macEnv,
|
|
},
|
|
}
|
|
result := data.Environ()
|
|
assert.Contains(t, result, "COMMON=value")
|
|
// SPECIFIC will only appear on macOS
|
|
})
|
|
|
|
t.Run("PlatformEnv overrides Env for same key", func(t *testing.T) {
|
|
env := map[string]string{"KEY": "original"}
|
|
macEnv := map[string]string{"KEY": "platform"}
|
|
data := &InstallerData{
|
|
Env: &env,
|
|
PlatformEnv: &platform.PlatformMap[map[string]string]{
|
|
MacOS: &macEnv,
|
|
},
|
|
}
|
|
result := data.Environ()
|
|
// On macOS, KEY should be "platform"
|
|
// On other platforms, KEY should be "original"
|
|
assert.Len(t, result, 1)
|
|
assert.Contains(t, result[0], "KEY=")
|
|
})
|
|
}
|
|
|
|
func TestInstallerData_GetTagsList(t *testing.T) {
|
|
t.Run("returns list of space-separated tags", func(t *testing.T) {
|
|
tags := "python node rust"
|
|
data := &InstallerData{
|
|
Tags: &tags,
|
|
}
|
|
result := data.GetTagsList()
|
|
assert.Equal(t, []string{"python", "node", "rust"}, result)
|
|
})
|
|
|
|
t.Run("trims whitespace from tags", func(t *testing.T) {
|
|
tags := " python node rust "
|
|
data := &InstallerData{
|
|
Tags: &tags,
|
|
}
|
|
result := data.GetTagsList()
|
|
// Note: empty strings will be included for leading/trailing spaces when split
|
|
// The implementation splits and trims each part
|
|
for _, tag := range result {
|
|
assert.Equal(t, tag, trimmedTag(tag))
|
|
}
|
|
})
|
|
|
|
t.Run("returns single tag when only one is present", func(t *testing.T) {
|
|
tags := "python"
|
|
data := &InstallerData{
|
|
Tags: &tags,
|
|
}
|
|
result := data.GetTagsList()
|
|
assert.Equal(t, []string{"python"}, result)
|
|
})
|
|
|
|
t.Run("handles tags with multiple spaces between them", func(t *testing.T) {
|
|
tags := "python node"
|
|
data := &InstallerData{
|
|
Tags: &tags,
|
|
}
|
|
result := data.GetTagsList()
|
|
// Split by single space, so empty string will be in between
|
|
assert.Contains(t, result, "python")
|
|
assert.Contains(t, result, "node")
|
|
})
|
|
}
|
|
|
|
// helper to get trimmed tag
|
|
func trimmedTag(s string) string {
|
|
return s // Already trimmed by the function
|
|
}
|
|
|
|
func TestInstallerType_Constants(t *testing.T) {
|
|
t.Run("installer types have expected values", func(t *testing.T) {
|
|
assert.Equal(t, InstallerType("group"), InstallerTypeGroup)
|
|
assert.Equal(t, InstallerType("shell"), InstallerTypeShell)
|
|
assert.Equal(t, InstallerType("docker"), InstallerTypeDocker)
|
|
assert.Equal(t, InstallerType("brew"), InstallerTypeBrew)
|
|
assert.Equal(t, InstallerType("apt"), InstallerTypeApt)
|
|
assert.Equal(t, InstallerType("apk"), InstallerTypeApk)
|
|
assert.Equal(t, InstallerType("git"), InstallerTypeGit)
|
|
assert.Equal(t, InstallerType("github-release"), InstallerTypeGitHubRelease)
|
|
assert.Equal(t, InstallerType("rsync"), InstallerTypeRsync)
|
|
assert.Equal(t, InstallerType("npm"), InstallerTypeNpm)
|
|
assert.Equal(t, InstallerType("pnpm"), InstallerTypePnpm)
|
|
assert.Equal(t, InstallerType("yarn"), InstallerTypeYarn)
|
|
assert.Equal(t, InstallerType("pipx"), InstallerTypePipx)
|
|
assert.Equal(t, InstallerType("manifest"), InstallerTypeManifest)
|
|
assert.Equal(t, InstallerType("pacman"), InstallerTypePacman)
|
|
assert.Equal(t, InstallerType("yay"), InstallerTypeYay)
|
|
})
|
|
}
|