Files
yay/pkg/text/text_test.go
Jo 3b79933638 test: harden coverage and make tests parallel-safe (#2797)
* Test coverage hardening and parallel-safe refactors

Add focused test coverage for under-tested operational paths, edge branches,
and make key table-driven suites parallel-safe. Also fixed flaky installer
test execution under repeated/parallel runs.

* chore: fix goimports ordering for golangci-lint

* Test(download): expand integration coverage for download paths

Add integration scenarios for force/pull repo updates, repo-only and skip semantics,
and direct AUR download/scanner coverage used by getpkgbuild and package preparation.

* Test(exe): relax systemd-run path assertion

Fix flaky TestBuildGitCmd by accepting an absolute systemd-run path while still
asserting the expected binary is used.

* Test(text): avoid race in color tests

Remove parallel execution from TestColorHash so global UseColor writes do not
overlap with color consumers in parallel tests and trigger race detector
failures.

* fix potential race conditions
2026-03-25 00:51:34 +01:00

190 lines
5.3 KiB
Go

//go:build !integration
// +build !integration
package text
import (
"io"
"os"
"path"
"strings"
"testing"
"github.com/leonelquinteros/gotext"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLessRunes(t *testing.T) {
t.Parallel()
type args struct {
iRunes []rune
jRunes []rune
}
tests := []struct {
name string
args args
want bool
}{
{name: "nilslices", args: args{iRunes: nil, jRunes: nil}, want: false},
{name: "emptyslices", args: args{iRunes: []rune{}, jRunes: []rune{}}, want: false},
{name: "simpleslice a,b", args: args{iRunes: []rune{'a'}, jRunes: []rune{'b'}}, want: true},
{name: "simpleslice b,a", args: args{iRunes: []rune{'b'}, jRunes: []rune{'a'}}, want: false},
{name: "equalslice", args: args{iRunes: []rune{'a', 'a', 'a'}, jRunes: []rune{'a', 'a', 'a'}}, want: false},
{name: "uppercase", args: args{iRunes: []rune{'a'}, jRunes: []rune{'A'}}, want: false},
{name: "longerFirstArg", args: args{iRunes: []rune{'a', 'b'}, jRunes: []rune{'a'}}, want: false},
{name: "longerSecondArg", args: args{iRunes: []rune{'a'}, jRunes: []rune{'a', 'b'}}, want: true},
{name: "utf8 less", args: args{iRunes: []rune{'世', '2', '0'}, jRunes: []rune{'世', '界', '3'}}, want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := LessRunes(tt.args.iRunes, tt.args.jRunes)
assert.Equal(t, tt.want, got)
})
}
}
func TestContinueTask(t *testing.T) {
t.Parallel()
type args struct {
s string
preset bool
noConfirm bool
input string
}
tests := []struct {
name string
args args
want bool
}{
{name: "noconfirm-true", args: args{s: "", preset: true, noConfirm: true}, want: true},
{name: "noconfirm-false", args: args{s: "", preset: false, noConfirm: true}, want: false},
{name: "noinput-false", args: args{s: "", preset: false, noConfirm: false}, want: false},
{name: "noinput-true", args: args{s: "", preset: true, noConfirm: false}, want: true},
{name: "input-false", args: args{s: "", input: "n", preset: true, noConfirm: false}, want: false},
{name: "input-true", args: args{s: "", input: "y", preset: false, noConfirm: false}, want: true},
{name: "input-false-complete", args: args{s: "", input: "no", preset: true, noConfirm: false}, want: false},
{name: "input-true-complete", args: args{s: "", input: "yes", preset: false, noConfirm: false}, want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// create io.Reader with value of input
in := strings.NewReader(tt.args.input)
logger := NewLogger(io.Discard, io.Discard, in, false, "test")
got := logger.ContinueTask(tt.args.s, tt.args.preset, tt.args.noConfirm)
require.Equal(t, tt.want, got)
})
}
}
func TestContinueTaskLocalized(t *testing.T) {
strCustom := `
msgid "yes"
msgstr "да"`
// Create Locales directory and files on temp location
tmpDir := t.TempDir()
dirname := path.Join(tmpDir, "en_US")
err := os.MkdirAll(dirname, os.ModePerm)
require.NoError(t, err)
fDefault, err := os.Create(path.Join(dirname, "yay.po"))
require.NoError(t, err)
defer fDefault.Close()
_, err = fDefault.WriteString(strCustom)
require.NoError(t, err)
gotext.Configure(tmpDir, "en_US", "yay")
require.Equal(t, "да", gotext.Get("yes"))
type args struct {
s string
preset bool
noConfirm bool
input string
}
tests := []struct {
name string
args args
want bool
}{
{name: "default input false", args: args{s: "", input: "n", preset: true, noConfirm: false}, want: false},
{name: "default input true", args: args{s: "", input: "y", preset: false, noConfirm: false}, want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
in := strings.NewReader(tt.args.input)
logger := NewLogger(io.Discard, io.Discard, in, false, "test")
got := logger.ContinueTask(tt.args.s, tt.args.preset, tt.args.noConfirm)
require.Equal(t, tt.want, got)
})
}
gotext.SetLanguage("")
}
func TestCreateRepoLink(t *testing.T) {
tests := []struct {
name string
useColor bool
repo string
arch string
pkgName string
text string
want string
}{
{
name: "color disabled returns text",
useColor: false,
repo: "core",
arch: "x86_64",
pkgName: "linux",
text: "core/linux",
want: "core/linux",
},
{
name: "unknown repo returns text",
useColor: true,
repo: "unknown",
arch: "x86_64",
pkgName: "linux",
text: "core/linux",
want: "core/linux",
},
{
name: "aur repo uses package url",
useColor: true,
repo: "aur",
arch: "any",
pkgName: "yay",
text: "aur/yay",
want: "\033]8;;https://aur.archlinux.org/packages/yay\033\\aur/yay\033]8;;\033\\",
},
{
name: "core repo uses arch in url",
useColor: true,
repo: "core",
arch: "x86_64",
pkgName: "linux",
text: "core/linux",
want: "\033]8;;https://archlinux.org/packages/core/x86_64/linux\033\\core/linux\033]8;;\033\\",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
original := UseColor
UseColor = tt.useColor
t.Cleanup(func() { UseColor = original })
got := CreateRepoLink(tt.repo, tt.arch, tt.pkgName, tt.text)
assert.Equal(t, tt.want, got)
})
}
}