* test(doctor): add comprehensive tests for fix and check functions Add edge case tests, e2e tests, and improve test coverage for: - database_test.go: database integrity and sync checks - git_test.go: git hooks, merge driver, sync branch tests - gitignore_test.go: gitignore validation - prefix_test.go: ID prefix handling - fix/fix_test.go: fix operations - fix/e2e_test.go: end-to-end fix scenarios - fix/fix_edge_cases_test.go: edge case handling * docs: add testing philosophy and anti-patterns guide - Create TESTING_PHILOSOPHY.md covering test pyramid, priority matrix, what NOT to test, and 5 anti-patterns with code examples - Add cross-reference from README_TESTING.md - Document beads-specific guidance (well-covered areas vs gaps) - Include target metrics (test-to-code ratio, execution time targets) * chore: revert .beads/ to upstream/main state * refactor(doctor): add category grouping and Ayu theme colors - Add Category field to DoctorCheck for organizing checks by type - Define category constants: Core, Git, Runtime, Data, Integration, Metadata - Update thanks command to use shared Ayu color palette from internal/ui - Simplify test fixtures by removing redundant test cases * fix(doctor): prevent test fork bomb and fix test failures - Add ErrTestBinary guard in getBdBinary() to prevent tests from recursively executing the test binary when calling bd subcommands - Update claude_test.go to use new check names (CLI Availability, Prime Documentation) - Fix syncbranch test path comparison by resolving symlinks (/var vs /private/var on macOS) - Fix permissions check to use exact comparison instead of bitmask - Fix UntrackedJSONL to use git commit --only to preserve staged changes - Fix MergeDriver edge case test by making both .git dir and config read-only - Add skipIfTestBinary helper for E2E tests that need real bd binary * test(doctor): skip read-only config test in CI environments GitHub Actions containers may have CAP_DAC_OVERRIDE or similar capabilities that allow writing to read-only files, causing the test to fail. Skip the test when CI=true or GITHUB_ACTIONS=true.
86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
package fix
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// UntrackedJSONL stages and commits untracked .beads/*.jsonl files.
|
|
// This fixes the issue where bd cleanup -f creates deletions.jsonl but
|
|
// leaves it untracked. (bd-pbj)
|
|
func UntrackedJSONL(path string) error {
|
|
if err := validateBeadsWorkspace(path); err != nil {
|
|
return err
|
|
}
|
|
|
|
beadsDir := filepath.Join(path, ".beads")
|
|
|
|
// Find untracked JSONL files
|
|
// Use --untracked-files=all to show individual files, not just the directory
|
|
cmd := exec.Command("git", "status", "--porcelain", "--untracked-files=all", ".beads/")
|
|
cmd.Dir = path
|
|
output, err := cmd.Output()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to check git status: %w", err)
|
|
}
|
|
|
|
// Parse output for untracked JSONL files
|
|
var untrackedFiles []string
|
|
for _, line := range strings.Split(string(output), "\n") {
|
|
line = strings.TrimSpace(line)
|
|
if line == "" {
|
|
continue
|
|
}
|
|
// Untracked files start with "?? "
|
|
if strings.HasPrefix(line, "?? ") {
|
|
file := strings.TrimPrefix(line, "?? ")
|
|
if strings.HasSuffix(file, ".jsonl") {
|
|
untrackedFiles = append(untrackedFiles, file)
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(untrackedFiles) == 0 {
|
|
fmt.Println(" No untracked JSONL files found")
|
|
return nil
|
|
}
|
|
|
|
// Stage the untracked files
|
|
for _, file := range untrackedFiles {
|
|
fullPath := filepath.Join(path, file)
|
|
// Verify file exists in .beads directory (security check)
|
|
if !strings.HasPrefix(fullPath, beadsDir) {
|
|
continue
|
|
}
|
|
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
|
|
continue
|
|
}
|
|
|
|
// #nosec G204 -- file is validated against a whitelist of JSONL files
|
|
addCmd := exec.Command("git", "add", file)
|
|
addCmd.Dir = path
|
|
if err := addCmd.Run(); err != nil {
|
|
return fmt.Errorf("failed to stage %s: %w", file, err)
|
|
}
|
|
fmt.Printf(" Staged %s\n", filepath.Base(file))
|
|
}
|
|
|
|
// Commit only the JSONL files we staged (using --only to preserve other staged changes)
|
|
commitMsg := "chore(beads): commit untracked JSONL files\n\nAuto-committed by bd doctor --fix (bd-pbj)"
|
|
commitArgs := []string{"commit", "--only", "-m", commitMsg}
|
|
commitArgs = append(commitArgs, untrackedFiles...)
|
|
commitCmd := exec.Command("git", commitArgs...) // #nosec G204 -- untrackedFiles validated above
|
|
commitCmd.Dir = path
|
|
commitCmd.Stdout = os.Stdout
|
|
commitCmd.Stderr = os.Stderr
|
|
|
|
if err := commitCmd.Run(); err != nil {
|
|
return fmt.Errorf("failed to commit: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|