modernc.org/z@v1.7.4/all_test.go (about)

     1  // Copyright 2021 The Zlib-Go Authors. All rights reserved.
     2  // Use of the source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package z // import "modernc.org/z"
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"os"
    12  	"path"
    13  	"path/filepath"
    14  	"runtime"
    15  	"runtime/debug"
    16  	"strings"
    17  	"testing"
    18  
    19  	"modernc.org/ccgo/v3/lib"
    20  )
    21  
    22  func caller(s string, va ...interface{}) {
    23  	if s == "" {
    24  		s = strings.Repeat("%v ", len(va))
    25  	}
    26  	_, fn, fl, _ := runtime.Caller(2)
    27  	fmt.Fprintf(os.Stderr, "# caller: %s:%d: ", path.Base(fn), fl)
    28  	fmt.Fprintf(os.Stderr, s, va...)
    29  	fmt.Fprintln(os.Stderr)
    30  	_, fn, fl, _ = runtime.Caller(1)
    31  	fmt.Fprintf(os.Stderr, "# \tcallee: %s:%d: ", path.Base(fn), fl)
    32  	fmt.Fprintln(os.Stderr)
    33  	os.Stderr.Sync()
    34  }
    35  
    36  func dbg(s string, va ...interface{}) {
    37  	if s == "" {
    38  		s = strings.Repeat("%v ", len(va))
    39  	}
    40  	_, fn, fl, _ := runtime.Caller(1)
    41  	fmt.Fprintf(os.Stderr, "# dbg %s:%d: ", path.Base(fn), fl)
    42  	fmt.Fprintf(os.Stderr, s, va...)
    43  	fmt.Fprintln(os.Stderr)
    44  	os.Stderr.Sync()
    45  }
    46  
    47  func TODO(...interface{}) string { //TODOOK
    48  	_, fn, fl, _ := runtime.Caller(1)
    49  	return fmt.Sprintf("# TODO: %s:%d:\n", path.Base(fn), fl) //TODOOK
    50  }
    51  
    52  func stack() string { return string(debug.Stack()) }
    53  
    54  func use(...interface{}) {}
    55  
    56  func init() {
    57  	use(caller, dbg, TODO, stack) //TODOOK
    58  }
    59  
    60  // ----------------------------------------------------------------------------
    61  
    62  var (
    63  	xtags = flag.String("xtags", "non.existent.tag", "")
    64  )
    65  
    66  func TestMain(m *testing.M) {
    67  	flag.Parse()
    68  	rc := m.Run()
    69  	os.Exit(rc)
    70  }
    71  
    72  func Test(t *testing.T) {
    73  	tmpDir, err := ioutil.TempDir("", "go-test-zlib-")
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  
    78  	defer os.RemoveAll(tmpDir)
    79  
    80  	wd, err := ccgo.AbsCwd()
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  
    85  	goos := runtime.GOOS
    86  	goarch := runtime.GOARCH
    87  	mg := filepath.Join(wd, "internal", fmt.Sprintf("minigzip_%s_%s.go", goos, goarch))
    88  	ex := filepath.Join(wd, "internal", fmt.Sprintf("example_%s_%s.go", goos, goarch))
    89  	mgBin := "minigzip"
    90  	exBin := "example"
    91  	if goos == "windows" {
    92  		mgBin += ".exe"
    93  		exBin += ".exe"
    94  	}
    95  	if ccgo.Shell("go", "build", "-tags", *xtags, "-o", filepath.Join(tmpDir, mgBin), mg); err != nil {
    96  		t.Fatal(err)
    97  	}
    98  
    99  	if ccgo.Shell("go", "build", "-tags", *xtags, "-o", filepath.Join(tmpDir, exBin), ex); err != nil {
   100  		t.Fatal(err)
   101  	}
   102  
   103  	if err := ccgo.InDir(tmpDir, func() error {
   104  		switch goos {
   105  		case "windows":
   106  			if err := ccgo.InDir(tmpDir, func() error {
   107  				if out, err := ccgo.Shell("cmd.exe", "/c", fmt.Sprintf("echo hello world | %s | %[1]s -d", mgBin, exBin)); err != nil {
   108  					return fmt.Errorf("%s\nFAIL: %v", out, err)
   109  				}
   110  
   111  				return nil
   112  			}); err != nil {
   113  				t.Fatal(err)
   114  			}
   115  		default:
   116  			if err := ccgo.InDir(tmpDir, func() error {
   117  				mgBin = "./" + mgBin
   118  				exBin = "./" + exBin
   119  				if out, err := ccgo.Shell("sh", "-c", fmt.Sprintf("echo hello world | %s | %[1]s -d && %s tmp", mgBin, exBin)); err != nil {
   120  					return fmt.Errorf("%s\nFAIL: %v", out, err)
   121  				}
   122  
   123  				return nil
   124  			}); err != nil {
   125  				t.Fatal(err)
   126  			}
   127  		}
   128  
   129  		return nil
   130  	}); err != nil {
   131  		t.Fatal(err)
   132  	}
   133  }