test(agentcompat): harden deterministic fixtures

Co-authored-by: naiba/CloudCode <hi+cloudcode@nai.ba>
This commit is contained in:
naiba
2026-07-20 04:42:03 +00:00
co-authored by naiba/CloudCode
parent fcf58b0b4b
commit 0543995ec3
9 changed files with 717 additions and 54 deletions
@@ -0,0 +1,62 @@
package testpaths
import (
"errors"
"os"
"path/filepath"
)
func NezhaSource(start string) (string, error) {
if configured := os.Getenv("NEZHA_SOURCE"); configured != "" {
return absoluteDirectory(configured)
}
return findModuleRoot(start)
}
func AgentSource(nezhaSource string) (string, error) {
if configured := os.Getenv("AGENT_SOURCE"); configured != "" {
return absoluteDirectory(configured)
}
root, err := absoluteDirectory(nezhaSource)
if err != nil {
return "", err
}
return absoluteDirectory(filepath.Join(filepath.Dir(root), "agent"))
}
func absoluteDirectory(raw string) (string, error) {
if raw == "" || !filepath.IsAbs(raw) {
return "", errors.New("source path must be absolute")
}
clean := filepath.Clean(raw)
info, err := os.Stat(clean)
if err != nil {
return "", err
}
if !info.IsDir() {
return "", errors.New("source path must be a directory")
}
return clean, nil
}
func findModuleRoot(start string) (string, error) {
if start == "" {
start, _ = os.Getwd()
}
absolute, err := filepath.Abs(start)
if err != nil {
return "", err
}
if info, statErr := os.Stat(absolute); statErr == nil && !info.IsDir() {
absolute = filepath.Dir(absolute)
}
for directory := absolute; ; directory = filepath.Dir(directory) {
if _, err := os.Stat(filepath.Join(directory, "go.mod")); err == nil {
return directory, nil
}
parent := filepath.Dir(directory)
if parent == directory {
return "", errors.New("module root not found")
}
}
}
@@ -0,0 +1,61 @@
package testpaths
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestNezhaSource_UsesModuleRootFromNonRepositoryCWD(t *testing.T) {
t.Setenv("NEZHA_SOURCE", "")
root := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(root, "go.mod"), []byte("module example.test\n"), 0o600))
nonRepository := t.TempDir()
original, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(nonRepository))
t.Cleanup(func() { require.NoError(t, os.Chdir(original)) })
resolved, err := NezhaSource(root)
require.NoError(t, err)
require.Equal(t, root, resolved)
}
func TestSourceResolversHonorExplicitEnvironmentPaths(t *testing.T) {
// Given
nezha := t.TempDir()
agent := t.TempDir()
t.Setenv("NEZHA_SOURCE", nezha)
t.Setenv("AGENT_SOURCE", agent)
// When
resolvedNezha, nezhaErr := NezhaSource(t.TempDir())
resolvedAgent, agentErr := AgentSource(nezha)
// Then
require.NoError(t, nezhaErr)
require.NoError(t, agentErr)
require.Equal(t, nezha, resolvedNezha)
require.Equal(t, agent, resolvedAgent)
}
func TestAgentSource_ResolvesAdjacentCheckoutThroughSymlink(t *testing.T) {
t.Setenv("AGENT_SOURCE", "")
parent := t.TempDir()
nezha := filepath.Join(parent, "nezha")
agent := filepath.Join(parent, "agent")
require.NoError(t, os.MkdirAll(nezha, 0o700))
require.NoError(t, os.MkdirAll(agent, 0o700))
require.NoError(t, os.WriteFile(filepath.Join(nezha, "go.mod"), []byte("module nezha.test\n"), 0o600))
require.NoError(t, os.WriteFile(filepath.Join(agent, "go.mod"), []byte("module agent.test\n"), 0o600))
linkParent := filepath.Join(t.TempDir(), "checkout")
require.NoError(t, os.Symlink(parent, linkParent))
resolved, err := AgentSource(filepath.Join(linkParent, "nezha"))
require.NoError(t, err)
require.Equal(t, filepath.Join(linkParent, "agent"), resolved)
}