Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sciabarracom committed Jan 4, 2025
1 parent 531bafb commit b1f8a47
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 48 deletions.
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func RemoveAll(dir string) error {
}

func TestMain(m *testing.M) {
//debugging = true
//tracing = true
debugging = true
tracing = true
wd, _ := os.Getwd()
workDir, _ = filepath.Abs(wd)
homeDir, _ = homedir.Dir()
Expand Down
2 changes: 1 addition & 1 deletion ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ func Ops(base string, args []string) error {
// go down using args as subcommands
err := os.Chdir(base)
debug("Ops chdir", base)

if err != nil {
return err
}
Expand All @@ -218,6 +217,7 @@ func Ops(base string, args []string) error {
isSubCmd := false

err = ensurePrereq(base)
debug("Ops ensurePrereq", err)
if err != nil {
fmt.Println("ERROR: cannot ensure prerequisites: " + err.Error())
os.Exit(1)
Expand Down
29 changes: 29 additions & 0 deletions ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,35 @@ import (
"golang.org/x/exp/slices"
)

func Example_opsArg1() {
// test
_ = os.Chdir(workDir)
olaris, _ := filepath.Abs(joinpath("tests", "olaris"))
err := Ops(olaris, split("testcmd"))
fmt.Println(err)
// Output:
// -

/*
pr(2, err)
err = Ops(olaris, split("testcmd arg"))
pr(3, err)
err = Ops(olaris, split("testcmd arg VAR=1"))
pr(4, err)
err = Ops(olaris, split("testcmd VAR=1 arg"))
pr(5, err)
// Output:
// (olaris) task [-t opsfile.yml testcmd --]
// 2 <nil>
// (olaris) task [-t opsfile.yml testcmd -- arg]
// 3 <nil>
// (olaris) task [-t opsfile.yml testcmd VAR=1 -- arg]
// 4 <nil>
// (olaris) task [-t opsfile.yml testcmd VAR=1 -- arg]
//5 <nil>
*/
}

func Example_opsArg() {
// test
_ = os.Chdir(workDir)
Expand Down
89 changes: 59 additions & 30 deletions prereq.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,6 @@ import (

var PrereqSeenMap = map[string]string{}

// Define the Go structs
type Prereq struct {
Version int `yaml:"version"`
Tasks map[string]PrereqTask `yaml:"tasks"`
}

type PrereqTask struct {
Description *string `yaml:"description,omitempty"` // Make description optional
Vars map[string]string `yaml:"vars,omitempty"`
}

// execute prereq task
func execPrereqTask(bindir string, name string) error {
me, err := os.Executable()
Expand All @@ -67,25 +56,65 @@ func execPrereqTask(bindir string, name string) error {
}

// load prerequisites in current dir
func loadPrereq(dir string) (Prereq, error) {
var prereq Prereq = Prereq{}
func loadPrereq(dir string) (tasks []string, versions []string, err error) {
err = nil
tasks = []string{}
versions = []string{}

if !exists(dir, PREREQ) {
return prereq, fmt.Errorf("not found %s", dir)
return
}
trace("found prereq.yml in ", dir)

dat, err := os.ReadFile(joinpath(dir, PREREQ))
data, err := os.ReadFile(joinpath(dir, PREREQ))
if err != nil {
return prereq, err
return
}

err = yaml.Unmarshal(dat, &prereq)
if err != nil {
return prereq, err
/*
You have tasks = []strings and versions = []strings
and dat a string with a YAML in format:
```yaml
something:
tasks:
task:
vars:
VERSION: "123"
anothertask:
```
I want to parse the file, find the entries under `tasks` vith a var with VERSION
and append, in order, to tasks the name of the task
and to version the version found
I have to skip the entries without a version and everyhing not in tasks
I wnato just the plain code no procedures
*/
var root yaml.Node
if err = yaml.Unmarshal([]byte(data), &root); err != nil {
return
}

return prereq, err
for i := 0; i < len(root.Content[0].Content); i += 2 {
if root.Content[0].Content[i].Value == "tasks" {
tasksNode := root.Content[0].Content[i+1]
for j := 0; j < len(tasksNode.Content); j += 2 {
taskName := tasksNode.Content[j].Value
taskVars := tasksNode.Content[j+1]
for k := 0; k < len(taskVars.Content); k += 2 {
if taskVars.Content[k].Value == "vars" {
varsNode := taskVars.Content[k+1]
for l := 0; l < len(varsNode.Content); l += 2 {
if varsNode.Content[l].Value == "VERSION" {
version := varsNode.Content[l+1].Value
tasks = append(tasks, taskName)
versions = append(versions, version)
}
}
}
}
}
}
}
return
}

func binDir() (string, error) {
Expand Down Expand Up @@ -158,16 +187,11 @@ func touchAndClean(dir string, name string, version string) error {
}

// download a prerequisite
func downloadPrereq(name string, task PrereqTask) error {
func downloadPrereq(name string, version string) error {

// names and version
// xname = executeable name
// vname = versioned executable name
version, ok := task.Vars["VERSION"]
if !ok {
trace("return because no version for ", name)
return nil
}
xname := addExeExt(name)
vname := xname + "-" + version

Expand Down Expand Up @@ -233,10 +257,15 @@ func ensurePrereq(root string) error {
return err
}
trace("ensurePrereq in", root)
prereq, err := loadPrereq(root)
for task := range prereq.Tasks {
trace("prereq", task)
err = downloadPrereq(task, prereq.Tasks[task])
tasks, versions, err := loadPrereq(root)
if err != nil {
return err
}
trace(tasks, versions, err)
for i, task := range tasks {
version := versions[i]
trace("prereq", task, version)
err = downloadPrereq(task, version)
if err != nil {
fmt.Printf("error in prereq %s: %v\n", task, err)
}
Expand Down
29 changes: 16 additions & 13 deletions prereq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ func Example_execPrereqTask() {
func Example_loadPrereq() {
//downloadPrereq("")
dir := joinpath(workDir, "tests")
_, err := loadPrereq(dir)
fmt.Println(npath(err.Error()))
prq, err := loadPrereq(joinpath(dir, "prereq"))
t, v, err := loadPrereq(dir)
fmt.Println(err, len(t), len(v))
dir = joinpath(dir, "prereq")
//dir = "/home/msciab/.ops/0.1.0/olaris/"
tasks, versions, err := loadPrereq(dir)
//fmt.Println(prq)
fmt.Println(err, *prq.Tasks["bun"].Description, prq.Tasks["bun"].Vars["VERSION"])
fmt.Println(err, tasks)
fmt.Println(versions)
// Output:
// not found /work/tests
// <nil> bun v1.11.20
// <nil> 0 0
// <nil> [bun coreutils]
// [v1.11.20 0.0.27]
}

func Example_ensureBindir() {
Expand Down Expand Up @@ -74,7 +78,6 @@ func Example_touchAndClean() {
if err != nil {
RemoveAll(bindir)
}

bindir, _ = EnsureBindir()
touch(bindir, "hello")
err = touchAndClean(bindir, "hello", "1.2.3")
Expand All @@ -94,14 +97,14 @@ func Example_downloadPrereq() {
PrereqSeenMap = map[string]string{}

prqdir := joinpath(joinpath(workDir, "tests"), "prereq")
prq, _ := loadPrereq(prqdir)
fmt.Println("1", downloadPrereq("bun", prq.Tasks["bun"]))
fmt.Println("2", downloadPrereq("bun", prq.Tasks["bun"]))
tasks, versions, _ := loadPrereq(prqdir)
fmt.Println("1", downloadPrereq(tasks[0], versions[0]))
fmt.Println("2", downloadPrereq(tasks[0], versions[0]))

prq, _ = loadPrereq(joinpath(prqdir, "sub"))
//fmt.Println(prq)
tasks, versions, _ = loadPrereq(joinpath(prqdir, "sub"))
//fmt.Println(tasks, versions)
//fmt.Println(PrereqSeenMap)
fmt.Println("3", downloadPrereq("bun", prq.Tasks["bun"]))
fmt.Println("3", downloadPrereq("bun", versions[0]))
// Output:
// downloading bun v1.11.20
// 1 <nil>
Expand Down
4 changes: 2 additions & 2 deletions tests/prereq_yml.bats
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ setup() {
export COUNT=1
}

@test "OS=linux ARCH=amd64" {
@test "OS=linux ARCH=amd64" {
OS=linux ARCH=amd64
mkdir -p _bin/$OS-$ARCH
run env __OS=$OS __ARCH=$ARCH task -t prereq/olaris/prereq.yml -d _bin/$OS-$ARCH all
find _bin/$OS-$ARCH -type f | xargs file | grep ELF | grep x86-64 | wc -l | xargs | tee _count
assert_equal "$(cat _count)" $COUNT
}

@test "OS=linux ARCH=arm64" {
@test "OS=linux ARCH=arm64" {
OS=linux ARCH=arm64
mkdir -p _bin/$OS-$ARCH
run env __OS=$OS __ARCH=$ARCH task -t prereq/olaris/prereq.yml -d _bin/$OS-$ARCH all
Expand Down

0 comments on commit b1f8a47

Please sign in to comment.