Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
emilwareus committed Oct 17, 2023
1 parent c1fac10 commit 20e3e86
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
23 changes: 18 additions & 5 deletions internal/file/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
56 changes: 54 additions & 2 deletions internal/file/fingerprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package file
import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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())
}
Expand All @@ -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(), "/")

}

Expand Down

0 comments on commit 20e3e86

Please sign in to comment.