gitlab.com/CoiaPrant/sqlite3@v1.19.1/tcl_test.go (about)

     1  // Copyright 2020 The Sqlite 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 sqlite // import "gitlab.com/CoiaPrant/sqlite3"
     6  
     7  import (
     8  	"bytes"
     9  	"flag"
    10  	"fmt"
    11  	"os"
    12  	"os/exec"
    13  	"path/filepath"
    14  	"runtime"
    15  	"strings"
    16  	"testing"
    17  
    18  	"modernc.org/tcl"
    19  )
    20  
    21  var (
    22  	oMaxError = flag.Uint("maxerror", 0, "argument of -maxerror passed to the Tcl test suite")
    23  	oStart    = flag.String("start", "", "argument of -start passed to the Tcl test suite (-start=[$permutation:]$testfile)")
    24  	oTclSuite = flag.String("suite", "full", "Tcl test suite [test-file] to run")
    25  	oVerbose  = flag.String("verbose", "0", "argument of -verbose passed to the Tcl test suite, must be set to a boolean (0, 1) or to \"file\"")
    26  )
    27  
    28  func TestTclTest(t *testing.T) {
    29  	if testing.Short() {
    30  		t.Skip("skipping test in short mode")
    31  	}
    32  
    33  	if err := setMaxOpenFiles(1024); err != nil { // Avoid misc7.test hanging for a long time.
    34  		t.Fatal(err)
    35  	}
    36  
    37  	blacklist := map[string]struct{}{}
    38  	switch runtime.GOARCH {
    39  	case "386", "arm":
    40  		// # This test causes thrashing on machines with smaller amounts of
    41  		// # memory.  Make sure the host has at least 8GB available before running
    42  		// # this test.
    43  		blacklist["bigsort.test"] = struct{}{}
    44  	case "s390x":
    45  		//TODO
    46  		blacklist["sysfault.test"] = struct{}{}
    47  	}
    48  	switch runtime.GOOS {
    49  	case "windows":
    50  		// See https://gitlab.com/cznic/sqlite/-/issues/23#note_599920077 for details.
    51  		blacklist["symlink2.test"] = struct{}{}
    52  	}
    53  	tclTests := "testdata/tcl/*"
    54  	m, err := filepath.Glob(filepath.FromSlash(tclTests))
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  
    59  	dir, err := os.MkdirTemp("", "sqlite-test-")
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	defer os.RemoveAll(dir)
    65  
    66  	bin := "testfixture"
    67  	if runtime.GOOS == "windows" {
    68  		bin += ".exe"
    69  	}
    70  	testfixture := filepath.Join(dir, bin)
    71  	args0 := []string{"build", "-o", testfixture}
    72  	tags := "-tags=libc.nofsync"
    73  	if s := *oXTags; s != "" {
    74  		tags += "," + s
    75  	}
    76  	args0 = append(args0, tags, "gitlab.com/CoiaPrant/sqlite3/internal/testfixture")
    77  	cmd := exec.Command("go", args0...)
    78  	if out, err := cmd.CombinedOutput(); err != nil {
    79  		t.Fatalf("%s\n%v", out, err)
    80  	}
    81  
    82  	wd, err := os.Getwd()
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	defer os.Chdir(wd)
    88  
    89  	if err := os.Chdir(dir); err != nil {
    90  		t.Fatal(err)
    91  	}
    92  
    93  	for _, v := range m {
    94  		if _, ok := blacklist[filepath.Base(v)]; ok {
    95  			trc("skipping %v", v)
    96  			continue
    97  		}
    98  
    99  		s := filepath.Join(wd, v)
   100  		d := filepath.Join(dir, filepath.Base(v))
   101  		f, err := os.ReadFile(s)
   102  		if err != nil {
   103  			t.Fatal(err)
   104  		}
   105  
   106  		if err := os.WriteFile(d, f, 0660); err != nil {
   107  			t.Fatal(err)
   108  		}
   109  	}
   110  
   111  	library := filepath.Join(dir, "library")
   112  	if err := tcl.Library(library); err != nil {
   113  		t.Fatal(err)
   114  	}
   115  
   116  	os.Setenv("TCL_LIBRARY", library)
   117  	var args []string
   118  	switch s := *oTclSuite; s {
   119  	case "":
   120  		args = []string{"all.test"}
   121  	default:
   122  		a := strings.Split(s, " ")
   123  		args = append([]string{"permutations.test"}, a...)
   124  	}
   125  	if *oVerbose != "" {
   126  		args = append(args, fmt.Sprintf("-verbose=%s", *oVerbose))
   127  	}
   128  	if *oMaxError != 0 {
   129  		args = append(args, fmt.Sprintf("-maxerror=%d", *oMaxError))
   130  	}
   131  	if *oStart != "" {
   132  		args = append(args, fmt.Sprintf("-start=%s", *oStart))
   133  	}
   134  	os.Setenv("PATH", fmt.Sprintf("%s%c%s", dir, os.PathListSeparator, os.Getenv("PATH")))
   135  	cmd = exec.Command(bin, args...)
   136  	var w echoWriter
   137  	cmd.Stdout = &w
   138  	cmd.Stderr = &w
   139  	if err := cmd.Run(); err != nil {
   140  		t.Fatal(err)
   141  	}
   142  
   143  	if b := w.w.Bytes(); bytes.Contains(b, []byte("while executing")) {
   144  		t.Fatal("silent/unreported error detected in output")
   145  	}
   146  }
   147  
   148  type echoWriter struct {
   149  	w bytes.Buffer
   150  }
   151  
   152  func (w *echoWriter) Write(b []byte) (int, error) {
   153  	os.Stdout.Write(b)
   154  	return w.w.Write(b)
   155  }