feat(infra): auto-commit con 4 cambios
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
+74
-6
@@ -1,8 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -56,8 +58,19 @@ func cmdRun(args []string) {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
// When fn run executes a scoped `go test -run`, mirror its output into a
|
||||
// buffer so we can detect a "no tests to run" result — which go test reports
|
||||
// with exit 0 and would otherwise be a silent false-green (e.g. the extracted
|
||||
// unit_tests names drifted from the code). See issue 0167.
|
||||
guardGoTest := fn.Lang == "go" && isGoTestRun(cmd)
|
||||
var outBuf bytes.Buffer
|
||||
if guardGoTest {
|
||||
cmd.Stdout = io.MultiWriter(os.Stdout, &outBuf)
|
||||
cmd.Stderr = io.MultiWriter(os.Stderr, &outBuf)
|
||||
} else {
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
}
|
||||
cmd.Stdin = os.Stdin
|
||||
|
||||
fmt.Fprintf(os.Stderr, "[fn run] %s (%s/%s) %s\n", fn.ID, fn.Lang, fn.Kind, strings.Join(passArgs, " "))
|
||||
@@ -66,6 +79,13 @@ func cmdRun(args []string) {
|
||||
runErr := cmd.Run()
|
||||
durationMs := time.Since(t0).Milliseconds()
|
||||
|
||||
// A scoped go test that matched zero tests is a false-green: treat as failure.
|
||||
if guardGoTest && runErr == nil && strings.Contains(outBuf.String(), "no tests to run") {
|
||||
fmt.Fprintf(os.Stderr, "\n[fn run] error: -run no encontro ningun test para %s — los nombres de test extraidos no existen en el codigo; corre 'fn index'\n", fn.ID)
|
||||
logFnRunTelemetry(registryRoot, fn.ID, durationMs, false, "no_tests_run")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
exitCode := 0
|
||||
errClass := ""
|
||||
if runErr != nil {
|
||||
@@ -140,7 +160,7 @@ func resolveFunction(db *registry.DB, idOrName string) (*registry.Function, erro
|
||||
func buildCommand(fn *registry.Function, db *registry.DB, registryRoot, absPath string, args []string) (*exec.Cmd, error) {
|
||||
switch fn.Lang {
|
||||
case "go":
|
||||
return buildGoCommand(fn, registryRoot, absPath, args)
|
||||
return buildGoCommand(fn, db, registryRoot, absPath, args)
|
||||
case "py":
|
||||
return buildPyRunnerCommand(fn, db, registryRoot, args)
|
||||
case "bash":
|
||||
@@ -154,7 +174,7 @@ func buildCommand(fn *registry.Function, db *registry.DB, registryRoot, absPath
|
||||
}
|
||||
}
|
||||
|
||||
func buildGoCommand(fn *registry.Function, registryRoot, absPath string, args []string) (*exec.Cmd, error) {
|
||||
func buildGoCommand(fn *registry.Function, db *registry.DB, registryRoot, absPath string, args []string) (*exec.Cmd, error) {
|
||||
dir := filepath.Dir(absPath)
|
||||
env := append(os.Environ(), "CGO_ENABLED=1")
|
||||
|
||||
@@ -168,13 +188,23 @@ func buildGoCommand(fn *registry.Function, registryRoot, absPath string, args []
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
// Library code: if it has tests → go test
|
||||
// Library code with tests → go test, but scoped to THIS function's tests via
|
||||
// -run, so a flaky test of a sibling function in the same package does not
|
||||
// break `fn run`. Test names come from the indexer-extracted unit_tests table
|
||||
// (parsed from the real .go, reliable), never the .md frontmatter (can drift).
|
||||
// The cmdRun guard fails the run if -run matches zero tests, preventing a
|
||||
// silent "no tests to run" false-green. See issue 0167.
|
||||
if fn.Tested && fn.TestFilePath != "" {
|
||||
testAbs := filepath.Join(registryRoot, fn.TestFilePath)
|
||||
if _, err := os.Stat(testAbs); err == nil {
|
||||
relPkg, _ := filepath.Rel(registryRoot, dir)
|
||||
pkgPath := "./" + filepath.ToSlash(relPkg)
|
||||
cmdArgs := append([]string{"test", "-v", "-count=1", "-tags", "fts5", pkgPath}, args...)
|
||||
cmdArgs := []string{"test", "-v", "-count=1", "-tags", "fts5"}
|
||||
if names := goTestNames(db, fn.ID); len(names) > 0 {
|
||||
cmdArgs = append(cmdArgs, "-run", "^("+strings.Join(names, "|")+")$")
|
||||
}
|
||||
cmdArgs = append(cmdArgs, pkgPath)
|
||||
cmdArgs = append(cmdArgs, args...)
|
||||
cmd := exec.Command("go", cmdArgs...)
|
||||
cmd.Dir = registryRoot
|
||||
cmd.Env = env
|
||||
@@ -193,6 +223,44 @@ func buildGoCommand(fn *registry.Function, registryRoot, absPath string, args []
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
// goTestNames returns the top-level Go test function names registered for fn in
|
||||
// the indexer-extracted unit_tests table. These drive `go test -run` so that
|
||||
// `fn run` only executes the function's own tests, isolating it from flaky tests
|
||||
// of sibling functions in the same package. Returns nil if none are known (db is
|
||||
// nil, lookup fails, or no tests extracted), in which case the caller falls back
|
||||
// to running the whole package.
|
||||
func goTestNames(db *registry.DB, functionID string) []string {
|
||||
if db == nil {
|
||||
return nil
|
||||
}
|
||||
uts, err := db.GetUnitTestsByFunction(functionID)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var names []string
|
||||
for _, ut := range uts {
|
||||
if ut.Name != "" {
|
||||
names = append(names, ut.Name)
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// isGoTestRun reports whether cmd is a `go test ... -run ...` invocation, used to
|
||||
// enable the zero-tests-matched guard in cmdRun.
|
||||
func isGoTestRun(cmd *exec.Cmd) bool {
|
||||
var hasTest, hasRun bool
|
||||
for _, a := range cmd.Args {
|
||||
switch a {
|
||||
case "test":
|
||||
hasTest = true
|
||||
case "-run":
|
||||
hasRun = true
|
||||
}
|
||||
}
|
||||
return hasTest && hasRun
|
||||
}
|
||||
|
||||
|
||||
func buildBashCommand(absPath string, args []string) (*exec.Cmd, error) {
|
||||
cmdArgs := append([]string{absPath}, args...)
|
||||
|
||||
Reference in New Issue
Block a user