Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Michel Casabianca committed Apr 14, 2023
2 parents 007e8b9 + 07c07d2 commit f89d28d
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 3 deletions.
22 changes: 19 additions & 3 deletions neon/builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package builtin

import (
"fmt"
_build "github.com/c4s4/neon/neon/build"
_ "github.com/c4s4/neon/neon/task"
"github.com/c4s4/neon/neon/util"
"io/ioutil"
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"testing"
"time"

_build "github.com/c4s4/neon/neon/build"
_ "github.com/c4s4/neon/neon/task"
"github.com/c4s4/neon/neon/util"
)

const (
Expand All @@ -29,6 +31,20 @@ func Assert(actual, expected interface{}, t *testing.T) {
}
}

// Assert make an assertion for testing purpose, failing test if different:
// - actual: actual value
// - expected: regexp for expected value
// - t: test
func AssertRegexp(actual, expected string, t *testing.T) {
match, err := regexp.MatchString(expected, actual)
if err != nil {
t.Errorf("error matching regexp: %v", err)
}
if !match {
t.Errorf("actual (\"%s\") !~ expected (\"%s\")", actual, expected)
}
}

// TestIntegration runs all test build files (in test directory).
func TestIntegration(t *testing.T) {
builds, err := util.FindFiles(TestDir, []string{"**/*.yml"}, []string{}, false)
Expand Down
32 changes: 32 additions & 0 deletions neon/builtin/uuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package builtin

import (
"github.com/c4s4/neon/neon/build"
guuid "github.com/google/uuid"
)

func init() {
build.AddBuiltin(build.BuiltinDesc{
Name: "uuid",
Func: uuid,
Help: `Generate a random (version 4) UUID.
Returns:
- Generated UUID as a string.
Examples:
# generate an UUID
uuid()
# returns: "9063eafd-40b9-48d9-bf90-ce44e9207821"`,
})
}

func uuid() string {
UUID, err := guuid.NewRandom()
if err != nil {
panic("ERROR generating UUID: " + err.Error())
}
return UUID.String()
}
9 changes: 9 additions & 0 deletions neon/builtin/uuid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package builtin

import (
"testing"
)

func TestUuid(t *testing.T) {
AssertRegexp(uuid(), `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, t)
}
58 changes: 58 additions & 0 deletions neon/task/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package task

import (
"fmt"
"os"
"os/exec"
"reflect"

"github.com/c4s4/neon/neon/build"
)

func init() {
build.AddTask(build.TaskDesc{
Name: "start",
Func: start,
Args: reflect.TypeOf(startArgs{}),
Help: `Start a command in background and put its PID in a variable.
Arguments:
- start: command to run (string).
- pid: name of the variable to put PID into.
Examples:
# start a command in background and put its PID in 'pid' variable
- start: 'ls -al'
pid: pid
Notes:
- Commands run in the shell defined by shell field at the root of the build file
(or 'sh -c' on Unix and 'cmd /c' on Windows by default).`,
})
}

type startArgs struct {
Start string `neon:"name=start,string"`
Pid string `neon:"name=pid,string"`
}

func start(context *build.Context, args interface{}) error {
params := args.(startArgs)
cmd := exec.Command(params.Start)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
var err error
cmd.Env, err = context.EvaluateEnvironment()
if err != nil {
return fmt.Errorf("building environment: %v", err)
}
cmd.Environ()
if err := cmd.Start(); err != nil {
return err
}
context.SetProperty(params.Pid, cmd.Process.Pid)
return nil
}

0 comments on commit f89d28d

Please sign in to comment.