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

Set $HOME for tests #4108

Open
rockwotj opened this issue Sep 17, 2024 · 5 comments
Open

Set $HOME for tests #4108

rockwotj opened this issue Sep 17, 2024 · 5 comments

Comments

@rockwotj
Copy link
Contributor

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
Copy link
Collaborator

fmeum commented Sep 17, 2024

Looks reasonable to me, could you send a PR?

@fmeum
Copy link
Collaborator

fmeum commented Sep 17, 2024

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
Copy link
Contributor Author

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
Copy link
Contributor Author

Is that code you point to only used for windows?

@rockwotj
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants