ALR/pkg/repos/pull_test.go

145 lines
2.9 KiB
Go
Raw Normal View History

2024-01-22 10:36:06 +00:00
/*
2024-05-05 10:32:08 +00:00
* alr - Any Linux Repository
* Copyright (C) 2024 Евгений Храмов
2024-01-22 10:36:06 +00:00
*
* 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_test
import (
"context"
"os"
"path/filepath"
"testing"
2024-05-05 10:32:08 +00:00
"plemya-x.ru/alr/internal/config"
"plemya-x.ru/alr/internal/db"
2025-01-14 08:49:42 +00:00
database "plemya-x.ru/alr/internal/db"
2024-05-05 10:32:08 +00:00
"plemya-x.ru/alr/internal/types"
"plemya-x.ru/alr/pkg/repos"
2024-01-22 10:36:06 +00:00
)
2025-01-14 08:49:42 +00:00
type TestEnv struct {
Ctx context.Context
Cfg *TestALRConfig
Db *db.Database
}
type TestALRConfig struct {
CacheDir string
RepoDir string
PkgsDir string
}
2024-01-22 10:36:06 +00:00
2025-01-14 08:49:42 +00:00
func (c *TestALRConfig) GetPaths(ctx context.Context) *config.Paths {
return &config.Paths{
DBPath: ":memory:",
CacheDir: c.CacheDir,
RepoDir: c.RepoDir,
PkgsDir: c.PkgsDir,
}
}
2024-01-22 10:36:06 +00:00
2025-01-14 08:49:42 +00:00
func (c *TestALRConfig) Repos(ctx context.Context) []types.Repo {
return []types.Repo{}
}
func prepare(t *testing.T) *TestEnv {
t.Helper()
cacheDir, err := os.MkdirTemp("/tmp", "alr-pull-test.*")
2024-01-22 10:36:06 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2025-01-14 08:49:42 +00:00
repoDir := filepath.Join(cacheDir, "repo")
err = os.MkdirAll(repoDir, 0o755)
2024-01-22 10:36:06 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2025-01-14 08:49:42 +00:00
pkgsDir := filepath.Join(cacheDir, "pkgs")
err = os.MkdirAll(pkgsDir, 0o755)
2024-01-22 10:36:06 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2025-01-14 08:49:42 +00:00
cfg := &TestALRConfig{
CacheDir: cacheDir,
RepoDir: repoDir,
PkgsDir: pkgsDir,
}
ctx := context.Background()
db := database.New(cfg)
db.Init(ctx)
return &TestEnv{
Cfg: cfg,
Db: db,
Ctx: ctx,
}
2024-01-22 10:36:06 +00:00
}
2025-01-14 08:49:42 +00:00
func cleanup(t *testing.T, e *TestEnv) {
2024-01-22 10:36:06 +00:00
t.Helper()
2025-01-14 08:49:42 +00:00
err := os.RemoveAll(e.Cfg.CacheDir)
2024-01-22 10:36:06 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2025-01-14 08:49:42 +00:00
e.Db.Close()
2024-01-22 10:36:06 +00:00
}
func TestPull(t *testing.T) {
2025-01-14 08:49:42 +00:00
e := prepare(t)
defer cleanup(t, e)
2024-01-22 10:36:06 +00:00
2025-01-14 08:49:42 +00:00
rs := repos.New(
e.Cfg,
e.Db,
)
2024-01-22 10:36:06 +00:00
2025-01-14 08:49:42 +00:00
err := rs.Pull(e.Ctx, []types.Repo{
2024-01-22 10:36:06 +00:00
{
Name: "default",
2025-01-14 08:49:42 +00:00
URL: "https://gitea.plemya-x.ru/Plemya-x/xpamych-alr-repo.git",
2024-01-22 10:36:06 +00:00
},
})
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2025-01-14 08:49:42 +00:00
result, err := e.Db.GetPkgs(e.Ctx, "true")
2024-01-22 10:36:06 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
var pkgAmt int
for result.Next() {
var dbPkg db.Package
err = result.StructScan(&dbPkg)
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
pkgAmt++
}
2025-01-14 08:49:42 +00:00
if pkgAmt == 0 {
t.Errorf("Expected at least 1 matching package, but got %d", pkgAmt)
2024-01-22 10:36:06 +00:00
}
}