Skip to content

Commit

Permalink
feat: update parser to match the v4 format (#438)
Browse files Browse the repository at this point in the history
Co-authored-by: Elsie <[email protected]>
  • Loading branch information
saenai255 and Elsie19 authored Aug 5, 2023
1 parent 98ac6a5 commit 14908d3
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 4 deletions.
3 changes: 3 additions & 0 deletions server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ LDFLAGS= \

all: dist/webserver

test:
GO_ENV=test go test -v ./...

run:
(cd .. && docker compose up -d mariadb)

Expand Down
5 changes: 5 additions & 0 deletions server/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func (f boolFormatter) Format(str string) bool {
func getEnvVar[T any](key string, format formatter[T]) T {
val, ok := os.LookupEnv(key)
if !ok {
if os.Getenv("GO_ENV") == "test" {
fmt.Printf("Running in test mode. Using value '0' for '%s'\n", key)
return format.Format("0")
}

panic(fmt.Sprintf("could not find environment variable '%s'", key))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name="sample-valid-deb"
pkgname="sample-valid"
gives="sample-valid"
repology=("project: sample-valid")
pkgver="1.0.0"
url="https://example.com"
pkgdesc="Sample description"
hash="10101010"
arch=('amd64')
makedepends=("go" "gcc")
optdepends=("opt1" "opt2")
pacdeps=("pacdep1" "pacdep2")
depends=("dep1" "dep2")
ppa=("ppa1" "ppa2")
patch=("patch1" "patch2")
provides=("provides1" "provides2")
incompatible=("incompatible1" "incompatible2")
maintainer="pacstall <[email protected]>"
breaks=("breaks1" "breaks2")
replace=("replaces1" "replaces2")
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name="sample-valid-deb"
pkgname="sample-valid"
gives="sample-valid"
repology=("project: sample-valid")
url="https://example.com"
pkgdesc="Sample description"
hash="10101010"
arch=('amd64')
makedepends=("go" "gcc")
optdepends=("opt1" "opt2")
pacdeps=("pacdep1" "pacdep2")
depends=("dep1" "dep2")
ppa=("ppa1" "ppa2")
patch=("patch1" "patch2")
provides=("provides1" "provides2")
incompatible=("incompatible1" "incompatible2")
maintainer="pacstall <[email protected]>"
breaks=("breaks1" "breaks2")
replace=("replaces1" "replaces2")

pkgver() {
echo "1.2.3"
}
6 changes: 6 additions & 0 deletions server/types/pac/parser/pacscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ func buildCustomFormatScript(header []byte) []byte {
script = script + "echo ''\n"

for _, bashName := range pacsh.PacstallCVars {
// If the variable is a function, then we replace it with the output of the function
script += fmt.Sprintf(`
if [[ "$(declare -F -p %v)" ]]; then
%v=$(%v)
fi
`, bashName, bashName, bashName)
script += fmt.Sprintf("echo \"%s $%v\"", categoryToken, bashName) + "\n"
}

Expand Down
4 changes: 2 additions & 2 deletions server/types/pac/parser/pacsh/parse_pac_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

var ParsePacOutput = parseOutput
var PacstallCVars []string = []string{"name", "pkgname", "maintainer", "description", "url", "gives", "hash", "version"}
var PacstallCVars []string = []string{"name", "pkgname", "maintainer", "pkgdesc", "url", "gives", "hash", "pkgver"}
var PacstallCArrays []string = []string{}
var PacstallCMaps []string = []string{"depends", "breaks", "replace", "build_depends", "optdepends", "pacdeps", "patch", "ppa", "repology"}
var PacstallCMaps []string = []string{"depends", "breaks", "replace", "makedepends", "optdepends", "pacdeps", "patch", "ppa", "repology"}

const (
nameIdx = iota
Expand Down
4 changes: 2 additions & 2 deletions server/types/pac/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func parsePacscriptFiles(names []string) []*pac.Script {

log.Info("Parsing pacscripts...")
outChan := batch.Run(int(config.MaxOpenFiles), names, func(pacName string) (*pac.Script, error) {
out, err := parsePacscriptFile(config.GitClonePath, pacName)
out, err := ParsePacscriptFile(config.GitClonePath, pacName)
if err != nil {
log.Warn("Failed to parse %v. err: %v", pacName, err)
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func readPacscriptFile(rootDir, name string) (scriptBytes []byte, fileName strin
return scriptBytes, fileName, nil
}

func parsePacscriptFile(programsDirPath, name string) (pac.Script, error) {
func ParsePacscriptFile(programsDirPath, name string) (pac.Script, error) {
pacshell, filename, err := readPacscriptFile(programsDirPath, name)
if err != nil {
return pac.Script{}, err
Expand Down
141 changes: 141 additions & 0 deletions server/types/pac/parser/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package parser_test

import (
"os"
"path"
"testing"

"pacstall.dev/webserver/types/pac"
"pacstall.dev/webserver/types/pac/parser"
"pacstall.dev/webserver/types/pac/parser/pacsh"
)

var FIXTURES_DIR = func() string {
dir, err := os.Getwd()
if err != nil {
panic(err)
}

return path.Join(dir, "../../../fixtures")
}()

var TEST_PROGRAMS_DIR = path.Join(FIXTURES_DIR, "test-programs")

func assertEquals(t *testing.T, what string, expected interface{}, actual interface{}) {
if actual != expected {
t.Errorf("expected %v '%v', got '%v'", what, expected, actual)
}
}

func assertArrayEquals(t *testing.T, what string, expected []string, actual []string) {
if len(actual) != len(expected) {
t.Errorf("expected %v '%v', got '%v'", what, expected, actual)
}

for idx := range expected {
if expected[idx] != actual[idx] {
t.Errorf("expected %v '%v', got '%v'", what, expected, actual)
}
}
}

func assertPacscriptEquals(t *testing.T, expected pac.Script, actual pac.Script) {
assertEquals(t, "name", expected.Name, actual.Name)
assertEquals(t, "package name", expected.PackageName, actual.PackageName)
assertEquals(t, "maintainer", expected.Maintainer, actual.Maintainer)
assertEquals(t, "description", expected.Description, actual.Description)
assertEquals(t, "gives", expected.Gives, actual.Gives)
assertEquals(t, "hash", *expected.Hash, *actual.Hash)
assertEquals(t, "version", expected.Version, actual.Version)
assertArrayEquals(t, "breaks", expected.Breaks, actual.Breaks)
assertArrayEquals(t, "replace", expected.Replace, actual.Replace)
assertEquals(t, "pretty name", expected.PrettyName, actual.PrettyName)
assertEquals(t, "url", expected.URL, actual.URL)
assertArrayEquals(t, "runtime dependencies", expected.RuntimeDependencies, actual.RuntimeDependencies)
assertArrayEquals(t, "build dependencies", expected.BuildDependencies, actual.BuildDependencies)
assertArrayEquals(t, "optional dependencies", expected.OptionalDependencies, actual.OptionalDependencies)
assertArrayEquals(t, "pacstall dependencies", expected.PacstallDependencies, actual.PacstallDependencies)
assertArrayEquals(t, "ppa", expected.PPA, actual.PPA)
assertArrayEquals(t, "patch", expected.Patch, actual.Patch)
assertArrayEquals(t, "required by", expected.RequiredBy, actual.RequiredBy)
assertArrayEquals(t, "repology", expected.Repology, actual.Repology)
assertEquals(t, "update status", expected.UpdateStatus, actual.UpdateStatus)
}

func Test_ParsePacscriptFile_Valid(t *testing.T) {
if pacsh.CreateTempDirectory("./tmp") != nil {
t.Errorf("failed to create temp directory")
return
}

actual, err := parser.ParsePacscriptFile(TEST_PROGRAMS_DIR, "sample-valid-deb")
if err != nil {
t.Errorf("expected no error, got %v", err)
return
}

hash := "10101010"
expected := pac.Script{
Name: "sample-valid-deb",
PackageName: "sample-valid",
Maintainer: "pacstall <[email protected]>",
Description: "Sample description",
Gives: "sample-valid",
Hash: &hash,
Version: "1.0.0",
Breaks: []string{"breaks1", "breaks2"},
Replace: []string{"replaces1", "replaces2"},
PrettyName: "Sample Valid",
URL: "https://example.com",
RuntimeDependencies: []string{"dep1", "dep2"},
BuildDependencies: []string{"go", "gcc"},
OptionalDependencies: []string{"opt1", "opt2"},
PacstallDependencies: []string{"pacdep1", "pacdep2"},
PPA: []string{"ppa1", "ppa2"},
Patch: []string{"patch1", "patch2"},
RequiredBy: []string{},
Repology: []string{"project: sample-valid"},
UpdateStatus: pac.UpdateStatus.Unknown,
}

assertPacscriptEquals(t, expected, actual)
}

func Test_ParsePacscriptFile_WithPkgverFunc_Valid(t *testing.T) {
if pacsh.CreateTempDirectory("./tmp") != nil {
t.Errorf("failed to create temp directory")
return
}

actual, err := parser.ParsePacscriptFile(TEST_PROGRAMS_DIR, "sample-valid-with-pkgver-func-deb")
if err != nil {
t.Errorf("expected no error, got %v", err)
return
}

hash := "10101010"
expected := pac.Script{
Name: "sample-valid-deb",
PackageName: "sample-valid",
Maintainer: "pacstall <[email protected]>",
Description: "Sample description",
Gives: "sample-valid",
Hash: &hash,
Version: "1.2.3",
Breaks: []string{"breaks1", "breaks2"},
Replace: []string{"replaces1", "replaces2"},
PrettyName: "Sample Valid",
URL: "https://example.com",
RuntimeDependencies: []string{"dep1", "dep2"},
BuildDependencies: []string{"go", "gcc"},
OptionalDependencies: []string{"opt1", "opt2"},
PacstallDependencies: []string{"pacdep1", "pacdep2"},
PPA: []string{"ppa1", "ppa2"},
Patch: []string{"patch1", "patch2"},
RequiredBy: []string{},
Repology: []string{"project: sample-valid"},
UpdateStatus: pac.UpdateStatus.Unknown,
}

assertPacscriptEquals(t, expected, actual)
}

0 comments on commit 14908d3

Please sign in to comment.