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

assert: ensure message is always displayed & fix under bazel #276

Merged
merged 6 commits into from
Oct 12, 2023

Conversation

cstrahan
Copy link
Contributor

@cstrahan cstrahan commented Oct 2, 2023

Note that I still need to run the tests, but I wanted to get this PR open ASAP.

This PR fixes #274.

Now, when running under bazel test we'll see the following sort of error message when the target test source file can't be found:

--- FAIL: TestAssertShouldWorkUnderBazel (0.00s)
    your_test.go:40: the test source file does not exist: /private/var/tmp/_bazel_charles/99cf13450b07a7c7594bbbe785fbd4ef/sandbox/darwin-sandbox/20/execroot/__main__/bazel-out/darwin_arm64-fastbuild/bin/some_package/your_package_test_/your_package_test.runfiles/__main__/some_package/your_test.go
        it appears that you are running this test under Bazel (target: //some_package:your_package_test);
        check that your test source files are added as test data in your go_test targets.
        example:
            go_test(
                name = "your_package_test",
                     srcs = ["your_test.go"],
                     deps = ["@tools_gotest_v3//assert"],
                     data = glob(["*_test.go"] # <====== test source files added as test data here!
            )
    your_test.go:40: assertion failed: but assert failed to find the expression to print: a should equal b
FAIL

If the user adds the test source file to their test data in the respective go_test target, they'll now see something like:

--- FAIL: TestAssertShouldWorkUnderBazel (0.00s)
    your_test.go:40: assertion failed: a is not b: a should equal b
FAIL

@CLAassistant
Copy link

CLAassistant commented Oct 2, 2023

CLA assistant check
All committers have signed the CLA.

@cstrahan
Copy link
Contributor Author

cstrahan commented Oct 2, 2023

All tests run via go test ./... pass, with the exception of these two (which I believe are due to an issue with the tests when run under macOS, as they were failing before my changes):

--- FAIL: TestChangeWorkingDir (0.00s)
    --- FAIL: TestChangeWorkingDir/changed_to_dir (0.00s)
        env_test.go:138: assertion failed: /private/var/folders/vc/8x3823591cz2_gpk94zpjfv00000gq/T/TestChangeWorkingDir-1487778779 (string) != /var/folders/vc/8x3823591cz2_gpk94zpjfv00000gq/T/TestChangeWorkingDir-1487778779 (string)
--- FAIL: TestChangeWorkingDir_IntegrationWithCleanup (0.00s)
    --- FAIL: TestChangeWorkingDir_IntegrationWithCleanup/cleanup_in_subtest (0.00s)
        env_test.go:157: assertion failed: /private/var/folders/vc/8x3823591cz2_gpk94zpjfv00000gq/T/TestChangeWorkingDir_IntegrationWithCleanup-2603325723 (string) != /var/folders/vc/8x3823591cz2_gpk94zpjfv00000gq/T/TestChangeWorkingDir_IntegrationWithCleanup-2603325723 (string)

I'll see if I can fix the lint issue.

@cstrahan
Copy link
Contributor Author

cstrahan commented Oct 2, 2023

Hmm. The ci/circleci: test-golang-1.20 check is failing:

∅  .
✓  assert (689ms)
∅  icmd/internal/stub
∅  internal/assert
∅  internal/cleanup
∅  internal/difflib
∅  internal/maint
∅  x
✓  internal/format (71ms)
✓  assert/opt (72ms)
✓  skip (75ms)
✓  env (75ms)
✓  assert/cmp (75ms)
✓  golden (75ms)
✓  internal/source (76ms)
✓  fs (170ms)
✖  poll (271ms)
✓  icmd (1.411s)
✓  x/subtest (4ms)
✓  assert/cmd/gty-migrate-from-testify (20.483s)

=== Skipped
=== SKIP: env TestPatchAllWindows (0.00s)
    env_test.go:70: runtime.GOOS != "windows"

=== Failed
=== FAIL: poll TestWaitOn_WithCompare (0.09s)
    poll_test.go:86: assertion failed: string "timeout hit after 10ms: first check never completed" does not contain "assertion failed: 3 (int) != 4 (int)"

DONE 289 tests, 1 skipped, 1 failure in 54.413s

Exited with code exit status 1

but that test passes with [email protected] on my machine (running macOS). Maybe a flaky test? Might bump CI with an empty commit.

@cstrahan
Copy link
Contributor Author

cstrahan commented Oct 2, 2023

Yay! The tests pass. But I just realized that my editor was hiding the fact that tabs and spaces got mixed together in that big error message string. Just pushed a commit to fix that up.

And move logic to a function so that variables can be closer to where they are used
Copy link
Member

@dnephin dnephin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR! I made a couple small changes, but overall this looks great.

Primarily I moved the variables to a bazel.go file, and extracted some of the logic to a new function. This way the variables and the code can be located a bit closer together.

I also extracted the multi-line error message to a raw string to make it a bit easier to read in code. The raw string removes the need to escape the double quotes.

If these changes look alright to you I think this is ready to be merged.

name = "your_package_test",
srcs = ["your_test.go"],
deps = ["@tools_gotest_v3//assert"],
data = glob(["*_test.go"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On this line I added the trailing ) which I believe was missing, and removed the "test source files added as test data here!" comment. Does that seem ok? I guess the glob should capture all the test files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Yes, I think this is great 👍

@cstrahan
Copy link
Contributor Author

@dnephin Those changes look great! Don't see anything else I'd change. Thanks!

@dnephin dnephin merged commit 1fa4ef3 into gotestyourself:main Oct 12, 2023
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

Successfully merging this pull request may close these issues.

source.CallExprArgs fails to open target source file when run under bazel
3 participants