Limit readiness scans to selected package inputs

This commit is contained in:
2026-12-06 03:33:52 +00:00
parent 3f7dd4b2e5
commit 4b515404a8
6 changed files with 193 additions and 24 deletions

View File

@@ -386,6 +386,30 @@ func ReadTree(ctx context.Context, repositoryDir, commit string) ([]TreeEntry, e
return entries, nil
}
// ReadTreeDirectory lists immediate entries of a resolved repository-relative
// directory in a captured commit. Entry paths remain repository-relative.
func ReadTreeDirectory(ctx context.Context, repositoryDir, commit, directory string) ([]TreeEntry, error) {
if !validObjectID(commit) {
return nil, fmt.Errorf("invalid commit object ID %q", commit)
}
if directory == "" || path.IsAbs(directory) || path.Clean(directory) != directory || directory == ".." || strings.HasPrefix(directory, "../") {
return nil, fmt.Errorf("invalid committed directory %q", directory)
}
args := []string{"--literal-pathspecs", "ls-tree", "-z", "--full-tree", commit}
if directory != "." {
args = append(args, "--", directory+"/")
}
output, err := readLocalObjects(ctx, repositoryDir, nil, args...)
if err != nil {
return nil, fmt.Errorf("enumerate committed directory %q: %w", directory, err)
}
entries, err := parseTreeEntries(output)
if err != nil {
return nil, fmt.Errorf("parse committed directory %q: %w", directory, err)
}
return entries, nil
}
// ReadBlob reads the bytes for a Git blob object ID without consulting the
// worktree.
func ReadBlob(ctx context.Context, repositoryDir, objectID string) ([]byte, error) {

View File

@@ -6,10 +6,50 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
)
func TestReadTreeDirectoryListsOnlyImmediateCapturedEntries(t *testing.T) {
fixture := newRemoteFixture(t)
for _, name := range []string{"pkg[literal]/main.go", "pkg[literal]/nested/deep.go", "unrelated/elsewhere.txt"} {
filename := filepath.Join(fixture.work, filepath.FromSlash(name))
if err := os.MkdirAll(filepath.Dir(filename), 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filename, []byte("captured"), 0600); err != nil {
t.Fatal(err)
}
}
gitRun(t, fixture.work, "add", "--all")
gitRun(t, fixture.work, "commit", "-m", "capture package inputs")
commit := strings.TrimSpace(gitText(t, fixture.work, "rev-parse", "HEAD"))
if err := os.Remove(filepath.Join(fixture.work, "pkg[literal]", "main.go")); err != nil {
t.Fatal(err)
}
for _, test := range []struct {
directory string
want []string
}{
{directory: ".", want: []string{"pkg[literal]", "source.txt", "unrelated"}},
{directory: "pkg[literal]", want: []string{"pkg[literal]/main.go", "pkg[literal]/nested"}},
{directory: "pkg[literal]/nested", want: []string{"pkg[literal]/nested/deep.go"}},
} {
entries, err := ReadTreeDirectory(t.Context(), fixture.work, commit, test.directory)
if err != nil {
t.Fatal(err)
}
var names []string
for _, entry := range entries {
names = append(names, entry.Path)
}
if !slices.Equal(names, test.want) {
t.Fatalf("directory %q entries=%v want=%v", test.directory, names, test.want)
}
}
}
func TestInspectTaggedSourceSupportsLightweightAndAnnotatedTags(t *testing.T) {
fixture := newRemoteFixture(t)
gitRun(t, fixture.work, "tag", "v1.2.3")

View File

@@ -55,12 +55,12 @@ func inspectCommittedBuild(ctx context.Context, source releaseidentity.ResolvedS
if destination.Kind != git.DestinationDirectory {
return fmt.Errorf("committed main package is %s", destination.Kind)
}
entries, err := git.ReadTree(ctx, source.RepositoryDir, commit)
entries, err := git.ReadTreeDirectory(ctx, source.RepositoryDir, commit, destination.Path)
if err != nil {
return err
}
for _, entry := range entries {
if path.Dir(entry.Path) != destination.Path || !strings.HasSuffix(entry.Path, ".go") || strings.HasSuffix(entry.Path, "_test.go") {
if entry.Type == "tree" || !strings.HasSuffix(entry.Path, ".go") || strings.HasSuffix(entry.Path, "_test.go") {
continue
}
if _, err := committedFile(ctx, source.RepositoryDir, commit, entry.Path); err == nil {

View File

@@ -1,6 +1,7 @@
package readiness
import (
"fmt"
"os"
"os/exec"
"path/filepath"
@@ -11,6 +12,92 @@ import (
"go.maximumdirect.net/releasekit/internal/releaseidentity"
)
func TestReadinessInspectsImmediateLiteralPackageInputs(t *testing.T) {
for _, test := range []struct {
name, mainPackage, link, target string
files []string
manyFiles, wantPassed bool
}{
{name: "root", mainPackage: ".", files: []string{"main.go"}, wantPassed: true},
{name: "nested", mainPackage: "./cmd/app", files: []string{"cmd/app/main.go"}, wantPassed: true},
{name: "literal path", mainPackage: "./cmd/[app]", files: []string{"cmd/[app]/main.go"}, wantPassed: true},
{name: "only tests and nested sources", mainPackage: "./cmd/app", files: []string{"cmd/app/main_test.go", "cmd/app/deeper/main.go"}},
{name: "directory with Go suffix", mainPackage: "./cmd/app", files: []string{"cmd/app/main.go/contents.txt"}},
{name: "missing directory", mainPackage: "./absent"},
{name: "unreadable symlink", mainPackage: ".", link: "main.go", target: "missing.go"},
{name: "readable input after broken symlink", mainPackage: ".", files: []string{"valid.go"}, link: "broken.go", target: "missing.go", wantPassed: true},
{name: "symlinked input", mainPackage: "./cmd/app", files: []string{"shared/source.data"}, link: "cmd/app/main.go", target: "../../shared/source.data", wantPassed: true},
{name: "symlinked directory", mainPackage: "./alias", files: []string{"cmd/app/main.go"}, link: "alias", target: "cmd/app", wantPassed: true},
{name: "several batches without Go inputs", mainPackage: ".", manyFiles: true},
} {
t.Run(test.name, func(t *testing.T) {
repo := t.TempDir()
run := func(args ...string) string {
t.Helper()
command := exec.Command("git", args...)
command.Dir = repo
output, err := command.CombinedOutput()
if err != nil {
t.Fatalf("git %v: %v: %s", args, err, output)
}
return strings.TrimSpace(string(output))
}
run("init", "--initial-branch=main")
run("config", "user.name", "ReleaseKit Test")
run("config", "user.email", "releasekit@example.test")
write := func(name, contents string) {
t.Helper()
filename := filepath.Join(repo, filepath.FromSlash(name))
if err := os.MkdirAll(filepath.Dir(filename), 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filename, []byte(contents), 0600); err != nil {
t.Fatal(err)
}
}
write("go.mod", "module example.com/fixture\n\ngo 1.25.0\n")
for _, name := range test.files {
write(name, "package main\nfunc main() {}\n")
}
directory, count := "unrelated/subtree", 8
if test.manyFiles {
directory, count = ".", 260
}
for index := range count {
write(filepath.ToSlash(filepath.Join(directory, fmt.Sprintf("file-%03d.txt", index))), "ordinary input")
}
if test.link != "" {
filename := filepath.Join(repo, filepath.FromSlash(test.link))
if err := os.MkdirAll(filepath.Dir(filename), 0700); err != nil {
t.Fatal(err)
}
if err := os.Symlink(test.target, filename); err != nil {
t.Skipf("symlinks unavailable: %v", err)
}
}
run("add", "--all")
run("commit", "-m", "capture package")
captured := run("rev-parse", "HEAD")
for _, commit := range []string{"", captured} {
request := Request{RepositoryDir: repo, Commit: commit, Product: "Example", Context: "local", Offline: true, Source: &releaseidentity.SourceSpec{GoEnabled: true}, Operations: []string{"build"}, Build: &artifact.Settings{MainPackage: test.mainPackage, BinaryName: "fixture", Targets: []artifact.Target{{OS: "linux", Arch: "amd64"}}}}
report := Run(t.Context(), request)
found := false
for _, check := range report.Checks {
if check.ID == "build-inputs" {
found = true
if (check.Status == "passed") != test.wantPassed {
t.Fatalf("commit=%q check=%+v", commit, check)
}
}
}
if !found {
t.Fatalf("build inputs were not inspected: %+v", report.Checks)
}
}
})
}
}
func TestCommittedReadinessUsesCapturedModuleAndMainPackage(t *testing.T) {
repo := t.TempDir()
run := func(args ...string) string {

View File

@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"go/version"
"io"
"os"
"path/filepath"
"strings"
@@ -333,33 +334,43 @@ func inspectBuild(source releaseidentity.ResolvedSource, build artifact.Settings
if !info.IsDir() {
return errors.New("main package input is not a directory")
}
entries, err := os.ReadDir(path)
directory, err := os.Open(path)
if err != nil {
return err
}
defer directory.Close()
var inputErr error
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".go") && !strings.HasSuffix(entry.Name(), "_test.go") {
filename := filepath.Join(path, entry.Name())
info, err := os.Stat(filename)
if err != nil {
inputErr = err
continue
for {
entries, err := directory.ReadDir(128)
if err != nil && !errors.Is(err, io.EOF) {
return err
}
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".go") && !strings.HasSuffix(entry.Name(), "_test.go") {
filename := filepath.Join(path, entry.Name())
info, err := os.Stat(filename)
if err != nil {
inputErr = err
continue
}
if !info.Mode().IsRegular() {
continue
}
file, err := os.Open(filename)
if err != nil {
inputErr = err
continue
}
closeErr := file.Close()
if closeErr != nil {
inputErr = closeErr
continue
}
return nil
}
if !info.Mode().IsRegular() {
continue
}
file, err := os.Open(filename)
if err != nil {
inputErr = err
continue
}
closeErr := file.Close()
if closeErr != nil {
inputErr = closeErr
continue
}
return nil
}
if errors.Is(err, io.EOF) {
break
}
}
if inputErr != nil {