From 89a9ae0ce2534f605e6edd6fd9370b2dfa9af479 Mon Sep 17 00:00:00 2001 From: tw93 Date: Sat, 7 Mar 2026 10:10:41 +0800 Subject: [PATCH] fix(analyze): count top-level files in json output --- cmd/analyze/analyze_test.go | 25 +++++++++++++++++++++++++ cmd/analyze/json.go | 4 +++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/cmd/analyze/analyze_test.go b/cmd/analyze/analyze_test.go index b0e9007..7a2fecd 100644 --- a/cmd/analyze/analyze_test.go +++ b/cmd/analyze/analyze_test.go @@ -91,6 +91,31 @@ func TestScanPathConcurrentBasic(t *testing.T) { } } +func TestPerformScanForJSONCountsTopLevelFiles(t *testing.T) { + root := t.TempDir() + + rootFile := filepath.Join(root, "root.txt") + if err := os.WriteFile(rootFile, []byte("root-data"), 0o644); err != nil { + t.Fatalf("write root file: %v", err) + } + + nested := filepath.Join(root, "nested") + if err := os.MkdirAll(nested, 0o755); err != nil { + t.Fatalf("create nested dir: %v", err) + } + + nestedFile := filepath.Join(nested, "nested.txt") + if err := os.WriteFile(nestedFile, []byte("nested-data"), 0o644); err != nil { + t.Fatalf("write nested file: %v", err) + } + + result := performScanForJSON(root) + + if result.TotalFiles != 2 { + t.Fatalf("expected 2 files in JSON output, got %d", result.TotalFiles) + } +} + func TestDeletePathWithProgress(t *testing.T) { skipIfFinderUnavailable(t) diff --git a/cmd/analyze/json.go b/cmd/analyze/json.go index 056edff..1c2ab44 100644 --- a/cmd/analyze/json.go +++ b/cmd/analyze/json.go @@ -58,6 +58,8 @@ func performScanForJSON(path string) jsonOutput { info, err := item.Info() if err == nil { size = info.Size() + atomic.AddInt64(&filesScanned, 1) + atomic.AddInt64(&bytesScanned, size) } } @@ -74,6 +76,6 @@ func performScanForJSON(path string) jsonOutput { Path: path, Entries: entries, TotalSize: totalSize, - TotalFiles: filesScanned, + TotalFiles: atomic.LoadInt64(&filesScanned), } }