tests: add more cli entrypoints functionnal tests

This commit is contained in:
Romain Bertrand 2025-12-16 12:17:07 +01:00
parent 827b193202
commit e6902583fc
2 changed files with 328 additions and 6 deletions

View file

@ -1,10 +1,14 @@
//go:build functional
// +build functional
package functional
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"code.gitea.io/sdk/gitea"
@ -12,12 +16,12 @@ import (
// TestEnv holds the functional test environment
type TestEnv struct {
Token string
Hostname string
Owner string
RepoName string
Client *gitea.Client
T *testing.T
Token string
Hostname string
Owner string
RepoName string
Client *gitea.Client
T *testing.T
}
// NewTestEnv creates a new functional test environment from environment variables
@ -98,6 +102,7 @@ func (env *TestEnv) CleanupIssue(issueNumber int64) {
}
// CreateTestPullRequest creates a test PR (requires a branch to exist)
// Note: Creating PRs requires multiple branches, so this is a placeholder for future use
func (env *TestEnv) CreateTestPullRequest(title, body, head, base string) int64 {
opts := gitea.CreatePullRequestOption{
Head: head,
@ -115,6 +120,18 @@ func (env *TestEnv) CreateTestPullRequest(title, body, head, base string) int64
return pr.Index
}
// ListPullRequests gets all PRs in the repository
func (env *TestEnv) ListPullRequests() ([]*gitea.PullRequest, error) {
prs, _, err := env.Client.ListRepoPullRequests(env.Owner, env.RepoName, gitea.ListPullRequestsOptions{})
return prs, err
}
// GetPullRequest gets a specific PR
func (env *TestEnv) GetPullRequest(prNumber int64) (*gitea.PullRequest, error) {
pr, _, err := env.Client.GetPullRequest(env.Owner, env.RepoName, prNumber)
return pr, err
}
// CleanupPullRequest closes a test PR
func (env *TestEnv) CleanupPullRequest(prNumber int64) {
closed := gitea.StateClosed
@ -133,3 +150,72 @@ func (env *TestEnv) VerifyAPIConnection() {
env.T.Fatalf("failed to verify API connection: %v", err)
}
}
// GetBinaryPath returns the path to the built fgj binary
func (env *TestEnv) GetBinaryPath() string {
binaryPath := os.Getenv("FGJ_BINARY_PATH")
if binaryPath == "" {
// Look for the binary in common locations
candidates := []string{
"./bin/fgj",
"bin/fgj",
"/home/romain/work/fgj/bin/fgj",
}
for _, candidate := range candidates {
if _, err := os.Stat(candidate); err == nil {
// Convert to absolute path
if abs, err := filepath.Abs(candidate); err == nil {
return abs
}
return candidate
}
}
// If no binary found, return default (will error when executed)
binaryPath = "./bin/fgj"
}
return binaryPath
}
// CLIResult holds the result of a CLI command execution
type CLIResult struct {
Stdout string
Stderr string
ExitCode int
}
// RunCLI executes a CLI command and returns stdout, stderr, and exit code
func (env *TestEnv) RunCLI(args ...string) *CLIResult {
cmd := exec.Command(env.GetBinaryPath(), args...)
// Set up environment with test credentials
cmd.Env = os.Environ()
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
exitCode := 0
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
exitCode = exitErr.ExitCode()
}
}
result := &CLIResult{
Stdout: stdout.String(),
Stderr: stderr.String(),
ExitCode: exitCode,
}
env.T.Logf("Command: %s %v", env.GetBinaryPath(), args)
env.T.Logf("Exit code: %d", exitCode)
if stdout.Len() > 0 {
env.T.Logf("Stdout:\n%s", result.Stdout)
}
if stderr.Len() > 0 {
env.T.Logf("Stderr:\n%s", result.Stderr)
}
return result
}