Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing/testfs: tests including absolute root-based paths fail #51378

Closed
clohr-vc opened this issue Feb 27, 2022 · 2 comments
Closed

testing/testfs: tests including absolute root-based paths fail #51378

clohr-vc opened this issue Feb 27, 2022 · 2 comments

Comments

@clohr-vc
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.17.5 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/xxx/.cache/go-build"
GOENV="/home/xxx/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/xxx/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/xxx/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib64/golang"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib64/golang/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.5"
GCCGO="gccgo"
AR="ar"
CC="x86_64-solus-linux-gcc"
CXX="x86_64-solus-linux-g++"
CGO_ENABLED="1"
GOMOD="/home/xxx/git/gitlab.com/xxx/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build599005499=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://go.dev/play/p/psQhY3pZTkA

package main

import (
	"testing/fstest"
)

func main() {

	// Let's try to access file setup using relative path
	// This will work as expected.
	if err := testFS("mount/drive/file"); err != nil {
		panic(err)
	}

	// Now let's switch to an absolute path
	// This will fail with error 'file does not exist'
	if err := testFS("/mount/drive/file"); err != nil {
		panic(err)
	}
}

func testFS(path string) error {
	m := fstest.MapFS{
		path: {},
	}

	_, err := m.ReadFile(path)
	return err
}

What did you expect to see?

Judging from the documentation on testfs.MapFS, I'd expect any path present in the map to be treated as existing, regardless of containing the root directory or not.

What did you see instead?

When configuring paths and files with absolute paths including the root directory, any access to those will fail with file does not exist error messages.

I'm not 100% sure about the reason behind this, I suspect it derives from unclear definitions of what a root volume is on non unix systems (as discussed here #44279 ). Since the testing package requires explicit setup of file structures and hierarchies and essentially abstracts the real file system for tests, I don't really understand why it wouldn't work here either, though?

@seankhliao
Copy link
Member

MapFS implements io/fs.FS which disallows absolute paths.

@clohr-vc
Copy link
Author

@seankhliao is it maybe worth adding some hint about this to the docs? I see quite a few issues around this misconception.

@golang golang locked and limited conversation to collaborators Feb 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants