Can cause issues on Windows consider filepath.Join instead
file := filepath.Join(t.TempDir(), "source.go")
1// SPDX-License-Identifier: MIT23package processor45import (6 "math/rand/v2"7 "os"8 "path/filepath"9 "runtime"10 "testing"11)1213func TestGetExtension(t *testing.T) {14 got := getExtension("something.c")15 expected := "c"1617 if got != expected {18 t.Errorf("Expected %s got %s", expected, got)19 }20}2122func TestGetExtensionNoExtension(t *testing.T) {23 got := getExtension("something")24 expected := "something"2526 if got != expected {27 t.Errorf("Expected %s got %s", expected, got)28 }29}3031func TestGetExtensionMultipleDots(t *testing.T) {32 got := getExtension(".travis.yml")33 expected := "travis.yml"3435 if got != expected {36 t.Errorf("Expected %s got %s", expected, got)37 }38}3940func TestGetExtensionMultipleExtensions(t *testing.T) {41 got := getExtension("something.go.yml")42 expected := "go.yml"4344 if got != expected {45 t.Errorf("Expected %s got %s", expected, got)46 }47}4849func TestGetExtensionStartsWith(t *testing.T) {50 got := getExtension(".gitignore")51 expected := ".gitignore"5253 if got != expected {54 t.Errorf("Expected %s got %s", expected, got)55 }56}5758func TestGetExtensionTypeScriptDefinition(t *testing.T) {59 got := getExtension("test.d.ts")60 expected := "d.ts"6162 if got != expected {63 t.Errorf("Expected %s got %s", expected, got)64 }65}6667func TestGetExtensionSecondPass(t *testing.T) {68 got := getExtension("test.d.ts")69 got = getExtension(got)70 expected := "ts"7172 if got != expected {73 t.Errorf("Expected %s got %s", expected, got)74 }75}7677func TestNewFileJobFullname(t *testing.T) {78 ProcessConstants()79 cleanVisitedPaths()80 AllowListExtensions = []string{}8182 fi, _ := os.Stat("../examples/issue114/makefile")83 job := newFileJob("../examples/issue114/", "makefile", fi)8485 if job.PossibleLanguages[0] != "Makefile" {86 t.Error("Expected makefile got", job.PossibleLanguages[0])87 }88}8990func TestNewFileJob(t *testing.T) {91 ProcessConstants()92 cleanVisitedPaths()9394 fi, _ := os.Stat("../examples/issue114/java")95 job := newFileJob("../examples/issue114/", "java", fi)9697 if job.PossibleLanguages[0] != "#!" {98 t.Error("Expected special value #! got", job.PossibleLanguages[0])99 }100}101102func TestNewFileJobGitIgnore(t *testing.T) {103 AllowListExtensions = []string{}104 ProcessConstants()105 cleanVisitedPaths()106 CountIgnore = true107108 fi, _ := os.Stat("../examples/issue114/.gitignore")109 job := newFileJob("../examples/issue114/", ".gitignore", fi)110111 if job.PossibleLanguages[0] != "gitignore" {112 t.Error("Expected gitignore got", job.PossibleLanguages[0])113 }114}115116func TestNewFileJobIgnore(t *testing.T) {117 AllowListExtensions = []string{}118 ProcessConstants()119 cleanVisitedPaths()120121 fi, _ := os.Stat("../examples/issue114/.ignore")122 job := newFileJob("../examples/issue114/", ".ignore", fi)123124 if job.PossibleLanguages[0] != "ignore" {125 t.Error("Expected ignore got", job.PossibleLanguages[0])126 }127}128129func TestNewFileJobLicense(t *testing.T) {130 ProcessConstants()131 cleanVisitedPaths()132133 fi, _ := os.Stat("../examples/issue114/license")134 job := newFileJob("../examples/issue114/", "license", fi)135136 if job.PossibleLanguages[0] != "License" {137 t.Error("Expected License got", job.PossibleLanguages[0])138 }139}140141func TestNewFileJobYAML(t *testing.T) {142 ProcessConstants()143 cleanVisitedPaths()144145 fi, _ := os.Stat("../examples/issue114/.travis.yml")146 job := newFileJob("../examples/issue114/", ".travis.yml", fi)147148 found := false149 for _, j := range job.PossibleLanguages {150 if j == "YAML" {151 found = true152 }153 }154155 if !found {156 t.Error("Expected YAML in but didn't find", job.PossibleLanguages)157 }158}159160func TestNewFileJobYAMLCloudformation(t *testing.T) {161 ProcessConstants()162 cleanVisitedPaths()163164 fi, _ := os.Stat("../examples/issue114/.travis.yml")165 job := newFileJob("../examples/issue114/", ".travis.yml", fi)166167 found := false168 for _, j := range job.PossibleLanguages {169 if j == "CloudFormation (YAML)" {170 found = true171 }172 }173174 if !found {175 t.Error("Expected CloudFormation in but didn't find", job.PossibleLanguages)176 }177}178179func TestNewFileJobSize(t *testing.T) {180 ProcessConstants()181 cleanVisitedPaths()182 NoLarge = true183 LargeByteCount = 1184185 fi, _ := os.Stat("file_test.go")186 job := newFileJob("file_test.go", "file_test.go", fi)187188 if job != nil {189 t.Error("Expected nil got", job)190 }191192 NoLarge = false193 LargeByteCount = 1000000194}195196func TestNewFileJobBrokenSymlink(t *testing.T) {197 if runtime.GOOS == "windows" {198 t.Skip("skipping symlink test on Windows due to privilege requirements")199 }200201 ProcessConstants()202 cleanVisitedPaths()203 IncludeSymLinks = true204 defer func() {205 IncludeSymLinks = false206 }()207208 // Create a temp directory to work in209 file := filepath.Join(t.TempDir(), "source.go")210 err := os.WriteFile(file, []byte("package main\n"), 0644)211 if err != nil {212 t.Fatal(err)213 }214 symPath := filepath.Join(t.TempDir(), "broken.go")215 err = os.Symlink(file, symPath)216 if err != nil {217 t.Fatal(err)218 }219 // Delete the file breaks the symlink220 err = os.Remove(file)221 if err != nil {222 t.Fatal(err)223 }224225 fi, _ := os.Lstat(symPath)226 job := newFileJob(symPath, "broken.go", fi)227228 if job != nil {229 t.Error("Expected nil for broken symlink got", job)230 }231}232233func BenchmarkGetExtensionDifferent(b *testing.B) {234 for i := 0; i < b.N; i++ {235236 b.StopTimer()237 name := randStringBytes(3) + "." + randStringBytes(2)238 b.StartTimer()239240 getExtension(name)241 }242}243244func BenchmarkGetExtensionSame(b *testing.B) {245 name := randStringBytes(7) + "." + randStringBytes(3)246247 for i := 0; i < b.N; i++ {248 getExtension(name)249 }250}251252const letterBytes = "abcdefghijklmnopqrstuvwxyz"253254func randStringBytes(n int) string {255 b := make([]byte, n)256 for i := range b {257 b[i] = letterBytes[rand.IntN(len(letterBytes))]258 }259 return string(b)260}261262func TestNewFileJobCircularSymlink(t *testing.T) {263 if runtime.GOOS == "windows" {264 t.Skip("skipping symlink test on Windows due to privilege requirements")265 }266 ProcessConstants()267 cleanVisitedPaths()268 IncludeSymLinks = true269 defer func() { IncludeSymLinks = false }()270271 // Create a temp directory to work in272 dir, _ := filepath.EvalSymlinks(t.TempDir())273 link1 := filepath.Join(dir, "link1.go")274 link2 := filepath.Join(dir, "link2.go")275 // Create a loop: link1 -> link2 and link2 -> link1276 if err := os.Symlink(link2, link1); err != nil {277 t.Fatal("Failed to create first link:", err)278 }279 if err := os.Symlink(link1, link2); err != nil {280 t.Fatal("Failed to create circular link:", err)281 }282283 fi, err := os.Lstat(link1)284 if err != nil {285 t.Fatal(err)286 }287 // It should return the 'too many links' error.288 job := newFileJob(link1, "link1.go", fi)289290 if job != nil {291 t.Error("Expected nil for circular symlink, but got a FileJob")292 }293}294295func TestNewFileJobDuplicateCounting(t *testing.T) {296 if runtime.GOOS == "windows" {297 t.Skip("skipping symlink test on Windows due to privilege requirements")298 }299 ProcessConstants()300 cleanVisitedPaths()301 IncludeSymLinks = true302 defer func() { IncludeSymLinks = false }()303304 // on some systems like macOS, t.TempDir is also a symlink305 dir, _ := filepath.EvalSymlinks(t.TempDir())306 // Create a test file307 testFile := filepath.Join(dir, "file.go")308309 if err := os.WriteFile(testFile, []byte("package main"), 0644); err != nil {310 t.Fatal(err)311 }312313 // Create a symlink to the same file314 linkFile := filepath.Join(dir, "link.go")315 if err := os.Symlink(testFile, linkFile); err != nil {316 t.Fatalf("Failed to create link file: %s", err)317 }318 // Process the test file319 fi1, _ := os.Lstat(testFile)320 job1 := newFileJob(testFile, "file.go", fi1)321322 // Process the symlink (same target)323 fi2, _ := os.Lstat(linkFile)324 job2 := newFileJob(linkFile, "link.go", fi2)325326 // First count should go through327 if job1 == nil {328 t.Fatal("Expected first file job to be created")329 }330331 // Second count should be skipped332 if job2 != nil {333 t.Error("Expected nil for duplicate file through symlink, but got a FileJob")334 }335}
Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.