From 20e3e869ff3e08726a567565879d0bf288209da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20W=C3=A5reus?= Date: Thu, 12 Oct 2023 10:58:09 +0200 Subject: [PATCH] fixes after review --- internal/file/fingerprint.go | 23 ++++++++++--- internal/file/fingerprint_test.go | 56 +++++++++++++++++++++++++++++-- 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/internal/file/fingerprint.go b/internal/file/fingerprint.go index 17d35a31..17c92b94 100644 --- a/internal/file/fingerprint.go +++ b/internal/file/fingerprint.go @@ -34,7 +34,7 @@ var EXCLUDED_EXT = []string{ var EXCLUDED_FILE_ENDINGS = []string{"-doc", "changelog", "config", "copying", "license", "authors", "news", "licenses", "notice", "readme", "swiftdoc", "texidoc", "todo", "version", "ignore", "manifest", "sqlite", "sqlite3"} -var ECLUDED_FILES = []string{ +var EXCLUDED_FILES = []string{ "gradlew", "gradlew.bat", "mvnw", "mvnw.cmd", "gradle-wrapper.jar", "maven-wrapper.jar", "thumbs.db", "babel.config.js", "license.txt", "license.md", "copying.lib", "makefile", } @@ -63,7 +63,7 @@ func isExcludedByExtension(filename string) bool { func isExcludedByFilename(filename string) bool { filenameLower := strings.ToLower(filename) - for _, file := range ECLUDED_FILES { + for _, file := range EXCLUDED_FILES { if filenameLower == file { return true } @@ -104,8 +104,7 @@ type FileFingerprint struct { } func (f FileFingerprint) ToString() string { - // Replace backslashes with forward slashes to make the path platform independent - path := strings.ReplaceAll(f.path, "\\", "/") + path := filepath.ToSlash(f.path) return fmt.Sprintf("file=%x,%d,%s", f.fingerprint, f.contentLength, path) } @@ -161,6 +160,15 @@ func (f *Fingerprinter) FingerprintFiles(rootPath string, exclusions []string) ( return fingerprints, err } +func isSymlink(filename string) (bool, error) { + info, err := os.Lstat(filename) + if err != nil { + return false, err + } + + return info.Mode()&os.ModeSymlink != 0, nil +} + func shouldProcessFile(fileInfo os.FileInfo, exclusions []string, path string) bool { if fileInfo.IsDir() { return false @@ -174,7 +182,12 @@ func shouldProcessFile(fileInfo os.FileInfo, exclusions []string, path string) b return false } - return true + isSymlink, err := isSymlink(path) + if err != nil { + return false + } + + return !isSymlink } func computeMD5(filename string) (FileFingerprint, error) { diff --git a/internal/file/fingerprint_test.go b/internal/file/fingerprint_test.go index 2948f170..25a2aea8 100644 --- a/internal/file/fingerprint_test.go +++ b/internal/file/fingerprint_test.go @@ -3,6 +3,7 @@ package file import ( "fmt" "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -38,6 +39,43 @@ func TestIsExcludedFile(t *testing.T) { assert.False(t, isExcludedFile("file.jar"), "Expected .jar to not be excluded") } +func TestShouldProcessFile(t *testing.T) { + // Create a temporary directory to use for testing + tempDir, err := os.MkdirTemp("", "should-process-file-test") + if err != nil { + t.Fatalf("Failed to create temporary directory: %v", err) + } + defer os.RemoveAll(tempDir) + + // Create a test file and a symbolic link to the file in the temporary directory + testFile := filepath.Join(tempDir, "test.py") + if err := os.WriteFile(testFile, []byte("test"), 0600); err != nil { + t.Fatalf("Failed to create test file %s: %v", testFile, err) + } + testLink := filepath.Join(tempDir, "test-link.py") + if err := os.Symlink(testFile, testLink); err != nil { + t.Fatalf("Failed to create symbolic link %s: %v", testLink, err) + } + + // Test with a regular file + fileInfo, err := os.Stat(testFile) + if err != nil { + t.Fatalf("Failed to get file info for %s: %v", testFile, err) + } + if !shouldProcessFile(fileInfo, []string{}, testFile) { + t.Errorf("Expected shouldProcessFile to return true for %s, but it returned false", testFile) + } + + // Test with a symbolic link + linkInfo, err := os.Stat(testLink) + if err != nil { + t.Fatalf("Failed to get file info for %s: %v", testLink, err) + } + if shouldProcessFile(linkInfo, []string{}, testLink) { + t.Errorf("Expected shouldProcessFile to return false for %s, but it returned true", testLink) + } +} + func TestNewFingerprinter(t *testing.T) { assert.NotNil(t, NewFingerprinter()) } @@ -64,13 +102,27 @@ func TestFingerprintFiles(t *testing.T) { } func TestFingerprintFilesBackslash(t *testing.T) { + + tempDir, err := os.MkdirTemp("", "slash-test") + if err != nil { + t.Fatalf("Failed to create temporary directory: %v", err) + } + defer os.RemoveAll(tempDir) + testFile := filepath.Join(tempDir, "testfile.py") + + testFileSlashes := filepath.ToSlash(testFile) + fingerprint := FileFingerprint{ - path: "testdata\\fingerprinter\\testfile.py", + path: testFile, contentLength: 21, fingerprint: []byte{114, 33, 77, 180, 225, 229, 67, 1, 141, 27, 175, 232, 110, 163, 180, 68, 68, 68, 68, 68, 68}, } - assert.Equal(t, "file=72214db4e1e543018d1bafe86ea3b4444444444444,21,testdata/fingerprinter/testfile.py", fingerprint.ToString()) + assert.Equal(t, fmt.Sprintf("file=72214db4e1e543018d1bafe86ea3b4444444444444,21,%s", testFileSlashes), fingerprint.ToString()) + + // Make sure it only contains "/" and not "\" + assert.NotContains(t, fingerprint.ToString(), "\\") + assert.Contains(t, fingerprint.ToString(), "/") }