mirror of
https://github.com/gogrlx/snack.git
synced 2026-04-02 05:08:42 -07:00
Merge pull request #35 from gogrlx/cd/pacman-test-coverage
test(pacman): add comprehensive unit tests for pure functions
This commit is contained in:
161
pacman/helpers_test.go
Normal file
161
pacman/helpers_test.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package pacman
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/gogrlx/snack"
|
||||
)
|
||||
|
||||
func TestFormatTargets_Empty(t *testing.T) {
|
||||
assert.Empty(t, formatTargets(nil))
|
||||
}
|
||||
|
||||
func TestFormatTargets_NamesOnly(t *testing.T) {
|
||||
targets := []snack.Target{
|
||||
{Name: "vim"},
|
||||
{Name: "git"},
|
||||
{Name: "curl"},
|
||||
}
|
||||
got := formatTargets(targets)
|
||||
assert.Equal(t, []string{"vim", "git", "curl"}, got)
|
||||
}
|
||||
|
||||
func TestFormatTargets_WithVersions(t *testing.T) {
|
||||
targets := []snack.Target{
|
||||
{Name: "vim", Version: "9.1.0-1"},
|
||||
{Name: "git"},
|
||||
{Name: "curl", Version: "8.7.1-1"},
|
||||
}
|
||||
got := formatTargets(targets)
|
||||
assert.Equal(t, []string{"vim=9.1.0-1", "git", "curl=8.7.1-1"}, got)
|
||||
}
|
||||
|
||||
func TestBuildArgs_AllOptions(t *testing.T) {
|
||||
opts := snack.Options{
|
||||
Root: "/mnt",
|
||||
Sudo: true,
|
||||
AssumeYes: true,
|
||||
DryRun: true,
|
||||
}
|
||||
cmd, args := buildArgs([]string{"-S", "pkg"}, opts)
|
||||
assert.Equal(t, "sudo", cmd)
|
||||
// Should have: pacman -r /mnt -S pkg --noconfirm --print
|
||||
assert.Contains(t, args, "pacman")
|
||||
assert.Contains(t, args, "-r")
|
||||
assert.Contains(t, args, "/mnt")
|
||||
assert.Contains(t, args, "--noconfirm")
|
||||
assert.Contains(t, args, "--print")
|
||||
}
|
||||
|
||||
func TestBuildArgs_NoOptions(t *testing.T) {
|
||||
cmd, args := buildArgs([]string{"-Q"}, snack.Options{})
|
||||
assert.Equal(t, "pacman", cmd)
|
||||
assert.Equal(t, []string{"-Q"}, args)
|
||||
}
|
||||
|
||||
func TestBuildArgs_SudoPrependsCommand(t *testing.T) {
|
||||
cmd, args := buildArgs([]string{"-S", "vim"}, snack.Options{Sudo: true})
|
||||
assert.Equal(t, "sudo", cmd)
|
||||
require.True(t, len(args) >= 1)
|
||||
assert.Equal(t, "pacman", args[0])
|
||||
}
|
||||
|
||||
func TestBuildArgs_RootBeforeBaseArgs(t *testing.T) {
|
||||
_, args := buildArgs([]string{"-S", "vim"}, snack.Options{Root: "/alt"})
|
||||
// -r /alt should come before -S vim
|
||||
rIdx := -1
|
||||
sIdx := -1
|
||||
for i, a := range args {
|
||||
if a == "-r" {
|
||||
rIdx = i
|
||||
}
|
||||
if a == "-S" {
|
||||
sIdx = i
|
||||
}
|
||||
}
|
||||
assert.Greater(t, sIdx, rIdx, "root flag should come before base args")
|
||||
}
|
||||
|
||||
func TestParseUpgrades_Empty(t *testing.T) {
|
||||
assert.Empty(t, parseUpgrades(""))
|
||||
}
|
||||
|
||||
func TestParseUpgrades_Standard(t *testing.T) {
|
||||
input := `linux 6.7.3.arch1-1 -> 6.7.4.arch1-1
|
||||
vim 9.0.2-1 -> 9.1.0-1
|
||||
`
|
||||
pkgs := parseUpgrades(input)
|
||||
require.Len(t, pkgs, 2)
|
||||
assert.Equal(t, "linux", pkgs[0].Name)
|
||||
assert.Equal(t, "6.7.4.arch1-1", pkgs[0].Version)
|
||||
assert.True(t, pkgs[0].Installed)
|
||||
assert.Equal(t, "vim", pkgs[1].Name)
|
||||
assert.Equal(t, "9.1.0-1", pkgs[1].Version)
|
||||
}
|
||||
|
||||
func TestParseUpgrades_FallbackFormat(t *testing.T) {
|
||||
// Some versions of pacman might output "pkg newver" without the arrow
|
||||
input := "pkg 2.0\n"
|
||||
pkgs := parseUpgrades(input)
|
||||
require.Len(t, pkgs, 1)
|
||||
assert.Equal(t, "pkg", pkgs[0].Name)
|
||||
assert.Equal(t, "2.0", pkgs[0].Version)
|
||||
}
|
||||
|
||||
func TestParseUpgrades_WhitespaceLines(t *testing.T) {
|
||||
input := "\n \nlinux 6.7.3 -> 6.7.4\n\n"
|
||||
pkgs := parseUpgrades(input)
|
||||
require.Len(t, pkgs, 1)
|
||||
}
|
||||
|
||||
func TestParseGroupPkgSet_Empty(t *testing.T) {
|
||||
set := parseGroupPkgSet("")
|
||||
assert.Empty(t, set)
|
||||
}
|
||||
|
||||
func TestParseGroupPkgSet_Standard(t *testing.T) {
|
||||
input := `base-devel autoconf
|
||||
base-devel automake
|
||||
base-devel binutils
|
||||
base-devel gcc
|
||||
base-devel make
|
||||
`
|
||||
set := parseGroupPkgSet(input)
|
||||
assert.Len(t, set, 5)
|
||||
assert.Contains(t, set, "autoconf")
|
||||
assert.Contains(t, set, "automake")
|
||||
assert.Contains(t, set, "binutils")
|
||||
assert.Contains(t, set, "gcc")
|
||||
assert.Contains(t, set, "make")
|
||||
}
|
||||
|
||||
func TestParseGroupPkgSet_SingleField(t *testing.T) {
|
||||
// Lines with fewer than 2 fields should be skipped
|
||||
input := "orphan\ngroup pkg\n"
|
||||
set := parseGroupPkgSet(input)
|
||||
assert.Len(t, set, 1)
|
||||
assert.Contains(t, set, "pkg")
|
||||
}
|
||||
|
||||
func TestParseGroupPkgSet_Duplicates(t *testing.T) {
|
||||
input := `group pkg1
|
||||
group pkg1
|
||||
group pkg2
|
||||
`
|
||||
set := parseGroupPkgSet(input)
|
||||
assert.Len(t, set, 2)
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
p := New()
|
||||
assert.NotNil(t, p)
|
||||
assert.Equal(t, "pacman", p.Name())
|
||||
}
|
||||
|
||||
func TestSupportsDryRun(t *testing.T) {
|
||||
p := New()
|
||||
assert.True(t, p.SupportsDryRun())
|
||||
}
|
||||
163
pacman/parse_test.go
Normal file
163
pacman/parse_test.go
Normal file
@@ -0,0 +1,163 @@
|
||||
package pacman
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestParseList_Empty(t *testing.T) {
|
||||
assert.Empty(t, parseList(""))
|
||||
}
|
||||
|
||||
func TestParseList_WhitespaceOnly(t *testing.T) {
|
||||
assert.Empty(t, parseList(" \n \n\n"))
|
||||
}
|
||||
|
||||
func TestParseList_SinglePackage(t *testing.T) {
|
||||
pkgs := parseList("vim 9.1.0-1\n")
|
||||
require.Len(t, pkgs, 1)
|
||||
assert.Equal(t, "vim", pkgs[0].Name)
|
||||
assert.Equal(t, "9.1.0-1", pkgs[0].Version)
|
||||
assert.True(t, pkgs[0].Installed)
|
||||
}
|
||||
|
||||
func TestParseList_MalformedLine(t *testing.T) {
|
||||
// Line with only one field should be skipped
|
||||
pkgs := parseList("orphaned-line\nvalid 1.0\n")
|
||||
require.Len(t, pkgs, 1)
|
||||
assert.Equal(t, "valid", pkgs[0].Name)
|
||||
}
|
||||
|
||||
func TestParseList_ExtraFields(t *testing.T) {
|
||||
// Extra fields beyond name+version should be ignored
|
||||
pkgs := parseList("pkg 1.0 extra stuff\n")
|
||||
require.Len(t, pkgs, 1)
|
||||
assert.Equal(t, "pkg", pkgs[0].Name)
|
||||
assert.Equal(t, "1.0", pkgs[0].Version)
|
||||
}
|
||||
|
||||
func TestParseList_TrailingNewlines(t *testing.T) {
|
||||
pkgs := parseList("a 1.0\nb 2.0\n\n\n")
|
||||
require.Len(t, pkgs, 2)
|
||||
}
|
||||
|
||||
func TestParseSearch_Empty(t *testing.T) {
|
||||
assert.Empty(t, parseSearch(""))
|
||||
}
|
||||
|
||||
func TestParseSearch_NoDescription(t *testing.T) {
|
||||
// Package line with no following description line
|
||||
pkgs := parseSearch("core/pkg 1.0\n")
|
||||
require.Len(t, pkgs, 1)
|
||||
assert.Equal(t, "core", pkgs[0].Repository)
|
||||
assert.Equal(t, "pkg", pkgs[0].Name)
|
||||
assert.Equal(t, "1.0", pkgs[0].Version)
|
||||
assert.Empty(t, pkgs[0].Description)
|
||||
}
|
||||
|
||||
func TestParseSearch_NoRepo(t *testing.T) {
|
||||
// Name without repo/ prefix
|
||||
pkgs := parseSearch("standalone 3.0\n A standalone package\n")
|
||||
require.Len(t, pkgs, 1)
|
||||
assert.Empty(t, pkgs[0].Repository)
|
||||
assert.Equal(t, "standalone", pkgs[0].Name)
|
||||
assert.Equal(t, "A standalone package", pkgs[0].Description)
|
||||
}
|
||||
|
||||
func TestParseSearch_InstalledBrackets(t *testing.T) {
|
||||
pkgs := parseSearch("extra/tmux 3.4-1 [installed]\n A terminal multiplexer\n")
|
||||
require.Len(t, pkgs, 1)
|
||||
assert.True(t, pkgs[0].Installed)
|
||||
}
|
||||
|
||||
func TestParseSearch_InstalledPartialVersion(t *testing.T) {
|
||||
// [installed: 3.3-1] format
|
||||
pkgs := parseSearch("extra/tmux 3.4-1 [installed: 3.3-1]\n A terminal multiplexer\n")
|
||||
require.Len(t, pkgs, 1)
|
||||
assert.True(t, pkgs[0].Installed)
|
||||
}
|
||||
|
||||
func TestParseSearch_NotInstalled(t *testing.T) {
|
||||
pkgs := parseSearch("community/rare-pkg 0.1-1\n Something obscure\n")
|
||||
require.Len(t, pkgs, 1)
|
||||
assert.False(t, pkgs[0].Installed)
|
||||
}
|
||||
|
||||
func TestParseSearch_MultiplePackages(t *testing.T) {
|
||||
input := `core/linux 6.7.4.arch1-1 [installed]
|
||||
The Linux kernel and modules
|
||||
extra/linux-lts 6.6.14-1
|
||||
The LTS Linux kernel and modules
|
||||
community/linux-zen 6.7.4.zen1-1
|
||||
The Zen Linux kernel
|
||||
`
|
||||
pkgs := parseSearch(input)
|
||||
require.Len(t, pkgs, 3)
|
||||
assert.Equal(t, "linux", pkgs[0].Name)
|
||||
assert.Equal(t, "linux-lts", pkgs[1].Name)
|
||||
assert.Equal(t, "linux-zen", pkgs[2].Name)
|
||||
}
|
||||
|
||||
func TestParseSearch_DescriptionOnlyLines(t *testing.T) {
|
||||
// Lines starting with whitespace without a preceding header should be skipped
|
||||
input := ` orphaned description
|
||||
core/valid 1.0
|
||||
Real description
|
||||
`
|
||||
pkgs := parseSearch(input)
|
||||
require.Len(t, pkgs, 1)
|
||||
assert.Equal(t, "valid", pkgs[0].Name)
|
||||
assert.Equal(t, "Real description", pkgs[0].Description)
|
||||
}
|
||||
|
||||
func TestParseInfo_Empty(t *testing.T) {
|
||||
assert.Nil(t, parseInfo(""))
|
||||
}
|
||||
|
||||
func TestParseInfo_NoName(t *testing.T) {
|
||||
// If Name field is missing, returns nil
|
||||
pkg := parseInfo("Version : 1.0\nDescription : Something\n")
|
||||
assert.Nil(t, pkg)
|
||||
}
|
||||
|
||||
func TestParseInfo_AllFields(t *testing.T) {
|
||||
input := `Repository : extra
|
||||
Name : neovim
|
||||
Version : 0.10.0-1
|
||||
Description : Fork of Vim aiming to improve user experience
|
||||
Architecture : x86_64
|
||||
URL : https://neovim.io
|
||||
`
|
||||
pkg := parseInfo(input)
|
||||
require.NotNil(t, pkg)
|
||||
assert.Equal(t, "neovim", pkg.Name)
|
||||
assert.Equal(t, "0.10.0-1", pkg.Version)
|
||||
assert.Equal(t, "Fork of Vim aiming to improve user experience", pkg.Description)
|
||||
assert.Equal(t, "x86_64", pkg.Arch)
|
||||
assert.Equal(t, "extra", pkg.Repository)
|
||||
}
|
||||
|
||||
func TestParseInfo_ColonInValue(t *testing.T) {
|
||||
// Value itself contains colons — only the first colon should be used as delimiter
|
||||
input := `Name : pkg
|
||||
Description : A tool: does things: many of them
|
||||
Version : 1.0
|
||||
`
|
||||
pkg := parseInfo(input)
|
||||
require.NotNil(t, pkg)
|
||||
assert.Equal(t, "A tool: does things: many of them", pkg.Description)
|
||||
}
|
||||
|
||||
func TestParseInfo_UnrecognizedKeys(t *testing.T) {
|
||||
input := `Name : test
|
||||
Licenses : MIT
|
||||
Version : 2.0
|
||||
Packager : Someone
|
||||
`
|
||||
pkg := parseInfo(input)
|
||||
require.NotNil(t, pkg)
|
||||
assert.Equal(t, "test", pkg.Name)
|
||||
assert.Equal(t, "2.0", pkg.Version)
|
||||
}
|
||||
Reference in New Issue
Block a user