...

Source file src/go.starlark.net/starlarktest/starlarktest.go

Documentation: go.starlark.net/starlarktest

     1  // Copyright 2017 The Bazel Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package starlarktest defines utilities for testing Starlark programs.
     6  //
     7  // Clients can call LoadAssertModule to load a module that defines
     8  // several functions useful for testing.  See assert.star for its
     9  // definition.
    10  //
    11  // The assert.error function, which reports errors to the current Go
    12  // testing.T, requires that clients call SetReporter(thread, t) before use.
    13  package starlarktest // import "go.starlark.net/starlarktest"
    14  
    15  import (
    16  	_ "embed"
    17  	"fmt"
    18  	"os"
    19  	"path/filepath"
    20  	"regexp"
    21  	"strings"
    22  	"sync"
    23  
    24  	"go.starlark.net/starlark"
    25  	"go.starlark.net/starlarkstruct"
    26  )
    27  
    28  const localKey = "Reporter"
    29  
    30  // A Reporter is a value to which errors may be reported.
    31  // It is satisfied by *testing.T.
    32  type Reporter interface {
    33  	Error(args ...interface{})
    34  }
    35  
    36  // SetReporter associates an error reporter (such as a testing.T in
    37  // a Go test) with the Starlark thread so that Starlark programs may
    38  // report errors to it.
    39  func SetReporter(thread *starlark.Thread, r Reporter) {
    40  	thread.SetLocal(localKey, r)
    41  }
    42  
    43  // GetReporter returns the Starlark thread's error reporter.
    44  // It must be preceded by a call to SetReporter.
    45  func GetReporter(thread *starlark.Thread) Reporter {
    46  	r, ok := thread.Local(localKey).(Reporter)
    47  	if !ok {
    48  		panic("internal error: starlarktest.SetReporter was not called")
    49  	}
    50  	return r
    51  }
    52  
    53  var (
    54  	once   sync.Once
    55  	assert starlark.StringDict
    56  	//go:embed assert.star
    57  	assertFileSrc string
    58  	assertErr     error
    59  )
    60  
    61  // LoadAssertModule loads the assert module.
    62  // It is concurrency-safe and idempotent.
    63  func LoadAssertModule() (starlark.StringDict, error) {
    64  	once.Do(func() {
    65  		predeclared := starlark.StringDict{
    66  			"error":   starlark.NewBuiltin("error", error_),
    67  			"catch":   starlark.NewBuiltin("catch", catch),
    68  			"matches": starlark.NewBuiltin("matches", matches),
    69  			"module":  starlark.NewBuiltin("module", starlarkstruct.MakeModule),
    70  			"_freeze": starlark.NewBuiltin("freeze", freeze),
    71  		}
    72  		thread := new(starlark.Thread)
    73  		assert, assertErr = starlark.ExecFile(thread, "assert.star", assertFileSrc, predeclared)
    74  	})
    75  	return assert, assertErr
    76  }
    77  
    78  // catch(f) evaluates f() and returns its evaluation error message
    79  // if it failed or None if it succeeded.
    80  func catch(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    81  	var fn starlark.Callable
    82  	if err := starlark.UnpackArgs("catch", args, kwargs, "fn", &fn); err != nil {
    83  		return nil, err
    84  	}
    85  	if _, err := starlark.Call(thread, fn, nil, nil); err != nil {
    86  		return starlark.String(err.Error()), nil
    87  	}
    88  	return starlark.None, nil
    89  }
    90  
    91  // matches(pattern, str) reports whether string str matches the regular expression pattern.
    92  func matches(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    93  	var pattern, str string
    94  	if err := starlark.UnpackArgs("matches", args, kwargs, "pattern", &pattern, "str", &str); err != nil {
    95  		return nil, err
    96  	}
    97  	ok, err := regexp.MatchString(pattern, str)
    98  	if err != nil {
    99  		return nil, fmt.Errorf("matches: %s", err)
   100  	}
   101  	return starlark.Bool(ok), nil
   102  }
   103  
   104  // error(x) reports an error to the Go test framework.
   105  func error_(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
   106  	if len(args) != 1 {
   107  		return nil, fmt.Errorf("error: got %d arguments, want 1", len(args))
   108  	}
   109  	buf := new(strings.Builder)
   110  	stk := thread.CallStack()
   111  	stk.Pop()
   112  	fmt.Fprintf(buf, "%sError: ", stk)
   113  	if s, ok := starlark.AsString(args[0]); ok {
   114  		buf.WriteString(s)
   115  	} else {
   116  		buf.WriteString(args[0].String())
   117  	}
   118  	GetReporter(thread).Error(buf.String())
   119  	return starlark.None, nil
   120  }
   121  
   122  // freeze(x) freezes its operand.
   123  func freeze(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
   124  	if len(kwargs) > 0 {
   125  		return nil, fmt.Errorf("freeze does not accept keyword arguments")
   126  	}
   127  	if len(args) != 1 {
   128  		return nil, fmt.Errorf("freeze got %d arguments, wants 1", len(args))
   129  	}
   130  	args[0].Freeze()
   131  	return args[0], nil
   132  }
   133  
   134  // DataFile returns the effective filename of the specified
   135  // test data resource.  The function abstracts differences between
   136  // 'go build', under which a test runs in its package directory,
   137  // and Blaze, under which a test runs in the root of the tree.
   138  var DataFile = func(pkgdir, filename string) string {
   139  	// Check if we're being run by Bazel and change directories if so.
   140  	// TEST_SRCDIR and TEST_WORKSPACE are set by the Bazel test runner, so that makes a decent check
   141  	testSrcdir := os.Getenv("TEST_SRCDIR")
   142  	testWorkspace := os.Getenv("TEST_WORKSPACE")
   143  	if testSrcdir != "" && testWorkspace != "" {
   144  		return filepath.Join(testSrcdir, "net_starlark_go", pkgdir, filename)
   145  	}
   146  
   147  	// Under go test, ignore pkgdir, which is the directory of the
   148  	// current package relative to the module root.
   149  	return filename
   150  }
   151  

View as plain text