From d81e4a5558b380f166f4327203cc221c29890ddb Mon Sep 17 00:00:00 2001 From: apocelipes Date: Mon, 13 Apr 2026 12:03:06 +0800 Subject: [PATCH] fix: fix file tests --- SCC-OUTPUT-REPORT.html | 146 +++++++++++++++++++------------------- examples/symlink/link.py | 1 - examples/symlink/link2.py | 1 - examples/symlink/link3.py | 1 - examples/symlink/link4.py | 1 - examples/symlink/test.py | 7 -- main_test.go | 53 ++++++++++++++ processor/file.go | 4 ++ processor/file_test.go | 47 +++++++----- processor/processor.go | 1 + processor/result.go | 1 + test-all.sh | 13 ---- 12 files changed, 162 insertions(+), 114 deletions(-) delete mode 120000 examples/symlink/link.py delete mode 120000 examples/symlink/link2.py delete mode 120000 examples/symlink/link3.py delete mode 120000 examples/symlink/link4.py delete mode 100644 examples/symlink/test.py diff --git a/SCC-OUTPUT-REPORT.html b/SCC-OUTPUT-REPORT.html index ebfcac96..faa8efa0 100644 --- a/SCC-OUTPUT-REPORT.html +++ b/SCC-OUTPUT-REPORT.html @@ -13,13 +13,13 @@ Go 34 - 27374 - 1796 - 614 - 24964 - 2018 - 558764 - 8190 + 27608 + 1829 + 633 + 25146 + 2080 + 564680 + 8285 processor/constants.go @@ -63,13 +63,13 @@ main_test.go - 890 - 70 - 15 - 805 - 228 - 21740 - 471 + 943 + 76 + 17 + 850 + 250 + 23270 + 493 processor/workers.go @@ -77,19 +77,19 @@ 128 92 646 - 213 - 26343 - 505 + 212 + 26321 + 504 processor/processor.go - 749 + 750 159 123 - 467 + 468 89 - 21834 - 495 + 21855 + 496 cmd/badges/main.go @@ -130,6 +130,16 @@ 57 12733 293 + + processor/file_test.go + + 335 + 66 + 13 + 256 + 67 + 7741 + 154 cmd/badges/main_test.go @@ -150,6 +160,16 @@ 68 7503 131 + + processor/workers_regression_test.go + + 249 + 49 + 7 + 193 + 50 + 4794 + 121 processor/workers_tokei_test.go @@ -180,26 +200,6 @@ 53 6269 151 - - processor/file_test.go - - 213 - 50 - 1 - 162 - 37 - 4585 - 97 - - processor/workers_regression_test.go - - 212 - 42 - 5 - 165 - 42 - 4015 - 105 processor/similar_flags_test.go @@ -210,6 +210,16 @@ 5 3105 78 + + processor/file.go + + 173 + 27 + 19 + 127 + 46 + 3958 + 106 processor/locomo.go @@ -230,16 +240,6 @@ 22 2741 70 - - processor/file.go - - 153 - 23 - 16 - 114 - 43 - 3527 - 94 processor/structs_test.go @@ -253,13 +253,13 @@ processor/result.go - 136 + 137 24 3 - 109 + 110 31 - 3226 - 91 + 3247 + 92 processor/trace_test.go @@ -320,16 +320,6 @@ 0 2209 35 - - processor/cocomo_test.go - - 37 - 8 - 4 - 25 - 6 - 686 - 23 processor/bloom.go @@ -340,6 +330,16 @@ 2 1062 29 + + processor/cocomo_test.go + + 37 + 8 + 4 + 25 + 6 + 686 + 23 processor/helpers_test.go @@ -364,15 +364,15 @@ Total 34 - 27374 - 1796 - 614 - 24964 - 2018 - 558764 - 8190 + 27608 + 1829 + 633 + 25146 + 2080 + 564680 + 8285 - Estimated Cost to Develop (organic) $792,091
Estimated Schedule Effort (organic) 12.59 months
Estimated People Required (organic) 5.59
+ Estimated Cost to Develop (organic) $798,155
Estimated Schedule Effort (organic) 12.62 months
Estimated People Required (organic) 5.62
diff --git a/examples/symlink/link.py b/examples/symlink/link.py deleted file mode 120000 index 94656643..00000000 --- a/examples/symlink/link.py +++ /dev/null @@ -1 +0,0 @@ -test.py \ No newline at end of file diff --git a/examples/symlink/link2.py b/examples/symlink/link2.py deleted file mode 120000 index 91e08e13..00000000 --- a/examples/symlink/link2.py +++ /dev/null @@ -1 +0,0 @@ -link.py \ No newline at end of file diff --git a/examples/symlink/link3.py b/examples/symlink/link3.py deleted file mode 120000 index f65f2d44..00000000 --- a/examples/symlink/link3.py +++ /dev/null @@ -1 +0,0 @@ -link2.py \ No newline at end of file diff --git a/examples/symlink/link4.py b/examples/symlink/link4.py deleted file mode 120000 index b59ed54c..00000000 --- a/examples/symlink/link4.py +++ /dev/null @@ -1 +0,0 @@ -link3.py \ No newline at end of file diff --git a/examples/symlink/test.py b/examples/symlink/test.py deleted file mode 100644 index d673ec48..00000000 --- a/examples/symlink/test.py +++ /dev/null @@ -1,7 +0,0 @@ -# this is a comment. - -import os - -for e in os.scandir('.'): - if e.is_file(): - print(e) diff --git a/main_test.go b/main_test.go index 4cb34da9..c4fa233f 100644 --- a/main_test.go +++ b/main_test.go @@ -6,6 +6,7 @@ import ( "os/exec" "path/filepath" "regexp" + "runtime" "slices" "strconv" "strings" @@ -721,6 +722,58 @@ func TestFileGCCount(t *testing.T) { } } +func TestIncludeSymlinks(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("skipping symlink test on Windows due to privilege requirements") + } + + tmpDir, err := filepath.EvalSymlinks(t.TempDir()) + if err != nil { + t.Fatal(err) + } + + dirA := filepath.Join(tmpDir, "a") + dirB := filepath.Join(tmpDir, "b") + if err := os.Mkdir(dirA, 0755); err != nil { + t.Fatal(err) + } + if err := os.Mkdir(dirB, 0755); err != nil { + t.Fatal(err) + } + + const fileName = "source.go" + if err := os.WriteFile(filepath.Join(dirA, fileName), []byte("package main\n"), 0644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dirB, fileName), []byte("package main\n"), 0644); err != nil { + t.Fatal(err) + } + // link to another dir, should be counted under --include-symlinks + if err := os.Symlink(filepath.Join(dirB, fileName), filepath.Join(dirA, "link1.go")); err != nil { + t.Fatal(err) + } + // link to the same dir, this should be ignored in all times + if err := os.Symlink(filepath.Join(dirA, fileName), filepath.Join(dirA, "link2.go")); err != nil { + t.Fatal(err) + } + + output, err := runSCC("-f", "json", "--no-scc-ignore", dirA) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(output, `"Count":1`) { + t.Errorf("count without symlink failed, output:\n%s", output) + } + + output, err = runSCC("-f", "json", "--no-scc-ignore", "--include-symlinks", dirA) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(output, `"Count":2`) { + t.Errorf("count includes symlink failed, output:\n%s", output) + } +} + func TestLanguageNameTruncate(t *testing.T) { output, err := runSCC("examples/language") if err != nil { diff --git a/processor/file.go b/processor/file.go index 5becde7f..7ceb0a31 100644 --- a/processor/file.go +++ b/processor/file.go @@ -44,6 +44,10 @@ func getExtension(name string) string { return extension.(string) } +func cleanVisitedPaths() { + visitedPaths.Clear() +} + func newFileJob(path, name string, fileInfo os.FileInfo) *FileJob { if NoLarge { if fileInfo.Size() >= LargeByteCount { diff --git a/processor/file_test.go b/processor/file_test.go index ce601e4f..3492f86d 100644 --- a/processor/file_test.go +++ b/processor/file_test.go @@ -7,7 +7,6 @@ import ( "os" "path/filepath" "runtime" - "sync" "testing" ) @@ -77,6 +76,7 @@ func TestGetExtensionSecondPass(t *testing.T) { func TestNewFileJobFullname(t *testing.T) { ProcessConstants() + cleanVisitedPaths() AllowListExtensions = []string{} fi, _ := os.Stat("../examples/issue114/makefile") @@ -89,6 +89,7 @@ func TestNewFileJobFullname(t *testing.T) { func TestNewFileJob(t *testing.T) { ProcessConstants() + cleanVisitedPaths() fi, _ := os.Stat("../examples/issue114/java") job := newFileJob("../examples/issue114/", "java", fi) @@ -101,6 +102,7 @@ func TestNewFileJob(t *testing.T) { func TestNewFileJobGitIgnore(t *testing.T) { AllowListExtensions = []string{} ProcessConstants() + cleanVisitedPaths() CountIgnore = true fi, _ := os.Stat("../examples/issue114/.gitignore") @@ -114,6 +116,7 @@ func TestNewFileJobGitIgnore(t *testing.T) { func TestNewFileJobIgnore(t *testing.T) { AllowListExtensions = []string{} ProcessConstants() + cleanVisitedPaths() fi, _ := os.Stat("../examples/issue114/.ignore") job := newFileJob("../examples/issue114/", ".ignore", fi) @@ -125,6 +128,7 @@ func TestNewFileJobIgnore(t *testing.T) { func TestNewFileJobLicense(t *testing.T) { ProcessConstants() + cleanVisitedPaths() fi, _ := os.Stat("../examples/issue114/license") job := newFileJob("../examples/issue114/", "license", fi) @@ -136,6 +140,7 @@ func TestNewFileJobLicense(t *testing.T) { func TestNewFileJobYAML(t *testing.T) { ProcessConstants() + cleanVisitedPaths() fi, _ := os.Stat("../examples/issue114/.travis.yml") job := newFileJob("../examples/issue114/", ".travis.yml", fi) @@ -154,6 +159,7 @@ func TestNewFileJobYAML(t *testing.T) { func TestNewFileJobYAMLCloudformation(t *testing.T) { ProcessConstants() + cleanVisitedPaths() fi, _ := os.Stat("../examples/issue114/.travis.yml") job := newFileJob("../examples/issue114/", ".travis.yml", fi) @@ -172,6 +178,7 @@ func TestNewFileJobYAMLCloudformation(t *testing.T) { func TestNewFileJobSize(t *testing.T) { ProcessConstants() + cleanVisitedPaths() NoLarge = true LargeByteCount = 1 @@ -192,20 +199,27 @@ func TestNewFileJobBrokenSymlink(t *testing.T) { } ProcessConstants() + cleanVisitedPaths() IncludeSymLinks = true + defer func() { + IncludeSymLinks = false + }() // Create a temp directory to work in - dir, err := os.MkdirTemp("", "scc-broken-symlink-test") + file := filepath.Join(t.TempDir(), "source.go") + err := os.WriteFile(file, []byte("package main\n"), 0644) if err != nil { - t.Fatal("Failed to create temp dir:", err) + t.Fatal(err) } - defer os.RemoveAll(dir) - - // Create a symlink that points to a path that doesn't exist - symPath := dir + "/broken.go" - err = os.Symlink("/this/path/does/not/exist.go", symPath) + symPath := filepath.Join(t.TempDir(), "broken.go") + err = os.Symlink(file, symPath) + if err != nil { + t.Fatal(err) + } + // Delete the file breaks the symlink + err = os.Remove(file) if err != nil { - t.Fatal("Failed to create broken symlink:", err) + t.Fatal(err) } fi, _ := os.Lstat(symPath) @@ -214,8 +228,6 @@ func TestNewFileJobBrokenSymlink(t *testing.T) { if job != nil { t.Error("Expected nil for broken symlink got", job) } - - IncludeSymLinks = false } func BenchmarkGetExtensionDifferent(b *testing.B) { @@ -252,11 +264,12 @@ func TestNewFileJobCircularSymlink(t *testing.T) { t.Skip("skipping symlink test on Windows due to privilege requirements") } ProcessConstants() + cleanVisitedPaths() IncludeSymLinks = true defer func() { IncludeSymLinks = false }() - visitedPaths = sync.Map{} + // Create a temp directory to work in - dir := t.TempDir() + dir, _ := filepath.EvalSymlinks(t.TempDir()) link1 := filepath.Join(dir, "link1.go") link2 := filepath.Join(dir, "link2.go") // Create a loop: link1 -> link2 and link2 -> link1 @@ -284,12 +297,12 @@ func TestNewFileJobDuplicateCounting(t *testing.T) { t.Skip("skipping symlink test on Windows due to privilege requirements") } ProcessConstants() + cleanVisitedPaths() IncludeSymLinks = true defer func() { IncludeSymLinks = false }() - visitedPaths = sync.Map{} - // Create Temp directory - dir := t.TempDir() + // on some systems like macOS, t.TempDir is also a symlink + dir, _ := filepath.EvalSymlinks(t.TempDir()) // Create a test file testFile := filepath.Join(dir, "file.go") @@ -300,7 +313,7 @@ func TestNewFileJobDuplicateCounting(t *testing.T) { // Create a symlink to the same file linkFile := filepath.Join(dir, "link.go") if err := os.Symlink(testFile, linkFile); err != nil { - t.Skip("Symlinks not supported:", err) + t.Fatalf("Failed to create link file: %s", err) } // Process the test file fi1, _ := os.Lstat(testFile) diff --git a/processor/processor.go b/processor/processor.go index 069444d8..2ac57a4b 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -625,6 +625,7 @@ func Process() { ProcessConstants() processFlags() + cleanVisitedPaths() // Clean up any invalid arguments before setting everything up if len(DirFilePaths) == 0 { diff --git a/processor/result.go b/processor/result.go index 347892ac..f7f2db31 100644 --- a/processor/result.go +++ b/processor/result.go @@ -18,6 +18,7 @@ import ( func ProcessResult() ([]LanguageSummary, error) { ProcessConstants() processFlags() + cleanVisitedPaths() if len(DirFilePaths) == 0 { DirFilePaths = append(DirFilePaths, ".") diff --git a/test-all.sh b/test-all.sh index e9a207b0..244b0465 100755 --- a/test-all.sh +++ b/test-all.sh @@ -278,19 +278,6 @@ else echo -e "${GREEN}PASSED minified ignored check" fi -a=$(./scc ./examples/symlink/ --no-scc-ignore) -b=$(./scc --include-symlinks ./examples/symlink/ --no-scc-ignore) -if [ "$a" == "$b" ]; then - echo "$a" - echo "$b" - echo -e "${RED}=======================================================" - echo -e "FAILED symlink check" - echo -e "=================================================${NC}" - exit -else - echo -e "${GREEN}PASSED minified ignored check" -fi - if ./scc ./examples/minified/ --no-min-gen --no-scc-ignore | grep -q "\$0"; then echo -e "${GREEN}PASSED removed min" else