Skip to content

Commit

Permalink
chore: improve resolvers tests
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Aguilar <[email protected]>
  • Loading branch information
thepabloaguilar committed Apr 28, 2024
1 parent 0951418 commit c7b510c
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 16 deletions.
142 changes: 142 additions & 0 deletions internal/storage/fs/git/reference_resolvers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package git

import (
"fmt"
"testing"

"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)

func TestStaticResolver(t *testing.T) {
t.Run("should resolve static references correctly", func(t *testing.T) {
repo := newGitRepo(t)
resolver := StaticResolver()

commitHash := repo.createCommit(t)
resolvedHash, err := resolver(repo.repo, "main")

require.NoError(t, err)
require.Equal(t, commitHash, resolvedHash)

repo.checkout(t, "new-branch")

commitHash = repo.createCommit(t)
resolvedHash, err = resolver(repo.repo, "new-branch")

require.NoError(t, err)
require.Equal(t, commitHash, resolvedHash)
})
}

func TestSemverResolver(t *testing.T) {
t.Run("should resolve semver tags correctly when the reference is a constraint", func(t *testing.T) {
repo := newGitRepo(t)
resolver := SemverResolver()
constraint := "v0.1.*"

commitHash := repo.createCommit(t)
repo.createTag(t, "v0.1.0", commitHash)

resolvedHash, err := resolver(repo.repo, constraint)

require.NoError(t, err)
require.Equal(t, commitHash, resolvedHash)

// When commiting again and creating a new tag respecting the constraint should resolve nicely

Check failure on line 50 in internal/storage/fs/git/reference_resolvers_test.go

View workflow job for this annotation

GitHub Actions / Lint Go

`commiting` is a misspelling of `committing` (misspell)
commitHash = repo.createCommit(t)
repo.createTag(t, "v0.1.4", commitHash)

resolvedHash, err = resolver(repo.repo, constraint)

require.NoError(t, err)
require.Equal(t, commitHash, resolvedHash)
})

t.Run("should resolve semver tags correctly when the reference is not a constraint", func(t *testing.T) {
repo := newGitRepo(t)
resolver := SemverResolver()

commitHash := repo.createCommit(t)
repo.createTag(t, "v0.1.0", commitHash)

resolvedHash, err := resolver(repo.repo, "v0.1.0")

require.NoError(t, err)
require.Equal(t, commitHash, resolvedHash)
})

t.Run("should return an error when no matching tag was found", func(t *testing.T) {
repo := newGitRepo(t)
resolver := SemverResolver()

commitHash := repo.createCommit(t)
repo.createTag(t, "v0.1.0", commitHash)

_, err := resolver(repo.repo, "v1.*.*")

require.ErrorContains(t, err, "could not find the specified tag reference")
})
}

type gitRepoTest struct {
repo *git.Repository
}

func (g *gitRepoTest) createCommit(t *testing.T) plumbing.Hash {
t.Helper()

workTree, err := g.repo.Worktree()
require.NoError(t, err)

fileName := uuid.NewString()

file, err := workTree.Filesystem.Create(fileName)
require.NoError(t, err)
defer file.Close()

_, err = workTree.Add(fileName)
require.NoError(t, err)

commitHash, err := workTree.Commit(fmt.Sprintf("adding %s", fileName), &git.CommitOptions{})
require.NoError(t, err)

return commitHash
}

func (g *gitRepoTest) checkout(t *testing.T, branchName string) {
t.Helper()

workTree, err := g.repo.Worktree()
require.NoError(t, err)

err = workTree.Checkout(&git.CheckoutOptions{
Branch: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branchName)),
Create: true,
})
require.NoError(t, err)
}

func (g *gitRepoTest) createTag(t *testing.T, tag string, commit plumbing.Hash) {
t.Helper()

_, err := g.repo.CreateTag(tag, commit, nil)
require.NoError(t, err)
}

func newGitRepo(t *testing.T) *gitRepoTest {
t.Helper()

repo, err := git.InitWithOptions(memory.NewStorage(), memfs.New(), git.InitOptions{
DefaultBranch: "refs/heads/main",
})
require.NoError(t, err)

return &gitRepoTest{
repo: repo,
}
}
17 changes: 1 addition & 16 deletions internal/storage/fs/git/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/pem"
"errors"
"net/http/httptest"
"os"
"testing"
Expand Down Expand Up @@ -247,20 +246,6 @@ func Test_Store_View_WithSemverRevision(t *testing.T) {
return
}

// Test if we resolve the semver constraint to the same hash from the actual HEAD
hash, err := store.resolve("v0.1.*")
require.NoError(t, err)
require.Equal(t, head, hash.String())

// Test if we resolve the semver tag to the same hash from the actual HEAD
hash, err = store.resolve(tag)
require.NoError(t, err)
require.Equal(t, head, hash.String())

// Test if we resolve a non-matching reference it fails
_, err = store.resolve("v1.1.*")
require.ErrorIs(t, err, errors.New("could not find the specified tag reference"))

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

Expand Down Expand Up @@ -337,7 +322,7 @@ flags:
t.Log("received new snapshot")

// Test if we can resolve to the new tag
hash, err = store.resolve("v0.1.*")
hash, err := store.resolve("v0.1.*")
require.NoError(t, err)
require.Equal(t, commit.String(), hash.String())

Expand Down

0 comments on commit c7b510c

Please sign in to comment.