89 lines
2.6 KiB
Go
89 lines
2.6 KiB
Go
package builtins_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"ucl.lmika.dev/ucl"
|
|
"ucl.lmika.dev/ucl/builtins"
|
|
)
|
|
|
|
func TestOS_Env(t *testing.T) {
|
|
tests := []struct {
|
|
descr string
|
|
eval string
|
|
want any
|
|
}{
|
|
{descr: "env value", eval: `os:env "MY_ENV"`, want: "my env value"},
|
|
{descr: "missing env value", eval: `os:env "MISSING_THING"`, want: ""},
|
|
{descr: "default env value (str)", eval: `os:env "MISSING_THING" "my default"`, want: "my default"},
|
|
{descr: "default env value (int)", eval: `os:env "MISSING_THING" 1352`, want: 1352},
|
|
{descr: "default env value (nil)", eval: `os:env "MISSING_THING" ()`, want: nil},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.descr, func(t *testing.T) {
|
|
t.Setenv("MY_ENV", "my env value")
|
|
|
|
inst := ucl.New(
|
|
ucl.WithModule(builtins.OS()),
|
|
)
|
|
res, err := inst.EvalString(context.Background(), tt.eval)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tt.want, res)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOS_Exec(t *testing.T) {
|
|
tests := []struct {
|
|
descr string
|
|
eval string
|
|
want any
|
|
}{
|
|
{descr: "run command 1", eval: `os:exec "echo" "hello, world"`, want: "hello, world\n"},
|
|
{descr: "run command 2", eval: `os:exec "date" "+%Y%m%d"`, want: time.Now().Format("20060102") + "\n"},
|
|
{descr: "run command 3", eval: `os:exec "tr" "[a-z]" "[A-Z]" -in "hello"`, want: "HELLO"},
|
|
{descr: "run command 4", eval: `os:exec "tr" "-d" "e" -in "hello"`, want: "hllo"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.descr, func(t *testing.T) {
|
|
inst := ucl.New(
|
|
ucl.WithModule(builtins.OS()),
|
|
)
|
|
res, err := inst.EvalString(context.Background(), tt.eval)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tt.want, res)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOS_Bang(t *testing.T) {
|
|
tests := []struct {
|
|
descr string
|
|
eval string
|
|
want any
|
|
}{
|
|
{descr: "run command 1", eval: `os:! "echo 'hello, world'"`, want: "hello, world\n"},
|
|
{descr: "run command 2", eval: `os:! "date +%Y%m%d"`, want: time.Now().Format("20060102") + "\n"},
|
|
{descr: "run command 3", eval: `os:! "hello" "tr [a-z] [A-Z]"`, want: "HELLO"},
|
|
{descr: "run command 4", eval: `os:! "hello" "tr -d e" "hello"`, want: "hllo"},
|
|
{descr: "run command 5", eval: `"hello" | os:! "tr [a-z] [A-Z]"`, want: "HELLO"},
|
|
{descr: "run command 6", eval: `"hello" | os:! "tr -d e"`, want: "hllo"},
|
|
{descr: "run command 7", eval: `"hello" | os:! "tr -d e" | os:! "tr [a-z] [A-Z]"`, want: "HLLO"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.descr, func(t *testing.T) {
|
|
inst := ucl.New(
|
|
ucl.WithModule(builtins.OS()),
|
|
)
|
|
res, err := inst.EvalString(context.Background(), tt.eval)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tt.want, res)
|
|
})
|
|
}
|
|
}
|