bazel-contrib / rules_go

Go rules for Bazel
Apache License 2.0
1.39k stars 662 forks source link

Set $HOME for tests #4108

Open rockwotj opened 2 months ago

rockwotj commented 2 months ago

What version of rules_go are you using?

Latest (50.1)

What version of gazelle are you using?

N/A

What version of Bazel are you using?

7.3

Does this issue reproduce with the latest releases of all the above?

Yes

What operating system and processor architecture are you using?

Linux x86_64

Any other potentially useful information about your toolchain?

Nope

What did you do?

Use a test that calls os.LookupEnv("HOME") and requires that $HOME is set.

What did you expect to see?

That $HOME is set to $TEST_TMPDIR as recommended here: https://bazel.build/reference/test-encyclopedia.

What did you see instead?

$HOME is not set.

We can work around this by doing, but it'd be better to not have to do this

env = {"HOME": "$TEST_TMPDIR"}
fmeum commented 2 months ago

Looks reasonable to me, could you send a PR?

fmeum commented 2 months ago

Actually, it looks like Bazel's test setup already does this: https://cs.opensource.google/bazel/bazel/+/master:tools/test/windows/tw.cc;l=543?q=%22%5C%22HOME%5C%22%22&ss=bazel%2Fbazel

Do you have a reproducer for a situation in which it isn't set?

rockwotj commented 2 months ago

Diff of the example in this repo:

$ git diff | more
diff --git a/examples/basic-gazelle/pkg/roll/roll_dice_test.go b/examples/basic-gazelle/pkg/roll/roll_dice_test.go
index c69c685b..ec8bb77e 100644
--- a/examples/basic-gazelle/pkg/roll/roll_dice_test.go
+++ b/examples/basic-gazelle/pkg/roll/roll_dice_test.go
@@ -15,6 +15,7 @@
 package roll

 import (
+       "os"
        "testing"
 )

@@ -25,3 +26,11 @@ func TestGenerateNumber(t *testing.T) {
                t.Error("got an empty string")
        }
 }
+
+func TestHomeSet(t *testing.T) {
+       result := os.Getenv("HOME")
+
+       if result == "" {
+               t.Error("HOME is not set")
+       }
+}
$ bazel test ...
INFO: Analyzed 5 targets (0 packages loaded, 0 targets configured).
FAIL: //pkg/roll:roll_test (see /home/rockwood/.cache/bazel/_bazel_rockwood/825fc735dcab734b0628bcd575d00c0e/execroot/__main__/bazel-out/k8-fastbuild/testlogs/pkg/roll/roll_test/test.log)
INFO: From Testing //pkg/roll:roll_test:
==================== Test output for //pkg/roll:roll_test:
--- FAIL: TestHomeSet (0.00s)
    roll_dice_test.go:34: HOME is not set
FAIL
================================================================================
INFO: Found 4 targets and 1 test target...
INFO: Elapsed time: 0.084s, Critical Path: 0.02s
INFO: 2 processes: 1 internal, 1 processwrapper-sandbox.
INFO: Build completed, 1 test FAILED, 2 total actions
//pkg/roll:roll_test                                                     FAILED in 0.0s
  /home/rockwood/.cache/bazel/_bazel_rockwood/825fc735dcab734b0628bcd575d00c0e/execroot/__main__/bazel-out/k8-fastbuild/testlogs/pkg/roll/roll_test/test.log

Executed 1 out of 1 test: 1 fails locally.
rockwotj commented 2 months ago

Is that code you point to only used for windows?

rockwotj commented 2 months ago

Is something buggy here: https://cs.opensource.google/bazel/bazel/+/master:tools/test/test-setup.sh;l=66