Skip to content

WIP: update git repo #347

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
git: add failing test for FetchAfterClone
  • Loading branch information
johnstcn committed Sep 12, 2024
commit ec3fc5ba4d328cb4af2c00d6247b541ed417b7c2
55 changes: 55 additions & 0 deletions git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"

"github.com/coder/envbuilder/git"
"github.com/coder/envbuilder/options"
Expand Down Expand Up @@ -234,6 +236,59 @@ func TestShallowCloneRepo(t *testing.T) {
})
}

func TestFetchAfterClone(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
// setup a git repo
srvDir := t.TempDir()
srvFS := osfs.New(srvDir, osfs.WithChrootOS())
repo := gittest.NewRepo(t, srvFS)
repo.Commit(gittest.Commit(t, "README.md", "Hello, worldd!", "initial commit"))
headBefore, err := repo.Repo.Head()
require.NoError(t, err)
srv := httptest.NewServer(gittest.NewServer(srvFS))

// clone to a tempdir
clientDir := t.TempDir()
clientFS := osfs.New(clientDir, osfs.WithChrootOS())
cloned, err := git.CloneRepo(ctx, git.CloneRepoOptions{
Path: "/repo",
RepoURL: srv.URL,
Storage: clientFS,
})

require.NoError(t, err)
require.True(t, cloned)

// add some commits on the server
repo.Commit(gittest.Commit(t, "README.md", "Hello, world!", "fix typo"))
// ensure state
bs, err := os.ReadFile(filepath.Join(srvDir, "README.md"))
require.NoError(t, err)
require.Equal(t, "Hello, world!", strings.TrimSpace(string(bs)))
headAfter, err := repo.Repo.Head()
require.NoError(t, err)
require.NotEqual(t, headBefore.Hash(), headAfter.Hash())

// run CloneRepo again
clonedAgain, err := git.CloneRepo(ctx, git.CloneRepoOptions{
Path: "/repo",
RepoURL: srv.URL,
Storage: clientFS,
})
require.NoError(t, err)
require.True(t, clonedAgain)

// Inspect the cloned repo and check last commit
headFile, err := clientFS.Open(filepath.Join(".git/refs/heads/main"))
require.NoError(t, err)
var sb strings.Builder
_, err = io.Copy(&sb, headFile)
require.NoError(t, err)
require.Equal(t, headAfter, sb.String())
}

func TestCloneRepoSSH(t *testing.T) {
t.Parallel()

Expand Down