ucl/ucl/builtins/os_test.go
lmika dd516feed4
All checks were successful
Build / build (push) Successful in 2m39s
Added os:exec builtin (#2)
Co-authored-by: Leon Mika <lmika@lmika.com>
Reviewed-on: #2
2025-10-24 06:09:45 +00:00

61 lines
1.5 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 1", eval: `os:exec "date" "+%Y%m%d"`, want: time.Now().Format("20060102") + "\n"},
}
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)
})
}
}