diff --git a/common/build/buildutils.go b/common/build/buildutils.go index 4184403aa..6558d7aa6 100644 --- a/common/build/buildutils.go +++ b/common/build/buildutils.go @@ -25,13 +25,12 @@ import ( const ( BuildInfoDetails = "details" - BuildTempPath = "jfrog/builds/" ProjectConfigBuildNameKey = "name" ) func CreateBuildInfoService() *build.BuildInfoService { buildInfoService := build.NewBuildInfoService() - buildInfoService.SetTempDirPath(filepath.Join(coreutils.GetCliPersistentTempDirPath(), BuildTempPath)) + buildInfoService.SetTempDirPath(filepath.Join(coreutils.GetCliPersistentTempDirPath(), buildInfoService.GetUserSpecificBuildDirName())) buildInfoService.SetLogger(log.Logger) return buildInfoService } @@ -66,7 +65,8 @@ func PrepareBuildPrerequisites(buildConfiguration *BuildConfiguration) (build *b func GetBuildDir(buildName, buildNumber, projectKey string) (string, error) { hash := sha256.Sum256([]byte(buildName + "_" + buildNumber + "_" + projectKey)) - buildsDir := filepath.Join(coreutils.GetCliPersistentTempDirPath(), BuildTempPath, hex.EncodeToString(hash[:])) + buildsDir := filepath.Join(coreutils.GetCliPersistentTempDirPath(), + build.NewBuildInfoService().GetUserSpecificBuildDirName(), hex.EncodeToString(hash[:])) err := os.MkdirAll(buildsDir, 0777) if errorutils.CheckError(err) != nil { return "", err diff --git a/common/build/buildutils_test.go b/common/build/buildutils_test.go index 4c54fcc89..cba149c18 100644 --- a/common/build/buildutils_test.go +++ b/common/build/buildutils_test.go @@ -1,19 +1,24 @@ package build import ( - biutils "github.com/jfrog/build-info-go/utils" "os" + "os/user" "path/filepath" "strconv" + "strings" "testing" "time" + biutils "github.com/jfrog/build-info-go/utils" + + "github.com/jfrog/build-info-go/build" "github.com/jfrog/jfrog-cli-core/v2/utils/tests" testsutils "github.com/jfrog/jfrog-client-go/utils/tests" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" artclientutils "github.com/jfrog/jfrog-client-go/artifactory/services/utils" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) var timestamp = strconv.FormatInt(time.Now().Unix(), 10) @@ -245,3 +250,102 @@ func TestIsLoadedFromConfigFile(t *testing.T) { assert.Equal(t, buildName, buildNameFile) assert.Equal(t, buildNumber, artclientutils.LatestBuildNumberKey) } + +func TestCreateBuildInfoService(t *testing.T) { + service := CreateBuildInfoService() + + // Service should not be nil + require.NotNil(t, service, "CreateBuildInfoService should not return nil") + + // Get the user-specific build directory name + userSpecificDir := service.GetUserSpecificBuildDirName() + assert.NotEmpty(t, userSpecificDir, "User-specific build directory should not be empty") + + // Should contain "jfrog-" prefix (user-specific path) + assert.True(t, strings.Contains(userSpecificDir, build.BuildsJfrogPath), + "Build directory should contain '%s', got: %s", build.BuildsJfrogPath, userSpecificDir) + + // Should contain "builds" directory + assert.True(t, strings.Contains(userSpecificDir, build.BuildsDirPath), + "Build directory should contain '%s', got: %s", build.BuildsDirPath, userSpecificDir) +} + +func TestCreateBuildInfoServiceContainsUsername(t *testing.T) { + currentUser, err := user.Current() + require.NoError(t, err, "Failed to get current user") + + service := CreateBuildInfoService() + userSpecificDir := service.GetUserSpecificBuildDirName() + + // The path should contain the current username + expectedPrefix := build.BuildsJfrogPath + currentUser.Username + assert.True(t, strings.Contains(userSpecificDir, expectedPrefix), + "Build directory should contain '%s', got: %s", expectedPrefix, userSpecificDir) +} + +func TestGetBuildDir(t *testing.T) { + const testBuildName = "test-build-dir" + const testBuildNumber = "1" + const testProjectKey = "" + + // Get the build directory + buildDir, err := GetBuildDir(testBuildName, testBuildNumber, testProjectKey) + require.NoError(t, err, "GetBuildDir should not return error") + + // Cleanup after test + defer func() { + // Remove the created directory + _ = os.RemoveAll(buildDir) + }() + + // Directory should be created + assert.DirExists(t, buildDir, "Build directory should be created") + + // Path should contain user-specific directory + assert.True(t, strings.Contains(buildDir, build.BuildsJfrogPath), + "Build directory path should contain '%s', got: %s", build.BuildsJfrogPath, buildDir) + + // Path should start with CLI persistent temp dir + assert.True(t, strings.HasPrefix(buildDir, coreutils.GetCliPersistentTempDirPath()), + "Build directory should be under CLI temp dir. Got: %s, Expected prefix: %s", + buildDir, coreutils.GetCliPersistentTempDirPath()) +} + +func TestGetBuildDirUserIsolation(t *testing.T) { + currentUser, err := user.Current() + require.NoError(t, err, "Failed to get current user") + + buildDir, err := GetBuildDir("isolation-test", "1", "") + require.NoError(t, err) + + // Cleanup after test + defer func() { + _ = os.RemoveAll(buildDir) + }() + + // Path should contain user-specific directory with username + expectedUserDir := build.BuildsJfrogPath + currentUser.Username + assert.True(t, strings.Contains(buildDir, expectedUserDir), + "Build directory should contain user-specific path '%s', got: %s", expectedUserDir, buildDir) +} + +func TestGetBuildDirDifferentBuildsGetDifferentPaths(t *testing.T) { + // Create two different builds + buildDir1, err := GetBuildDir("build-a", "1", "") + require.NoError(t, err) + defer func() { _ = os.RemoveAll(buildDir1) }() + + buildDir2, err := GetBuildDir("build-b", "1", "") + require.NoError(t, err) + defer func() { _ = os.RemoveAll(buildDir2) }() + + // Different builds should get different directories (different hash) + assert.NotEqual(t, buildDir1, buildDir2, + "Different builds should have different directories") + + // But both should be under the same user-specific parent + parent1 := filepath.Dir(buildDir1) + parent2 := filepath.Dir(buildDir2) + assert.Equal(t, parent1, parent2, + "Different builds should share the same user-specific parent directory") +} diff --git a/go.mod b/go.mod index b7ab847a3..6c14378e5 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/jfrog/jfrog-cli-core/v2 -go 1.25.4 +go 1.25.5 require github.com/c-bata/go-prompt v0.2.6 // Should not be updated to 0.2.6 due to a bug (https://github.com/jfrog/jfrog-cli-core/pull/372) @@ -13,7 +13,7 @@ require ( github.com/google/uuid v1.6.0 github.com/gookit/color v1.6.0 github.com/jedib0t/go-pretty/v6 v6.6.8 - github.com/jfrog/build-info-go v1.8.9-0.20251223092904-9e9460642431 + github.com/jfrog/build-info-go v1.13.1-0.20260106203543-03b99793ca5a github.com/jfrog/gofrog v1.7.6 github.com/jfrog/jfrog-client-go v1.55.1-0.20251223101502-1a13a993b0c7 github.com/magiconair/properties v1.8.10 @@ -94,6 +94,6 @@ require ( // replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go master -// replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go dev +//replace github.com/jfrog/build-info-go => github.com/fluxxBot/build-info-go v1.10.10-0.20260105064157-73c3f6f22ba2 // replace github.com/jfrog/gofrog => github.com/jfrog/gofrog v1.3.3-0.20231223133729-ef57bd08cedc diff --git a/go.sum b/go.sum index 48b11b339..345a8e234 100644 --- a/go.sum +++ b/go.sum @@ -94,8 +94,8 @@ github.com/jedib0t/go-pretty/v6 v6.6.8 h1:JnnzQeRz2bACBobIaa/r+nqjvws4yEhcmaZ4n1 github.com/jedib0t/go-pretty/v6 v6.6.8/go.mod h1:YwC5CE4fJ1HFUDeivSV1r//AmANFHyqczZk+U6BDALU= github.com/jfrog/archiver/v3 v3.6.1 h1:LOxnkw9pOn45DzCbZNFV6K0+6dCsQ0L8mR3ZcujO5eI= github.com/jfrog/archiver/v3 v3.6.1/go.mod h1:VgR+3WZS4N+i9FaDwLZbq+jeU4B4zctXL+gL4EMzfLw= -github.com/jfrog/build-info-go v1.8.9-0.20251223092904-9e9460642431 h1:HyGqdD957CrW6T1Xst0CxTR0XqJ0baIo/uL4I5lhyuo= -github.com/jfrog/build-info-go v1.8.9-0.20251223092904-9e9460642431/go.mod h1:9W4U440fdTHwW1HiB/R0VQvz/5q8ZHsms9MWcq+JrdY= +github.com/jfrog/build-info-go v1.13.1-0.20260106203543-03b99793ca5a h1:vFjLpuSwRCQCPGjGoZcC3zn+cAGYmsuKV4mSXHq94LU= +github.com/jfrog/build-info-go v1.13.1-0.20260106203543-03b99793ca5a/go.mod h1:+OCtMb22/D+u7Wne5lzkjJjaWr0LRZcHlDwTH86Mpwo= github.com/jfrog/gofrog v1.7.6 h1:QmfAiRzVyaI7JYGsB7cxfAJePAZTzFz0gRWZSE27c6s= github.com/jfrog/gofrog v1.7.6/go.mod h1:ntr1txqNOZtHplmaNd7rS4f8jpA5Apx8em70oYEe7+4= github.com/jfrog/jfrog-client-go v1.55.1-0.20251223101502-1a13a993b0c7 h1:5JUiqmBV9ikFOZEH+ZgvJLHshT1aAuw08bfdJOLHbzQ=