increased test coverage and splitted some utility functions by OS
This commit is contained in:
parent
7d2cf1e687
commit
f100adead3
@ -30,10 +30,13 @@ func TestFuncOs(t *testing.T) {
|
|||||||
/* 16 */ {`builtin "os.file"; it=fileReadIterator("test-file.txt"); it++`, "uno", nil},
|
/* 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()},
|
/* 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},
|
/* 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", ".")
|
// t.Setenv("EXPR_PATH", ".")
|
||||||
|
|
||||||
runTestSuiteSpec(t, section, inputs, 18)
|
// runTestSuiteSpec(t, section, inputs, 19)
|
||||||
// runTestSuite(t, section, inputs)
|
runTestSuite(t, section, inputs)
|
||||||
}
|
}
|
||||||
|
|||||||
118
t_utils-unix_test.go
Normal file
118
t_utils-unix_test.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -164,3 +164,48 @@ func TestCopyMap(t *testing.T) {
|
|||||||
t.Errorf("utils.CopyMap() failed")
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
48
utils-unix.go
Normal file
48
utils-unix.go
Normal file
@ -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
|
||||||
|
}
|
||||||
18
utils-windows.go
Normal file
18
utils-windows.go
Normal file
@ -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
|
||||||
|
}
|
||||||
38
utils.go
38
utils.go
@ -6,11 +6,7 @@ package expr
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"os/user"
|
|
||||||
"path"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func IsString(v any) (ok bool) {
|
func IsString(v any) (ok bool) {
|
||||||
@ -234,37 +230,3 @@ func ForAll[T, V any](ts []T, fn func(T) V) []V {
|
|||||||
}
|
}
|
||||||
return result
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user