Search code examples
gotestingbazel

Different file permission error in "go test" and "bazel test"


If a test wants to assert the file permission error, for example, writing to the root of file system, "go test" returns an syscall.EACCES error, while "bazel test" returns an syscall.EPERM. How to make the test passes in both "bazel test" and "go test"?

An example can be found here.


Solution

  • You can disable the sandbox by using bazel --spawn_strategy=standalone test //.... I suspect this will work around the problem.

    However, you may want to consider whether writing to / is the behavior that you want to test. If you need to run code on a different operating system or inside a Docker container, you'll get different behavior in this case, so you could think about testing a more predictable code path, or mocking out the file access layer to isolate your tests from it.