...

Source file src/cuelang.org/go/doc/tutorial/basics/script_test.go

Documentation: cuelang.org/go/doc/tutorial/basics

     1  package basics
     2  
     3  import (
     4  	"io/fs"
     5  	"os"
     6  	"path"
     7  	"path/filepath"
     8  	"regexp"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/rogpeppe/go-internal/testscript"
    13  	"golang.org/x/tools/txtar"
    14  
    15  	"cuelang.org/go/cmd/cue/cmd"
    16  	"cuelang.org/go/cue/parser"
    17  	"cuelang.org/go/internal/cuetest"
    18  )
    19  
    20  // TestLatest checks that the examples match the latest language standard,
    21  // even if still valid in backwards compatibility mode.
    22  func TestLatest(t *testing.T) {
    23  	if err := filepath.WalkDir(".", func(fullpath string, entry fs.DirEntry, err error) error {
    24  		if err != nil {
    25  			return err
    26  		}
    27  		if !strings.HasSuffix(fullpath, ".txtar") {
    28  			return nil
    29  		}
    30  
    31  		a, err := txtar.ParseFile(fullpath)
    32  		if err != nil {
    33  			return err
    34  		}
    35  
    36  		for _, f := range a.Files {
    37  			t.Run(path.Join(fullpath, f.Name), func(t *testing.T) {
    38  				if !strings.HasSuffix(f.Name, ".cue") {
    39  					return
    40  				}
    41  				v := parser.FromVersion(parser.Latest)
    42  				_, err := parser.ParseFile(f.Name, f.Data, v)
    43  				if err != nil {
    44  					t.Errorf("%v: %v", fullpath, err)
    45  				}
    46  			})
    47  		}
    48  		return nil
    49  	}); err != nil {
    50  		t.Fatal(err)
    51  	}
    52  }
    53  
    54  func TestScript(t *testing.T) {
    55  	tourDir := regexp.MustCompile(`^\d+_.+`)
    56  	entries, err := os.ReadDir(".")
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	didWork := false
    61  	for _, entry := range entries {
    62  		if !entry.IsDir() || !tourDir.MatchString(entry.Name()) {
    63  			continue
    64  		}
    65  		didWork = true
    66  		testscript.Run(t, testscript.Params{
    67  			Dir:                 entry.Name(),
    68  			UpdateScripts:       cuetest.UpdateGoldenFiles,
    69  			RequireExplicitExec: true,
    70  		})
    71  	}
    72  	if !didWork {
    73  		t.Fatal("failed to find any steps in the tour")
    74  	}
    75  }
    76  
    77  func TestMain(m *testing.M) {
    78  	os.Exit(testscript.RunMain(m, map[string]func() int{
    79  		"cue": cmd.MainTest,
    80  	}))
    81  }
    82  

View as plain text