|
| 1 | +package git_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/github/git-bundle-server/internal/cmd" |
| 9 | + "github.com/github/git-bundle-server/internal/git" |
| 10 | + . "github.com/github/git-bundle-server/internal/testhelpers" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/mock" |
| 13 | +) |
| 14 | + |
| 15 | +var createIncrementalBundleTests = []struct { |
| 16 | + title string |
| 17 | + |
| 18 | + // Inputs |
| 19 | + repoDir string |
| 20 | + filename string |
| 21 | + prereqs []string |
| 22 | + |
| 23 | + // Mocked responses |
| 24 | + bundleCreate Pair[int, error] |
| 25 | + bundleCreateStderr string |
| 26 | + |
| 27 | + // Expected values |
| 28 | + expectedBundleCreated bool |
| 29 | + expectErr bool |
| 30 | +}{ |
| 31 | + { |
| 32 | + "Successful bundle creation", |
| 33 | + |
| 34 | + "/test/home/git-bundle-server/git/test/myrepo/", |
| 35 | + "/test/home/git-bundle-server/www/test/myrepo/bundle-1234.bundle", |
| 36 | + []string{"^018d4b8a"}, |
| 37 | + |
| 38 | + NewPair[int, error](0, nil), |
| 39 | + "", |
| 40 | + |
| 41 | + true, |
| 42 | + false, |
| 43 | + }, |
| 44 | + { |
| 45 | + "Successful no-op (empty bundle)", |
| 46 | + |
| 47 | + "/test/home/git-bundle-server/git/test/myrepo/", |
| 48 | + "/test/home/git-bundle-server/www/test/myrepo/bundle-5678.bundle", |
| 49 | + []string{"^0793b0ce", "^3649daa0"}, |
| 50 | + |
| 51 | + NewPair[int, error](128, nil), |
| 52 | + "fatal: Refusing to create empty bundle", |
| 53 | + |
| 54 | + false, |
| 55 | + false, |
| 56 | + }, |
| 57 | +} |
| 58 | + |
| 59 | +func TestGit_CreateIncrementalBundle(t *testing.T) { |
| 60 | + // Set up mocks |
| 61 | + testLogger := &MockTraceLogger{} |
| 62 | + testCommandExecutor := &MockCommandExecutor{} |
| 63 | + |
| 64 | + gitHelper := git.NewGitHelper(testLogger, testCommandExecutor) |
| 65 | + |
| 66 | + for _, tt := range createIncrementalBundleTests { |
| 67 | + t.Run(tt.title, func(t *testing.T) { |
| 68 | + var stdin io.Reader |
| 69 | + var stdout io.Writer |
| 70 | + |
| 71 | + // Mock responses |
| 72 | + testCommandExecutor.On("Run", |
| 73 | + mock.Anything, |
| 74 | + "git", |
| 75 | + []string{"-C", tt.repoDir, "bundle", "create", tt.filename, "--stdin", "--branches"}, |
| 76 | + mock.MatchedBy(func(settings []cmd.Setting) bool { |
| 77 | + var ok bool |
| 78 | + stdin = nil |
| 79 | + stdout = nil |
| 80 | + for _, setting := range settings { |
| 81 | + switch setting.Key { |
| 82 | + case cmd.StdinKey: |
| 83 | + stdin, ok = setting.Value.(io.Reader) |
| 84 | + if !ok { |
| 85 | + return false |
| 86 | + } |
| 87 | + case cmd.StderrKey: |
| 88 | + stdout, ok = setting.Value.(io.Writer) |
| 89 | + if !ok { |
| 90 | + return false |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + return stdin != nil && stdout != nil |
| 95 | + }), |
| 96 | + ).Run(func(mock.Arguments) { |
| 97 | + stdout.Write([]byte(tt.bundleCreateStderr)) |
| 98 | + }).Return(tt.bundleCreate.First, tt.bundleCreate.Second) |
| 99 | + |
| 100 | + // Run 'CreateIncrementalBundle()' |
| 101 | + actualBundleCreated, err := gitHelper.CreateIncrementalBundle(context.Background(), tt.repoDir, tt.filename, tt.prereqs) |
| 102 | + |
| 103 | + // Assert on expected values |
| 104 | + assert.Equal(t, tt.expectedBundleCreated, actualBundleCreated) |
| 105 | + if tt.expectErr { |
| 106 | + assert.Error(t, err) |
| 107 | + } else { |
| 108 | + assert.NoError(t, err) |
| 109 | + } |
| 110 | + mock.AssertExpectationsForObjects(t, testCommandExecutor) |
| 111 | + |
| 112 | + // Check the content of stdin |
| 113 | + expectedStdin := ConcatLines(tt.prereqs) |
| 114 | + expectedStdinLen := len(expectedStdin) |
| 115 | + stdinBytes := make([]byte, expectedStdinLen+1) |
| 116 | + numRead, err := stdin.Read(stdinBytes) |
| 117 | + assert.NoError(t, err) |
| 118 | + assert.Equal(t, expectedStdinLen, numRead) |
| 119 | + assert.Equal(t, expectedStdin, string(stdinBytes[:expectedStdinLen])) |
| 120 | + |
| 121 | + // Reset mocks |
| 122 | + testCommandExecutor.Mock = mock.Mock{} |
| 123 | + }) |
| 124 | + } |
| 125 | +} |
0 commit comments