interp: fix the logic to skip source files based on OS or CPU arch

For example, on architecture GOARCH=amd64, a file named `foobar_amd64.go` would be skipped instead of being read and parsed. The function `skipFile` is fixed and missing tests are added.
This commit is contained in:
Marc Vertes
2022-10-24 15:48:04 +02:00
committed by GitHub
parent a5242cbb9e
commit e4e3d11772
2 changed files with 24 additions and 4 deletions

View File

@@ -147,10 +147,22 @@ func skipFile(ctx *build.Context, p string, skipTest bool) bool {
}
a := strings.Split(p[i+1:], "_")
last := len(a) - 1
if last1 := last - 1; last1 >= 0 && a[last1] == ctx.GOOS && a[last] == ctx.GOARCH {
return false
if last-1 >= 0 {
switch x, y := a[last-1], a[last]; {
case x == ctx.GOOS:
if knownArch[y] {
return y != ctx.GOARCH
}
return false
case knownOs[x] && knownArch[y]:
return true
case knownArch[y] && y != ctx.GOARCH:
return true
default:
return false
}
}
if s := a[last]; s != ctx.GOOS && s != ctx.GOARCH && knownOs[s] || knownArch[s] {
if x := a[last]; knownOs[x] && x != ctx.GOOS || knownArch[x] && x != ctx.GOARCH {
return true
}
return false

View File

@@ -51,7 +51,7 @@ func TestBuildTag(t *testing.T) {
}
}
func TestBuildFile(t *testing.T) {
func TestSkipFile(t *testing.T) {
// Assume a specific OS, arch and go pattern no matter the real underlying system
ctx := build.Context{
GOARCH: "amd64",
@@ -65,10 +65,18 @@ func TestBuildFile(t *testing.T) {
{"bar_linux.go", false},
{"bar_maix.go", false},
{"bar_mlinux.go", false},
{"bar_aix_foo.go", false},
{"bar_linux_foo.go", false},
{"bar_foo_amd64.go", false},
{"bar_foo_arm.go", true},
{"bar_aix_s390x.go", true},
{"bar_aix_amd64.go", true},
{"bar_linux_arm.go", true},
{"bar_amd64.go", false},
{"bar_arm.go", true},
}
for _, test := range tests {