feat(assessment): report planned bank coverage
This commit is contained in:
1 parent
76d32bb79f
commit
a69b1a79bb
10 files changed
+1211
-288
No files matched your search
@@ -0,0 +1,225 @@
|
||||
// Package bankcoverage implements the local assessment bank coverage command.
|
||||
package bankcoverage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
assessment "lincodex/backend/internal/usecase/assessment"
|
||||
)
|
||||
|
||||
// Run reports how valid bundle drafts and implementation paths match the bank manifest.
|
||||
func Run(arguments []string, stdout, stderr io.Writer) error {
|
||||
const location = "bankcoverage.Run"
|
||||
flags := flag.NewFlagSet("assessment-bank-coverage", flag.ContinueOnError)
|
||||
flags.SetOutput(stderr)
|
||||
manifestPath := flags.String("manifest", "testdata/assessment/bank_manifest.json", "path to the planned bank manifest")
|
||||
bundleDir := flags.String("bundles", "testdata/assessment", "directory containing bank bundle JSON files and solution files")
|
||||
strict := flags.Bool("strict", false, "fail when planned families, review declarations, or implementation paths are missing")
|
||||
if err := flags.Parse(arguments); err != nil {
|
||||
return fmt.Errorf("%s - parse arguments: %w", location, err)
|
||||
}
|
||||
if flags.NArg() != 0 {
|
||||
return fmt.Errorf("%s - unexpected positional arguments", location)
|
||||
}
|
||||
content, err := os.ReadFile(*manifestPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - read manifest: %w", location, err)
|
||||
}
|
||||
manifest, err := assessment.ParseBankCoverageManifest(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
inventory, err := loadBankCoverageInventory(*bundleDir, *manifestPath, manifest)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - load bundle inventory: %w", location, err)
|
||||
}
|
||||
report, err := assessment.BuildBankCoverageReport(manifest, inventory)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - build report: %w", location, err)
|
||||
}
|
||||
if err := printBankCoverageReport(stdout, report); err != nil {
|
||||
return fmt.Errorf("%s - write report: %w", location, err)
|
||||
}
|
||||
if *strict {
|
||||
return report.StrictError()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadBankCoverageInventory(bundleDir, manifestPath string, manifest assessment.BankCoverageManifest) (result assessment.BankCoverageInventory, returnErr error) {
|
||||
const location = "bankcoverage.loadBankCoverageInventory"
|
||||
manifestPath, err := canonicalPath(manifestPath)
|
||||
if err != nil {
|
||||
return assessment.BankCoverageInventory{}, fmt.Errorf("%s - resolve manifest path: %w", location, err)
|
||||
}
|
||||
bundleRoot, err := os.OpenRoot(bundleDir)
|
||||
if err != nil {
|
||||
return assessment.BankCoverageInventory{}, fmt.Errorf("%s - open bundle directory: %w", location, err)
|
||||
}
|
||||
defer func() {
|
||||
if err := bundleRoot.Close(); err != nil {
|
||||
returnErr = errors.Join(returnErr, fmt.Errorf("%s - close bundle directory: %w", location, err))
|
||||
}
|
||||
}()
|
||||
inventory := assessment.BankCoverageInventory{}
|
||||
err = fs.WalkDir(bundleRoot.FS(), ".", func(path string, entry fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return fmt.Errorf("%s - scan bundle directory: %w", location, walkErr)
|
||||
}
|
||||
if entry.IsDir() || filepath.Ext(path) != ".json" {
|
||||
return nil
|
||||
}
|
||||
candidatePath, err := canonicalPath(filepath.Join(bundleDir, filepath.FromSlash(path)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - resolve candidate path %s: %w", location, filepath.ToSlash(path), err)
|
||||
}
|
||||
if candidatePath == manifestPath {
|
||||
return nil
|
||||
}
|
||||
content, err := bundleRoot.ReadFile(filepath.FromSlash(path))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - read bundle %s: %w", location, filepath.ToSlash(path), err)
|
||||
}
|
||||
bundle, err := assessment.ParseBundle(bytes.NewReader(content))
|
||||
if err != nil {
|
||||
inventory.InvalidBundleFiles = append(inventory.InvalidBundleFiles, fmt.Sprintf("%s: %v", filepath.ToSlash(path), err))
|
||||
return nil
|
||||
}
|
||||
drafts, err := bundle.Drafts()
|
||||
if err != nil {
|
||||
inventory.InvalidBundleFiles = append(inventory.InvalidBundleFiles, fmt.Sprintf("%s: %v", filepath.ToSlash(path), err))
|
||||
return nil
|
||||
}
|
||||
for _, draft := range drafts {
|
||||
core := draft.Core()
|
||||
inventory.Families = append(inventory.Families, assessment.BankCoverageFamilyRecord{
|
||||
Key: string(core.Key), Category: string(core.Category), Version: core.Version,
|
||||
Author: string(core.Author), Reviewer: string(core.Reviewer),
|
||||
ReviewStatus: string(core.ReviewStatus), Source: filepath.ToSlash(path),
|
||||
})
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return assessment.BankCoverageInventory{}, err
|
||||
}
|
||||
if err := collectImplementationPaths(bundleRoot, manifest, &inventory); err != nil {
|
||||
return assessment.BankCoverageInventory{}, fmt.Errorf("%s - collect implementation paths: %w", location, err)
|
||||
}
|
||||
return inventory, nil
|
||||
}
|
||||
|
||||
func collectImplementationPaths(root *os.Root, manifest assessment.BankCoverageManifest, inventory *assessment.BankCoverageInventory) error {
|
||||
const location = "bankcoverage.collectImplementationPaths"
|
||||
for _, category := range manifest.Categories {
|
||||
for _, topic := range category.Topics {
|
||||
for _, key := range topic.FamilyKeys {
|
||||
for _, language := range category.RequiredLanguages {
|
||||
relative := filepath.Join(category.ImplementationsDir, key+implementationExtension(language))
|
||||
info, err := root.Stat(relative)
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("%s - inspect %s: %w", location, filepath.ToSlash(relative), err)
|
||||
}
|
||||
if info.Mode().IsRegular() && info.Size() > 0 {
|
||||
inventory.ImplementationFiles = append(inventory.ImplementationFiles, filepath.ToSlash(relative))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func canonicalPath(path string) (string, error) {
|
||||
absPath, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
resolvedPath, err := filepath.EvalSymlinks(absPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Clean(resolvedPath), nil
|
||||
}
|
||||
|
||||
func implementationExtension(language string) string {
|
||||
if language == "python" {
|
||||
return ".py"
|
||||
}
|
||||
return ".go"
|
||||
}
|
||||
|
||||
func printBankCoverageReport(writer io.Writer, report assessment.BankCoverageReport) error {
|
||||
const location = "bankcoverage.printBankCoverageReport"
|
||||
write := func(format string, values ...any) error {
|
||||
if _, err := fmt.Fprintf(writer, format, values...); err != nil {
|
||||
return fmt.Errorf("%s - write output: %w", location, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := write("Assessment bank coverage inventory\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := write("Family records are valid drafts from bundle files; this report does not publish or calibrate them.\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, category := range report.Categories {
|
||||
if err := write("%s: %d/%d planned family keys present; %d keys inventoried; %d/%d implementation paths present\n",
|
||||
category.Code, category.PresentFamilies, category.PlannedFamilies, category.InventoriedFamilies,
|
||||
category.PresentImplementationFiles, category.RequiredImplementationFiles); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := write("Review declarations: %d approved, %d pending, %d rejected; %d distinct family keys inventoried\n",
|
||||
report.DeclaredApprovedReviews, report.PendingReviewDeclarations, report.RejectedReviewDeclarations, report.InventoriedFamilies); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, names := range []struct {
|
||||
label string
|
||||
names []string
|
||||
}{
|
||||
{"Missing family keys", report.MissingFamilyKeys},
|
||||
{"Families without an approved independent reviewer declaration", report.MissingIndependentReviews},
|
||||
{"Missing Go/Python implementation paths", report.MissingImplementationFiles},
|
||||
{"Unassigned family keys", report.UnassignedFamilyKeys},
|
||||
{"Unassigned categories", report.UnassignedCategories},
|
||||
{"Category mismatches", report.CategoryMismatches},
|
||||
{"Invalid bundle records", report.InvalidBundleFiles},
|
||||
{"Evidence not verifiable by this check", report.UnverifiableEvidence},
|
||||
} {
|
||||
if err := printNames(writer, names.label, names.names); err != nil {
|
||||
return fmt.Errorf("%s - print %s: %w", location, names.label, err)
|
||||
}
|
||||
}
|
||||
if report.CalibrationMeasurementRequired {
|
||||
if err := write("Calibration thresholds remain external measurement gates; no quality result is inferred.\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func printNames(writer io.Writer, label string, names []string) error {
|
||||
if len(names) == 0 {
|
||||
return nil
|
||||
}
|
||||
if _, err := fmt.Fprintf(writer, "%s (%d):\n", label, len(names)); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, name := range names {
|
||||
if _, err := fmt.Fprintf(writer, " %s\n", name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package bankcoverage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const assessmentFixturePath = "../../../../../testdata/assessment"
|
||||
|
||||
func TestRunReportsIncompleteInventoryWithoutFailing(t *testing.T) {
|
||||
manifestPath, bundleDir := prepareCoverageFixtures(t)
|
||||
var output bytes.Buffer
|
||||
if err := Run([]string{"--manifest", manifestPath, "--bundles", bundleDir}, &output, &output); err != nil {
|
||||
t.Fatalf("run() error = %v, want informational success", err)
|
||||
}
|
||||
if !strings.Contains(output.String(), "backend: 0/25 planned family keys present") ||
|
||||
!strings.Contains(output.String(), "network: 0/15 planned family keys present") {
|
||||
t.Fatalf("report omitted incomplete family counts:\n%s", output.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunStrictRefusesIncompleteInventory(t *testing.T) {
|
||||
manifestPath, bundleDir := prepareCoverageFixtures(t)
|
||||
var output bytes.Buffer
|
||||
err := Run([]string{"--strict", "--manifest", manifestPath, "--bundles", bundleDir}, &output, &output)
|
||||
if err == nil || !strings.Contains(err.Error(), "assessment.BankCoverageReport.StrictError") {
|
||||
t.Fatalf("run(--strict) error = %v, want strict incomplete-inventory error", err)
|
||||
}
|
||||
if !strings.Contains(output.String(), "Missing family keys (40)") {
|
||||
t.Fatalf("strict report omitted missing keys:\n%s", output.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunReportsMalformedBundleAndStrictModeRejectsIt(t *testing.T) {
|
||||
manifestPath, bundleDir := prepareCoverageFixtures(t)
|
||||
malformedPath := filepath.Join(bundleDir, "malformed.json")
|
||||
if err := os.WriteFile(malformedPath, []byte("not a bundle"), 0o600); err != nil {
|
||||
t.Fatalf("write malformed bundle: %v", err)
|
||||
}
|
||||
var output bytes.Buffer
|
||||
if err := Run([]string{"--manifest", manifestPath, "--bundles", bundleDir}, &output, &output); err != nil {
|
||||
t.Fatalf("run() error = %v, want informational success", err)
|
||||
}
|
||||
if !strings.Contains(output.String(), "Invalid bundle records (1)") || !strings.Contains(output.String(), "malformed.json") {
|
||||
t.Fatalf("report omitted malformed bundle diagnostic:\n%s", output.String())
|
||||
}
|
||||
if err := Run([]string{"--strict", "--manifest", manifestPath, "--bundles", bundleDir}, &output, &output); err == nil {
|
||||
t.Fatal("run(--strict) error = nil, want malformed bundle refusal")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunSkipsTheCustomManifestInsideBundleDirectory(t *testing.T) {
|
||||
_, bundleDir := prepareCoverageFixtures(t)
|
||||
manifestPath := filepath.Join(bundleDir, "plan-custom.json")
|
||||
content, err := os.ReadFile(filepath.Join(assessmentFixturePath, "bank_manifest.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("read manifest fixture: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(manifestPath, content, 0o600); err != nil {
|
||||
t.Fatalf("write custom manifest: %v", err)
|
||||
}
|
||||
var output bytes.Buffer
|
||||
if err := Run([]string{"--manifest", manifestPath, "--bundles", bundleDir}, &output, &output); err != nil {
|
||||
t.Fatalf("run() error = %v, want report success", err)
|
||||
}
|
||||
if strings.Contains(output.String(), "plan-custom.json") {
|
||||
t.Fatalf("custom manifest was parsed as a bundle:\n%s", output.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunPropagatesOutputWriteFailure(t *testing.T) {
|
||||
manifestPath, bundleDir := prepareCoverageFixtures(t)
|
||||
writeErr := errors.New("closed output")
|
||||
err := Run([]string{"--manifest", manifestPath, "--bundles", bundleDir}, failingCoverageWriter{err: writeErr}, &bytes.Buffer{})
|
||||
if !errors.Is(err, writeErr) {
|
||||
t.Fatalf("run() error = %v, want wrapped output error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func prepareCoverageFixtures(t *testing.T) (string, string) {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
bundleDir := filepath.Join(root, "bundles")
|
||||
if err := os.Mkdir(bundleDir, 0o700); err != nil {
|
||||
t.Fatalf("create bundle directory: %v", err)
|
||||
}
|
||||
manifestContent, err := os.ReadFile(filepath.Join(assessmentFixturePath, "bank_manifest.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("read manifest fixture: %v", err)
|
||||
}
|
||||
manifestPath := filepath.Join(root, "bank-plan.json")
|
||||
if err := os.WriteFile(manifestPath, manifestContent, 0o600); err != nil {
|
||||
t.Fatalf("write manifest fixture: %v", err)
|
||||
}
|
||||
bundleContent, err := os.ReadFile(filepath.Join(assessmentFixturePath, "backend_demo_bundle.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("read demo bundle fixture: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(bundleDir, "backend_demo_bundle.json"), bundleContent, 0o600); err != nil {
|
||||
t.Fatalf("write demo bundle fixture: %v", err)
|
||||
}
|
||||
return manifestPath, bundleDir
|
||||
}
|
||||
|
||||
type failingCoverageWriter struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (writer failingCoverageWriter) Write([]byte) (int, error) {
|
||||
return 0, writer.err
|
||||
}
|
||||
@@ -72,9 +72,9 @@ func NewStorage(settings config.Config) (*Storage, func(), error) {
|
||||
CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
|
||||
},
|
||||
// A non-seekable upload cannot be replayed safely.
|
||||
Retryer: aws.NopRetryer{},
|
||||
RequestChecksumCalculation: aws.RequestChecksumCalculationWhenRequired,
|
||||
ResponseChecksumValidation: aws.ResponseChecksumValidationWhenRequired,
|
||||
Retryer: aws.NopRetryer{},
|
||||
RequestChecksumCalculation: aws.RequestChecksumCalculationWhenRequired,
|
||||
ResponseChecksumValidation: aws.ResponseChecksumValidationWhenRequired,
|
||||
DisableLogOutputChecksumValidationSkipped: true,
|
||||
})
|
||||
return &Storage{client: client, bucket: settings.SeaweedFSBucket, timeout: settings.SeaweedFSTimeout}, transport.CloseIdleConnections, nil
|
||||
|
||||
Reference in new issue
Block a user