feature: use environment in addition to flags to control test options

This applies to -syscall, -unsafe and -unrestricted flags with the
corresponding env variables YAEGI_SYSCALL, YAEGI_UNSAFE and
YAEGI_UNRESTRICTED, already used in the same way for the run
sub-command.
This commit is contained in:
Marc Vertes
2020-10-15 18:42:04 +02:00
committed by GitHub
parent 8916618a81
commit 4b3e9ee231

View File

@@ -7,6 +7,7 @@ import (
"go/build" "go/build"
"os" "os"
"regexp" "regexp"
"strconv"
"strings" "strings"
"testing" "testing"
@@ -28,13 +29,15 @@ func test(arg []string) (err error) {
run string run string
short bool short bool
tags string tags string
useUnrestricted bool
useUnsafe bool
useSyscall bool
timeout string timeout string
verbose bool verbose bool
) )
// The following flags are initialized from environment.
useSyscall, _ := strconv.ParseBool(os.Getenv("YAEGI_SYSCALL"))
useUnrestricted, _ := strconv.ParseBool(os.Getenv("YAEGI_UNRESTRICTED"))
useUnsafe, _ := strconv.ParseBool(os.Getenv("YAEGI_UNSAFE"))
tflag := flag.NewFlagSet("test", flag.ContinueOnError) tflag := flag.NewFlagSet("test", flag.ContinueOnError)
tflag.StringVar(&bench, "bench", "", "Run only those benchmarks matching a regular expression.") tflag.StringVar(&bench, "bench", "", "Run only those benchmarks matching a regular expression.")
tflag.BoolVar(&benchmem, "benchmem", false, "Print memory allocation statistics for benchmarks.") tflag.BoolVar(&benchmem, "benchmem", false, "Print memory allocation statistics for benchmarks.")
@@ -46,9 +49,9 @@ func test(arg []string) (err error) {
tflag.BoolVar(&short, "short", false, "Tell long-running tests to shorten their run time.") tflag.BoolVar(&short, "short", false, "Tell long-running tests to shorten their run time.")
tflag.StringVar(&tags, "tags", "", "Set a list of build tags.") tflag.StringVar(&tags, "tags", "", "Set a list of build tags.")
tflag.StringVar(&timeout, "timeout", "", "If a test binary runs longer than duration d, panic.") tflag.StringVar(&timeout, "timeout", "", "If a test binary runs longer than duration d, panic.")
tflag.BoolVar(&useUnrestricted, "unrestricted", false, "Include unrestricted symbols.") tflag.BoolVar(&useUnrestricted, "unrestricted", useUnrestricted, "Include unrestricted symbols.")
tflag.BoolVar(&useUnsafe, "unsafe", false, "Include usafe symbols.") tflag.BoolVar(&useUnsafe, "unsafe", useUnsafe, "Include usafe symbols.")
tflag.BoolVar(&useSyscall, "syscall", false, "Include syscall symbols.") tflag.BoolVar(&useSyscall, "syscall", useSyscall, "Include syscall symbols.")
tflag.BoolVar(&verbose, "v", false, "Verbose output: log all tests as they are run.") tflag.BoolVar(&verbose, "v", false, "Verbose output: log all tests as they are run.")
tflag.Usage = func() { tflag.Usage = func() {
fmt.Println("Usage: yaegi test [options] [path]") fmt.Println("Usage: yaegi test [options] [path]")
@@ -105,12 +108,22 @@ func test(arg []string) (err error) {
i.Use(interp.Symbols) i.Use(interp.Symbols)
if useSyscall { if useSyscall {
i.Use(syscall.Symbols) i.Use(syscall.Symbols)
// Using a environment var allows a nested interpreter to import the syscall package.
if err := os.Setenv("YAEGI_SYSCALL", "1"); err != nil {
return err
}
} }
if useUnrestricted { if useUnrestricted {
i.Use(unrestricted.Symbols) i.Use(unrestricted.Symbols)
if err := os.Setenv("YAEGI_UNRESTRICTED", "1"); err != nil {
return err
}
} }
if useUnsafe { if useUnsafe {
i.Use(unsafe.Symbols) i.Use(unsafe.Symbols)
if err := os.Setenv("YAEGI_UNSAFE", "1"); err != nil {
return err
}
} }
if err = i.EvalTest(path); err != nil { if err = i.EvalTest(path); err != nil {
return err return err