|
1 | 1 | package build |
2 | 2 |
|
3 | 3 | import ( |
4 | | - biutils "github.com/jfrog/build-info-go/utils" |
5 | 4 | "os" |
| 5 | + "os/user" |
6 | 6 | "path/filepath" |
7 | 7 | "strconv" |
| 8 | + "strings" |
8 | 9 | "testing" |
9 | 10 | "time" |
10 | 11 |
|
| 12 | + biutils "github.com/jfrog/build-info-go/utils" |
| 13 | + |
| 14 | + "github.com/jfrog/build-info-go/build" |
11 | 15 | "github.com/jfrog/jfrog-cli-core/v2/utils/tests" |
12 | 16 | testsutils "github.com/jfrog/jfrog-client-go/utils/tests" |
13 | 17 |
|
14 | 18 | "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" |
15 | 19 | artclientutils "github.com/jfrog/jfrog-client-go/artifactory/services/utils" |
16 | 20 | "github.com/stretchr/testify/assert" |
| 21 | + "github.com/stretchr/testify/require" |
17 | 22 | ) |
18 | 23 |
|
19 | 24 | var timestamp = strconv.FormatInt(time.Now().Unix(), 10) |
@@ -245,3 +250,102 @@ func TestIsLoadedFromConfigFile(t *testing.T) { |
245 | 250 | assert.Equal(t, buildName, buildNameFile) |
246 | 251 | assert.Equal(t, buildNumber, artclientutils.LatestBuildNumberKey) |
247 | 252 | } |
| 253 | + |
| 254 | +func TestCreateBuildInfoService(t *testing.T) { |
| 255 | + service := CreateBuildInfoService() |
| 256 | + |
| 257 | + // Service should not be nil |
| 258 | + require.NotNil(t, service, "CreateBuildInfoService should not return nil") |
| 259 | + |
| 260 | + // Get the user-specific build directory name |
| 261 | + userSpecificDir := service.GetUserSpecificBuildDirName() |
| 262 | + assert.NotEmpty(t, userSpecificDir, "User-specific build directory should not be empty") |
| 263 | + |
| 264 | + // Should contain "jfrog-" prefix (user-specific path) |
| 265 | + assert.True(t, strings.Contains(userSpecificDir, build.BuildsJfrogPath), |
| 266 | + "Build directory should contain '%s', got: %s", build.BuildsJfrogPath, userSpecificDir) |
| 267 | + |
| 268 | + // Should contain "builds" directory |
| 269 | + assert.True(t, strings.Contains(userSpecificDir, build.BuildsDirPath), |
| 270 | + "Build directory should contain '%s', got: %s", build.BuildsDirPath, userSpecificDir) |
| 271 | +} |
| 272 | + |
| 273 | +func TestCreateBuildInfoServiceContainsUsername(t *testing.T) { |
| 274 | + currentUser, err := user.Current() |
| 275 | + require.NoError(t, err, "Failed to get current user") |
| 276 | + |
| 277 | + service := CreateBuildInfoService() |
| 278 | + userSpecificDir := service.GetUserSpecificBuildDirName() |
| 279 | + |
| 280 | + // The path should contain the current username |
| 281 | + expectedPrefix := build.BuildsJfrogPath + currentUser.Username |
| 282 | + assert.True(t, strings.Contains(userSpecificDir, expectedPrefix), |
| 283 | + "Build directory should contain '%s', got: %s", expectedPrefix, userSpecificDir) |
| 284 | +} |
| 285 | + |
| 286 | +func TestGetBuildDir(t *testing.T) { |
| 287 | + const testBuildName = "test-build-dir" |
| 288 | + const testBuildNumber = "1" |
| 289 | + const testProjectKey = "" |
| 290 | + |
| 291 | + // Get the build directory |
| 292 | + buildDir, err := GetBuildDir(testBuildName, testBuildNumber, testProjectKey) |
| 293 | + require.NoError(t, err, "GetBuildDir should not return error") |
| 294 | + |
| 295 | + // Cleanup after test |
| 296 | + defer func() { |
| 297 | + // Remove the created directory |
| 298 | + _ = os.RemoveAll(buildDir) |
| 299 | + }() |
| 300 | + |
| 301 | + // Directory should be created |
| 302 | + assert.DirExists(t, buildDir, "Build directory should be created") |
| 303 | + |
| 304 | + // Path should contain user-specific directory |
| 305 | + assert.True(t, strings.Contains(buildDir, build.BuildsJfrogPath), |
| 306 | + "Build directory path should contain '%s', got: %s", build.BuildsJfrogPath, buildDir) |
| 307 | + |
| 308 | + // Path should start with CLI persistent temp dir |
| 309 | + assert.True(t, strings.HasPrefix(buildDir, coreutils.GetCliPersistentTempDirPath()), |
| 310 | + "Build directory should be under CLI temp dir. Got: %s, Expected prefix: %s", |
| 311 | + buildDir, coreutils.GetCliPersistentTempDirPath()) |
| 312 | +} |
| 313 | + |
| 314 | +func TestGetBuildDirUserIsolation(t *testing.T) { |
| 315 | + currentUser, err := user.Current() |
| 316 | + require.NoError(t, err, "Failed to get current user") |
| 317 | + |
| 318 | + buildDir, err := GetBuildDir("isolation-test", "1", "") |
| 319 | + require.NoError(t, err) |
| 320 | + |
| 321 | + // Cleanup after test |
| 322 | + defer func() { |
| 323 | + _ = os.RemoveAll(buildDir) |
| 324 | + }() |
| 325 | + |
| 326 | + // Path should contain user-specific directory with username |
| 327 | + expectedUserDir := build.BuildsJfrogPath + currentUser.Username |
| 328 | + assert.True(t, strings.Contains(buildDir, expectedUserDir), |
| 329 | + "Build directory should contain user-specific path '%s', got: %s", expectedUserDir, buildDir) |
| 330 | +} |
| 331 | + |
| 332 | +func TestGetBuildDirDifferentBuildsGetDifferentPaths(t *testing.T) { |
| 333 | + // Create two different builds |
| 334 | + buildDir1, err := GetBuildDir("build-a", "1", "") |
| 335 | + require.NoError(t, err) |
| 336 | + defer func() { _ = os.RemoveAll(buildDir1) }() |
| 337 | + |
| 338 | + buildDir2, err := GetBuildDir("build-b", "1", "") |
| 339 | + require.NoError(t, err) |
| 340 | + defer func() { _ = os.RemoveAll(buildDir2) }() |
| 341 | + |
| 342 | + // Different builds should get different directories (different hash) |
| 343 | + assert.NotEqual(t, buildDir1, buildDir2, |
| 344 | + "Different builds should have different directories") |
| 345 | + |
| 346 | + // But both should be under the same user-specific parent |
| 347 | + parent1 := filepath.Dir(buildDir1) |
| 348 | + parent2 := filepath.Dir(buildDir2) |
| 349 | + assert.Equal(t, parent1, parent2, |
| 350 | + "Different builds should share the same user-specific parent directory") |
| 351 | +} |
0 commit comments