Skip to content

Commit

Permalink
Add and use a new IsPathAccessible API before accessing the .git dir (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalbe4 authored Aug 18, 2024
1 parent 512e430 commit c6062ab
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
11 changes: 9 additions & 2 deletions utils/io/fileutils/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"encoding/json"
"errors"
biutils "github.com/jfrog/build-info-go/utils"
"io"
"net/url"
"os"
Expand All @@ -14,6 +13,8 @@ import (
"reflect"
"strings"

biutils "github.com/jfrog/build-info-go/utils"

"github.com/jfrog/build-info-go/entities"
"github.com/jfrog/gofrog/crypto"
gofrog "github.com/jfrog/gofrog/io"
Expand All @@ -31,14 +32,20 @@ func GetFileSeparator() string {
return string(os.PathSeparator)
}

// Check if path exists.
// Checks if the path exists.
// If path points at a symlink and `preserveSymLink == true`,
// function will return `true` regardless of the symlink target
func IsPathExists(path string, preserveSymLink bool) bool {
_, err := GetFileInfo(path, preserveSymLink)
return !os.IsNotExist(err)
}

// Checks if the path is accessible.
func IsPathAccessible(path string) bool {
_, err := GetFileInfo(path, true)
return err == nil
}

// Check if path points at a file.
// If path points at a symlink and `preserveSymLink == true`,
// function will return `true` regardless of the symlink target
Expand Down
56 changes: 54 additions & 2 deletions utils/io/fileutils/files_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,70 @@
package fileutils

import (
biutils "github.com/jfrog/build-info-go/utils"
"github.com/jfrog/jfrog-client-go/utils/io"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
"testing"

biutils "github.com/jfrog/build-info-go/utils"
"github.com/jfrog/jfrog-client-go/utils/io"

"github.com/stretchr/testify/assert"
)

func TestIsPathExistsAndIsPathAccessible(t *testing.T) {
var symlinkPath string
symlinkCreated := false

// Create a temporary file
tempFile, err := os.CreateTemp("", "testfile")
assert.NoError(t, err)

// Close the file immediately after creation to ensure it is not locked
assert.NoError(t, tempFile.Close())

defer func() {
// Remove the symlink before removing the file it references.
if symlinkCreated {
assert.NoError(t, os.Remove(symlinkPath))
}
assert.NoError(t, os.Remove(tempFile.Name()))
}()

// Test for an existing file
assert.True(t, IsPathExists(tempFile.Name(), false))
assert.True(t, IsPathAccessible(tempFile.Name()))

// Test for a non-existing file
assert.False(t, IsPathExists(tempFile.Name()+"_nonexistent", false))

// Create a temporary directory
tempDir := t.TempDir()

// Test for an existing directory
assert.True(t, IsPathExists(tempDir, false))
assert.True(t, IsPathAccessible(tempDir))

// Test for a non-existing directory
assert.False(t, IsPathExists(tempDir+"_nonexistent", false))
assert.False(t, IsPathAccessible(tempDir+"_nonexistent"))

// Create a symlink and test with preserveSymLink true and false
symlinkPath = tempFile.Name() + "_symlink"
err = os.Symlink(tempFile.Name(), symlinkPath)
assert.NoError(t, err)
// It is best to remove the symlink before removing the file it
// references. We use this variable to flag to the defer function
// to remove the symlink.
symlinkCreated = true

assert.True(t, IsPathExists(symlinkPath, true))
assert.True(t, IsPathExists(symlinkPath, false))
assert.True(t, IsPathAccessible(symlinkPath))
}

func TestIsSsh(t *testing.T) {
testRuns := []struct {
url string
Expand Down
10 changes: 5 additions & 5 deletions utils/vcsdetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ func (vc *VcsCache) getCacheSize() int32 {
return atomic.LoadInt32(vc.vcsDirSize)
}

// Search for '.git' directory inside 'path', incase there is one, extract the details and add a new entry to the cache(key:path in the file system ,value: git revision & url).
// otherwise, search in the parent folder and try:
// 1. search for .git, and save the details for the current dir and all subpath
// Search for '.git' directory inside 'path'. In case there is one,
// extract the details and add a new entry to the cache(key:path in the file system ,value: git revision & url).
// Otherwise, move in the parent folder and do the following:
// 1. Search for .git, and save the details for the current dir and all subpath
// 2. .git not found, go to parent dir and repeat
// 3. not found on the root directory, add all subpath to cache with nil as a value
func (vc *VcsCache) GetVcsDetails(path string) (revision, refUrl, branch string, err error) {
Expand Down Expand Up @@ -91,8 +92,7 @@ func (vc *VcsCache) clearCacheIfExceedsMax() {
}

func tryGetGitDetails(path string) (string, string, string, error) {
exists := fileutils.IsPathExists(filepath.Join(path, ".git"), false)
if exists {
if fileutils.IsPathAccessible(filepath.Join(path, ".git")) {
return extractGitDetails(path)
}
return "", "", "", nil
Expand Down

0 comments on commit c6062ab

Please sign in to comment.