aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/cgo/internal/testplugin
diff options
context:
space:
mode:
authorAustin Clements <austin@google.com>2023-05-22 10:32:31 -0400
committerAustin Clements <austin@google.com>2023-05-22 20:56:09 +0000
commita1f3dc33dced2d337261b693b1ef1bf6a5bbce8d (patch)
tree991824c08b95c01cf7591d90db9cdeb1c9ac562d /src/cmd/cgo/internal/testplugin
parent96a773d61bc60022af0097f4a08495880a5ea33e (diff)
downloadgo-a1f3dc33dced2d337261b693b1ef1bf6a5bbce8d.tar.xz
cmd/cgo: merge overlayDir into one package
There are many copies of overlaydir_test.go between the cgo tests from when these couldn't share code. Now that they can, merge these copies into a cmd/cgo/internal/cgotest package. Change-Id: I203217f5d08e6306cb049a13718652cf7c447b80 Reviewed-on: https://go-review.googlesource.com/c/go/+/497078 Run-TryBot: Austin Clements <austin@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/cmd/cgo/internal/testplugin')
-rw-r--r--src/cmd/cgo/internal/testplugin/overlaydir_test.go78
-rw-r--r--src/cmd/cgo/internal/testplugin/plugin_test.go3
2 files changed, 2 insertions, 79 deletions
diff --git a/src/cmd/cgo/internal/testplugin/overlaydir_test.go b/src/cmd/cgo/internal/testplugin/overlaydir_test.go
deleted file mode 100644
index e2c32d83ce..0000000000
--- a/src/cmd/cgo/internal/testplugin/overlaydir_test.go
+++ /dev/null
@@ -1,78 +0,0 @@
-// 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 plugin_test
-
-import (
- "io"
- "os"
- "path/filepath"
- "strings"
-)
-
-// overlayDir makes a minimal-overhead copy of srcRoot in which new files may be added.
-//
-// TODO: Once we no longer need to support the misc module in GOPATH mode,
-// factor this function out into a package to reduce duplication.
-func overlayDir(dstRoot, srcRoot string) error {
- dstRoot = filepath.Clean(dstRoot)
- if err := os.MkdirAll(dstRoot, 0777); err != nil {
- return err
- }
-
- srcRoot, err := filepath.Abs(srcRoot)
- if err != nil {
- return err
- }
-
- return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
- if err != nil || srcPath == srcRoot {
- return err
- }
-
- suffix := strings.TrimPrefix(srcPath, srcRoot)
- for len(suffix) > 0 && suffix[0] == filepath.Separator {
- suffix = suffix[1:]
- }
- dstPath := filepath.Join(dstRoot, suffix)
-
- perm := info.Mode() & os.ModePerm
- if info.Mode()&os.ModeSymlink != 0 {
- info, err = os.Stat(srcPath)
- if err != nil {
- return err
- }
- perm = info.Mode() & os.ModePerm
- }
-
- // Always copy directories (don't symlink them).
- // If we add a file in the overlay, we don't want to add it in the original.
- if info.IsDir() {
- return os.MkdirAll(dstPath, perm|0200)
- }
-
- // If the OS supports symlinks, use them instead of copying bytes.
- if err := os.Symlink(srcPath, dstPath); err == nil {
- return nil
- }
-
- // Otherwise, copy the bytes.
- src, err := os.Open(srcPath)
- if err != nil {
- return err
- }
- defer src.Close()
-
- dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
- if err != nil {
- return err
- }
-
- _, err = io.Copy(dst, src)
- if closeErr := dst.Close(); err == nil {
- err = closeErr
- }
- return err
- })
-}
diff --git a/src/cmd/cgo/internal/testplugin/plugin_test.go b/src/cmd/cgo/internal/testplugin/plugin_test.go
index e211a96304..4c8e3d7833 100644
--- a/src/cmd/cgo/internal/testplugin/plugin_test.go
+++ b/src/cmd/cgo/internal/testplugin/plugin_test.go
@@ -6,6 +6,7 @@ package plugin_test
import (
"bytes"
+ "cmd/cgo/internal/cgotest"
"context"
"flag"
"fmt"
@@ -80,7 +81,7 @@ func testMain(m *testing.M) int {
"testdata": modRoot,
filepath.Join("altpath", "testdata"): altRoot,
} {
- if err := overlayDir(dstRoot, srcRoot); err != nil {
+ if err := cgotest.OverlayDir(dstRoot, srcRoot); err != nil {
log.Panic(err)
}
prettyPrintf("mkdir -p %s\n", dstRoot)