github.com/kidsbmilk/gofronted_all@v0.0.0-20220701224323-6479d5976c5d/libgo/misc/cgo/test/pkg_test.go (about)

     1  // Copyright 2019 The Go 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 cgotest
     6  
     7  import (
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  	"runtime"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  // TestCrossPackageTests compiles and runs tests that depend on imports of other
    17  // local packages, using source code stored in the testdata directory.
    18  //
    19  // The tests in the misc directory tree do not have a valid import path in
    20  // GOPATH mode, so they previously used relative imports. However, relative
    21  // imports do not work in module mode. In order to make the test work in both
    22  // modes, we synthesize a GOPATH in which the module paths are equivalent, and
    23  // run the tests as a subprocess.
    24  //
    25  // If and when we no longer support these tests in GOPATH mode, we can remove
    26  // this shim and move the tests currently located in testdata back into the
    27  // parent directory.
    28  func TestCrossPackageTests(t *testing.T) {
    29  	switch runtime.GOOS {
    30  	case "android":
    31  		t.Skip("Can't exec cmd/go subprocess on Android.")
    32  	case "ios":
    33  		switch runtime.GOARCH {
    34  		case "arm64":
    35  			t.Skip("Can't exec cmd/go subprocess on iOS.")
    36  		}
    37  	}
    38  
    39  	GOPATH, err := os.MkdirTemp("", "cgotest")
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	defer os.RemoveAll(GOPATH)
    44  
    45  	modRoot := filepath.Join(GOPATH, "src", "cgotest")
    46  	if err := overlayDir(modRoot, "testdata"); err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	if err := os.WriteFile(filepath.Join(modRoot, "go.mod"), []byte("module cgotest\n"), 0666); err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	cmd := exec.Command("go", "test")
    54  	if testing.Verbose() {
    55  		cmd.Args = append(cmd.Args, "-v")
    56  	}
    57  	if testing.Short() {
    58  		cmd.Args = append(cmd.Args, "-short")
    59  	}
    60  	cmd.Dir = modRoot
    61  	cmd.Env = append(os.Environ(), "GOPATH="+GOPATH, "PWD="+cmd.Dir)
    62  	out, err := cmd.CombinedOutput()
    63  	if err == nil {
    64  		t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
    65  	} else {
    66  		t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
    67  	}
    68  }