diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:25:22 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:25:22 +0000 |
commit | f6ad4dcef54c5ce997a4bad5a6d86de229015700 (patch) | |
tree | 7cfa4e31ace5c2bd95c72b154d15af494b2bcbef /src/cmd/cgo/internal/teststdio/stdio_test.go | |
parent | Initial commit. (diff) | |
download | golang-1.22-f6ad4dcef54c5ce997a4bad5a6d86de229015700.tar.xz golang-1.22-f6ad4dcef54c5ce997a4bad5a6d86de229015700.zip |
Adding upstream version 1.22.1.upstream/1.22.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/cmd/cgo/internal/teststdio/stdio_test.go')
-rw-r--r-- | src/cmd/cgo/internal/teststdio/stdio_test.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/cmd/cgo/internal/teststdio/stdio_test.go b/src/cmd/cgo/internal/teststdio/stdio_test.go new file mode 100644 index 0000000..3883422 --- /dev/null +++ b/src/cmd/cgo/internal/teststdio/stdio_test.go @@ -0,0 +1,77 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package stdio_test + +import ( + "bytes" + "cmd/cgo/internal/cgotest" + "internal/testenv" + "log" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" +) + +func TestMain(m *testing.M) { + log.SetFlags(log.Lshortfile) + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + GOPATH, err := os.MkdirTemp("", "cgostdio") + if err != nil { + log.Panic(err) + } + defer os.RemoveAll(GOPATH) + os.Setenv("GOPATH", GOPATH) + + // Copy testdata into GOPATH/src/cgostdio, along with a go.mod file + // declaring the same path. + modRoot := filepath.Join(GOPATH, "src", "cgostdio") + if err := cgotest.OverlayDir(modRoot, "testdata"); err != nil { + log.Panic(err) + } + if err := os.Chdir(modRoot); err != nil { + log.Panic(err) + } + os.Setenv("PWD", modRoot) + if err := os.WriteFile("go.mod", []byte("module cgostdio\n"), 0666); err != nil { + log.Panic(err) + } + + return m.Run() +} + +// TestTestRun runs a cgo test that doesn't depend on non-standard libraries. +func TestTestRun(t *testing.T) { + testenv.MustHaveGoRun(t) + testenv.MustHaveCGO(t) + + for _, file := range [...]string{ + "chain.go", + "fib.go", + "hello.go", + } { + file := file + wantFile := strings.Replace(file, ".go", ".out", 1) + t.Run(file, func(t *testing.T) { + cmd := exec.Command("go", "run", file) + got, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("%v: %s\n%s", cmd, err, got) + } + got = bytes.ReplaceAll(got, []byte("\r\n"), []byte("\n")) + want, err := os.ReadFile(wantFile) + if err != nil { + t.Fatal("reading golden output:", err) + } + if !bytes.Equal(got, want) { + t.Errorf("'%v' output does not match expected in %s. Instead saw:\n%s", cmd, wantFile, got) + } + }) + } +} |