Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicated code for scaffolding git repository #2559

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions private/buf/bufsync/backfill_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (

"github.com/bufbuild/buf/private/buf/bufsync"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref"
"github.com/bufbuild/buf/private/pkg/command"
"github.com/bufbuild/buf/private/pkg/git"
"github.com/bufbuild/buf/private/pkg/git/gittest"
"github.com/bufbuild/buf/private/pkg/storage/storagegit"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -33,10 +33,10 @@ import (
func TestBackfilltags(t *testing.T) {
t.Parallel()
const defaultBranchName = "main"
repo, repoDir := scaffoldGitRepository(t, defaultBranchName)
repo := gittest.ScaffoldGitRepository(t, gittest.ScaffoldGitRepositoryWithOnlyInitialCommit())
moduleIdentityInHEAD, err := bufmoduleref.NewModuleIdentity("buf.build", "acme", "foo")
require.NoError(t, err)
prepareGitRepoBackfillTags(t, repoDir, moduleIdentityInHEAD)
prepareGitRepoBackfillTags(t, repo, moduleIdentityInHEAD)
mockHandler := newMockSyncHandler()
// prepare the top 5 commits as syncable commits, mark the rest as if they were already synced
var (
Expand Down Expand Up @@ -95,30 +95,20 @@ func TestBackfilltags(t *testing.T) {

// prepareGitRepoBackfillTags adds 20 commits and tags in the default branch, one tag per commit. It
// waits 1s between commit 5 and 6 to be easily used as the lookback commit limit time.
func prepareGitRepoBackfillTags(t *testing.T, repoDir string, moduleIdentity bufmoduleref.ModuleIdentity) {
runner := command.NewRunner()
func prepareGitRepoBackfillTags(t *testing.T, repo gittest.Repository, moduleIdentity bufmoduleref.ModuleIdentity) {
var commitsCounter int
doEmptyCommitAndTag := func(numOfCommits int) {
for i := 0; i < numOfCommits; i++ {
commitsCounter++
runInDir(
t, runner, repoDir,
"git", "commit", "--allow-empty",
"-m", fmt.Sprintf("commit %d", commitsCounter),
)
runInDir(
t, runner, repoDir,
"git", "tag", fmt.Sprintf("tag-%d", commitsCounter),
)
repo.Commit(t, fmt.Sprintf("commit %d", commitsCounter), nil)
repo.Tag(t, fmt.Sprintf("tag-%d", commitsCounter), "")
}
}
// write the base module in the root
writeFiles(t, repoDir, map[string]string{
repo.Commit(t, "commit 0", map[string]string{
"buf.yaml": fmt.Sprintf("version: v1\nname: %s\n", moduleIdentity.IdentityString()),
"foo/v1/foo.proto": "syntax = \"proto3\";\n\npackage foo.v1;\n\nmessage Foo {}\n",
})
runInDir(t, runner, repoDir, "git", "add", ".")
runInDir(t, runner, repoDir, "git", "commit", "-m", "commit 0")
// commit and tag
doEmptyCommitAndTag(5)
time.Sleep(1 * time.Second)
Expand Down
70 changes: 0 additions & 70 deletions private/buf/bufsync/bufsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,86 +15,16 @@
package bufsync_test

import (
"bytes"
"context"
"errors"
"io"
"os"
"path"
"path/filepath"
"strings"
"testing"
"time"

"github.com/bufbuild/buf/private/buf/bufsync"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref"
"github.com/bufbuild/buf/private/pkg/command"
"github.com/bufbuild/buf/private/pkg/git"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
)

// scaffoldGitRepository returns an initialized git repository with a single commit, and returns the
// repository and its directory.
func scaffoldGitRepository(t *testing.T, defaultBranchName string) (git.Repository, string) {
runner := command.NewRunner()
repoDir := scaffoldGitRepositoryDir(t, runner, defaultBranchName)
dotGitPath := path.Join(repoDir, git.DotGitDir)
repo, err := git.OpenRepository(
context.Background(),
dotGitPath,
runner,
git.OpenRepositoryWithDefaultBranch(defaultBranchName),
)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, repo.Close())
})
return repo, repoDir
}

// scaffoldGitRepositoryDir prepares a git repository with an initial README, and a single commit.
// It returns the directory where the local git repo is.
func scaffoldGitRepositoryDir(t *testing.T, runner command.Runner, defaultBranchName string) string {
repoDir := t.TempDir()

// setup repo
runInDir(t, runner, repoDir, "git", "init", "--initial-branch", defaultBranchName)
runInDir(t, runner, repoDir, "git", "config", "user.name", "Buf TestBot")
runInDir(t, runner, repoDir, "git", "config", "user.email", "testbot@buf.build")

// write and commit a README file
writeFiles(t, repoDir, map[string]string{"README.md": "This is a scaffold repository.\n"})
runInDir(t, runner, repoDir, "git", "add", ".")
runInDir(t, runner, repoDir, "git", "commit", "-m", "Write README")

return repoDir
}

func runInDir(t *testing.T, runner command.Runner, dir string, cmd string, args ...string) {
stderr := bytes.NewBuffer(nil)
err := runner.Run(
context.Background(),
cmd,
command.RunWithArgs(args...),
command.RunWithDir(dir),
command.RunWithStderr(stderr),
)
if err != nil {
t.Logf("run %q", strings.Join(append([]string{cmd}, args...), " "))
_, err := io.Copy(os.Stderr, stderr)
require.NoError(t, err)
}
require.NoError(t, err)
}

func writeFiles(t *testing.T, directoryPath string, pathToContents map[string]string) {
for path, contents := range pathToContents {
require.NoError(t, os.MkdirAll(filepath.Join(directoryPath, filepath.Dir(path)), 0700))
require.NoError(t, os.WriteFile(filepath.Join(directoryPath, path), []byte(contents), 0600))
}
}

type mockClock struct {
now time.Time
}
Expand Down
51 changes: 22 additions & 29 deletions private/buf/bufsync/commits_to_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/bufbuild/buf/private/buf/bufsync"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref"
"github.com/bufbuild/buf/private/pkg/command"
"github.com/bufbuild/buf/private/pkg/git/gittest"
"github.com/bufbuild/buf/private/pkg/storage/storagegit"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
Expand All @@ -33,10 +34,9 @@ func TestCommitsToSyncWithNoPreviousSyncPoints(t *testing.T) {
require.NoError(t, err)
moduleIdentityOverride, err := bufmoduleref.NewModuleIdentity("buf.build", "acme", "bar")
require.NoError(t, err)
const defaultBranchName = "main"
repo, repoDir := scaffoldGitRepository(t, defaultBranchName)
repo := gittest.ScaffoldGitRepository(t, gittest.ScaffoldGitRepositoryWithOnlyInitialCommit())
runner := command.NewRunner()
prepareGitRepoSyncWithNoPreviousSyncPoints(t, runner, repoDir, moduleIdentityInHEAD, defaultBranchName)
prepareGitRepoSyncWithNoPreviousSyncPoints(t, runner, repo, moduleIdentityInHEAD, gittest.DefaultBranch)
type testCase struct {
name string
branch string
Expand All @@ -45,7 +45,7 @@ func TestCommitsToSyncWithNoPreviousSyncPoints(t *testing.T) {
testCases := []testCase{
{
name: "when_main",
branch: "main",
branch: gittest.DefaultBranch,
expectedCommits: 4, // doesn't include initial scaffolding empty commit
},
{
Expand All @@ -70,7 +70,7 @@ func TestCommitsToSyncWithNoPreviousSyncPoints(t *testing.T) {
func(tc testCase) {
t.Run(fmt.Sprintf("%s/override_%t", tc.name, withOverride), func(t *testing.T) {
// check out the branch to sync
runInDir(t, runner, repoDir, "git", "checkout", tc.branch)
repo.Checkout(t, tc.branch)
const moduleDir = "."
var opts []bufsync.SyncerOption
if withOverride {
Expand Down Expand Up @@ -105,42 +105,35 @@ func TestCommitsToSyncWithNoPreviousSyncPoints(t *testing.T) {
func prepareGitRepoSyncWithNoPreviousSyncPoints(
t *testing.T,
runner command.Runner,
saquibmian marked this conversation as resolved.
Show resolved Hide resolved
repoDir string,
repo gittest.Repository,
moduleIdentity bufmoduleref.ModuleIdentity,
defaultBranchName string,
) {
var allBranches = []string{defaultBranchName, "foo", "bar", "baz"}

var commitsCounter int
doEmptyCommit := func(numOfCommits int) {
doEmptyCommits := func(numOfCommits int) {
for i := 0; i < numOfCommits; i++ {
commitsCounter++
runInDir(
t, runner, repoDir,
"git", "commit", "--allow-empty",
"-m", fmt.Sprintf("commit %d", commitsCounter),
)
repo.Commit(t, fmt.Sprintf("commit %d", commitsCounter), nil)
}
}

// write the base module in the root
writeFiles(t, repoDir, map[string]string{
repo.Commit(t, "commit 0", map[string]string{
"buf.yaml": fmt.Sprintf("version: v1\nname: %s\n", moduleIdentity.IdentityString()),
})
runInDir(t, runner, repoDir, "git", "add", ".")
runInDir(t, runner, repoDir, "git", "commit", "-m", "commit 0")

doEmptyCommit(1)
runInDir(t, runner, repoDir, "git", "checkout", "-b", allBranches[1])
doEmptyCommit(2)
runInDir(t, runner, repoDir, "git", "checkout", defaultBranchName)
doEmptyCommit(1)
runInDir(t, runner, repoDir, "git", "checkout", "-b", allBranches[2])
doEmptyCommit(1)
runInDir(t, runner, repoDir, "git", "checkout", "-b", allBranches[3])
doEmptyCommit(1)
runInDir(t, runner, repoDir, "git", "checkout", allBranches[2])
doEmptyCommit(1)
runInDir(t, runner, repoDir, "git", "checkout", defaultBranchName)
doEmptyCommit(1)
doEmptyCommits(1)
repo.CheckoutB(t, allBranches[1])
doEmptyCommits(2)
repo.Checkout(t, defaultBranchName)
doEmptyCommits(1)
repo.CheckoutB(t, allBranches[2])
doEmptyCommits(1)
repo.CheckoutB(t, allBranches[3])
doEmptyCommits(1)
repo.Checkout(t, allBranches[2])
doEmptyCommits(1)
repo.Checkout(t, defaultBranchName)
doEmptyCommits(1)
}
22 changes: 9 additions & 13 deletions private/buf/bufsync/prepare_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/bufbuild/buf/private/buf/bufsync"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref"
"github.com/bufbuild/buf/private/pkg/command"
"github.com/bufbuild/buf/private/pkg/git/gittest"
"github.com/bufbuild/buf/private/pkg/storage/storagegit"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -70,9 +70,8 @@ func TestPrepareSyncDuplicateIdentities(t *testing.T) {
func(tc testCase) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
const defaultBranchName = "main"
repo, repoDir := scaffoldGitRepository(t, defaultBranchName)
prepareGitRepoMultiModule(t, repoDir, tc.modulesIdentitiesInHEAD)
repo := gittest.ScaffoldGitRepository(t, gittest.ScaffoldGitRepositoryWithOnlyInitialCommit())
prepareGitRepoMultiModule(t, repo, tc.modulesIdentitiesInHEAD)
var moduleDirs []string
for moduleDir := range tc.modulesIdentitiesInHEAD {
moduleDirs = append(moduleDirs, moduleDir)
Expand All @@ -93,7 +92,7 @@ func TestPrepareSyncDuplicateIdentities(t *testing.T) {
err = syncer.Sync(context.Background())
require.Error(t, err)
assert.Contains(t, err.Error(), repeatedIdentity.IdentityString())
assert.Contains(t, err.Error(), defaultBranchName)
assert.Contains(t, err.Error(), gittest.DefaultBranch)
for _, moduleDir := range moduleDirs {
assert.Contains(t, err.Error(), moduleDir)
}
Expand All @@ -103,14 +102,11 @@ func TestPrepareSyncDuplicateIdentities(t *testing.T) {
}

// prepareGitRepoMultiModule commits valid modules to the passed directories and module identities.
func prepareGitRepoMultiModule(t *testing.T, repoDir string, moduleDirsToIdentities map[string]bufmoduleref.ModuleIdentity) {
runner := command.NewRunner()
func prepareGitRepoMultiModule(t *testing.T, repo gittest.Repository, moduleDirsToIdentities map[string]bufmoduleref.ModuleIdentity) {
files := make(map[string]string)
for moduleDir, moduleIdentity := range moduleDirsToIdentities {
writeFiles(t, repoDir, map[string]string{
moduleDir + "/buf.yaml": fmt.Sprintf("version: v1\nname: %s\n", moduleIdentity.IdentityString()),
moduleDir + "/foo/v1/foo.proto": "syntax = \"proto3\";\n\npackage foo.v1;\n\nmessage Foo {}\n",
})
files[moduleDir+"/buf.yaml"] = fmt.Sprintf("version: v1\nname: %s\n", moduleIdentity.IdentityString())
files[moduleDir+"/foo/v1/foo.proto"] = "syntax = \"proto3\";\n\npackage foo.v1;\n\nmessage Foo {}\n"
}
runInDir(t, runner, repoDir, "git", "add", ".")
runInDir(t, runner, repoDir, "git", "commit", "-m", "commit")
repo.Commit(t, "commit", files)
}
Loading