github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libgit/autogit_manager_test.go (about)

     1  // Copyright 2017 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package libgit
     6  
     7  import (
     8  	"context"
     9  	"io"
    10  	"os"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/keybase/client/go/kbfs/data"
    15  	"github.com/keybase/client/go/kbfs/libcontext"
    16  	"github.com/keybase/client/go/kbfs/libfs"
    17  	"github.com/keybase/client/go/kbfs/libkbfs"
    18  	"github.com/keybase/client/go/kbfs/tlfhandle"
    19  	"github.com/keybase/client/go/protocol/keybase1"
    20  	"github.com/stretchr/testify/require"
    21  	billy "gopkg.in/src-d/go-billy.v4"
    22  	gogit "gopkg.in/src-d/go-git.v4"
    23  	"gopkg.in/src-d/go-git.v4/plumbing"
    24  	"gopkg.in/src-d/go-git.v4/plumbing/object"
    25  )
    26  
    27  func initConfigForAutogit(t *testing.T) (
    28  	ctx context.Context, config *libkbfs.ConfigLocal,
    29  	cancel context.CancelFunc, tempdir string) {
    30  	ctx = libcontext.BackgroundContextWithCancellationDelayer()
    31  	config = libkbfs.MakeTestConfigOrBustLoggedInWithMode(
    32  		t, 0, libkbfs.InitDefault, "user1", "user2")
    33  	success := false
    34  	ctx = context.WithValue(ctx, libkbfs.CtxAllowNameKey, kbfsRepoDir)
    35  
    36  	ctx, cancel = context.WithTimeout(ctx, 60*time.Second)
    37  
    38  	tempdir, err := os.MkdirTemp(os.TempDir(), "journal_server")
    39  	require.NoError(t, err)
    40  	defer func() {
    41  		if !success {
    42  			os.RemoveAll(tempdir)
    43  		}
    44  	}()
    45  
    46  	err = config.EnableDiskLimiter(tempdir)
    47  	require.NoError(t, err)
    48  	err = config.EnableJournaling(
    49  		ctx, tempdir, libkbfs.TLFJournalBackgroundWorkEnabled)
    50  	require.NoError(t, err)
    51  
    52  	success = true
    53  	return ctx, config, cancel, tempdir
    54  }
    55  
    56  func addFileToWorktreeWithInfo(
    57  	t *testing.T, repo *gogit.Repository, worktreeFS billy.Filesystem,
    58  	name, data, msg, userName, userEmail string, timestamp time.Time) (
    59  	hash plumbing.Hash) {
    60  	foo, err := worktreeFS.Create(name)
    61  	require.NoError(t, err)
    62  	defer foo.Close()
    63  	_, err = io.WriteString(foo, data)
    64  	require.NoError(t, err)
    65  	wt, err := repo.Worktree()
    66  	require.NoError(t, err)
    67  	_, err = wt.Add(name)
    68  	require.NoError(t, err)
    69  	hash, err = wt.Commit(msg, &gogit.CommitOptions{
    70  		Author: &object.Signature{
    71  			Name:  userName,
    72  			Email: userEmail,
    73  			When:  timestamp,
    74  		},
    75  	})
    76  	require.NoError(t, err)
    77  	return hash
    78  }
    79  
    80  func addFileToWorktree(
    81  	t *testing.T, repo *gogit.Repository, worktreeFS billy.Filesystem,
    82  	name, data string) {
    83  	_ = addFileToWorktreeWithInfo(
    84  		t, repo, worktreeFS, name, data, "foo commit", "me", "me@keyba.se",
    85  		time.Now())
    86  }
    87  
    88  func addFileToWorktreeAndCommit(
    89  	ctx context.Context, t *testing.T, config libkbfs.Config,
    90  	h *tlfhandle.Handle, repo *gogit.Repository, worktreeFS billy.Filesystem,
    91  	name, data string) {
    92  	addFileToWorktree(t, repo, worktreeFS, name, data)
    93  	commitWorktree(ctx, t, config, h, worktreeFS)
    94  }
    95  
    96  func commitWorktree(
    97  	ctx context.Context, t *testing.T, config libkbfs.Config,
    98  	h *tlfhandle.Handle, worktreeFS billy.Filesystem) {
    99  	err := worktreeFS.(*libfs.FS).SyncAll()
   100  	require.NoError(t, err)
   101  	jManager, err := libkbfs.GetJournalManager(config)
   102  	require.NoError(t, err)
   103  	rootNode, _, err := config.KBFSOps().GetOrCreateRootNode(
   104  		ctx, h, data.MasterBranch)
   105  	require.NoError(t, err)
   106  	err = jManager.FinishSingleOp(ctx,
   107  		rootNode.GetFolderBranch().Tlf, nil, keybase1.MDPriorityNormal)
   108  	require.NoError(t, err)
   109  }