Commit ffde2ddb authored by Bryan C. Mills's avatar Bryan C. Mills

misc/cgo/testshared: fix tests in module mode

Updates #30228

Change-Id: I5cc739eb9fdfb648ec45e350d43d4cb02e450553
Reviewed-on: https://go-review.googlesource.com/c/163211
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: default avatarJay Conrod <jayconrod@google.com>
parent 13d9a290
// 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 shared_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
}
symBase, err := filepath.Rel(srcRoot, dstRoot)
if err != nil {
symBase, 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.Mkdir(dstPath, perm)
}
// If the OS supports symlinks, use them instead of copying bytes.
if err := os.Symlink(filepath.Join(symBase, suffix), 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
})
}
This diff is collapsed.
package dep2 package dep2
import "depBase" import "testshared/depBase"
var W int = 1 var W int = 1
......
...@@ -7,8 +7,8 @@ package dep3 ...@@ -7,8 +7,8 @@ package dep3
// the type data in this case and later crash. // the type data in this case and later crash.
import ( import (
"dep2" "testshared/dep2"
"depBase" "testshared/depBase"
) )
type Dep3 struct { type Dep3 struct {
......
package main package main
import ( import (
"depBase" "testshared/depBase"
"os" "os"
"reflect" "reflect"
"runtime" "runtime"
......
package main package main
import "dep2" import "testshared/dep2"
func main() { func main() {
d := &dep2.Dep2{} d := &dep2.Dep2{}
......
package main package main
import "dep3" import "testshared/dep3"
func main() { func main() {
dep3.D3() dep3.D3()
......
package explicit package explicit
import ( import (
"implicit" "testshared/implicit"
) )
func E() int { func E() int {
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
package main package main
import ( import (
"globallib" "testshared/globallib"
) )
//go:noinline //go:noinline
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
package main package main
import "iface_a" import "testshared/iface_a"
import "iface_b" import "testshared/iface_b"
func main() { func main() {
if iface_a.F() != iface_b.F() { if iface_a.F() != iface_b.F() {
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
package iface_a package iface_a
import "iface_i" import "testshared/iface_i"
//go:noinline //go:noinline
func F() interface{} { func F() interface{} {
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
package iface_b package iface_b
import "iface_i" import "testshared/iface_i"
//go:noinline //go:noinline
func F() interface{} { func F() interface{} {
......
package main package main
import ( import (
"explicit" "testshared/explicit"
"implicit" "testshared/implicit"
) )
func main() { func main() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment