1
0
mirror of https://github.com/Jguer/yay.git synced 2026-04-08 23:31:10 +02:00
yay/pkg/cmd/graph/main_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

84 lines
1.8 KiB
Go

//go:build !integration
// +build !integration
package main
import (
"bytes"
"context"
"io"
"os"
"strings"
"testing"
"github.com/Jguer/aur"
"github.com/stretchr/testify/require"
"github.com/Jguer/yay/v12/pkg/db"
"github.com/Jguer/yay/v12/pkg/db/mock"
"github.com/Jguer/yay/v12/pkg/dep"
mockaur "github.com/Jguer/yay/v12/pkg/dep/mock"
"github.com/Jguer/yay/v12/pkg/text"
)
func TestGraphPackageRequiresSingleTarget(t *testing.T) {
t.Parallel()
logger := text.NewLogger(io.Discard, io.Discard, strings.NewReader(""), true, "test")
grapher := dep.NewGrapher(&mock.DBExecutor{}, &mockaur.MockAUR{}, false, false, false, false, false, logger)
err := graphPackage(context.Background(), grapher, []string{"one", "two"})
require.Error(t, err)
}
func TestGraphPackage(t *testing.T) {
t.Parallel()
logger := text.NewLogger(io.Discard, io.Discard, strings.NewReader(""), true, "test")
grapher := dep.NewGrapher(&mock.DBExecutor{
LocalPackageFn: func(string) db.IPackage { return nil },
}, &mockaur.MockAUR{
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
return []aur.Pkg{
{
Name: "target",
PackageBase: "target",
Version: "1.2.3",
},
}, nil
},
}, false, false, false, false, false, logger)
output := captureStdout(t, func() {
err := graphPackage(context.Background(), grapher, []string{"target"})
require.NoError(t, err)
})
require.Contains(t, output, "digraph {")
require.Contains(t, output, "layers map")
}
func captureStdout(t *testing.T, fn func()) string {
orig := os.Stdout
r, w, err := os.Pipe()
require.NoError(t, err)
os.Stdout = w
defer func() {
os.Stdout = orig
}()
buffer := &bytes.Buffer{}
done := make(chan struct{})
go func() {
_, _ = io.Copy(buffer, r)
close(done)
}()
fn()
require.NoError(t, w.Close())
<-done
return buffer.String()
}