// 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 . 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) }) } }