Skip to content

Commit 6b0c0b9

Browse files
committed
test(internal/civisibility): cover missing commit reuse
1 parent abcb432 commit 6b0c0b9

3 files changed

Lines changed: 76 additions & 5 deletions

File tree

internal/civisibility/integrations/civisibility_features.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ var (
7171

7272
// uploadRepositoryChangesFunc is a must-not-call test seam used to prove offline/file modes suppress git upload.
7373
uploadRepositoryChangesFunc = uploadRepositoryChanges
74+
75+
// getSearchCommitsFunc allows tests to exercise repository upload control flow without reading local git state.
76+
getSearchCommitsFunc = getSearchCommits
77+
78+
// unshallowGitRepositoryFunc allows tests to control the fallback branch without mutating the local repository.
79+
unshallowGitRepositoryFunc = utils.UnshallowGitRepository
80+
81+
// sendObjectsPackFileFunc allows tests to inspect the upload request without creating or sending real pack files.
82+
sendObjectsPackFileFunc = sendObjectsPackFile
7483
)
7584

7685
// ensureSettingsInitialization performs the one-time settings bootstrap, including any git upload work required before a final settings read.
@@ -380,7 +389,7 @@ func GetImpactedTestsAnalyzer() *impactedtests.ImpactedTestAnalyzer {
380389
// uploadRepositoryChanges discovers the commits and packfiles that must be uploaded so backend features can reason about the current repo state.
381390
func uploadRepositoryChanges() (bytes int64, err error) {
382391
// get the search commits response
383-
initialCommitData, err := getSearchCommits()
392+
initialCommitData, err := getSearchCommitsFunc()
384393
if err != nil {
385394
return 0, fmt.Errorf("civisibility: error getting the search commits response: %s", err)
386395
}
@@ -409,7 +418,7 @@ func uploadRepositoryChanges() (bytes int64, err error) {
409418
}
410419

411420
// there's some missing commits on the backend, first we need to check if we need to unshallow before sending anything...
412-
hasBeenUnshallowed, err := utils.UnshallowGitRepository()
421+
hasBeenUnshallowed, err := unshallowGitRepositoryFunc()
413422
if err != nil || !hasBeenUnshallowed {
414423
if err != nil {
415424
log.Warn("%s", err.Error())
@@ -418,11 +427,11 @@ func uploadRepositoryChanges() (bytes int64, err error) {
418427
// the initial commit data
419428

420429
// send the pack file with the missing commits
421-
return sendObjectsPackFile(initialCommitData.LocalCommits[0], initialMissingCommits, initialCommitData.RemoteCommits)
430+
return sendObjectsPackFileFunc(initialCommitData.LocalCommits[0], initialMissingCommits, initialCommitData.RemoteCommits)
422431
}
423432

424433
// after unshallowing the repository we need to get the search commits to calculate the missing commits again
425-
commitsData, err := getSearchCommits()
434+
commitsData, err := getSearchCommitsFunc()
426435
if err != nil {
427436
return 0, fmt.Errorf("civisibility: error getting the search commits response: %s", err)
428437
}
@@ -433,7 +442,7 @@ func uploadRepositoryChanges() (bytes int64, err error) {
433442
}
434443

435444
// send the pack file with the missing commits
436-
return sendObjectsPackFile(commitsData.LocalCommits[0], commitsData.missingCommits(), commitsData.RemoteCommits)
445+
return sendObjectsPackFileFunc(commitsData.LocalCommits[0], commitsData.missingCommits(), commitsData.RemoteCommits)
437446
}
438447

439448
// getSearchCommits gets the search commits response with the local and remote commits

internal/civisibility/integrations/civisibility_features_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
1213
)
1314

1415
func TestSearchCommitsResponseMissingCommitsPreservesLocalOrder(t *testing.T) {
@@ -40,3 +41,61 @@ func TestSearchCommitsResponseMissingCommitsReturnsEmptyWhenAllLocalCommitsAreRe
4041

4142
assert.Empty(t, response.missingCommits())
4243
}
44+
45+
func TestUploadRepositoryChangesSkipsUploadWhenNoCommitsAreMissing(t *testing.T) {
46+
resetCIVisibilityStateForTesting()
47+
48+
getSearchCommitsFunc = func() (*searchCommitsResponse, error) {
49+
return newSearchCommitsResponse(
50+
[]string{"remote-1", "remote-2"},
51+
[]string{"remote-2", "remote-1"},
52+
true,
53+
), nil
54+
}
55+
unshallowGitRepositoryFunc = func() (bool, error) {
56+
t.Fatal("unshallow should not run when all commits are already known")
57+
return false, nil
58+
}
59+
sendObjectsPackFileFunc = func(_ string, _ []string, _ []string) (int64, error) {
60+
t.Fatal("packfile upload should not run when all commits are already known")
61+
return 0, nil
62+
}
63+
64+
bytes, err := uploadRepositoryChanges()
65+
66+
require.NoError(t, err)
67+
assert.Zero(t, bytes)
68+
}
69+
70+
func TestUploadRepositoryChangesReusesInitialMissingCommitsWhenUnshallowIsUnavailable(t *testing.T) {
71+
resetCIVisibilityStateForTesting()
72+
73+
getSearchCommitsFunc = func() (*searchCommitsResponse, error) {
74+
return newSearchCommitsResponse(
75+
[]string{"local-1", "remote-1", "local-2"},
76+
[]string{"remote-1"},
77+
true,
78+
), nil
79+
}
80+
unshallowGitRepositoryFunc = func() (bool, error) {
81+
return false, nil
82+
}
83+
84+
var uploadedCommit string
85+
var uploadedIncludes []string
86+
var uploadedExcludes []string
87+
sendObjectsPackFileFunc = func(commitSha string, commitsToInclude []string, commitsToExclude []string) (int64, error) {
88+
uploadedCommit = commitSha
89+
uploadedIncludes = append([]string(nil), commitsToInclude...)
90+
uploadedExcludes = append([]string(nil), commitsToExclude...)
91+
return 42, nil
92+
}
93+
94+
bytes, err := uploadRepositoryChanges()
95+
96+
require.NoError(t, err)
97+
assert.Equal(t, int64(42), bytes)
98+
assert.Equal(t, "local-1", uploadedCommit)
99+
assert.Equal(t, []string{"local-1", "local-2"}, uploadedIncludes)
100+
assert.Equal(t, []string{"remote-1"}, uploadedExcludes)
101+
}

internal/civisibility/integrations/testing_helpers_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ func resetCIVisibilityStateForTesting() {
2828
sourceFileMetadataCache = sync.Map{}
2929

3030
uploadRepositoryChangesFunc = uploadRepositoryChanges
31+
getSearchCommitsFunc = getSearchCommits
32+
unshallowGitRepositoryFunc = utils.UnshallowGitRepository
33+
sendObjectsPackFileFunc = sendObjectsPackFile
3134

3235
utils.ResetCITags()
3336
utils.ResetCIMetrics()

0 commit comments

Comments
 (0)