tests: add parseActionsFromRepoChanges tests

This commit is contained in:
2025-01-19 12:09:24 +03:00
parent 4ca557402a
commit 9fc8709e7b
3 changed files with 213 additions and 30 deletions

View File

@ -31,6 +31,7 @@ import (
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/pelletier/go-toml/v2"
"go.elara.ws/vercmp"
"mvdan.cc/sh/v3/expand"
@ -52,8 +53,8 @@ const (
)
type action struct {
Type actionType
File string
Type actionType
FilePath string
}
// Pull pulls the provided repositories. If a repo doesn't exist, it will be cloned
@ -178,20 +179,10 @@ func (rs *Repos) Pull(ctx context.Context, repos []types.Repo) error {
return nil
}
func (rs *Repos) processRepoChanges(ctx context.Context, repo types.Repo, r *git.Repository, w *git.Worktree, old, new *plumbing.Reference) error {
oldCommit, err := r.CommitObject(old.Hash())
if err != nil {
return err
}
newCommit, err := r.CommitObject(new.Hash())
if err != nil {
return err
}
func parseActionsFromRepoChanges(ctx context.Context, r *git.Repository, w *git.Worktree, oldCommit, newCommit *object.Commit) ([]action, error) {
patch, err := oldCommit.Patch(newCommit)
if err != nil {
return err
return nil, err
}
var actions []action
@ -204,40 +195,61 @@ func (rs *Repos) processRepoChanges(ctx context.Context, repo types.Repo, r *git
if to == nil {
actions = append(actions, action{
Type: actionDelete,
File: from.Path(),
Type: actionDelete,
FilePath: from.Path(),
})
} else if from == nil {
actions = append(actions, action{
Type: actionUpdate,
File: to.Path(),
Type: actionUpdate,
FilePath: to.Path(),
})
} else {
if from.Path() != to.Path() {
actions = append(actions,
action{
Type: actionDelete,
File: from.Path(),
Type: actionDelete,
FilePath: from.Path(),
},
action{
Type: actionUpdate,
File: to.Path(),
Type: actionUpdate,
FilePath: to.Path(),
},
)
} else {
actions = append(actions, action{
Type: actionUpdate,
File: to.Path(),
Type: actionUpdate,
FilePath: to.Path(),
})
}
}
}
return actions, nil
}
func (rs *Repos) processRepoChanges(ctx context.Context, repo types.Repo, r *git.Repository, w *git.Worktree, old, new *plumbing.Reference) error {
oldCommit, err := r.CommitObject(old.Hash())
if err != nil {
return err
}
newCommit, err := r.CommitObject(new.Hash())
if err != nil {
return err
}
actions, err := parseActionsFromRepoChanges(
ctx, r, w, oldCommit, newCommit,
)
if err != nil {
return err
}
repoDir := w.Filesystem.Root()
parser := syntax.NewParser()
for _, action := range actions {
env := append(os.Environ(), "scriptdir="+filepath.Dir(filepath.Join(repoDir, action.File)))
env := append(os.Environ(), "scriptdir="+filepath.Dir(filepath.Join(repoDir, action.FilePath)))
runner, err := interp.New(
interp.Env(expand.ListEnviron(env...)),
interp.ExecHandler(handlers.NopExec),
@ -252,11 +264,11 @@ func (rs *Repos) processRepoChanges(ctx context.Context, repo types.Repo, r *git
switch action.Type {
case actionDelete:
if filepath.Base(action.File) != "alr.sh" {
if filepath.Base(action.FilePath) != "alr.sh" {
continue
}
scriptFl, err := oldCommit.File(action.File)
scriptFl, err := oldCommit.File(action.FilePath)
if err != nil {
return nil
}
@ -277,11 +289,11 @@ func (rs *Repos) processRepoChanges(ctx context.Context, repo types.Repo, r *git
return err
}
case actionUpdate:
if filepath.Base(action.File) != "alr.sh" {
action.File = filepath.Join(filepath.Dir(action.File), "alr.sh")
if filepath.Base(action.FilePath) != "alr.sh" {
action.FilePath = filepath.Join(filepath.Dir(action.FilePath), "alr.sh")
}
scriptFl, err := newCommit.File(action.File)
scriptFl, err := newCommit.File(action.FilePath)
if err != nil {
return nil
}

168
pkg/repos/utils_test.go Normal file
View File

@ -0,0 +1,168 @@
// ALR - Any Linux Repository
// Copyright (C) 2025 Евгений Храмов
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package repos
import (
"context"
"testing"
"time"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
memory "github.com/go-git/go-git/v5/storage/memory"
"github.com/stretchr/testify/assert"
)
func TestProcessRepoChanges(t *testing.T) {
type testEnv struct {
g *git.Repository
w *git.Worktree
oldCommit *object.Commit
newCommit *object.Commit
}
type testCase struct {
name string
prepareFunc func(t *testing.T, e *testEnv)
expected []action
}
commitAll := func(t *testing.T, e *testEnv) *object.Commit {
e.w.AddGlob("*")
hash, err := e.w.Commit("test commit", &git.CommitOptions{
Author: &object.Signature{
Name: "Ivan Ivanov",
Email: "author@email.com",
When: time.Now(),
},
})
assert.NoError(t, err)
commit, err := e.g.CommitObject(hash)
assert.NoError(t, err)
return commit
}
createFile := func(t *testing.T, e *testEnv, name string, content []byte) {
f, err := e.w.Filesystem.Create(name)
assert.NoError(t, err)
defer f.Close()
f.Write(content)
}
removeFile := func(t *testing.T, e *testEnv, name string) {
err := e.w.Filesystem.Remove(name)
assert.NoError(t, err)
}
for _, tc := range []testCase{
{
name: "Add package",
prepareFunc: func(
t *testing.T,
e *testEnv,
) {
createFile(t, e, "alr-repo.toml", []byte("foo-bar-buz"))
e.oldCommit = commitAll(t, e)
createFile(t, e, "package/alr.sh", []byte("aa"))
e.newCommit = commitAll(t, e)
},
expected: []action{
{
Type: actionUpdate,
FilePath: "package/alr.sh",
},
},
},
{
name: "Update package",
prepareFunc: func(
t *testing.T,
e *testEnv,
) {
createFile(t, e, "alr-repo.toml", []byte("foo-bar-buz"))
createFile(t, e, "package/alr.sh", []byte("old content"))
e.oldCommit = commitAll(t, e)
createFile(t, e, "package/alr.sh", []byte("new content"))
e.newCommit = commitAll(t, e)
},
expected: []action{
{
Type: actionUpdate,
FilePath: "package/alr.sh",
},
},
},
{
name: "Remove package",
prepareFunc: func(
t *testing.T,
e *testEnv,
) {
createFile(t, e, "alr-repo.toml", []byte("foo-bar-buz"))
commitAll(t, e)
createFile(t, e, "package/alr.sh", []byte("test"))
e.oldCommit = commitAll(t, e)
removeFile(t, e, "package/alr.sh")
e.newCommit = commitAll(t, e)
},
expected: []action{
{
Type: actionDelete,
FilePath: "package/alr.sh",
},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
storer := memory.NewStorage()
worktree := memfs.New()
g, err := git.Init(storer, worktree)
assert.NoError(t, err)
w, err := g.Worktree()
assert.NoError(t, err)
e := &testEnv{
g,
w,
nil,
nil,
}
tc.prepareFunc(t, e)
assert.NotNil(t, e.oldCommit)
assert.NotNil(t, e.newCommit)
actions, err := parseActionsFromRepoChanges(ctx, g, w, e.oldCommit, e.newCommit)
assert.NoError(t, err)
assert.Equal(t, tc.expected, actions)
})
}
}