Compare commits
4 Commits
c832359e43
...
3e6d5f57cf
Author | SHA1 | Date | |
---|---|---|---|
3e6d5f57cf | |||
db5c344b2d | |||
a750f4630e | |||
852e98088b |
@ -7,7 +7,6 @@ import (
|
||||
"go.arsenm.dev/logger/log"
|
||||
"go.arsenm.dev/lure/internal/config"
|
||||
"go.arsenm.dev/lure/internal/db"
|
||||
"modernc.org/sqlite"
|
||||
)
|
||||
|
||||
var gdb *sqlx.DB
|
||||
@ -20,8 +19,6 @@ func init() {
|
||||
log.Fatal("Your package cache database is using the old database engine. Please remove ~/.cache/lure and then run `lure ref`.").Send()
|
||||
}
|
||||
}
|
||||
|
||||
sqlite.MustRegisterScalarFunction("json_array_contains", 2, db.JsonArrayContains)
|
||||
|
||||
gdb, err = sqlx.Open("sqlite", config.DBPath)
|
||||
if err != nil {
|
||||
|
3
db.go
3
db.go
@ -7,7 +7,6 @@ import (
|
||||
"go.arsenm.dev/logger/log"
|
||||
"go.arsenm.dev/lure/internal/config"
|
||||
"go.arsenm.dev/lure/internal/db"
|
||||
"modernc.org/sqlite"
|
||||
)
|
||||
|
||||
var gdb *sqlx.DB
|
||||
@ -21,8 +20,6 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
sqlite.MustRegisterScalarFunction("json_array_contains", 2, db.JsonArrayContains)
|
||||
|
||||
gdb, err = sqlx.Open("sqlite", config.DBPath)
|
||||
if err != nil {
|
||||
log.Fatal("Error opening database").Err(err).Send()
|
||||
|
@ -12,6 +12,10 @@ import (
|
||||
"modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func init() {
|
||||
sqlite.MustRegisterScalarFunction("json_array_contains", 2, JsonArrayContains)
|
||||
}
|
||||
|
||||
// Package is a LURE package's database representation
|
||||
type Package struct {
|
||||
Name string `sh:"name,required" db:"name"`
|
||||
@ -109,7 +113,7 @@ func GetPkgs(db *sqlx.DB, where string, args ...any) (*sqlx.Rows, error) {
|
||||
// GetPkg returns a single package that match the where conditions
|
||||
func GetPkg(db *sqlx.DB, where string, args ...any) (*Package, error) {
|
||||
out := &Package{}
|
||||
err := db.Get(out, "SELECT * FROM pkgs WHERE "+where+"LIMIT 1", args...)
|
||||
err := db.Get(out, "SELECT * FROM pkgs WHERE "+where+" LIMIT 1", args...)
|
||||
return out, err
|
||||
}
|
||||
|
||||
|
234
internal/db/db_test.go
Normal file
234
internal/db/db_test.go
Normal file
@ -0,0 +1,234 @@
|
||||
package db_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.arsenm.dev/lure/internal/db"
|
||||
)
|
||||
|
||||
var testPkg = db.Package{
|
||||
Name: "test",
|
||||
Version: "0.0.1",
|
||||
Release: 1,
|
||||
Epoch: 2,
|
||||
Description: "Test package",
|
||||
Homepage: "https://lure.arsenm.dev",
|
||||
Maintainer: "Arsen Musayelyan <arsen@arsenm.dev>",
|
||||
Architectures: db.NewJSON([]string{"arm64", "amd64"}),
|
||||
Licenses: db.NewJSON([]string{"GPL-3.0-or-later"}),
|
||||
Provides: db.NewJSON([]string{"test"}),
|
||||
Conflicts: db.NewJSON([]string{"test"}),
|
||||
Replaces: db.NewJSON([]string{"test-old"}),
|
||||
Depends: db.NewJSON(map[string][]string{
|
||||
"": {"sudo"},
|
||||
}),
|
||||
BuildDepends: db.NewJSON(map[string][]string{
|
||||
"": {"golang"},
|
||||
"arch": {"go"},
|
||||
}),
|
||||
Repository: "default",
|
||||
}
|
||||
|
||||
func getDB(t *testing.T) (*sqlx.DB, error) {
|
||||
t.Helper()
|
||||
|
||||
gdb, err := sqlx.Open("sqlite", ":memory:")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = db.Init(gdb)
|
||||
return gdb, err
|
||||
}
|
||||
|
||||
func TestInit(t *testing.T) {
|
||||
gdb, err := sqlx.Open("sqlite", ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
defer gdb.Close()
|
||||
|
||||
err = db.Init(gdb)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
_, err = gdb.Exec("SELECT * FROM pkgs")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertPackage(t *testing.T) {
|
||||
gdb, err := getDB(t)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
defer gdb.Close()
|
||||
|
||||
err = db.InsertPackage(gdb, testPkg)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
dbPkg := db.Package{}
|
||||
err = sqlx.Get(gdb, &dbPkg, "SELECT * FROM pkgs WHERE name = 'test' AND repository = 'default'")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(testPkg, dbPkg) {
|
||||
t.Errorf("Expected test package to be the same as database package")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPkgs(t *testing.T) {
|
||||
gdb, err := getDB(t)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
defer gdb.Close()
|
||||
|
||||
x1 := testPkg
|
||||
x1.Name = "x1"
|
||||
x2 := testPkg
|
||||
x2.Name = "x2"
|
||||
|
||||
err = db.InsertPackage(gdb, x1)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
err = db.InsertPackage(gdb, x2)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
result, err := db.GetPkgs(gdb, "name LIKE 'x%'")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
for result.Next() {
|
||||
var dbPkg db.Package
|
||||
err = result.StructScan(&dbPkg)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(dbPkg.Name, "x") {
|
||||
t.Errorf("Expected package name to start with 'x', got %s", dbPkg.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPkg(t *testing.T) {
|
||||
gdb, err := getDB(t)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
defer gdb.Close()
|
||||
|
||||
x1 := testPkg
|
||||
x1.Name = "x1"
|
||||
x2 := testPkg
|
||||
x2.Name = "x2"
|
||||
|
||||
err = db.InsertPackage(gdb, x1)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
err = db.InsertPackage(gdb, x2)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
pkg, err := db.GetPkg(gdb, "name LIKE 'x%' ORDER BY name")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
if pkg.Name != "x1" {
|
||||
t.Errorf("Expected x1 package, got %s", pkg.Name)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(*pkg, x1) {
|
||||
t.Errorf("Expected x1 to be %v, got %v", x1, *pkg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeletePkgs(t *testing.T) {
|
||||
gdb, err := getDB(t)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
defer gdb.Close()
|
||||
|
||||
x1 := testPkg
|
||||
x1.Name = "x1"
|
||||
x2 := testPkg
|
||||
x2.Name = "x2"
|
||||
|
||||
err = db.InsertPackage(gdb, x1)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
err = db.InsertPackage(gdb, x2)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
err = db.DeletePkgs(gdb, "name = 'x1'")
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
var dbPkg db.Package
|
||||
err = gdb.Get(&dbPkg, "SELECT * FROM pkgs WHERE name LIKE 'x%' ORDER BY name LIMIT 1;")
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
if dbPkg.Name != "x2" {
|
||||
t.Errorf("Expected x2 package, got %s", dbPkg.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJsonArrayContains(t *testing.T) {
|
||||
gdb, err := getDB(t)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
defer gdb.Close()
|
||||
|
||||
x1 := testPkg
|
||||
x1.Name = "x1"
|
||||
x2 := testPkg
|
||||
x2.Name = "x2"
|
||||
x2.Provides.Val = append(x2.Provides.Val, "x")
|
||||
|
||||
err = db.InsertPackage(gdb, x1)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
err = db.InsertPackage(gdb, x2)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
var dbPkg db.Package
|
||||
err = gdb.Get(&dbPkg, "SELECT * FROM pkgs WHERE json_array_contains(provides, 'x');")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
if dbPkg.Name != "x2" {
|
||||
t.Errorf("Expected x2 package, got %s", dbPkg.Name)
|
||||
}
|
||||
}
|
69
internal/repos/find_test.go
Normal file
69
internal/repos/find_test.go
Normal file
@ -0,0 +1,69 @@
|
||||
package repos_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.arsenm.dev/lure/internal/db"
|
||||
"go.arsenm.dev/lure/internal/repos"
|
||||
"go.arsenm.dev/lure/internal/types"
|
||||
)
|
||||
|
||||
func TestFindPkgs(t *testing.T) {
|
||||
gdb, err := sqlx.Open("sqlite", ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
defer gdb.Close()
|
||||
|
||||
err = db.Init(gdb)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
setCfgDirs(t)
|
||||
defer removeCacheDir(t)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
err = repos.Pull(ctx, gdb, []types.Repo{
|
||||
{
|
||||
Name: "default",
|
||||
URL: "https://github.com/Arsen6331/lure-repo.git",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
found, notFound, err := repos.FindPkgs(gdb, []string{"itd", "nonexistentpackage1", "nonexistentpackage2"})
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(notFound, []string{"nonexistentpackage1", "nonexistentpackage2"}) {
|
||||
t.Errorf("Expected 'nonexistentpackage{1,2} not to be found")
|
||||
}
|
||||
|
||||
if len(found) != 1 {
|
||||
t.Errorf("Expected 1 package found, got %d", len(found))
|
||||
}
|
||||
|
||||
itdPkgs, ok := found["itd"]
|
||||
if !ok {
|
||||
t.Fatalf("Expected 'itd' packages to be found")
|
||||
}
|
||||
|
||||
if len(itdPkgs) < 2 {
|
||||
t.Errorf("Expected two 'itd' packages to be found")
|
||||
}
|
||||
|
||||
for i, pkg := range itdPkgs {
|
||||
if !strings.HasPrefix(pkg.Name, "itd") {
|
||||
t.Errorf("Expected package name of all found packages to start with 'itd', got %s on element %d", pkg.Name, i)
|
||||
}
|
||||
}
|
||||
}
|
95
internal/repos/pull_test.go
Normal file
95
internal/repos/pull_test.go
Normal file
@ -0,0 +1,95 @@
|
||||
package repos_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.arsenm.dev/lure/internal/config"
|
||||
"go.arsenm.dev/lure/internal/db"
|
||||
"go.arsenm.dev/lure/internal/repos"
|
||||
"go.arsenm.dev/lure/internal/types"
|
||||
)
|
||||
|
||||
func setCfgDirs(t *testing.T) {
|
||||
t.Helper()
|
||||
|
||||
var err error
|
||||
config.CacheDir, err = os.MkdirTemp("/tmp", "lure-pull-test.*")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
config.RepoDir = filepath.Join(config.CacheDir, "repo")
|
||||
config.PkgsDir = filepath.Join(config.CacheDir, "pkgs")
|
||||
|
||||
err = os.MkdirAll(config.RepoDir, 0o755)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
err = os.MkdirAll(config.PkgsDir, 0o755)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
config.DBPath = filepath.Join(config.CacheDir, "db")
|
||||
}
|
||||
|
||||
func removeCacheDir(t *testing.T) {
|
||||
t.Helper()
|
||||
|
||||
err := os.RemoveAll(config.CacheDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPull(t *testing.T) {
|
||||
gdb, err := sqlx.Open("sqlite", ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
defer gdb.Close()
|
||||
|
||||
err = db.Init(gdb)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
setCfgDirs(t)
|
||||
defer removeCacheDir(t)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
err = repos.Pull(ctx, gdb, []types.Repo{
|
||||
{
|
||||
Name: "default",
|
||||
URL: "https://github.com/Arsen6331/lure-repo.git",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
result, err := db.GetPkgs(gdb, "name LIKE 'itd%'")
|
||||
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)
|
||||
}
|
||||
}
|
206
internal/shutils/decoder/decoder_test.go
Normal file
206
internal/shutils/decoder/decoder_test.go
Normal file
@ -0,0 +1,206 @@
|
||||
package decoder_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.arsenm.dev/lure/distro"
|
||||
"go.arsenm.dev/lure/internal/shutils/decoder"
|
||||
"mvdan.cc/sh/v3/interp"
|
||||
"mvdan.cc/sh/v3/syntax"
|
||||
)
|
||||
|
||||
type BuildVars struct {
|
||||
Name string `sh:"name,required"`
|
||||
Version string `sh:"version,required"`
|
||||
Release int `sh:"release,required"`
|
||||
Epoch uint `sh:"epoch"`
|
||||
Description string `sh:"desc"`
|
||||
Homepage string `sh:"homepage"`
|
||||
Maintainer string `sh:"maintainer"`
|
||||
Architectures []string `sh:"architectures"`
|
||||
Licenses []string `sh:"license"`
|
||||
Provides []string `sh:"provides"`
|
||||
Conflicts []string `sh:"conflicts"`
|
||||
Depends []string `sh:"deps"`
|
||||
BuildDepends []string `sh:"build_deps"`
|
||||
Replaces []string `sh:"replaces"`
|
||||
}
|
||||
|
||||
const testScript = `
|
||||
name='test'
|
||||
version='0.0.1'
|
||||
release=1
|
||||
epoch=2
|
||||
desc="Test package"
|
||||
homepage='https://lure.arsenm.dev'
|
||||
maintainer='Arsen Musayelyan <arsen@arsenm.dev>'
|
||||
architectures=('arm64' 'amd64')
|
||||
license=('GPL-3.0-or-later')
|
||||
provides=('test')
|
||||
conflicts=('test')
|
||||
replaces=('test-old')
|
||||
replaces_test_os=('test-legacy')
|
||||
|
||||
deps=('sudo')
|
||||
|
||||
build_deps=('golang')
|
||||
build_deps_arch=('go')
|
||||
|
||||
test() {
|
||||
echo "Test"
|
||||
}
|
||||
|
||||
package() {
|
||||
install-binary test
|
||||
}
|
||||
`
|
||||
|
||||
var osRelease = &distro.OSRelease{
|
||||
ID: "test_os",
|
||||
Like: []string{"arch"},
|
||||
}
|
||||
|
||||
func TestDecodeVars(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
fl, err := syntax.NewParser().Parse(strings.NewReader(testScript), "lure.sh")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
runner, err := interp.New()
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
err = runner.Run(ctx, fl)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
dec := decoder.New(osRelease, runner)
|
||||
|
||||
var bv BuildVars
|
||||
err = dec.DecodeVars(&bv)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
expected := BuildVars{
|
||||
Name: "test",
|
||||
Version: "0.0.1",
|
||||
Release: 1,
|
||||
Epoch: 2,
|
||||
Description: "Test package",
|
||||
Homepage: "https://lure.arsenm.dev",
|
||||
Maintainer: "Arsen Musayelyan <arsen@arsenm.dev>",
|
||||
Architectures: []string{"arm64", "amd64"},
|
||||
Licenses: []string{"GPL-3.0-or-later"},
|
||||
Provides: []string{"test"},
|
||||
Conflicts: []string{"test"},
|
||||
Replaces: []string{"test-legacy"},
|
||||
Depends: []string{"sudo"},
|
||||
BuildDepends: []string{"go"},
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(bv, expected) {
|
||||
t.Errorf("Expected %v, got %v", expected, bv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeVarsMissing(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
const testScript = `
|
||||
name='test'
|
||||
epoch=2
|
||||
desc="Test package"
|
||||
homepage='https://lure.arsenm.dev'
|
||||
maintainer='Arsen Musayelyan <arsen@arsenm.dev>'
|
||||
architectures=('arm64' 'amd64')
|
||||
license=('GPL-3.0-or-later')
|
||||
provides=('test')
|
||||
conflicts=('test')
|
||||
replaces=('test-old')
|
||||
replaces_test_os=('test-legacy')
|
||||
|
||||
deps=('sudo')
|
||||
|
||||
build_deps=('golang')
|
||||
build_deps_arch=('go')
|
||||
|
||||
test() {
|
||||
echo "Test"
|
||||
}
|
||||
|
||||
package() {
|
||||
install-binary test
|
||||
}
|
||||
`
|
||||
|
||||
fl, err := syntax.NewParser().Parse(strings.NewReader(testScript), "lure.sh")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
runner, err := interp.New()
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
err = runner.Run(ctx, fl)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
dec := decoder.New(osRelease, runner)
|
||||
|
||||
var bv BuildVars
|
||||
err = dec.DecodeVars(&bv)
|
||||
|
||||
var notFoundErr decoder.VarNotFoundError
|
||||
if !errors.As(err, ¬FoundErr) {
|
||||
t.Fatalf("Expected VarNotFoundError, got %T %v", err, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetFunc(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
fl, err := syntax.NewParser().Parse(strings.NewReader(testScript), "lure.sh")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
runner, err := interp.New()
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
err = runner.Run(ctx, fl)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
dec := decoder.New(osRelease, runner)
|
||||
fn, ok := dec.GetFunc("test")
|
||||
if !ok {
|
||||
t.Fatalf("Expected test() function to exist")
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
err = fn(ctx, interp.StdIO(os.Stdin, buf, buf))
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
if buf.String() != "Test\n" {
|
||||
t.Fatalf(`Expected "Test\n", got %#v`, buf.String())
|
||||
}
|
||||
}
|
106
internal/shutils/exec_test.go
Normal file
106
internal/shutils/exec_test.go
Normal file
@ -0,0 +1,106 @@
|
||||
package shutils_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.arsenm.dev/lure/distro"
|
||||
"go.arsenm.dev/lure/internal/shutils"
|
||||
"go.arsenm.dev/lure/internal/shutils/decoder"
|
||||
"mvdan.cc/sh/v3/interp"
|
||||
"mvdan.cc/sh/v3/syntax"
|
||||
)
|
||||
|
||||
const testScript = `
|
||||
name='test'
|
||||
version='0.0.1'
|
||||
release=1
|
||||
epoch=2
|
||||
desc="Test package"
|
||||
homepage='https://lure.arsenm.dev'
|
||||
maintainer='Arsen Musayelyan <arsen@arsenm.dev>'
|
||||
architectures=('arm64' 'amd64')
|
||||
license=('GPL-3.0-or-later')
|
||||
provides=('test')
|
||||
conflicts=('test')
|
||||
replaces=('test-old')
|
||||
replaces_test_os=('test-legacy')
|
||||
|
||||
deps=('sudo')
|
||||
|
||||
build_deps=('golang')
|
||||
build_deps_arch=('go')
|
||||
|
||||
test() {
|
||||
test-cmd "Hello, World"
|
||||
test-fb
|
||||
}
|
||||
|
||||
package() {
|
||||
install-binary test
|
||||
}
|
||||
`
|
||||
|
||||
var osRelease = &distro.OSRelease{
|
||||
ID: "test_os",
|
||||
Like: []string{"arch"},
|
||||
}
|
||||
|
||||
func TestExecFuncs(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
fl, err := syntax.NewParser().Parse(strings.NewReader(testScript), "lure.sh")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
runner, err := interp.New()
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
err = runner.Run(ctx, fl)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
dec := decoder.New(osRelease, runner)
|
||||
fn, ok := dec.GetFunc("test")
|
||||
if !ok {
|
||||
t.Fatalf("Expected test() function to exist")
|
||||
}
|
||||
|
||||
eh := shutils.ExecFuncs{
|
||||
"test-cmd": func(hc interp.HandlerContext, name string, args []string) error {
|
||||
if name != "test-cmd" {
|
||||
t.Errorf("Expected name to be 'test-cmd', got '%s'", name)
|
||||
}
|
||||
|
||||
if len(args) < 1 {
|
||||
t.Fatalf("Expected at least one argument, got %d", len(args))
|
||||
}
|
||||
|
||||
if args[0] != "Hello, World" {
|
||||
t.Errorf("Expected first argument to be 'Hello, World', got '%s'", args[0])
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
fbInvoked := false
|
||||
fbHandler := func(context.Context, []string) error {
|
||||
fbInvoked = true
|
||||
return nil
|
||||
}
|
||||
|
||||
err = fn(ctx, interp.ExecHandler(eh.ExecHandler(fbHandler)))
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
if !fbInvoked {
|
||||
t.Errorf("Expected fallback handler to be invoked")
|
||||
}
|
||||
}
|
40
internal/shutils/nop_test.go
Normal file
40
internal/shutils/nop_test.go
Normal file
@ -0,0 +1,40 @@
|
||||
package shutils_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.arsenm.dev/lure/internal/shutils"
|
||||
"mvdan.cc/sh/v3/interp"
|
||||
"mvdan.cc/sh/v3/syntax"
|
||||
)
|
||||
|
||||
func TestNopExec(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
fl, err := syntax.NewParser().Parse(strings.NewReader(`/bin/echo test`), "lure.sh")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
runner, err := interp.New(
|
||||
interp.ExecHandler(shutils.NopExec),
|
||||
interp.StdIO(os.Stdin, buf, buf),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
err = runner.Run(ctx, fl)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
if buf.String() != "" {
|
||||
t.Fatalf("Expected empty string, got %#v", buf.String())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user