diff --git a/t_builtin-os-file_test.go b/t_builtin-os-file_test.go index f828396..1521328 100644 --- a/t_builtin-os-file_test.go +++ b/t_builtin-os-file_test.go @@ -30,10 +30,13 @@ func TestFuncOs(t *testing.T) { /* 16 */ {`builtin "os.file"; it=fileReadIterator("test-file.txt"); it++`, "uno", nil}, /* 17 */ {`builtin "os.file"; it=fileReadIterator("test-file.txt"); it++;it++;it++`, nil, io.EOF.Error()}, /* 18 */ {`builtin "os.file"; it=fileReadIterator("test-file.txt"); it.clean`, nil, nil}, + /* 19 */ {`builtin "os.file"; it=fileReadIterator("test-file.txt"); string(it)`, `$(fileReadTextIterator@"test-file.txt")`, nil}, + /* 20 */ {`builtin "os.file"; handle=fileOpen("/etc/hosts"); fileClose(handle); string(handle)`, `reader`, nil}, + /* 21 */ {`builtin "os.file"; handle=fileCreate("/tmp/dummy"); fileClose(handle); string(handle)`, `writer`, nil}, } // t.Setenv("EXPR_PATH", ".") - runTestSuiteSpec(t, section, inputs, 18) - // runTestSuite(t, section, inputs) + // runTestSuiteSpec(t, section, inputs, 19) + runTestSuite(t, section, inputs) } diff --git a/t_utils-unix_test.go b/t_utils-unix_test.go new file mode 100644 index 0000000..4c166b9 --- /dev/null +++ b/t_utils-unix_test.go @@ -0,0 +1,118 @@ +//go:build unix + +// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com). +// All rights reserved. + +// t_utils_test.go +package expr + +import ( + "errors" + "os/user" + "path" + "testing" +) + +func TestExpandPathRootOk(t *testing.T) { + source := "~root" + wantValue := "/root" + // wantErr := errors.New(`test expected string, got list ([])`) + wantErr := error(nil) + + gotValue, gotErr := ExpandPath(source) + + if gotErr != nil && gotErr.Error() != wantErr.Error() { + t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`, + source, gotValue, gotErr, wantValue, wantErr) + } else if gotValue != wantValue { + t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`, + source, gotValue, gotErr, wantValue, wantErr) + } +} + +func TestExpandPathRootSubDirOk(t *testing.T) { + source := "~root/test" + wantValue := "/root/test" + // wantErr := errors.New(`test expected string, got list ([])`) + wantErr := error(nil) + + gotValue, gotErr := ExpandPath(source) + + if gotErr != nil && gotErr.Error() != wantErr.Error() { + t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`, + source, gotValue, gotErr, wantValue, wantErr) + } else if gotValue != wantValue { + t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`, + source, gotValue, gotErr, wantValue, wantErr) + } +} + +func TestExpandPathUser(t *testing.T) { + u, _ := user.Current() + source := "~" + wantValue := path.Join("/home", u.Username) + // wantErr := errors.New(`test expected string, got list ([])`) + wantErr := error(nil) + + gotValue, gotErr := ExpandPath(source) + + if gotErr != nil { + t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`, + source, gotValue, gotErr, wantValue, wantErr) + } else if gotValue != wantValue { + t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`, + source, gotValue, gotErr, wantValue, wantErr) + } +} + +func TestExpandPathEnv(t *testing.T) { + u, _ := user.Current() + source := "$HOME" + wantValue := path.Join("/home", u.Username) + // wantErr := errors.New(`test expected string, got list ([])`) + wantErr := error(nil) + + gotValue, gotErr := ExpandPath(source) + + if gotErr != nil { + t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`, + source, gotValue, gotErr, wantValue, wantErr) + } else if gotValue != wantValue { + t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`, + source, gotValue, gotErr, wantValue, wantErr) + } +} + +func TestExpandPathErr(t *testing.T) { + source := "~fake-user/test" + wantValue := "~fake-user/test" + wantErr := errors.New(`user: unknown user fake-user`) + + gotValue, gotErr := ExpandPath(source) + + if gotErr != nil && gotErr.Error() != wantErr.Error() { + t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`, + source, gotValue, gotErr, wantValue, wantErr) + } else if gotValue != wantValue { + t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`, + source, gotValue, gotErr, wantValue, wantErr) + } +} + +func TestExpandPathUserErr(t *testing.T) { + u, _ := user.Current() + source := "~" + wantValue := path.Join("/home", u.Username) + // wantErr := errors.New(`test expected string, got list ([])`) + wantErr := error(nil) + + gotValue, gotErr := ExpandPath(source) + + if gotErr != nil { + t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`, + source, gotValue, gotErr, wantValue, wantErr) + } else if gotValue != wantValue { + t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`, + source, gotValue, gotErr, wantValue, wantErr) + } +} diff --git a/t_utils_test.go b/t_utils_test.go index 372c875..cf496cb 100644 --- a/t_utils_test.go +++ b/t_utils_test.go @@ -164,3 +164,48 @@ func TestCopyMap(t *testing.T) { t.Errorf("utils.CopyMap() failed") } } + +func TestToStringOk(t *testing.T) { + source := "ciao" + wantValue := "ciao" + wantErr := error(nil) + + gotValue, gotErr := ToGoString(source, "test") + + if gotErr != nil { + t.Error(gotErr) + } else if gotValue != wantValue { + t.Errorf("toGoString(%v, \"test\") gotValue=%v, gotErr=%v -> wantValue=%v, wantErr=%v", + source, gotValue, gotErr, wantValue, wantErr) + } +} + +func TestToStringErr(t *testing.T) { + source := newListA() + wantValue := "" + wantErr := errors.New(`test expected string, got list ([])`) + + gotValue, gotErr := ToGoString(source, "test") + + if gotErr != nil && gotErr.Error() != wantErr.Error() { + t.Errorf(`ToGoString(%v, "test") gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`, + source, gotValue, gotErr, wantValue, wantErr) + } else if gotValue != wantValue { + t.Errorf(`ToString(%v, "test") gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`, + source, gotValue, gotErr, wantValue, wantErr) + } +} + +func TestIsFunctorSucc(t *testing.T) { + f := NewGolangFunctor(isNilFunc) + if !isFunctor(f) { + t.Errorf("isNilFunc() evalued as not a functor") + } +} + +func TestIsFunctorFail(t *testing.T) { + f := int64(1) + if isFunctor(f) { + t.Errorf("int evalued as a functor") + } +} diff --git a/utils-unix.go b/utils-unix.go new file mode 100644 index 0000000..c5774db --- /dev/null +++ b/utils-unix.go @@ -0,0 +1,48 @@ +//go:build unix + +// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com). +// All rights reserved. + +// utils-unix.go +package expr + +import ( + "os" + "os/user" + "path" + "strings" +) + +func ExpandPath(sourcePath string) (expandedPath string, err error) { + for expandedPath = os.ExpandEnv(sourcePath); expandedPath != sourcePath; expandedPath = os.ExpandEnv(sourcePath) { + sourcePath = expandedPath + } + + if strings.HasPrefix(sourcePath, "~") { + var home, userName, remainder string + + slashPos := strings.IndexRune(sourcePath, '/') + if slashPos > 0 { + userName = sourcePath[1:slashPos] + remainder = sourcePath[slashPos:] + } else { + userName = sourcePath[1:] + } + + if len(userName) == 0 { + home, err = os.UserHomeDir() + if err != nil { + return + } + } else { + var userInfo *user.User + userInfo, err = user.Lookup(userName) + if err != nil { + return + } + home = userInfo.HomeDir + } + expandedPath = path.Join(home, remainder) + } + return +} diff --git a/utils-windows.go b/utils-windows.go new file mode 100644 index 0000000..038b33f --- /dev/null +++ b/utils-windows.go @@ -0,0 +1,18 @@ +//go:build windows + +// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com). +// All rights reserved. + +// utils-unix.go +package expr + +import ( + "os" +) + +func ExpandPath(sourcePath string) (expandedPath string, err error) { + for expandedPath = os.ExpandEnv(sourcePath); expandedPath != sourcePath; expandedPath = os.ExpandEnv(sourcePath) { + sourcePath = expandedPath + } + return +} diff --git a/utils.go b/utils.go index 03a0fda..1dcc34d 100644 --- a/utils.go +++ b/utils.go @@ -6,11 +6,7 @@ package expr import ( "fmt" - "os" - "os/user" - "path" "reflect" - "strings" ) func IsString(v any) (ok bool) { @@ -234,37 +230,3 @@ func ForAll[T, V any](ts []T, fn func(T) V) []V { } return result } - -func ExpandPath(sourcePath string) (expandedPath string, err error) { - for expandedPath = os.ExpandEnv(sourcePath); expandedPath != sourcePath; expandedPath = os.ExpandEnv(sourcePath) { - sourcePath = expandedPath - } - - if strings.HasPrefix(sourcePath, "~") { - var home, userName, remainder string - - slashPos := strings.IndexRune(sourcePath, '/') - if slashPos > 0 { - userName = sourcePath[1:slashPos] - remainder = sourcePath[slashPos:] - } else { - userName = sourcePath[1:] - } - - if len(userName) == 0 { - home, err = os.UserHomeDir() - if err != nil { - return - } - } else { - var userInfo *user.User - userInfo, err = user.Lookup(userName) - if err != nil { - return - } - home = userInfo.HomeDir - } - expandedPath = path.Join(home, remainder) - } - return -}