lure/pkg/repos/pull_test.go

110 lines
2.4 KiB
Go
Raw Normal View History

/*
* LURE - Linux User REpository
2023-09-20 22:38:22 +00:00
* Copyright (C) 2023 Elara Musayelyan
*
* 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/>.
*/
2022-12-27 19:23:34 +00:00
package repos_test
import (
"context"
"os"
"path/filepath"
"testing"
2023-10-08 00:34:39 +00:00
"lure.sh/lure/internal/config"
"lure.sh/lure/internal/db"
"lure.sh/lure/internal/types"
"lure.sh/lure/pkg/repos"
2022-12-27 19:23:34 +00:00
)
func setCfgDirs(t *testing.T) {
t.Helper()
2023-09-19 21:28:05 +00:00
paths := config.GetPaths()
2022-12-27 19:23:34 +00:00
var err error
2023-09-19 21:28:05 +00:00
paths.CacheDir, err = os.MkdirTemp("/tmp", "lure-pull-test.*")
2022-12-27 19:23:34 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
paths.RepoDir = filepath.Join(paths.CacheDir, "repo")
paths.PkgsDir = filepath.Join(paths.CacheDir, "pkgs")
2022-12-27 19:23:34 +00:00
2023-09-19 21:28:05 +00:00
err = os.MkdirAll(paths.RepoDir, 0o755)
2022-12-27 19:23:34 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
err = os.MkdirAll(paths.PkgsDir, 0o755)
2022-12-27 19:23:34 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
paths.DBPath = filepath.Join(paths.CacheDir, "db")
2022-12-27 19:23:34 +00:00
}
func removeCacheDir(t *testing.T) {
t.Helper()
2023-09-19 21:28:05 +00:00
err := os.RemoveAll(config.GetPaths().CacheDir)
2022-12-27 19:23:34 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
}
func TestPull(t *testing.T) {
2023-09-19 21:28:05 +00:00
_, err := db.Open(":memory:")
2022-12-27 19:23:34 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
defer db.Close()
2022-12-27 19:23:34 +00:00
setCfgDirs(t)
defer removeCacheDir(t)
ctx := context.Background()
2023-09-19 21:28:05 +00:00
err = repos.Pull(ctx, []types.Repo{
2022-12-27 19:23:34 +00:00
{
Name: "default",
URL: "https://github.com/Arsen6331/lure-repo.git",
},
})
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
result, err := db.GetPkgs("name LIKE 'itd%'")
2022-12-27 19:23:34 +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++
}
if pkgAmt < 2 {
t.Errorf("Expected 2 packages to match, got %d", pkgAmt)
}
}